forked from jmckaskill/go-capnproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.go
181 lines (154 loc) · 4.31 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
package capn
import (
"bytes"
"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 than 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 uint64(len(b.Data)) > uint64(math.MaxUint32-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)}
}
if len(data) > 0 {
return m.segments[0]
}
return &Segment{m, nil, 0xFFFFFFFF}
}
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))}
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.
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 little32(buf.Bytes()[:]) >= uint32(MaxSegmentNumber) {
return nil, ErrTooMuchData
}
segnum := int(little32(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 := little32(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:]
m := &multiBuffer{make([]*Segment, segnum)}
for i := 0; i < segnum; i++ {
sz := int(little32(hdrv[4*i:])) * 8
m.segments[i] = &Segment{m, datav[:sz], uint32(i)}
datav = datav[sz:]
}
return m.segments[0], 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)
putLittle32(hdrv, segnum-1)
for i := uint32(0); i < segnum; i++ {
seg, _ := s.Message.Lookup(i)
putLittle32(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
}