Lines Matching refs:b
31 b := &Builder{}
32 b.Bytes = make([]byte, initialSize)
33 b.head = UOffsetT(initialSize)
34 b.minalign = 1
35 b.vtables = make([]UOffsetT, 0, 16) // sensible default capacity
37 return b
42 func (b *Builder) Reset() {
43 if b.Bytes != nil {
44 b.Bytes = b.Bytes[:cap(b.Bytes)]
47 if b.vtables != nil {
48 b.vtables = b.vtables[:0]
51 if b.vtable != nil {
52 b.vtable = b.vtable[:0]
55 b.head = UOffsetT(len(b.Bytes))
56 b.minalign = 1
57 b.nested = false
58 b.finished = false
64 func (b *Builder) FinishedBytes() []byte {
65 b.assertFinished()
66 return b.Bytes[b.Head():]
70 func (b *Builder) StartObject(numfields int) {
71 b.assertNotNested()
72 b.nested = true
75 if cap(b.vtable) < numfields || b.vtable == nil {
76 b.vtable = make([]UOffsetT, numfields)
78 b.vtable = b.vtable[:numfields]
79 for i := 0; i < len(b.vtable); i++ {
80 b.vtable[i] = 0
84 b.objectEnd = b.Offset()
106 func (b *Builder) WriteVtable() (n UOffsetT) {
109 b.PrependSOffsetT(0)
111 objectOffset := b.Offset()
115 i := len(b.vtable) - 1
116 for ; i >= 0 && b.vtable[i] == 0; i-- {
118 b.vtable = b.vtable[:i+1]
125 for i := len(b.vtables) - 1; i >= 0; i-- {
127 vt2Offset := b.vtables[i]
128 vt2Start := len(b.Bytes) - int(vt2Offset)
129 vt2Len := GetVOffsetT(b.Bytes[vt2Start:])
133 vt2 := b.Bytes[vt2Start+metadata : vt2End]
137 if vtableEqual(b.vtable, objectOffset, vt2) {
148 for i := len(b.vtable) - 1; i >= 0; i-- {
150 if b.vtable[i] != 0 {
153 off = objectOffset - b.vtable[i]
156 b.PrependVOffsetT(VOffsetT(off))
162 objectSize := objectOffset - b.objectEnd
163 b.PrependVOffsetT(VOffsetT(objectSize))
166 vBytes := (len(b.vtable) + VtableMetadataFields) * SizeVOffsetT
167 b.PrependVOffsetT(VOffsetT(vBytes))
171 objectStart := SOffsetT(len(b.Bytes)) - SOffsetT(objectOffset)
172 WriteSOffsetT(b.Bytes[objectStart:],
173 SOffsetT(b.Offset())-SOffsetT(objectOffset))
177 b.vtables = append(b.vtables, b.Offset())
181 objectStart := SOffsetT(len(b.Bytes)) - SOffsetT(objectOffset)
182 b.head = UOffsetT(objectStart)
186 WriteSOffsetT(b.Bytes[b.head:],
190 b.vtable = b.vtable[:0]
195 func (b *Builder) EndObject() UOffsetT {
196 b.assertNested()
197 n := b.WriteVtable()
198 b.nested = false
204 func (b *Builder) growByteBuffer() {
205 if (int64(len(b.Bytes)) & int64(0xC0000000)) != 0 {
208 newLen := len(b.Bytes) * 2
213 if cap(b.Bytes) >= newLen {
214 b.Bytes = b.Bytes[:newLen]
216 extension := make([]byte, newLen-len(b.Bytes))
217 b.Bytes = append(b.Bytes, extension...)
221 copy(b.Bytes[middle:], b.Bytes[:middle])
226 func (b *Builder) Head() UOffsetT {
227 return b.head
231 func (b *Builder) Offset() UOffsetT {
232 return UOffsetT(len(b.Bytes)) - b.head
236 func (b *Builder) Pad(n int) {
238 b.PlaceByte(0)
247 func (b *Builder) Prep(size, additionalBytes int) {
249 if size > b.minalign {
250 b.minalign = size
254 alignSize := (^(len(b.Bytes) - int(b.Head()) + additionalBytes)) + 1
258 for int(b.head) <= alignSize+size+additionalBytes {
259 oldBufSize := len(b.Bytes)
260 b.growByteBuffer()
261 b.head += UOffsetT(len(b.Bytes) - oldBufSize)
263 b.Pad(alignSize)
267 func (b *Builder) PrependSOffsetT(off SOffsetT) {
268 b.Prep(SizeSOffsetT, 0) // Ensure alignment is already done.
269 if !(UOffsetT(off) <= b.Offset()) {
272 off2 := SOffsetT(b.Offset()) - off + SOffsetT(SizeSOffsetT)
273 b.PlaceSOffsetT(off2)
277 func (b *Builder) PrependUOffsetT(off UOffsetT) {
278 b.Prep(SizeUOffsetT, 0) // Ensure alignment is already done.
279 if !(off <= b.Offset()) {
282 off2 := b.Offset() - off + UOffsetT(SizeUOffsetT)
283 b.PlaceUOffsetT(off2)
291 func (b *Builder) StartVector(elemSize, numElems, alignment int) UOffsetT {
292 b.assertNotNested()
293 b.nested = true
294 b.Prep(SizeUint32, elemSize*numElems)
295 b.Prep(alignment, elemSize*numElems) // Just in case alignment > int.
296 return b.Offset()
300 func (b *Builder) EndVector(vectorNumElems int) UOffsetT {
301 b.assertNested()
304 b.PlaceUOffsetT(UOffsetT(vectorNumElems))
306 b.nested = false
307 return b.Offset()
311 func (b *Builder) CreateString(s string) UOffsetT {
312 b.assertNotNested()
313 b.nested = true
315 b.Prep(int(SizeUOffsetT), (len(s)+1)*SizeByte)
316 b.PlaceByte(0)
320 b.head -= l
321 copy(b.Bytes[b.head:b.head+l], s)
323 return b.EndVector(len(s))
327 func (b *Builder) CreateByteString(s []byte) UOffsetT {
328 b.assertNotNested()
329 b.nested = true
331 b.Prep(int(SizeUOffsetT), (len(s)+1)*SizeByte)
332 b.PlaceByte(0)
336 b.head -= l
337 copy(b.Bytes[b.head:b.head+l], s)
339 return b.EndVector(len(s))
343 func (b *Builder) CreateByteVector(v []byte) UOffsetT {
344 b.assertNotNested()
345 b.nested = true
347 b.Prep(int(SizeUOffsetT), len(v)*SizeByte)
351 b.head -= l
352 copy(b.Bytes[b.head:b.head+l], v)
354 return b.EndVector(len(v))
357 func (b *Builder) assertNested() {
362 if !b.nested {
367 func (b *Builder) assertNotNested() {
376 if b.nested {
381 func (b *Builder) assertFinished() {
387 if !b.finished {
395 func (b *Builder) PrependBoolSlot(o int, x, d bool) {
404 b.PrependByteSlot(o, val, def)
410 func (b *Builder) PrependByteSlot(o int, x, d byte) {
412 b.PrependByte(x)
413 b.Slot(o)
420 func (b *Builder) PrependUint8Slot(o int, x, d uint8) {
422 b.PrependUint8(x)
423 b.Slot(o)
430 func (b *Builder) PrependUint16Slot(o int, x, d uint16) {
432 b.PrependUint16(x)
433 b.Slot(o)
440 func (b *Builder) PrependUint32Slot(o int, x, d uint32) {
442 b.PrependUint32(x)
443 b.Slot(o)
450 func (b *Builder) PrependUint64Slot(o int, x, d uint64) {
452 b.PrependUint64(x)
453 b.Slot(o)
460 func (b *Builder) PrependInt8Slot(o int, x, d int8) {
462 b.PrependInt8(x)
463 b.Slot(o)
470 func (b *Builder) PrependInt16Slot(o int, x, d int16) {
472 b.PrependInt16(x)
473 b.Slot(o)
480 func (b *Builder) PrependInt32Slot(o int, x, d int32) {
482 b.PrependInt32(x)
483 b.Slot(o)
490 func (b *Builder) PrependInt64Slot(o int, x, d int64) {
492 b.PrependInt64(x)
493 b.Slot(o)
500 func (b *Builder) PrependFloat32Slot(o int, x, d float32) {
502 b.PrependFloat32(x)
503 b.Slot(o)
510 func (b *Builder) PrependFloat64Slot(o int, x, d float64) {
512 b.PrependFloat64(x)
513 b.Slot(o)
520 func (b *Builder) PrependUOffsetTSlot(o int, x, d UOffsetT) {
522 b.PrependUOffsetT(x)
523 b.Slot(o)
530 func (b *Builder) PrependStructSlot(voffset int, x, d UOffsetT) {
532 b.assertNested()
533 if x != b.Offset() {
536 b.Slot(voffset)
541 func (b *Builder) Slot(slotnum int) {
542 b.vtable[slotnum] = UOffsetT(b.Offset())
547 func (b *Builder) FinishWithFileIdentifier(rootTable UOffsetT, fid []byte) {
553 b.Prep(b.minalign, SizeInt32+fileIdentifierLength)
556 b.PlaceByte(fid[i])
559 b.Finish(rootTable)
563 func (b *Builder) Finish(rootTable UOffsetT) {
564 b.assertNotNested()
565 b.Prep(b.minalign, SizeUOffsetT)
566 b.PrependUOffsetT(rootTable)
567 b.finished = true
571 func vtableEqual(a []UOffsetT, objectStart UOffsetT, b []byte) bool {
572 if len(a)*SizeVOffsetT != len(b) {
577 x := GetVOffsetT(b[i*SizeVOffsetT : (i+1)*SizeVOffsetT])
594 func (b *Builder) PrependBool(x bool) {
595 b.Prep(SizeBool, 0)
596 b.PlaceBool(x)
601 func (b *Builder) PrependUint8(x uint8) {
602 b.Prep(SizeUint8, 0)
603 b.PlaceUint8(x)
608 func (b *Builder) PrependUint16(x uint16) {
609 b.Prep(SizeUint16, 0)
610 b.PlaceUint16(x)
615 func (b *Builder) PrependUint32(x uint32) {
616 b.Prep(SizeUint32, 0)
617 b.PlaceUint32(x)
622 func (b *Builder) PrependUint64(x uint64) {
623 b.Prep(SizeUint64, 0)
624 b.PlaceUint64(x)
629 func (b *Builder) PrependInt8(x int8) {
630 b.Prep(SizeInt8, 0)
631 b.PlaceInt8(x)
636 func (b *Builder) PrependInt16(x int16) {
637 b.Prep(SizeInt16, 0)
638 b.PlaceInt16(x)
643 func (b *Builder) PrependInt32(x int32) {
644 b.Prep(SizeInt32, 0)
645 b.PlaceInt32(x)
650 func (b *Builder) PrependInt64(x int64) {
651 b.Prep(SizeInt64, 0)
652 b.PlaceInt64(x)
657 func (b *Builder) PrependFloat32(x float32) {
658 b.Prep(SizeFloat32, 0)
659 b.PlaceFloat32(x)
664 func (b *Builder) PrependFloat64(x float64) {
665 b.Prep(SizeFloat64, 0)
666 b.PlaceFloat64(x)
671 func (b *Builder) PrependByte(x byte) {
672 b.Prep(SizeByte, 0)
673 b.PlaceByte(x)
678 func (b *Builder) PrependVOffsetT(x VOffsetT) {
679 b.Prep(SizeVOffsetT, 0)
680 b.PlaceVOffsetT(x)
684 func (b *Builder) PlaceBool(x bool) {
685 b.head -= UOffsetT(SizeBool)
686 WriteBool(b.Bytes[b.head:], x)
690 func (b *Builder) PlaceUint8(x uint8) {
691 b.head -= UOffsetT(SizeUint8)
692 WriteUint8(b.Bytes[b.head:], x)
696 func (b *Builder) PlaceUint16(x uint16) {
697 b.head -= UOffsetT(SizeUint16)
698 WriteUint16(b.Bytes[b.head:], x)
702 func (b *Builder) PlaceUint32(x uint32) {
703 b.head -= UOffsetT(SizeUint32)
704 WriteUint32(b.Bytes[b.head:], x)
708 func (b *Builder) PlaceUint64(x uint64) {
709 b.head -= UOffsetT(SizeUint64)
710 WriteUint64(b.Bytes[b.head:], x)
714 func (b *Builder) PlaceInt8(x int8) {
715 b.head -= UOffsetT(SizeInt8)
716 WriteInt8(b.Bytes[b.head:], x)
720 func (b *Builder) PlaceInt16(x int16) {
721 b.head -= UOffsetT(SizeInt16)
722 WriteInt16(b.Bytes[b.head:], x)
726 func (b *Builder) PlaceInt32(x int32) {
727 b.head -= UOffsetT(SizeInt32)
728 WriteInt32(b.Bytes[b.head:], x)
732 func (b *Builder) PlaceInt64(x int64) {
733 b.head -= UOffsetT(SizeInt64)
734 WriteInt64(b.Bytes[b.head:], x)
738 func (b *Builder) PlaceFloat32(x float32) {
739 b.head -= UOffsetT(SizeFloat32)
740 WriteFloat32(b.Bytes[b.head:], x)
744 func (b *Builder) PlaceFloat64(x float64) {
745 b.head -= UOffsetT(SizeFloat64)
746 WriteFloat64(b.Bytes[b.head:], x)
750 func (b *Builder) PlaceByte(x byte) {
751 b.head -= UOffsetT(SizeByte)
752 WriteByte(b.Bytes[b.head:], x)
756 func (b *Builder) PlaceVOffsetT(x VOffsetT) {
757 b.head -= UOffsetT(SizeVOffsetT)
758 WriteVOffsetT(b.Bytes[b.head:], x)
762 func (b *Builder) PlaceSOffsetT(x SOffsetT) {
763 b.head -= UOffsetT(SizeSOffsetT)
764 WriteSOffsetT(b.Bytes[b.head:], x)
768 func (b *Builder) PlaceUOffsetT(x UOffsetT) {
769 b.head -= UOffsetT(SizeUOffsetT)
770 WriteUOffsetT(b.Bytes[b.head:], x)