| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE.md file. | |
| 4 | |
| 5 package dartino; | |
| 6 | |
| 7 public class MessageBuilder { | |
| 8 public MessageBuilder(int space) { | |
| 9 segments = 1; | |
| 10 first = new BuilderSegment(this, 0, space); | |
| 11 last = first; | |
| 12 } | |
| 13 | |
| 14 public BuilderSegment first() { return first; } | |
| 15 | |
| 16 public int segments() { return segments; } | |
| 17 | |
| 18 public Builder initRoot(Builder builder, int size) { | |
| 19 // Return value and arguments use the same space. Therefore, | |
| 20 // the size of any struct needs to be at least 8 bytes in order | |
| 21 // to have room for the return address. | |
| 22 if (size == 0) size = 8; | |
| 23 int offset = first.allocate(56 + size); | |
| 24 builder.segment = first; | |
| 25 builder.base = offset + 56; | |
| 26 return builder; | |
| 27 } | |
| 28 | |
| 29 public int computeUsed() { | |
| 30 int result = 0; | |
| 31 BuilderSegment current = first; | |
| 32 while (current != null) { | |
| 33 result += current.used(); | |
| 34 current = current.next(); | |
| 35 } | |
| 36 return result; | |
| 37 } | |
| 38 | |
| 39 public BuilderSegment findSegmentForBytes(int bytes) { | |
| 40 if (last.hasSpaceForBytes(bytes)) return last; | |
| 41 int capacity = (bytes > 8192) ? bytes : 8192; | |
| 42 BuilderSegment segment = new BuilderSegment(this, segments++, capacity); | |
| 43 last.setNext(segment); | |
| 44 last = segment; | |
| 45 return segment; | |
| 46 } | |
| 47 | |
| 48 private BuilderSegment first; | |
| 49 private BuilderSegment last; | |
| 50 private int segments; | |
| 51 }; | |
| OLD | NEW |