59 buffer->allocator = allocator; |
59 buffer->allocator = allocator; |
60 buffer->flags = flags; |
60 buffer->flags = flags; |
61 if (!space) { |
61 if (!space) { |
62 buffer->bytes = cxMalloc(allocator, capacity); |
62 buffer->bytes = cxMalloc(allocator, capacity); |
63 if (buffer->bytes == NULL) { |
63 if (buffer->bytes == NULL) { |
64 return -1; |
64 return -1; // LCOV_EXCL_LINE |
65 } |
65 } |
66 buffer->flags |= CX_BUFFER_FREE_CONTENTS; |
66 buffer->flags |= CX_BUFFER_FREE_CONTENTS; |
67 } else { |
67 } else { |
68 buffer->bytes = space; |
68 buffer->bytes = space; |
69 } |
69 } |
99 CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer)); |
99 CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer)); |
100 if (buf == NULL) return NULL; |
100 if (buf == NULL) return NULL; |
101 if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) { |
101 if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) { |
102 return buf; |
102 return buf; |
103 } else { |
103 } else { |
|
104 // LCOV_EXCL_START |
104 cxFree(allocator, buf); |
105 cxFree(allocator, buf); |
105 return NULL; |
106 return NULL; |
|
107 // LCOV_EXCL_STOP |
106 } |
108 } |
107 } |
109 } |
108 |
110 |
109 void cxBufferFree(CxBuffer *buffer) { |
111 void cxBufferFree(CxBuffer *buffer) { |
110 if (buffer == NULL) return; |
112 if (buffer == NULL) return; |
188 } else if (cxReallocate(buffer->allocator, |
190 } else if (cxReallocate(buffer->allocator, |
189 (void **) &buffer->bytes, newcap) == 0) { |
191 (void **) &buffer->bytes, newcap) == 0) { |
190 buffer->capacity = newcap; |
192 buffer->capacity = newcap; |
191 return 0; |
193 return 0; |
192 } else { |
194 } else { |
193 return -1; |
195 return -1; // LCOV_EXCL_LINE |
194 } |
196 } |
195 } |
197 } |
196 |
198 |
197 /** |
199 /** |
198 * Helps flushing data to the flush target of a buffer. |
200 * Helps flushing data to the flush target of a buffer. |
262 if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
264 if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
263 if (buffer->flush_blkmax > 0 && required > buffer->flush_threshold) { |
265 if (buffer->flush_blkmax > 0 && required > buffer->flush_threshold) { |
264 perform_flush = true; |
266 perform_flush = true; |
265 } else { |
267 } else { |
266 if (cxBufferMinimumCapacity(buffer, required)) { |
268 if (cxBufferMinimumCapacity(buffer, required)) { |
267 return 0; |
269 return 0; // LCOV_EXCL_LINE |
268 } |
270 } |
269 } |
271 } |
270 } else { |
272 } else { |
271 if (buffer->flush_blkmax > 0) { |
273 if (buffer->flush_blkmax > 0) { |
272 perform_flush = true; |
274 perform_flush = true; |
450 |
452 |
451 // auto extend buffer, if required and enabled |
453 // auto extend buffer, if required and enabled |
452 if (buffer->capacity < req_capacity) { |
454 if (buffer->capacity < req_capacity) { |
453 if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
455 if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
454 if (cxBufferMinimumCapacity(buffer, req_capacity)) { |
456 if (cxBufferMinimumCapacity(buffer, req_capacity)) { |
455 return -1; |
457 return -1; // LCOV_EXCL_LINE |
456 } |
458 } |
457 movebytes = buffer->size; |
459 movebytes = buffer->size; |
458 } else { |
460 } else { |
459 movebytes = buffer->capacity - shift; |
461 movebytes = buffer->capacity - shift; |
460 } |
462 } |