OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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 file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef VM_DATASTREAM_H_ | 5 #ifndef VM_DATASTREAM_H_ |
6 #define VM_DATASTREAM_H_ | 6 #define VM_DATASTREAM_H_ |
7 | 7 |
8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
9 #include "platform/utils.h" | 9 #include "platform/utils.h" |
10 #include "vm/allocation.h" | 10 #include "vm/allocation.h" |
11 #include "vm/globals.h" | 11 #include "vm/globals.h" |
12 | 12 |
13 namespace dart { | 13 namespace dart { |
14 | 14 |
15 static const int8_t kDataBitsPerByte = 7; | 15 static const int8_t kDataBitsPerByte = 7; |
16 static const int8_t kByteMask = (1 << kDataBitsPerByte) - 1; | 16 static const int8_t kByteMask = (1 << kDataBitsPerByte) - 1; |
17 static const int8_t kMaxUnsignedDataPerByte = kByteMask; | 17 static const int8_t kMaxUnsignedDataPerByte = kByteMask; |
18 static const int8_t kMinDataPerByte = -(1 << (kDataBitsPerByte - 1)); | 18 static const int8_t kMinDataPerByte = -(1 << (kDataBitsPerByte - 1)); |
19 static const int8_t kMaxDataPerByte = (~kMinDataPerByte & kByteMask); | 19 static const int8_t kMaxDataPerByte = (~kMinDataPerByte & kByteMask); |
20 static const uint8_t kEndByteMarker = (255 - kMaxDataPerByte); | 20 static const uint8_t kEndByteMarker = (255 - kMaxDataPerByte); |
| 21 static const uint8_t kEndUnsignedByteMarker = (255 - kMaxUnsignedDataPerByte); |
21 | 22 |
22 typedef uint8_t* (*ReAlloc)(uint8_t* ptr, intptr_t old_size, intptr_t new_size); | 23 typedef uint8_t* (*ReAlloc)(uint8_t* ptr, intptr_t old_size, intptr_t new_size); |
23 | 24 |
24 // Stream for reading various types from a buffer. | 25 // Stream for reading various types from a buffer. |
25 class ReadStream : public ValueObject { | 26 class ReadStream : public ValueObject { |
26 public: | 27 public: |
27 ReadStream(const uint8_t* buffer, intptr_t size) : buffer_(buffer), | 28 ReadStream(const uint8_t* buffer, intptr_t size) : buffer_(buffer), |
28 current_(buffer), | 29 current_(buffer), |
29 end_(buffer + size) {} | 30 end_(buffer + size) {} |
30 | 31 |
(...skipping 25 matching lines...) Expand all Loading... |
56 }; | 57 }; |
57 | 58 |
58 template<typename T> | 59 template<typename T> |
59 class Raw<8, T> { | 60 class Raw<8, T> { |
60 public: | 61 public: |
61 static T Read(ReadStream* st) { | 62 static T Read(ReadStream* st) { |
62 return bit_cast<T>(st->Read<int64_t>()); | 63 return bit_cast<T>(st->Read<int64_t>()); |
63 } | 64 } |
64 }; | 65 }; |
65 | 66 |
| 67 // Reads 'len' bytes from the stream. |
66 void ReadBytes(uint8_t* addr, intptr_t len) { | 68 void ReadBytes(uint8_t* addr, intptr_t len) { |
67 ASSERT((end_ - current_) >= len); | 69 ASSERT((end_ - current_) >= len); |
68 memmove(addr, current_, len); | 70 memmove(addr, current_, len); |
69 current_ += len; | 71 current_ += len; |
70 } | 72 } |
71 | 73 |
| 74 intptr_t CurrentPosition() const { return current_ - buffer_; } |
| 75 |
| 76 void SetPosition(intptr_t value) { |
| 77 ASSERT((end_ - buffer_) > value); |
| 78 current_ = buffer_ + value; |
| 79 } |
| 80 |
| 81 intptr_t ReadUnsigned() { |
| 82 return Read<intptr_t>(kEndUnsignedByteMarker); |
| 83 } |
| 84 |
72 private: | 85 private: |
73 template<typename T> | 86 template<typename T> |
74 T Read() { | 87 T Read() { |
| 88 return Read<T>(kEndByteMarker); |
| 89 } |
| 90 |
| 91 template<typename T> |
| 92 T Read(uint8_t end_byte_marker) { |
75 uint8_t b = ReadByte(); | 93 uint8_t b = ReadByte(); |
76 if (b > kMaxUnsignedDataPerByte) { | 94 if (b > kMaxUnsignedDataPerByte) { |
77 return static_cast<T>(b) - kEndByteMarker; | 95 return static_cast<T>(b) - end_byte_marker; |
78 } | 96 } |
79 T r = 0; | 97 T r = 0; |
80 uint8_t s = 0; | 98 uint8_t s = 0; |
81 do { | 99 do { |
82 r |= static_cast<T>(b) << s; | 100 r |= static_cast<T>(b) << s; |
83 s += kDataBitsPerByte; | 101 s += kDataBitsPerByte; |
84 b = ReadByte(); | 102 b = ReadByte(); |
85 } while (b <= kMaxUnsignedDataPerByte); | 103 } while (b <= kMaxUnsignedDataPerByte); |
86 return r | ((static_cast<T>(b) - kEndByteMarker) << s); | 104 return r | ((static_cast<T>(b) - end_byte_marker) << s); |
87 } | 105 } |
88 | 106 |
89 uint8_t ReadByte() { | 107 uint8_t ReadByte() { |
90 ASSERT(current_ < end_); | 108 ASSERT(current_ < end_); |
91 return *current_++; | 109 return *current_++; |
92 } | 110 } |
93 | 111 |
94 private: | 112 private: |
95 const uint8_t* buffer_; | 113 const uint8_t* buffer_; |
96 const uint8_t* current_; | 114 const uint8_t* current_; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 }; | 173 }; |
156 | 174 |
157 template<typename T> | 175 template<typename T> |
158 class Raw<8, T> { | 176 class Raw<8, T> { |
159 public: | 177 public: |
160 static void Write(WriteStream* st, T value) { | 178 static void Write(WriteStream* st, T value) { |
161 st->Write<int64_t>(bit_cast<int64_t>(value)); | 179 st->Write<int64_t>(bit_cast<int64_t>(value)); |
162 } | 180 } |
163 }; | 181 }; |
164 | 182 |
| 183 void WriteUnsigned(intptr_t value) { |
| 184 ASSERT((value >= 0) && (value <= kIntptrMax)); |
| 185 while (value > kMaxUnsignedDataPerByte) { |
| 186 WriteByte(static_cast<uint8_t>(value & kByteMask)); |
| 187 value = value >> kDataBitsPerByte; |
| 188 } |
| 189 WriteByte(static_cast<uint8_t>(value + kEndUnsignedByteMarker)); |
| 190 } |
| 191 |
165 void WriteBytes(const uint8_t* addr, intptr_t len) { | 192 void WriteBytes(const uint8_t* addr, intptr_t len) { |
166 if ((end_ - current_) < len) { | 193 if ((end_ - current_) < len) { |
167 Resize(len); | 194 Resize(len); |
168 } | 195 } |
169 ASSERT((end_ - current_) >= len); | 196 ASSERT((end_ - current_) >= len); |
170 memmove(current_, addr, len); | 197 memmove(current_, addr, len); |
171 current_ += len; | 198 current_ += len; |
172 } | 199 } |
173 | 200 |
174 private: | 201 private: |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 uint8_t* current_; | 238 uint8_t* current_; |
212 intptr_t current_size_; | 239 intptr_t current_size_; |
213 ReAlloc alloc_; | 240 ReAlloc alloc_; |
214 | 241 |
215 DISALLOW_COPY_AND_ASSIGN(WriteStream); | 242 DISALLOW_COPY_AND_ASSIGN(WriteStream); |
216 }; | 243 }; |
217 | 244 |
218 } // namespace dart | 245 } // namespace dart |
219 | 246 |
220 #endif // VM_DATASTREAM_H_ | 247 #endif // VM_DATASTREAM_H_ |
OLD | NEW |