OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #ifndef VM_DATASTREAM_H_ | |
6 #define VM_DATASTREAM_H_ | |
7 | |
8 #include "platform/assert.h" | |
9 #include "vm/allocation.h" | |
10 #include "vm/globals.h" | |
11 | |
12 namespace dart { | |
13 | |
14 static const int8_t kSerializedBitsPerByte = 7; | |
hausner
2012/06/22 00:02:27
I know you just copied this code from another file
siva
2012/06/22 17:59:02
I have renamed the constants removing the Serializ
hausner
2012/06/22 18:54:37
Thanks for making the names shorter. I still have
siva
2012/06/22 20:36:29
Used calculations for 63, -64, 127 etc.
On 2012/0
| |
15 static const int8_t kMaxSerializedUnsignedValuePerByte = 127; | |
16 static const int8_t kMaxSerializedValuePerByte = 63; | |
17 static const int8_t kMinSerializedValuePerByte = -64; | |
18 static const uint8_t kEndByteMarker = (255 - kMaxSerializedValuePerByte); | |
19 static const int8_t kSerializedByteMask = (1 << kSerializedBitsPerByte) - 1; | |
20 | |
21 typedef uint8_t* (*ReAlloc)(uint8_t* ptr, intptr_t old_size, intptr_t new_size); | |
22 | |
23 // Stream for reading various types from a buffer. | |
24 class ReadStream : public ValueObject { | |
25 public: | |
26 ReadStream(const uint8_t* buffer, intptr_t size) : buffer_(buffer), | |
27 current_(buffer), | |
28 end_(buffer + size) {} | |
29 | |
30 template<int N, typename T> | |
31 class Raw { }; | |
32 | |
33 template<typename T> | |
34 class Raw<1, T> { | |
35 public: | |
36 static T Read(ReadStream* st) { | |
37 return bit_cast<T>(st->ReadByte()); | |
38 } | |
39 }; | |
40 | |
41 template<typename T> | |
42 class Raw<2, T> { | |
43 public: | |
44 static T Read(ReadStream* st) { | |
45 return bit_cast<T>(st->Read<int16_t>()); | |
46 } | |
47 }; | |
48 | |
49 template<typename T> | |
50 class Raw<4, T> { | |
51 public: | |
52 static T Read(ReadStream* st) { | |
53 return bit_cast<T>(st->Read<int32_t>()); | |
54 } | |
55 }; | |
56 | |
57 template<typename T> | |
58 class Raw<8, T> { | |
59 public: | |
60 static T Read(ReadStream* st) { | |
61 return bit_cast<T>(st->Read<int64_t>()); | |
62 } | |
63 }; | |
64 | |
65 void ReadBytes(uint8_t* addr, intptr_t len) { | |
66 ASSERT((current_ + len) < end_); | |
67 memmove(addr, current_, len); | |
68 current_ += len; | |
69 } | |
70 | |
71 private: | |
72 template<typename T> | |
73 T Read() { | |
74 uint8_t b = ReadByte(); | |
75 if (b > kMaxSerializedUnsignedValuePerByte) { | |
76 return static_cast<T>(b) - kEndByteMarker; | |
77 } | |
78 T r = 0; | |
79 uint8_t s = 0; | |
80 do { | |
81 r |= static_cast<T>(b) << s; | |
82 s += kSerializedBitsPerByte; | |
83 b = ReadByte(); | |
84 } while (b <= kMaxSerializedUnsignedValuePerByte); | |
85 return r | ((static_cast<T>(b) - kEndByteMarker) << s); | |
86 } | |
87 | |
88 uint8_t ReadByte() { | |
89 ASSERT(current_ < end_); | |
90 return *current_++; | |
91 } | |
92 | |
93 private: | |
94 const uint8_t* buffer_; | |
95 const uint8_t* current_; | |
96 const uint8_t* end_; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(ReadStream); | |
99 }; | |
100 | |
101 | |
102 // Stream for writing various types into a buffer. | |
103 class WriteStream : public ValueObject { | |
104 public: | |
105 static const int kBufferIncrementSize = 64 * KB; | |
106 | |
107 WriteStream(uint8_t** buffer, ReAlloc alloc) : | |
108 buffer_(buffer), | |
109 end_(NULL), | |
110 current_(NULL), | |
111 current_size_(0), | |
112 alloc_(alloc) { | |
113 ASSERT(buffer != NULL); | |
114 ASSERT(alloc != NULL); | |
115 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(NULL, | |
116 0, | |
117 kBufferIncrementSize)); | |
118 ASSERT(*buffer_ != NULL); | |
119 current_ = *buffer_; | |
120 current_size_ = kBufferIncrementSize; | |
121 end_ = *buffer_ + kBufferIncrementSize; | |
122 } | |
123 | |
124 uint8_t* buffer() const { return *buffer_; } | |
125 int bytes_written() const { return current_ - *buffer_; } | |
126 | |
127 void set_current(uint8_t* value) { current_ = value; } | |
128 | |
129 template<int N, typename T> | |
130 class Raw { }; | |
131 | |
132 template<typename T> | |
133 class Raw<1, T> { | |
134 public: | |
135 static void Write(WriteStream* st, T value) { | |
136 st->WriteByte(bit_cast<int8_t>(value)); | |
137 } | |
138 }; | |
139 | |
140 template<typename T> | |
141 class Raw<2, T> { | |
142 public: | |
143 static void Write(WriteStream* st, T value) { | |
144 st->Write<int16_t>(bit_cast<int16_t>(value)); | |
145 } | |
146 }; | |
147 | |
148 template<typename T> | |
149 class Raw<4, T> { | |
150 public: | |
151 static void Write(WriteStream* st, T value) { | |
152 st->Write<int32_t>(bit_cast<int32_t>(value)); | |
153 } | |
154 }; | |
155 | |
156 template<typename T> | |
157 class Raw<8, T> { | |
158 public: | |
159 static void Write(WriteStream* st, T value) { | |
160 st->Write<int64_t>(bit_cast<int64_t>(value)); | |
161 } | |
162 }; | |
163 | |
164 private: | |
165 template<typename T> | |
166 void Write(T value) { | |
167 T v = value; | |
168 while (v < kMinSerializedValuePerByte || | |
169 v > kMaxSerializedValuePerByte) { | |
170 WriteByte(static_cast<uint8_t>(v & kSerializedByteMask)); | |
171 v = v >> kSerializedBitsPerByte; | |
172 } | |
173 WriteByte(static_cast<uint8_t>(v + kEndByteMarker)); | |
174 } | |
175 | |
176 void WriteByte(uint8_t value) { | |
177 if (current_ >= end_) { | |
178 intptr_t new_size = (current_size_ + kBufferIncrementSize); | |
179 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(*buffer_, | |
180 current_size_, | |
181 new_size)); | |
182 ASSERT(*buffer_ != NULL); | |
183 current_ = *buffer_ + current_size_; | |
184 current_size_ = new_size; | |
185 end_ = *buffer_ + new_size; | |
186 } | |
187 ASSERT(current_ < end_); | |
188 *current_++ = value; | |
189 } | |
190 | |
191 private: | |
192 uint8_t** const buffer_; | |
193 uint8_t* end_; | |
194 uint8_t* current_; | |
195 intptr_t current_size_; | |
196 ReAlloc alloc_; | |
197 | |
198 DISALLOW_COPY_AND_ASSIGN(WriteStream); | |
199 }; | |
200 | |
201 } // namespace dart | |
202 | |
203 #endif // VM_DATASTREAM_H_ | |
OLD | NEW |