-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbytebuf.go
More file actions
294 lines (261 loc) · 6.61 KB
/
cbytebuf.go
File metadata and controls
294 lines (261 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package cbytebuf
import (
"io"
"reflect"
"strconv"
"github.com/koykov/byteconv"
"github.com/koykov/cbyte"
)
// CByteBuf is a variable-size alloc-free buffer based on cbyte array.
// Also, no escapes to the heap since buffer doesn't contain any pointer.
type CByteBuf struct {
// Header to manipulate buffer size and fast slice construction.
h reflect.SliceHeader
// Helper temporary variable.
t int
}
// MarshallerTo interface to write struct like Protobuf.
type MarshallerTo interface {
Size() int
MarshalTo(data []byte) (int, error)
}
// NewCByteBuf makes new buffer.
func NewCByteBuf() *CByteBuf {
b := CByteBuf{}
return &b
}
// Len returns length of the buffer.
func (b *CByteBuf) Len() int {
return b.h.Len
}
// Cap returns capacity of the buffer.
func (b *CByteBuf) Cap() int {
return b.h.Cap
}
// ReadFrom implements io.ReaderFrom.
func (b *CByteBuf) ReadFrom(r io.Reader) (n int64, err error) {
if b.h.Cap == 0 {
if err = b.Grow(64); err != nil {
return 0, err
}
}
for {
if b.t == b.h.Cap {
if err = b.Grow(b.h.Cap * 2); err != nil {
return 0, err
}
}
b.t, err = r.Read(b.Bytes()[b.t:])
if b.t < 0 {
return n, ErrNegativeRead
}
b.h.Len += b.t
n += int64(b.t)
if err == io.EOF {
return n, nil
}
if err != nil {
return n, err
}
}
}
// WriteTo implements io.WriterTo.
func (b *CByteBuf) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write(b.Bytes())
return int64(n), err
}
// Write implements io.Writer.
func (b *CByteBuf) Write(data []byte) (int, error) {
b.t = len(data)
if b.h.Data == 0 {
// First write, need to create internal byte slice.
// Check write after reset.
if b.h.Cap == 0 {
b.h.Cap = b.t * 2
}
// Allocate byte array.
b.h.Data = uintptr(cbyte.Init(b.h.Cap))
if b.h.Data == 0 {
return 0, ErrBadAlloc
}
}
if b.h.Len+b.t > b.h.Cap {
// Increase capacity of the byte array due to not enough space in it.
err := b.Grow((b.h.Len + b.t) * 2)
if err != nil {
return 0, err
}
}
// Append data to the buffer.
b.h.Len += cbyte.Memcpy(uint64(b.h.Data), uint64(b.h.Len), data)
return b.t, ErrOk
}
// WriteMarshallerTo marshals data of struct implemented MarshallerTo interface into the buffer.
func (b *CByteBuf) WriteMarshallerTo(m MarshallerTo) (int, error) {
if m == nil {
return 0, ErrNilMarshaller
}
b.t = m.Size()
err := b.Grow(b.t)
if err != nil {
return 0, err
}
b.h.Len = b.t
return m.MarshalTo(b.Bytes())
}
// WriteByte implements io.ByteWriter.
func (b *CByteBuf) WriteByte(c byte) error {
_, err := b.Write([]byte{c})
return err
}
// WriteString implements io.StringWriter.
func (b *CByteBuf) WriteString(s string) (int, error) {
return b.Write(byteconv.S2B(s))
}
// WriteInt writes integer value in the buffer.
func (b *CByteBuf) WriteInt(i int64) (int, error) {
buf, err := b.subBuf(32)
if err != nil {
return 0, err
}
buf = strconv.AppendInt(buf, i, 10)
b.h.Len += len(buf)
return len(buf), nil
}
// WriteUint writes unsigned integer value in the buffer.
func (b *CByteBuf) WriteUint(u uint64) (int, error) {
buf, err := b.subBuf(32)
if err != nil {
return 0, err
}
buf = strconv.AppendUint(buf, u, 10)
b.h.Len += len(buf)
return len(buf), nil
}
// WriteFloat writes float value in the buffer.
func (b *CByteBuf) WriteFloat(f float64, prec int) (int, error) {
buf, err := b.subBuf(320 + prec)
if err != nil {
return 0, err
}
buf = strconv.AppendFloat(buf, f, 'f', prec, 64)
b.h.Len += len(buf)
return len(buf), nil
}
// WriteBool writes boolean value in the buffer.
func (b *CByteBuf) WriteBool(v bool) (int, error) {
if v {
return b.WriteString("true")
} else {
return b.WriteString("false")
}
}
// Grow increases or decrease capacity of the buffer.
func (b *CByteBuf) Grow(cap int) error {
if cap < 0 {
return ErrNegativeCap
}
if b.h.Data != 0 && b.h.Cap >= cap {
// Buffer has necessary space actually.
return ErrOk
}
// Save new capacity.
b.h.Cap = cap
if b.h.Len > b.h.Cap {
// Trim length to new capacity.
b.h.Len = b.h.Cap
}
// Allocate memory.
if b.h.Data == 0 {
// Grow after reset detected.
// Allocate byte array.
b.h.Data = uintptr(cbyte.Init(b.h.Cap))
} else {
// Reallocate byte array.
b.h.Data = uintptr(cbyte.GrowHeader(b.h))
}
if b.h.Data == 0 {
return ErrBadAlloc
}
return ErrOk
}
// GrowDelta increases or decrease capacity of the buffer using delta value.
//
// Delta may be negative, but if delta will less than -capacity, the error will be triggered.
func (b *CByteBuf) GrowDelta(delta int) error {
return b.Grow(b.h.Cap + delta)
}
// GrowLen increases or decrease length of the buffer.
//
// May increase capacity if needed.
func (b *CByteBuf) GrowLen(len int) error {
if b.h.Cap < len {
err := b.Grow(len)
if err != nil {
return err
}
}
b.h.Len = len
return nil
}
// Bytes returns contents of the buffer.
func (b *CByteBuf) Bytes() []byte {
return cbyte.Bytes(b.h)
}
// AppendBytes appends buffer value to destination and return it.
func (b *CByteBuf) AppendBytes(dst []byte) []byte {
return append(dst[:0], b.Bytes()...)
}
// Get the contents of the buffer as string.
func (b *CByteBuf) String() string {
return byteconv.B2S(b.Bytes())
}
// AppendString appends buffer value to destination string and return it.
func (b *CByteBuf) AppendString(dst string) string {
return byteconv.B2S(append(byteconv.S2B(dst)[:0], b.Bytes()...))
}
// ResetLen resets buffer length without releasing memory.
func (b *CByteBuf) ResetLen() {
b.h.Len = 0
}
// Reset all data accumulated in buffer.
//
// This method made special to use together with pools.
// Using the buffer data after call this func may crash your app.
// Buffer capacity keeps to reduce amount of further CGO calls.
func (b *CByteBuf) Reset() {
// sync.Pool may remove items in it without notifications, therefore need to release memory to prevent memory leaks.
// If you want to reset buffer length and keep allocated memory and buffer's capacity, then use ResetLen() instead.
b.release()
b.h.Len = 0
}
// Release manually releases of the underlying byte array.
//
// Using the buffer data after call this func may crash your app.
// This method truncates buffer's capacity.
func (b *CByteBuf) Release() {
b.release()
b.h.Len = 0
b.h.Cap = 0
}
// Internal release method.
func (b *CByteBuf) release() {
if b.h.Data == 0 {
return
}
// Free memory and reset pointer.
cbyte.ReleaseHeader(b.h)
b.h.Data = 0
}
// Grow buffer and return new space as a sub-buffer.
func (b *CByteBuf) subBuf(len int) ([]byte, error) {
if err := b.Grow(b.h.Len + len); err != nil {
return nil, err
}
bufH := reflect.SliceHeader{
Data: b.h.Data + uintptr(b.h.Len),
Len: 0,
Cap: len,
}
return cbyte.Bytes(bufH), nil
}