forked from glycerine/go-capnproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
capn.go
1504 lines (1284 loc) · 41.7 KB
/
capn.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package capn
import (
"encoding/binary"
"errors"
"math"
"github.com/glycerine/rbtree"
)
var (
ErrOverlarge = errors.New("capn: overlarge struct/list")
ErrOutOfBounds = errors.New("capn: write out of bounds")
ErrCopyDepth = errors.New("capn: copy depth too large")
ErrOverlap = errors.New("capn: overlapping data on copy")
errListSize = errors.New("capn: invalid list size")
errObjectType = errors.New("capn: invalid object type")
)
type ObjectType uint8
const (
TypeNull ObjectType = iota
TypeStruct
TypeList
TypePointerList
TypeBitList
)
func (o ObjectType) String() string {
switch o {
case TypeNull:
return "TypeNull"
case TypeStruct:
return "TypeStruct"
case TypeList:
return "TypeList"
case TypePointerList:
return "TypePointerList"
case TypeBitList:
return "TypeBitList"
default:
return "Unknown ObjectType"
}
}
type Message interface {
NewSegment(minsz int) (*Segment, error)
Lookup(segid uint32) (*Segment, error)
}
type Segment struct {
Message Message
Data []uint8
Id uint32
RootDone bool
}
type Object struct {
Segment *Segment
off int // in bytes
length int
datasz int // in bytes
ptrs int
typ ObjectType
flags uint
}
func (o Object) DupWithOff(off int) Object {
return Object{
Segment: o.Segment,
off: off,
length: o.length,
datasz: o.datasz,
ptrs: o.ptrs,
typ: o.typ,
flags: o.flags,
}
}
type Void struct{}
type Struct Object
type VoidList Object
type BitList Object
type Int8List Object
type UInt8List Object
type Int16List Object
type UInt16List Object
type Int32List Object
type UInt32List Object
type Float32List Object
type Int64List Object
type UInt64List Object
type Float64List Object
type PointerList Object
type TextList Object
type DataList Object
func (p VoidList) Len() int { return p.length }
func (p BitList) Len() int { return p.length }
func (p Int8List) Len() int { return p.length }
func (p UInt8List) Len() int { return p.length }
func (p Int16List) Len() int { return p.length }
func (p UInt16List) Len() int { return p.length }
func (p Int32List) Len() int { return p.length }
func (p UInt32List) Len() int { return p.length }
func (p Float32List) Len() int { return p.length }
func (p Int64List) Len() int { return p.length }
func (p UInt64List) Len() int { return p.length }
func (p Float64List) Len() int { return p.length }
func (p PointerList) Len() int { return p.length }
func (p TextList) Len() int { return p.length }
func (p DataList) Len() int { return p.length }
func (p Object) HasData() bool {
switch p.typ {
case TypeList:
return p.length > 0 && (p.datasz != 0 || p.ptrs != 0)
case TypePointerList:
return p.length > 0
case TypeBitList:
return p.length > 0
case TypeStruct:
return p.datasz != 0 || p.ptrs != 0
default:
return false
}
}
const (
maxDataSize = 0xFFFF * 8
maxPtrs = 0xFFFF
// flags
bitOffsetMask = 7
isBitListMember = 8
isListMember = 16
isCompositeList = 32
isRoot = 64
hasPointerTag = 128
)
// used in orable30BitOffsetPart() and signedOffsetFromStructPointer()
var Zerohi32 uint64
func init() {
// initialize Zerohi32 once
var minus1 int32 = -1
u32 := uint32(minus1)
Zerohi32 = uint64(u32)
}
func (s *Segment) Root(off int) Object {
if off+8 > len(s.Data) {
return Object{}
}
return s.readPtr(off)
}
func (s *Segment) NewRoot() (PointerList, int, error) {
n, err := s.create(8, Object{typ: TypePointerList, length: 1, ptrs: 1, flags: isRoot})
return PointerList(n), n.off / 8, err
}
func (s *Segment) NewText(v string) Object {
n := s.NewUInt8List(len(v) + 1)
copy(n.Segment.Data[n.off:], v)
return Object(n)
}
func (s *Segment) NewData(v []byte) Object {
n := s.NewUInt8List(len(v))
copy(n.Segment.Data[n.off:], v)
return Object(n)
}
func (s *Segment) NewBitList(sz int) BitList {
n, _ := s.create((sz+63)/8, Object{typ: TypeBitList, length: sz})
return BitList(n)
}
//func (s *Segment) NewVoidList(sz int) VoidList { return VoidList{typ: TypeList, length: sz, datasz: 0} }
func (s *Segment) NewVoidList(sz int) VoidList { return VoidList(s.newList(0, sz)) }
func (s *Segment) NewInt8List(sz int) Int8List { return Int8List(s.newList(1, sz)) }
func (s *Segment) NewUInt8List(sz int) UInt8List { return UInt8List(s.newList(1, sz)) }
func (s *Segment) NewInt16List(sz int) Int16List { return Int16List(s.newList(2, sz)) }
func (s *Segment) NewUInt16List(sz int) UInt16List { return UInt16List(s.newList(2, sz)) }
func (s *Segment) NewFloat32List(sz int) Float32List { return Float32List(s.newList(4, sz)) }
func (s *Segment) NewInt32List(sz int) Int32List { return Int32List(s.newList(4, sz)) }
func (s *Segment) NewUInt32List(sz int) UInt32List { return UInt32List(s.newList(4, sz)) }
func (s *Segment) NewFloat64List(sz int) Float64List { return Float64List(s.newList(8, sz)) }
func (s *Segment) NewInt64List(sz int) Int64List { return Int64List(s.newList(8, sz)) }
func (s *Segment) NewUInt64List(sz int) UInt64List { return UInt64List(s.newList(8, sz)) }
func (s *Segment) newList(datasz, length int) Object {
n, _ := s.create(datasz*length, Object{typ: TypeList, length: length, datasz: datasz})
return n
}
func (s *Segment) NewTextList(sz int) TextList { return TextList(s.NewPointerList(sz)) }
func (s *Segment) NewDataList(sz int) DataList { return DataList(s.NewPointerList(sz)) }
func (s *Segment) NewPointerList(sz int) PointerList {
n, _ := s.create(sz*8, Object{typ: TypePointerList, length: sz, ptrs: 1})
return PointerList(n)
}
/*
Canonicalization discussion on the Capnproto mailing list:
https://groups.google.com/d/msg/capnproto/V_NysVkvNgs/P5RfeQyMvpkJ
> Q:
> 1: lists of non-trivial but honogeneous structs;
> These will often be 7 (inline-composite). However, is it legal (and: common?) for such to be stored as 6 (pointer)? At the moment I can support both - I just want to make sure I'm not emitting something unusual.
Actually, through Cap'n Proto 0.4.x, it is not only allowed, it is expected. In fact, a struct list can be encoded with any element size, as long as the struct fits. For example, if a struct type Foo has two int16 fields, then List(Foo) can be encoded as a list of 32-bit values.
This rule was intended partly for optimization and partly to solve a common problem, for which I'll give a real example: In schema.capnp, to represent an interface's list of superclasses, I used a List(UInt64) containing the type IDs. Recently, when generics were implemented, I found I now additionally had to specify a type parameterization for each superclass, in addition to its type ID, since they could be generic. Because of the rule that struct lists may be encoded as primitive lists, I was able to simply replace my List(UInt64) with List(Superclass), where Superclass is a struct containing a UInt64 type ID as its @0 field. This change is backwards-compatible. Without this ability, I would have had to use parallel arrays, which would be terrible.
With all that said, in Cap'n Proto 0.5.x, we are making a change: When reading a struct list, the above still applies (a list of non-structs must be accepted), but when writing a struct list, the implementation should always prefer to encode the list using the inline-composite element type.
The reason for this change is so that we can define a canonicalization algorithm for Cap'n Proto messages that does not require knowledge of the schema yet produces stable canonicalizations even as new fields are added to struct types. The canonicalization algorithm needs to know when a value is a struct so that it can truncate trailing zeroes.
List(primitive) -> List(struct) updates are still allowed (because they have proven useful), but with the understanding that this kind of upgrade breaks canonicalization. This seems like an acceptable trade-off.
We also made a second change: Previously, consistent with the above rule, a list of structs where each struct contains a single boolean field was allowed to be encoded as a bit list. We now make a special exception to say that this is *not* allowed, because the implementation burden was too high and we have doubts about whether it would ever be used in practice. In other words, we used to allow List(Bool) -> List(struct) updates where the struct's @0 field was of type Bool, but we no longer allow this. For all types other than Bool, it is still allowed.
*/
const CanonicalizableOn = true
func (s *Segment) NewCompositeList(datasz, ptrs, length int) PointerList {
if datasz < 0 || datasz > maxDataSize || ptrs < 0 || ptrs > maxPtrs {
return PointerList{}
} else if ptrs > 0 || datasz > 8 || CanonicalizableOn {
datasz = (datasz + 7) &^ 7
n, _ := s.create(8+length*(datasz+8*ptrs), Object{typ: TypeList, length: length, datasz: datasz, ptrs: ptrs, flags: isCompositeList})
n.off += 8
hdr := structPointer | uint64(length)<<2 | uint64(datasz/8)<<32 | uint64(ptrs)<<48
binary.LittleEndian.PutUint64(s.Data[n.off-8:], hdr)
return PointerList(n)
} else if datasz > 4 {
datasz = (datasz + 7) &^ 7
} else if datasz > 2 {
datasz = (datasz + 3) &^ 3
}
n, _ := s.create(length*(datasz+8*ptrs), Object{typ: TypeList, length: length, datasz: datasz, ptrs: ptrs})
return PointerList(n)
}
func (s *Segment) NewRootStruct(datasz, ptrs int) Struct {
r, _, err := s.NewRoot()
if err != nil {
return Struct{}
}
v := s.NewStruct(datasz, ptrs)
r.Set(0, Object(v))
return v
}
func (s *Segment) NewStruct(datasz, ptrs int) Struct {
if datasz < 0 || datasz > maxDataSize || ptrs < 0 || ptrs > maxPtrs {
return Struct{}
}
datasz = (datasz + 7) &^ 7
n, _ := s.create(datasz+ptrs*8, Object{typ: TypeStruct, datasz: datasz, ptrs: ptrs})
return Struct(n)
}
// NewStructAR (AutoRoot): experimental Root setting: assumes the
// struct is the root iff it is the first allocation in a segment.
func (s *Segment) NewStructAR(datasz, ptrs int) Struct {
if s.RootDone {
return s.NewStruct(datasz, ptrs)
} else {
s.RootDone = true
return s.NewRootStruct(datasz, ptrs)
}
}
// sz is in bytes
func (s *Segment) create(sz int, n Object) (Object, error) {
// rounded up to word-boundary number of bytes:
sz = (sz + 7) &^ 7
if uint64(sz) > uint64(math.MaxUint32)-8 {
return Object{}, ErrOverlarge
}
if s == nil {
s = NewBuffer(nil)
}
tag := false
if len(s.Data)+sz > cap(s.Data) {
// If we can't fit the data in the current segment, we always
// return a far pointer to a tag in the new segment.
if (n.flags & isRoot) != 0 {
tag = true
sz += 8
}
news, err := s.Message.NewSegment(sz)
if err != nil {
return Object{}, err
}
// NewSegment is allowed to grow the segment and return it. In
// which case we don't want to create a tag.
if tag && news == s {
sz -= 8
tag = false
}
s = news
}
n.Segment = s
n.off = len(s.Data)
s.Data = s.Data[:len(s.Data)+sz] // NewSegment() makes this promise
if tag {
n.off += 8
binary.LittleEndian.PutUint64(s.Data[n.off-8:], n.value(n.off-8))
n.flags |= hasPointerTag
}
for i := n.off; i < len(s.Data); i++ {
s.Data[i] = 0
}
return n, nil
}
func (p Object) Type() ObjectType { return p.typ }
func (p Object) ToStruct() Struct {
if p.typ == TypeStruct {
return Struct(p)
} else {
return Struct{}
}
}
func (p Object) ToStructDefault(s *Segment, tagoff int) Struct {
if p.typ == TypeStruct {
return Struct(p)
} else {
return s.Root(tagoff).ToStruct()
}
}
func (p Object) ToText() string { return p.ToTextDefault("") }
func (p Object) ToTextDefault(def string) string {
if p.typ != TypeList || p.datasz != 1 || p.length == 0 || p.Segment.Data[p.off+p.length-1] != 0 {
return def
}
return string(p.Segment.Data[p.off : p.off+p.length-1])
}
func (p Object) ToData() []byte { return p.ToDataDefault(nil) }
func (p Object) ToDataDefault(def []byte) []byte {
if p.typ != TypeList || p.datasz != 1 {
return def
}
return p.Segment.Data[p.off : p.off+p.length]
}
func (p Object) ToDataTrimLastByte() []byte { return p.ToDataDefaultTrimLastByte(nil) }
func (p Object) ToDataDefaultTrimLastByte(def []byte) []byte {
if p.typ != TypeList || p.datasz != 1 || p.length == 0 || p.Segment.Data[p.off+p.length-1] != 0 {
return def
}
return p.Segment.Data[p.off : p.off+p.length-1]
}
// There is no need to check the object type for lists as:
// 1. Its a list (TypeList, TypeBitList, TypePointerList)
// 2. Its TypeStruct, but then the length is 0
// 3. Its TypeNull, but then the length is 0
func (p Object) ToVoidList() VoidList { return VoidList(p) }
func (p Object) ToBitList() BitList { return BitList(p) }
func (p Object) ToInt8List() Int8List { return Int8List(p) }
func (p Object) ToUInt8List() UInt8List { return UInt8List(p) }
func (p Object) ToInt16List() Int16List { return Int16List(p) }
func (p Object) ToUInt16List() UInt16List { return UInt16List(p) }
func (p Object) ToInt32List() Int32List { return Int32List(p) }
func (p Object) ToUInt32List() UInt32List { return UInt32List(p) }
func (p Object) ToFloat32List() Float32List { return Float32List(p) }
func (p Object) ToInt64List() Int64List { return Int64List(p) }
func (p Object) ToUInt64List() UInt64List { return UInt64List(p) }
func (p Object) ToFloat64List() Float64List { return Float64List(p) }
func (p Object) ToPointerList() PointerList { return PointerList(p) }
func (p Object) ToTextList() TextList { return TextList(p) }
func (p Object) ToDataList() DataList { return DataList(p) }
func (p Object) ToListDefault(s *Segment, tagoff int) Object {
switch p.typ {
case TypeList, TypeBitList, TypePointerList:
return p
default:
return s.Root(tagoff)
}
}
func (p Object) ToObjectDefault(s *Segment, tagoff int) Object {
if p.typ == TypeNull {
return s.Root(tagoff)
} else {
return p
}
}
func (p Struct) GetObject(off int) Object {
if uint(off) < uint(p.ptrs) {
return p.Segment.readPtr(p.off + p.datasz + off*8)
} else {
return Object{}
}
}
func (p Struct) SetObject(i int, src Object) {
if uint(i) < uint(p.ptrs) {
//replaceMe := p.Segment.readPtr(p.off + p.datasz + i*8)
//copyStructHandlingVersionSkew(replaceMe, src, nil, 0, 0, 0)
p.Segment.writePtr(p.off+p.datasz+i*8, src, nil, 0)
}
}
func (p Struct) Get1(bitoff int) bool {
off := uint(p.off*8 + bitoff)
if bitoff == 0 && (p.flags&isBitListMember) != 0 {
off += p.flags & bitOffsetMask
} else if bitoff < 0 || bitoff >= p.datasz*8 {
return false
}
return p.Segment.Data[off/8]&(1<<uint(off%8)) != 0
}
func (p Struct) Set1(bitoff int, v bool) {
off := uint(p.off*8 + bitoff)
if bitoff == 0 && (p.flags&isBitListMember) != 0 {
off += p.flags & bitOffsetMask
} else if bitoff < 0 || bitoff >= p.datasz*8 {
return
}
if v {
p.Segment.Data[off/8] |= 1 << (off % 8)
} else {
p.Segment.Data[off/8] &^= 1 << (off % 8)
}
}
func (p Struct) Get8(off int) uint8 {
if off < p.datasz {
return p.Segment.Data[uint(p.off)+uint(off)]
} else {
return 0
}
}
func (p Struct) Get16(off int) uint16 {
if off < p.datasz {
return binary.LittleEndian.Uint16(p.Segment.Data[uint(p.off)+uint(off):])
} else {
return 0
}
}
func (p Struct) Get32(off int) uint32 {
if off < p.datasz {
return binary.LittleEndian.Uint32(p.Segment.Data[uint(p.off)+uint(off):])
} else {
return 0
}
}
func (p Struct) Get64(off int) uint64 {
if off < p.datasz {
return binary.LittleEndian.Uint64(p.Segment.Data[uint(p.off)+uint(off):])
} else {
return 0
}
}
func (p Struct) Set8(off int, v uint8) {
if uint(off) < uint(p.datasz) {
p.Segment.Data[uint(p.off)+uint(off)] = v
}
}
func (p Struct) Set16(off int, v uint16) {
if uint(off) < uint(p.datasz) {
binary.LittleEndian.PutUint16(p.Segment.Data[uint(p.off)+uint(off):], v)
}
}
func (p Struct) Set32(off int, v uint32) {
if uint(off) < uint(p.datasz) {
binary.LittleEndian.PutUint32(p.Segment.Data[uint(p.off)+uint(off):], v)
}
}
func (p Struct) Set64(off int, v uint64) {
if uint(off) < uint(p.datasz) {
binary.LittleEndian.PutUint64(p.Segment.Data[uint(p.off)+uint(off):], v)
}
}
func (p BitList) At(i int) bool {
if i < 0 || i >= p.length {
return false
}
switch p.typ {
case TypePointerList:
m := p.Segment.readPtr(p.off + i*8)
return m.typ == TypeStruct && m.datasz > 0 && (m.Segment.Data[0]&1) != 0
case TypeList:
off := p.off + i*(p.datasz+p.ptrs*8)
return (p.Segment.Data[off] & 1) != 0
case TypeBitList:
return (p.Segment.Data[p.off+i/8] & (1 << uint(i%8))) != 0
default:
return false
}
}
func (p BitList) Set(i int, v bool) {
if i < 0 || i >= p.length {
return
}
switch p.typ {
case TypePointerList:
m := p.Segment.readPtr(p.off + i*8)
if m.typ == TypeStruct && m.datasz > 0 {
if v {
m.Segment.Data[0] |= 1
} else {
m.Segment.Data[0] &^= 1
}
}
case TypeList:
off := p.off + i*(p.datasz+p.ptrs*8)
if v {
p.Segment.Data[off] |= 1
} else {
p.Segment.Data[off] &^= 1
}
case TypeBitList:
if v {
p.Segment.Data[p.off+i/8] |= 1 << uint(i%8)
} else {
p.Segment.Data[p.off+i/8] &^= 1 << uint(i%8)
}
}
}
func (p Object) listData(i int, sz int) []byte {
if i < 0 || i >= p.length {
return nil
}
switch p.typ {
case TypePointerList:
m := p.Segment.readPtr(p.off + i*8)
if m.typ != TypeStruct || sz > m.datasz*8 {
return nil
}
return m.Segment.Data[m.off:]
case TypeList:
if sz > p.datasz*8 {
return nil
}
off := p.off + i*(p.datasz+p.ptrs*8)
return p.Segment.Data[off:]
default: // including TypeBitList as this is only used for 8 bit and larger
return nil
}
}
func (p Int8List) At(i int) int8 { return int8(UInt8List(p).At(i)) }
func (p UInt8List) At(i int) uint8 {
if data := Object(p).listData(i, 8); data != nil {
return data[0]
} else {
return 0
}
}
func (p Int16List) At(i int) int16 { return int16(UInt16List(p).At(i)) }
func (p UInt16List) At(i int) uint16 {
if data := Object(p).listData(i, 16); data != nil {
return binary.LittleEndian.Uint16(data)
} else {
return 0
}
}
func (p Int32List) At(i int) int32 { return int32(UInt32List(p).At(i)) }
func (p Float32List) At(i int) float32 { return math.Float32frombits(UInt32List(p).At(i)) }
func (p UInt32List) At(i int) uint32 {
if data := Object(p).listData(i, 32); data != nil {
return binary.LittleEndian.Uint32(data)
} else {
return 0
}
}
func (p Int64List) At(i int) int64 { return int64(UInt64List(p).At(i)) }
func (p Float64List) At(i int) float64 { return math.Float64frombits(UInt64List(p).At(i)) }
func (p UInt64List) At(i int) uint64 {
if data := Object(p).listData(i, 64); data != nil {
return binary.LittleEndian.Uint64(data)
} else {
return 0
}
}
func (p Int8List) Set(i int, v int8) { UInt8List(p).Set(i, uint8(v)) }
func (p UInt8List) Set(i int, v uint8) {
if data := Object(p).listData(i, 8); data != nil {
data[0] = v
}
}
func (p Int16List) Set(i int, v int16) { UInt16List(p).Set(i, uint16(v)) }
func (p UInt16List) Set(i int, v uint16) {
if data := Object(p).listData(i, 16); data != nil {
binary.LittleEndian.PutUint16(data, v)
}
}
func (p Int32List) Set(i int, v int32) { UInt32List(p).Set(i, uint32(v)) }
func (p Float32List) Set(i int, v float32) { UInt32List(p).Set(i, math.Float32bits(v)) }
func (p UInt32List) Set(i int, v uint32) {
if data := Object(p).listData(i, 32); data != nil {
binary.LittleEndian.PutUint32(data, v)
}
}
func (p Int64List) Set(i int, v int64) { UInt64List(p).Set(i, uint64(v)) }
func (p Float64List) Set(i int, v float64) { UInt64List(p).Set(i, math.Float64bits(v)) }
func (p UInt64List) Set(i int, v uint64) {
if data := Object(p).listData(i, 64); data != nil {
binary.LittleEndian.PutUint64(data, v)
}
}
func (p BitList) ToArray() []bool {
v := make([]bool, p.Len())
for i := range v {
v[i] = p.At(i)
}
return v
}
// UInt8List.ToArray contains an important optimization: the returned slice points directly
// to the underlying segment bytes, not a copy. This is typically what is wanted, but
// be aware if you change the segment you will change the contents of the returned byte slice.
func (p UInt8List) ToArray() []uint8 {
if p.typ == TypeList && p.datasz == 1 && p.ptrs == 0 {
// an important optimization to reduce copying: the returned slices points to the actual bytes instead of a copy.
// fmt.Printf("\n *** UInt8List.ToArray(): copy avoidance used!\n")
return p.Segment.Data[p.off : p.off+p.length]
}
v := make([]uint8, p.Len())
for i := range v {
v[i] = p.At(i)
}
return v
}
func (p Int8List) ToArray() []int8 {
v := make([]int8, p.Len())
for i := range v {
v[i] = p.At(i)
}
return v
}
func (p UInt16List) ToArray() []uint16 {
v := make([]uint16, p.Len())
for i := range v {
v[i] = p.At(i)
}
return v
}
func (p UInt16List) ToEnumArray() *[]uint16 {
v := make([]uint16, p.Len())
for i := range v {
v[i] = p.At(i)
}
return &v
}
func (p Int16List) ToArray() []int16 {
v := make([]int16, p.Len())
for i := range v {
v[i] = p.At(i)
}
return v
}
func (p UInt32List) ToArray() []uint32 {
v := make([]uint32, p.Len())
for i := range v {
v[i] = p.At(i)
}
return v
}
func (p Float32List) ToArray() []float32 {
v := make([]float32, p.Len())
for i := range v {
v[i] = p.At(i)
}
return v
}
func (p Int32List) ToArray() []int32 {
v := make([]int32, p.Len())
for i := range v {
v[i] = p.At(i)
}
return v
}
func (p Int64List) ToArray() []int64 {
v := make([]int64, p.Len())
for i := range v {
v[i] = p.At(i)
}
return v
}
func (p Int64List) ToIntArray() []int {
v := make([]int, p.Len())
for i := range v {
v[i] = int(p.At(i))
}
return v
}
func (p Float64List) ToArray() []float64 {
v := make([]float64, p.Len())
for i := range v {
v[i] = p.At(i)
}
return v
}
func (p UInt64List) ToArray() []uint64 {
v := make([]uint64, p.Len())
for i := range v {
v[i] = p.At(i)
}
return v
}
func (p TextList) ToArray() []string {
v := make([]string, p.Len())
for i := range v {
v[i] = p.At(i)
}
return v
}
func (p DataList) ToArray() [][]byte {
v := make([][]byte, p.Len())
for i := range v {
v[i] = p.At(i)
}
return v
}
func (p PointerList) ToArray() *[]Object {
v := make([]Object, p.Len())
for i := range v {
v[i] = p.At(i)
}
return &v
}
func (p TextList) At(i int) string { return PointerList(p).At(i).ToText() }
func (p TextList) AtAsBytes(i int) []byte { return PointerList(p).At(i).ToDataDefaultTrimLastByte(nil) }
func (p DataList) At(i int) []byte { return PointerList(p).At(i).ToData() }
func (p PointerList) At(i int) Object {
if i < 0 || i >= p.length {
return Object{}
}
switch p.typ {
case TypeList:
return Object{
Segment: p.Segment,
typ: TypeStruct,
off: p.off + i*(p.datasz+p.ptrs*8),
datasz: p.datasz,
ptrs: p.ptrs,
flags: isListMember,
}
case TypePointerList:
return p.Segment.readPtr(p.off + i*8)
case TypeBitList:
return Object{
Segment: p.Segment,
typ: TypeStruct,
off: p.off + i/8,
flags: uint(i%8) | isBitListMember,
datasz: 0,
ptrs: 0,
}
default:
return Object{}
}
}
// listpos allows us to use this routine for copying elements between lists
func copyStructHandlingVersionSkew(dest Object, src Object, copies *rbtree.Tree, depth int, destListPos int, srcListPos int) error {
// handle VoidList destinations
if dest.Segment == nil {
return nil
}
destElemSz := dest.datasz + dest.ptrs*8
srcElemSz := src.datasz + src.ptrs*8
// handle assignment into a size-zero object/empty struct/old version
//if destElemSz == 0 {
// return nil
// }
destListInc := destElemSz * destListPos
srcListInc := srcElemSz * srcListPos
toData := dest.Segment.Data[dest.off+destListInc : dest.off+destListInc+dest.datasz]
//fmt.Printf("\n\n debug: destElemSz = %d, srcElemSz = %d, destListInc = %d, srcListInc = %d, toData = %#v, len(toData)=%d\n", destElemSz, srcElemSz, destListInc, srcListInc, toData, len(toData))
// Q: how does version handling happen here, when the
// desination toData[] slice can be bigger or smaller
// than the source data slice, which is in
// src.Segment.Data[src.off:src.off+src.datasz] ?
//
// A: Newer fields only come *after* old fields. Note that
// copy only copies min(len(src), len(dst)) size,
// and then we manually zero the rest in the for loop
// that writes toData[j] = 0.
//
// data section:
//fmt.Printf("\n\n debug: len(src.Segment.Data) = %d, src.off(%d)+srcListInc(%d) = %d\n", len(src.Segment.Data), src.off, srcListInc, src.off+srcListInc)
from := src.Segment.Data[src.off+srcListInc : src.off+srcListInc+src.datasz]
//fmt.Printf("\n\n debug: len(src.Segment.Data) = %d, src.off(%d)+srcListInc(%d) = %d, len(from)=%d\n", len(src.Segment.Data), src.off, srcListInc, src.off+srcListInc, len(from))
copyCount := copy(toData, from)
//fmt.Printf("\n\n debug: copyCount = %d\n", copyCount)
toData = toData[copyCount:]
for j := range toData {
toData[j] = 0
}
// ptrs section:
// version handling: we ignore any extra-newer-pointers in src,
// i.e. the case when srcPtrSize > dstPtrSize, by only
// running j over the size of dstPtrSize, the destination size.
srcPtrSize := src.ptrs * 8
dstPtrSize := int(dest.ptrs * 8)
for j := 0; j < dstPtrSize; j += 8 {
if j < srcPtrSize {
m := src.Segment.readPtr(src.off + srcListInc + src.datasz + j)
//fmt.Printf("debug: PointerList.Set(i=%d, src=%#v). source ptr is m = %#v\n", i, src, m)
if err := dest.Segment.writePtr(dest.off+destListInc+dest.datasz+j, m, copies, depth+1); err != nil {
return err
}
} else {
// destination p is a newer version than source
// so these extra new pointer fields in p must be zeroed.
binary.LittleEndian.PutUint64(dest.Segment.Data[dest.off+destListInc+dest.datasz+j:], 0)
}
}
// Nothing more here: so any other pointers in srcPtrSize beyond
// those in dstPtrSize are ignored and discarded.
return nil
} // end copyStructHandlingVersionSkew()
func (p TextList) Set(i int, v string) { PointerList(p).Set(i, p.Segment.NewText(v)) }
func (p DataList) Set(i int, v []byte) { PointerList(p).Set(i, p.Segment.NewData(v)) }
func (p PointerList) Set(i int, src Object) error {
if i < 0 || i >= p.length {
return nil
}
switch p.typ {
case TypeList:
if src.typ != TypeStruct {
src = Object{}
}
err := copyStructHandlingVersionSkew(Object(p), src, nil, 0, i, 0)
if err != nil {
return err
}
return nil
case TypePointerList:
return p.Segment.writePtr(p.off+i*8, src, nil, 0)
case TypeBitList:
if src.ToStruct().Get1(0) {
p.Segment.Data[p.off+i/8] |= 1 << uint(i%8)
} else {
p.Segment.Data[p.off+i/8] &^= 1 << uint(i%8)
}
return nil
default:
return nil
}
}
func (s *Segment) lookupSegment(id uint32) (*Segment, error) {
if s.Id != id {
return s.Message.Lookup(id)
} else {
return s, nil
}
}
const (
structPointer = 0
listPointer = 1
farPointer = 2
doubleFarPointer = 6
voidList = 0
bit1List = 1
byte1List = 2
byte2List = 3
byte4List = 4
byte8List = 5
pointerList = 6
compositeList = 7
)
func (s *Segment) readPtr(off int) Object {
var err error
val := binary.LittleEndian.Uint64(s.Data[off:])
//fmt.Printf("readPtr see val= %x\n", val)
switch val & 7 {
case doubleFarPointer:
// A double far pointer points to a double pointer, where the
// first points to the actual data, and the second is the tag
// that would normally be placed right before the data (offset
// == 0).
faroff := int((uint32(val) >> 3) * 8)
segid := uint32(val >> 32)
if s, err = s.lookupSegment(segid); err != nil || uint(faroff)+16 > uint(len(s.Data)) {
return Object{}
}
far := binary.LittleEndian.Uint64(s.Data[faroff:])
tag := binary.LittleEndian.Uint64(s.Data[faroff+8:])
// The far tag should not be another double and the tag should
// be struct/list with a 0 offset.
if far&7 != farPointer || uint32(tag) > listPointer {
return Object{}
}
segid = uint32(far >> 32)
if s, err = s.lookupSegment(segid); err != nil {
return Object{}
}
// -8 because far pointers reference from the start of the
// segment, but offsets reference the end of the pointer data.