-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
list.go
700 lines (610 loc) · 16 KB
/
list.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
package capnp
import (
"errors"
"math"
)
// A List is a reference to an array of values.
type List struct {
seg *Segment
off Address
length int32
size ObjectSize
depthLimit uint
flags listFlags
}
// newPrimitiveList allocates a new list of primitive values, preferring placement in s.
func newPrimitiveList(s *Segment, sz Size, n int32) (List, error) {
total, ok := sz.times(n)
if !ok {
return List{}, errOverflow
}
s, addr, err := alloc(s, total)
if err != nil {
return List{}, err
}
return List{
seg: s,
off: addr,
length: n,
size: ObjectSize{DataSize: sz},
depthLimit: maxDepth,
}, nil
}
// NewCompositeList creates a new composite list, preferring placement
// in s.
func NewCompositeList(s *Segment, sz ObjectSize, n int32) (List, error) {
if !sz.isValid() {
return List{}, errObjectSize
}
sz.DataSize = sz.DataSize.padToWord()
total, ok := sz.totalSize().times(n)
if !ok || total > maxSize-wordSize {
return List{}, errOverflow
}
s, addr, err := alloc(s, wordSize+total)
if err != nil {
return List{}, err
}
// Add tag word
s.writeRawPointer(addr, rawStructPointer(pointerOffset(n), sz))
return List{
seg: s,
off: addr + Address(wordSize),
length: n,
size: sz,
flags: isCompositeList,
depthLimit: maxDepth,
}, nil
}
// ToList is deprecated in favor of Ptr.List.
func ToList(p Pointer) List {
return toPtr(p).List()
}
// ToListDefault is deprecated in favor of Ptr.ListDefault.
func ToListDefault(p Pointer, def []byte) (List, error) {
return toPtr(p).ListDefault(def)
}
// ToPtr converts the list to a generic pointer.
func (p List) ToPtr() Ptr {
return Ptr{
seg: p.seg,
off: p.off,
lenOrCap: uint32(p.length),
size: p.size,
depthLimit: p.depthLimit,
flags: listPtrFlag(p.flags),
}
}
// Segment returns the segment this pointer references.
func (p List) Segment() *Segment {
return p.seg
}
// IsValid returns whether the list is valid.
func (p List) IsValid() bool {
return p.seg != nil
}
// HasData reports whether the list's total size is non-zero.
func (p List) HasData() bool {
sz, ok := p.size.totalSize().times(p.length)
if !ok {
return false
}
return sz > 0
}
// readSize returns the list's size for the purposes of read limit
// accounting.
func (p List) readSize() Size {
if p.seg == nil {
return 0
}
e := p.size.totalSize()
if e == 0 {
e = wordSize
}
sz, ok := e.times(p.length)
if !ok {
return maxSize
}
return sz
}
// value returns the equivalent raw list pointer.
func (p List) value(paddr Address) rawPointer {
if p.seg == nil {
return 0
}
off := makePointerOffset(paddr, p.off)
if p.flags&isCompositeList != 0 {
// p.off points to the data not the header
return rawListPointer(off-1, compositeList, p.length*p.size.totalWordCount())
}
if p.flags&isBitList != 0 {
return rawListPointer(off, bit1List, p.length)
}
if p.size.PointerCount == 1 && p.size.DataSize == 0 {
return rawListPointer(off, pointerList, p.length)
}
if p.size.PointerCount != 0 {
panic(errListSize)
}
switch p.size.DataSize {
case 0:
return rawListPointer(off, voidList, p.length)
case 1:
return rawListPointer(off, byte1List, p.length)
case 2:
return rawListPointer(off, byte2List, p.length)
case 4:
return rawListPointer(off, byte4List, p.length)
case 8:
return rawListPointer(off, byte8List, p.length)
default:
panic(errListSize)
}
}
func (p List) underlying() Pointer {
return p
}
// Address returns the address the pointer references.
func (p List) Address() Address {
return p.off
}
// Len returns the length of the list.
func (p List) Len() int {
if p.seg == nil {
return 0
}
return int(p.length)
}
// elem returns the slice of segment data for a list element.
func (p List) elem(i int) (addr Address, sz Size) {
if p.seg == nil || i < 0 || i >= int(p.length) {
panic(errOutOfBounds)
}
if p.flags&isBitList != 0 {
addr = p.off.addOffset(BitOffset(i).offset())
return addr, 1
}
sz = p.size.totalSize()
addr, _ = p.off.element(int32(i), sz)
return addr, sz
}
func (p List) slice(i int) []byte {
addr, sz := p.elem(i)
return p.seg.slice(addr, sz)
}
// Struct returns the i'th element as a struct.
func (p List) Struct(i int) Struct {
if p.flags&isBitList != 0 {
return Struct{}
}
addr, _ := p.elem(i)
return Struct{
seg: p.seg,
off: addr,
size: p.size,
flags: isListMember,
depthLimit: p.depthLimit - 1,
}
}
// SetStruct set the i'th element to the value in s.
func (p List) SetStruct(i int, s Struct) error {
if p.flags&isBitList != 0 {
return errBitListStruct
}
return copyStruct(copyContext{}, p.Struct(i), s)
}
// A BitList is a reference to a list of booleans.
type BitList struct{ List }
// NewBitList creates a new bit list, preferring placement in s.
func NewBitList(s *Segment, n int32) (BitList, error) {
s, addr, err := alloc(s, Size(int64(n+7)/8))
if err != nil {
return BitList{}, err
}
return BitList{List{
seg: s,
off: addr,
length: n,
flags: isBitList,
depthLimit: maxDepth,
}}, nil
}
// At returns the i'th bit.
func (p BitList) At(i int) bool {
b := p.slice(i)
if b == nil {
return false
}
bit := BitOffset(i)
return b[0]&bit.mask() != 0
}
// Set sets the i'th bit to v.
func (p BitList) Set(i int, v bool) {
b := p.slice(i)
if b == nil {
panic(errOutOfBounds)
}
bit := BitOffset(i)
if v {
b[0] |= bit.mask()
} else {
b[0] &^= bit.mask()
}
}
// A PointerList is a reference to an array of pointers.
type PointerList struct{ List }
// NewPointerList allocates a new list of pointers, preferring placement in s.
func NewPointerList(s *Segment, n int32) (PointerList, error) {
total, ok := wordSize.times(n)
if !ok {
return PointerList{}, errOverflow
}
s, addr, err := alloc(s, total)
if err != nil {
return PointerList{}, err
}
return PointerList{List{
seg: s,
off: addr,
length: n,
size: ObjectSize{PointerCount: 1},
depthLimit: maxDepth,
}}, nil
}
// At is deprecated in favor of PtrAt.
func (p PointerList) At(i int) (Pointer, error) {
pi, err := p.PtrAt(i)
return pi.toPointer(), err
}
// PtrAt returns the i'th pointer in the list.
func (p PointerList) PtrAt(i int) (Ptr, error) {
addr, _ := p.elem(i)
return p.seg.readPtr(addr, p.depthLimit)
}
// Set is deprecated in favor of SetPtr.
func (p PointerList) Set(i int, v Pointer) error {
return p.SetPtr(i, toPtr(v))
}
// SetPtr sets the i'th pointer in the list to v.
func (p PointerList) SetPtr(i int, v Ptr) error {
addr, _ := p.elem(i)
return p.seg.writePtr(copyContext{}, addr, v)
}
// TextList is an array of pointers to strings.
type TextList struct{ List }
// NewTextList allocates a new list of text pointers, preferring placement in s.
func NewTextList(s *Segment, n int32) (TextList, error) {
pl, err := NewPointerList(s, n)
if err != nil {
return TextList{}, err
}
return TextList{pl.List}, nil
}
// At returns the i'th string in the list.
func (l TextList) At(i int) (string, error) {
addr, _ := l.elem(i)
p, err := l.seg.readPtr(addr, l.depthLimit)
if err != nil {
return "", err
}
return p.Text(), nil
}
// BytesAt returns the i'th element in the list as a byte slice.
// The underlying array of the slice is the segment data.
func (l TextList) BytesAt(i int) ([]byte, error) {
addr, _ := l.elem(i)
p, err := l.seg.readPtr(addr, l.depthLimit)
if err != nil {
return nil, err
}
b := p.Data()
if len(b) == 0 {
return b, nil
}
return b[:len(b)-1 : len(b)], nil
}
// Set sets the i'th string in the list to v.
func (l TextList) Set(i int, v string) error {
addr, _ := l.elem(i)
p, err := NewText(l.seg, v)
if err != nil {
return err
}
return p.seg.writePtr(copyContext{}, addr, p.List.ToPtr())
}
// DataList is an array of pointers to data.
type DataList struct{ List }
// NewDataList allocates a new list of data pointers, preferring placement in s.
func NewDataList(s *Segment, n int32) (DataList, error) {
pl, err := NewPointerList(s, n)
if err != nil {
return DataList{}, err
}
return DataList{pl.List}, nil
}
// At returns the i'th data in the list.
func (l DataList) At(i int) ([]byte, error) {
addr, _ := l.elem(i)
p, err := l.seg.readPtr(addr, l.depthLimit)
if err != nil {
return nil, err
}
return p.Data(), nil
}
// Set sets the i'th data in the list to v.
func (l DataList) Set(i int, v []byte) error {
addr, _ := l.elem(i)
p, err := NewData(l.seg, v)
if err != nil {
return err
}
return p.seg.writePtr(copyContext{}, addr, p.List.ToPtr())
}
// A VoidList is a list of zero-sized elements.
type VoidList struct{ List }
// NewVoidList creates a list of voids. No allocation is performed;
// s is only used for Segment()'s return value.
func NewVoidList(s *Segment, n int32) VoidList {
return VoidList{List{
seg: s,
length: n,
depthLimit: maxDepth,
}}
}
// A UInt8List is an array of UInt8 values.
type UInt8List struct{ List }
// NewUInt8List creates a new list of UInt8, preferring placement in s.
func NewUInt8List(s *Segment, n int32) (UInt8List, error) {
l, err := newPrimitiveList(s, 1, n)
if err != nil {
return UInt8List{}, err
}
return UInt8List{l}, nil
}
// NewText creates a new list of UInt8 from a string.
func NewText(s *Segment, v string) (UInt8List, error) {
// TODO(light): error if v is too long
l, err := NewUInt8List(s, int32(len(v)+1))
if err != nil {
return UInt8List{}, err
}
copy(l.seg.slice(l.off, Size(len(v))), v)
return l, nil
}
// NewData creates a new list of UInt8 from a byte slice.
func NewData(s *Segment, v []byte) (UInt8List, error) {
// TODO(light): error if v is too long
l, err := NewUInt8List(s, int32(len(v)))
if err != nil {
return UInt8List{}, err
}
copy(l.seg.slice(l.off, Size(len(v))), v)
return l, nil
}
// ToText is deprecated in favor of Ptr.Text.
func ToText(p Pointer) string {
return toPtr(p).TextDefault("")
}
// ToTextDefault is deprecated in favor of Ptr.TextDefault.
func ToTextDefault(p Pointer, def string) string {
return toPtr(p).TextDefault(def)
}
// ToData is deprecated in favor of Ptr.Data.
func ToData(p Pointer) []byte {
return toPtr(p).DataDefault(nil)
}
// ToDataDefault is deprecated in favor of Ptr.DataDefault.
func ToDataDefault(p Pointer, def []byte) []byte {
return toPtr(p).DataDefault(def)
}
func isOneByteList(p Ptr) bool {
return p.seg != nil && p.flags.ptrType() == listPtrType && p.size.isOneByte() && p.flags.listFlags()&isCompositeList == 0
}
// At returns the i'th element.
func (l UInt8List) At(i int) uint8 {
b := l.slice(i)
if b == nil {
panic(errOutOfBounds)
}
return b[0]
}
// Set sets the i'th element to v.
func (l UInt8List) Set(i int, v uint8) {
b := l.slice(i)
if b == nil {
panic(errOutOfBounds)
}
b[0] = v
}
// Int8List is an array of Int8 values.
type Int8List struct{ List }
// NewInt8List creates a new list of Int8, preferring placement in s.
func NewInt8List(s *Segment, n int32) (Int8List, error) {
l, err := newPrimitiveList(s, 1, n)
if err != nil {
return Int8List{}, err
}
return Int8List{l}, nil
}
// At returns the i'th element.
func (l Int8List) At(i int) int8 {
b := l.slice(i)
if b == nil {
panic(errOutOfBounds)
}
return int8(b[0])
}
// Set sets the i'th element to v.
func (l Int8List) Set(i int, v int8) {
b := l.slice(i)
if b == nil {
panic(errOutOfBounds)
}
b[0] = uint8(v)
}
// A UInt16List is an array of UInt16 values.
type UInt16List struct{ List }
// NewUInt16List creates a new list of UInt16, preferring placement in s.
func NewUInt16List(s *Segment, n int32) (UInt16List, error) {
l, err := newPrimitiveList(s, 2, n)
if err != nil {
return UInt16List{}, err
}
return UInt16List{l}, nil
}
// At returns the i'th element.
func (l UInt16List) At(i int) uint16 {
addr, _ := l.elem(i)
return l.seg.readUint16(addr)
}
// Set sets the i'th element to v.
func (l UInt16List) Set(i int, v uint16) {
addr, _ := l.elem(i)
l.seg.writeUint16(addr, v)
}
// Int16List is an array of Int16 values.
type Int16List struct{ List }
// NewInt16List creates a new list of Int16, preferring placement in s.
func NewInt16List(s *Segment, n int32) (Int16List, error) {
l, err := newPrimitiveList(s, 2, n)
if err != nil {
return Int16List{}, err
}
return Int16List{l}, nil
}
// At returns the i'th element.
func (l Int16List) At(i int) int16 {
addr, _ := l.elem(i)
return int16(l.seg.readUint16(addr))
}
// Set sets the i'th element to v.
func (l Int16List) Set(i int, v int16) {
addr, _ := l.elem(i)
l.seg.writeUint16(addr, uint16(v))
}
// UInt32List is an array of UInt32 values.
type UInt32List struct{ List }
// NewUInt32List creates a new list of UInt32, preferring placement in s.
func NewUInt32List(s *Segment, n int32) (UInt32List, error) {
l, err := newPrimitiveList(s, 4, n)
if err != nil {
return UInt32List{}, err
}
return UInt32List{l}, nil
}
// At returns the i'th element.
func (l UInt32List) At(i int) uint32 {
addr, _ := l.elem(i)
return l.seg.readUint32(addr)
}
// Set sets the i'th element to v.
func (l UInt32List) Set(i int, v uint32) {
addr, _ := l.elem(i)
l.seg.writeUint32(addr, v)
}
// Int32List is an array of Int32 values.
type Int32List struct{ List }
// NewInt32List creates a new list of Int32, preferring placement in s.
func NewInt32List(s *Segment, n int32) (Int32List, error) {
l, err := newPrimitiveList(s, 4, n)
if err != nil {
return Int32List{}, err
}
return Int32List{l}, nil
}
// At returns the i'th element.
func (l Int32List) At(i int) int32 {
addr, _ := l.elem(i)
return int32(l.seg.readUint32(addr))
}
// Set sets the i'th element to v.
func (l Int32List) Set(i int, v int32) {
addr, _ := l.elem(i)
l.seg.writeUint32(addr, uint32(v))
}
// UInt64List is an array of UInt64 values.
type UInt64List struct{ List }
// NewUInt64List creates a new list of UInt64, preferring placement in s.
func NewUInt64List(s *Segment, n int32) (UInt64List, error) {
l, err := newPrimitiveList(s, 8, n)
if err != nil {
return UInt64List{}, err
}
return UInt64List{l}, nil
}
// At returns the i'th element.
func (l UInt64List) At(i int) uint64 {
addr, _ := l.elem(i)
return l.seg.readUint64(addr)
}
// Set sets the i'th element to v.
func (l UInt64List) Set(i int, v uint64) {
addr, _ := l.elem(i)
l.seg.writeUint64(addr, v)
}
// Int64List is an array of Int64 values.
type Int64List struct{ List }
// NewInt64List creates a new list of Int64, preferring placement in s.
func NewInt64List(s *Segment, n int32) (Int64List, error) {
l, err := newPrimitiveList(s, 8, n)
if err != nil {
return Int64List{}, err
}
return Int64List{l}, nil
}
// At returns the i'th element.
func (l Int64List) At(i int) int64 {
addr, _ := l.elem(i)
return int64(l.seg.readUint64(addr))
}
// Set sets the i'th element to v.
func (l Int64List) Set(i int, v int64) {
addr, _ := l.elem(i)
l.seg.writeUint64(addr, uint64(v))
}
// Float32List is an array of Float32 values.
type Float32List struct{ List }
// NewFloat32List creates a new list of Float32, preferring placement in s.
func NewFloat32List(s *Segment, n int32) (Float32List, error) {
l, err := newPrimitiveList(s, 8, n)
if err != nil {
return Float32List{}, err
}
return Float32List{l}, nil
}
// At returns the i'th element.
func (l Float32List) At(i int) float32 {
addr, _ := l.elem(i)
return math.Float32frombits(l.seg.readUint32(addr))
}
// Set sets the i'th element to v.
func (l Float32List) Set(i int, v float32) {
addr, _ := l.elem(i)
l.seg.writeUint32(addr, math.Float32bits(v))
}
// Float64List is an array of Float64 values.
type Float64List struct{ List }
// NewFloat64List creates a new list of Float64, preferring placement in s.
func NewFloat64List(s *Segment, n int32) (Float64List, error) {
l, err := newPrimitiveList(s, 8, n)
if err != nil {
return Float64List{}, err
}
return Float64List{l}, nil
}
// At returns the i'th element.
func (l Float64List) At(i int) float64 {
addr, _ := l.elem(i)
return math.Float64frombits(l.seg.readUint64(addr))
}
// Set sets the i'th element to v.
func (l Float64List) Set(i int, v float64) {
addr, _ := l.elem(i)
l.seg.writeUint64(addr, math.Float64bits(v))
}
type listFlags uint8
const (
isCompositeList listFlags = 1 << iota
isBitList
)
var errBitListStruct = errors.New("capnp: SetStruct called on bit list")