forked from glycerine/go-capnproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.go
315 lines (268 loc) · 8.32 KB
/
mem.go
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package capn
import (
"bytes"
"encoding/binary"
"errors"
"io"
"math"
)
var (
errBufferCall = errors.New("capn: can't call on a memory buffer")
ErrInvalidSegment = errors.New("capn: invalid segment id")
ErrTooMuchData = errors.New("capn: too much data in stream")
)
type buffer Segment
// NewBuffer creates an expanding single segment buffer. Creating new objects
// will expand the buffer. Data can be nil (or length 0 with some capacity) if
// creating a new session. If parsing an existing segment then data should be
// the segment contents and will not be copied.
func NewBuffer(data []byte) *Segment {
if uint64(len(data)) > uint64(math.MaxUint32) {
return nil
}
b := &buffer{}
b.Message = b
b.Data = data
return (*Segment)(b)
}
func (b *buffer) NewSegment(minsz int) (*Segment, error) {
if minsz < 4096 {
minsz = 4096
}
if uint64(len(b.Data)) > uint64(math.MaxUint32)-uint64(minsz) {
return nil, ErrOverlarge
}
b.Data = append(b.Data, make([]byte, minsz)...)
b.Data = b.Data[:len(b.Data)-minsz]
return (*Segment)(b), nil
}
func (b *buffer) Lookup(segid uint32) (*Segment, error) {
if segid == 0 {
return (*Segment)(b), nil
} else {
return nil, ErrInvalidSegment
}
}
type MultiBuffer struct {
Segments []*Segment
}
// NewMultiBuffer creates a new multi segment message. Creating new objects
// will try and reuse the buffers available, but will create new ones if there
// is insufficient capacity. When parsing an existing message data should be
// the list of segments. The data buffers will not be copied.
func NewMultiBuffer(data [][]byte) *Segment {
m := &MultiBuffer{make([]*Segment, len(data))}
for i, d := range data {
m.Segments[i] = &Segment{m, d, uint32(i), false}
}
if len(data) > 0 {
return m.Segments[0]
}
return &Segment{Message: m, Data: nil, Id: 0xFFFFFFFF, RootDone: false}
}
var (
MaxSegmentNumber = 1024
MaxTotalSize = 1024 * 1024 * 1024
)
func (m *MultiBuffer) NewSegment(minsz int) (*Segment, error) {
for _, s := range m.Segments {
if len(s.Data)+minsz <= cap(s.Data) {
return s, nil
}
}
if minsz < 4096 {
minsz = 4096
}
s := &Segment{m, make([]byte, 0, minsz), uint32(len(m.Segments)), false}
m.Segments = append(m.Segments, s)
return s, nil
}
func (m *MultiBuffer) Lookup(segid uint32) (*Segment, error) {
if uint(segid) < uint(len(m.Segments)) {
return m.Segments[segid], nil
} else {
return nil, ErrInvalidSegment
}
}
// ReadFromStream reads a non-packed serialized stream from r. buf is used to
// buffer the read contents, can be nil, and is provided so that the buffer
// can be reused between messages. The returned segment is the first segment
// read, which contains the root pointer.
//
// Warning about buf reuse: It is safer to just pass nil for buf.
// When making multiple calls to ReadFromStream() with the same buf argument, you
// may overwrite the data in a previously returned Segment.
// The re-use of buf is an optimization for when you are actually
// done with any previously returned Segment which may have data still alive
// in buf.
//
func ReadFromStream(r io.Reader, buf *bytes.Buffer) (*Segment, error) {
if buf == nil {
buf = new(bytes.Buffer)
} else {
buf.Reset()
}
if _, err := io.CopyN(buf, r, 4); err != nil {
return nil, err
}
if binary.LittleEndian.Uint32(buf.Bytes()[:]) >= uint32(MaxSegmentNumber) {
return nil, ErrTooMuchData
}
segnum := int(binary.LittleEndian.Uint32(buf.Bytes()[:]) + 1)
hdrsz := 8*(segnum/2) + 4
if _, err := io.CopyN(buf, r, int64(hdrsz)); err != nil {
return nil, err
}
total := 0
for i := 0; i < segnum; i++ {
sz := binary.LittleEndian.Uint32(buf.Bytes()[4*i+4:])
if uint64(total)+uint64(sz)*8 > uint64(MaxTotalSize) {
return nil, ErrTooMuchData
}
total += int(sz) * 8
}
if _, err := io.CopyN(buf, r, int64(total)); err != nil {
return nil, err
}
hdrv := buf.Bytes()[4 : hdrsz+4]
datav := buf.Bytes()[hdrsz+4:]
if segnum == 1 {
sz := int(binary.LittleEndian.Uint32(hdrv)) * 8
return NewBuffer(datav[:sz]), nil
}
m := &MultiBuffer{make([]*Segment, segnum)}
for i := 0; i < segnum; i++ {
sz := int(binary.LittleEndian.Uint32(hdrv[4*i:])) * 8
m.Segments[i] = &Segment{m, datav[:sz], uint32(i), false}
datav = datav[sz:]
}
return m.Segments[0], nil
}
// ReadFromMemoryZeroCopy: like ReadFromStream, but reads a non-packed
// serialized stream that already resides in memory in the argument data.
// The returned segment is the first segment read, which contains
// the root pointer. The returned bytesRead says how many bytes were
// consumed from data in making seg. The caller should advance the
// data slice by doing data = data[bytesRead:] between successive calls
// to ReadFromMemoryZeroCopy().
func ReadFromMemoryZeroCopy(data []byte) (seg *Segment, bytesRead int64, err error) {
if len(data) < 4 {
return nil, 0, io.EOF
}
if binary.LittleEndian.Uint32(data[0:4]) >= uint32(MaxSegmentNumber) {
return nil, 0, ErrTooMuchData
}
segnum := int(binary.LittleEndian.Uint32(data[0:4]) + 1)
hdrsz := 8*(segnum/2) + 4
b := data[0:(hdrsz + 4)]
total := 0
for i := 0; i < segnum; i++ {
sz := binary.LittleEndian.Uint32(b[4*i+4:])
if uint64(total)+uint64(sz)*8 > uint64(MaxTotalSize) {
return nil, 0, ErrTooMuchData
}
total += int(sz) * 8
}
if total == 0 {
return nil, 0, io.EOF
}
hdrv := data[4:(hdrsz + 4)]
datav := data[hdrsz+4:]
m := &MultiBuffer{make([]*Segment, segnum)}
for i := 0; i < segnum; i++ {
sz := int(binary.LittleEndian.Uint32(hdrv[4*i:])) * 8
m.Segments[i] = &Segment{m, datav[:sz], uint32(i), false}
datav = datav[sz:]
}
return m.Segments[0], int64(4 + hdrsz + total), nil
}
func NewSingleSegmentMultiBuffer() *MultiBuffer {
m := &MultiBuffer{make([]*Segment, 1)}
m.Segments[0] = &Segment{}
return m
}
// ReadFromMemoryZeroCopyNoAlloc: like ReadFromMemoryZeroCopy,
// but avoid all allocations so we get zero GC pressure.
//
// This requires some strict but easy to meet pre-requisites:
//
// PRE: the capnp bytes in data must come from only one segment. Else we panic.
// PRE: multi must point to an existing MultiBuffer that has exactly one Segment
// that will be re-used and over-written. If in doubt,
// you can allocate a correct new one the first time
// by calling NewSingleSegmentMultiBuffer().
//
func ReadFromMemoryZeroCopyNoAlloc(data []byte, multi *MultiBuffer) (bytesRead int64, err error) {
if len(data) < 4 {
return 0, io.EOF
}
if binary.LittleEndian.Uint32(data[0:4]) >= uint32(MaxSegmentNumber) {
return 0, ErrTooMuchData
}
segnum := int(binary.LittleEndian.Uint32(data[0:4]) + 1)
if segnum != 1 {
panic("only one segment allowed in data read in with ReadFromMemoryZeroCopyNoAlloc()")
}
if multi == nil {
panic("multi must point to an existing MultiBuffer with a single Segment")
}
if len(multi.Segments) != 1 {
panic("only one segment allowed in the multi *MultiBuffer used in ReadFromMemoryZeroCopyNoAlloc()")
}
if multi.Segments[0] == nil {
panic("multi.Segment[0] must point to an allocated Segment{} to be resused in ReadFromMemoryZeroCopyNoAlloc()")
}
hdrsz := 8*(segnum/2) + 4
b := data[0:(hdrsz + 4)]
total := 0
for i := 0; i < segnum; i++ {
sz := binary.LittleEndian.Uint32(b[4*i+4:])
if uint64(total)+uint64(sz)*8 > uint64(MaxTotalSize) {
return 0, ErrTooMuchData
}
total += int(sz) * 8
}
if total == 0 {
return 0, io.EOF
}
hdrv := data[4:(hdrsz + 4)]
datav := data[hdrsz+4:]
sz := int(binary.LittleEndian.Uint32(hdrv)) * 8
seg := multi.Segments[0]
seg.Message = multi
seg.Data = datav[:sz]
seg.Id = 0
seg.RootDone = false
datav = datav[sz:]
return int64(4 + hdrsz + total), nil
}
// WriteTo writes the message that the segment is part of to the
// provided stream in serialized form.
func (s *Segment) WriteTo(w io.Writer) (int64, error) {
segnum := uint32(1)
for {
if seg, _ := s.Message.Lookup(segnum); seg == nil {
break
}
segnum++
}
hdrv := make([]uint8, 8*(segnum/2)+8)
binary.LittleEndian.PutUint32(hdrv, segnum-1)
for i := uint32(0); i < segnum; i++ {
seg, _ := s.Message.Lookup(i)
binary.LittleEndian.PutUint32(hdrv[4*i+4:], uint32(len(seg.Data)/8))
}
if n, err := w.Write(hdrv); err != nil {
return int64(n), err
}
written := int64(len(hdrv))
for i := uint32(0); i < segnum; i++ {
seg, _ := s.Message.Lookup(i)
if n, err := w.Write(seg.Data); err != nil {
return written + int64(n), err
} else {
written += int64(n)
}
}
return written, nil
}