204 (void **) &buffer->bytes, newcap) == 0) { |
204 (void **) &buffer->bytes, newcap) == 0) { |
205 buffer->capacity = newcap; |
205 buffer->capacity = newcap; |
206 return 0; |
206 return 0; |
207 } else { |
207 } else { |
208 return -1; // LCOV_EXCL_LINE |
208 return -1; // LCOV_EXCL_LINE |
|
209 } |
|
210 } |
|
211 |
|
212 void cxBufferShrink( |
|
213 CxBuffer *buffer, |
|
214 size_t reserve |
|
215 ) { |
|
216 // Ensure buffer is in a reallocatable state |
|
217 const int force_copy_flags = CX_BUFFER_COPY_ON_WRITE | CX_BUFFER_COPY_ON_EXTEND; |
|
218 if (buffer->flags & force_copy_flags) { |
|
219 // do nothing when we are not allowed to reallocate |
|
220 return; |
|
221 } |
|
222 |
|
223 // calculate new capacity |
|
224 size_t newCapacity = buffer->size + reserve; |
|
225 |
|
226 // If new capacity is smaller than current capacity, resize the buffer |
|
227 if (newCapacity < buffer->capacity) { |
|
228 if (0 == cxReallocate(buffer->allocator, &buffer->bytes, newCapacity)) { |
|
229 buffer->capacity = newCapacity; |
|
230 } |
209 } |
231 } |
210 } |
232 } |
211 |
233 |
212 static size_t cx_buffer_flush_helper( |
234 static size_t cx_buffer_flush_helper( |
213 const CxBuffer *buffer, |
235 const CxBuffer *buffer, |