@@ -71,7 +71,8 @@ template <typename T>
71
71
inline void WriterHost<BE, IE, WriterPolicyImpl>::write(const T* value, size_t len) {
72
72
assert (value != NULL , " invariant" );
73
73
assert (len > 0 , " invariant" );
74
- u1* const pos = ensure_size (sizeof (T) * len);
74
+ // Might need T + 1 size
75
+ u1* const pos = ensure_size (sizeof (T) * len + len);
75
76
if (pos) {
76
77
this ->set_current_pos (write (value, len, pos));
77
78
}
@@ -122,7 +123,8 @@ template <typename T>
122
123
inline void WriterHost<BE, IE, WriterPolicyImpl>::be_write(const T* value, size_t len) {
123
124
assert (value != NULL , " invariant" );
124
125
assert (len > 0 , " invariant" );
125
- u1* const pos = ensure_size (sizeof (T) * len);
126
+ // Might need T + 1 size
127
+ u1* const pos = ensure_size (sizeof (T) * len + len);
126
128
if (pos) {
127
129
this ->set_current_pos (BE::be_write (value, len, pos));
128
130
}
@@ -135,10 +137,17 @@ inline WriterHost<BE, IE, WriterPolicyImpl>::WriterHost(StorageType* storage, Th
135
137
_compressed_integers(compressed_integers()) {
136
138
}
137
139
140
+ // Extra size added as a safety cushion when dimensioning memory.
141
+ // With varint encoding, the worst case is
142
+ // associated with writing negative values.
143
+ // For example, writing a negative s1 (-1)
144
+ // will encode as 0xff 0x0f (2 bytes).
145
+ static const size_t size_safety_cushion = 1 ;
146
+
138
147
template <typename BE, typename IE, typename WriterPolicyImpl >
139
148
template <typename StorageType>
140
149
inline WriterHost<BE, IE, WriterPolicyImpl>::WriterHost(StorageType* storage, size_t size) :
141
- WriterPolicyImpl (storage, size),
150
+ WriterPolicyImpl (storage, size + size_safety_cushion ),
142
151
_compressed_integers(compressed_integers()) {
143
152
}
144
153
@@ -148,30 +157,19 @@ inline WriterHost<BE, IE, WriterPolicyImpl>::WriterHost(Thread* thread) :
148
157
_compressed_integers(compressed_integers()) {
149
158
}
150
159
151
- // Extra size added as a safety cushion when dimensioning memory.
152
- // With varint encoding, the worst case is
153
- // associated with writing negative values.
154
- // For example, writing a negative s1 (-1)
155
- // will encode as 0xff 0x0f (2 bytes).
156
- // In this example, the sizeof(T) == 1 and length == 1,
157
- // but the implementation will need to dimension
158
- // 2 bytes for the encoding.
159
- // Hopefully, negative values should be relatively rare.
160
- static const size_t size_safety_cushion = 1 ;
161
-
162
160
template <typename BE, typename IE, typename WriterPolicyImpl>
163
- inline u1* WriterHost<BE, IE, WriterPolicyImpl>::ensure_size(size_t requested ) {
161
+ inline u1* WriterHost<BE, IE, WriterPolicyImpl>::ensure_size(size_t requested_size ) {
164
162
if (!this ->is_valid ()) {
165
163
// cancelled
166
164
return NULL ;
167
165
}
168
- if (this ->available_size () < requested + size_safety_cushion ) {
169
- if (!this ->accommodate (this ->used_size (), requested + size_safety_cushion )) {
166
+ if (this ->available_size () < requested_size ) {
167
+ if (!this ->accommodate (this ->used_size (), requested_size )) {
170
168
assert (!this ->is_valid (), " invariant" );
171
169
return NULL ;
172
170
}
173
171
}
174
- assert (requested + size_safety_cushion <= this ->available_size (), " invariant" );
172
+ assert (requested_size <= this ->available_size (), " invariant" );
175
173
return this ->current_pos ();
176
174
}
177
175
0 commit comments