2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include "cx/buffer.h"
39 CxAllocator const *allocator,
42 if (allocator == NULL) allocator = cxDefaultAllocator;
43 buffer->allocator = allocator;
44 buffer->flags = flags;
46 buffer->bytes = cxMalloc(allocator, capacity);
47 if (buffer->bytes == NULL) {
50 buffer->flags |= CX_BUFFER_FREE_CONTENTS;
52 buffer->bytes = space;
54 buffer->capacity = capacity;
58 buffer->flush_func = NULL;
59 buffer->flush_target = NULL;
60 buffer->flush_blkmax = 0;
61 buffer->flush_blksize = 4096;
62 buffer->flush_threshold = SIZE_MAX;
67 void cxBufferDestroy(CxBuffer *buffer) {
68 if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS) {
69 cxFree(buffer->allocator, buffer->bytes);
73 CxBuffer *cxBufferCreate(
76 CxAllocator const *allocator,
79 CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer));
80 if (buf == NULL) return NULL;
81 if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) {
84 cxFree(allocator, buf);
89 void cxBufferFree(CxBuffer *buffer) {
90 if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS) {
91 cxFree(buffer->allocator, buffer->bytes);
93 cxFree(buffer->allocator, buffer);
119 if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) {
123 if (npos >= buffer->size) {
132 void cxBufferClear(CxBuffer *buffer) {
133 memset(buffer->bytes, 0, buffer->size);
138 int cxBufferEof(CxBuffer const *buffer) {
139 return buffer->pos >= buffer->size;
142 int cxBufferMinimumCapacity(
146 if (newcap <= buffer->capacity) {
150 if (cxReallocate(buffer->allocator,
151 (void **) &buffer->bytes, newcap) == 0) {
152 buffer->capacity = newcap;
160 * Helps flushing data to the flush target of a buffer.
162 * @param buffer the buffer containing the config
163 * @param space the data to flush
164 * @param size the element size
165 * @param nitems the number of items
166 * @return the number of items flushed
168 static size_t cx_buffer_write_flush_helper(
170 unsigned char const *space,
175 size_t remaining = nitems;
176 size_t max_items = buffer->flush_blksize / size;
177 while (remaining > 0) {
178 size_t items = remaining > max_items ? max_items : remaining;
179 size_t flushed = buffer->flush_func(
182 buffer->flush_target);
184 pos += (flushed * size);
185 remaining -= flushed;
187 // if no bytes can be flushed out anymore, we give up
191 return nitems - remaining;
194 size_t cxBufferWrite(
200 // optimize for easy case
201 if (size == 1 && (buffer->capacity - buffer->pos) >= nitems) {
202 memcpy(buffer->bytes + buffer->pos, ptr, nitems);
203 buffer->pos += nitems;
204 if (buffer->pos > buffer->size) {
205 buffer->size = buffer->pos;
211 size_t nitems_out = nitems;
212 if (cx_szmul(size, nitems, &len)) {
215 size_t required = buffer->pos + len;
216 if (buffer->pos > required) {
220 bool perform_flush = false;
221 if (required > buffer->capacity) {
222 if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND && required) {
223 if (buffer->flush_blkmax > 0 && required > buffer->flush_threshold) {
224 perform_flush = true;
226 if (cxBufferMinimumCapacity(buffer, required)) {
231 if (buffer->flush_blkmax > 0) {
232 perform_flush = true;
234 // truncate data to be written, if we can neither extend nor flush
235 len = buffer->capacity - buffer->pos;
239 nitems_out = len / size;
250 if (cx_szmul(buffer->flush_blkmax, buffer->flush_blksize, &flush_max)) {
253 size_t flush_pos = buffer->flush_func == NULL || buffer->flush_target == NULL
255 : cx_buffer_write_flush_helper(buffer, buffer->bytes, 1, buffer->pos);
256 if (flush_pos == buffer->pos) {
257 // entire buffer has been flushed, we can reset
258 buffer->size = buffer->pos = 0;
260 size_t items_flush; // how many items can also be directly flushed
261 size_t items_keep; // how many items have to be written to the buffer
263 items_flush = flush_max >= required ? nitems : (flush_max - flush_pos) / size;
264 if (items_flush > 0) {
265 items_flush = cx_buffer_write_flush_helper(buffer, ptr, size, items_flush / size);
266 // in case we could not flush everything, keep the rest
268 items_keep = nitems - items_flush;
269 if (items_keep > 0) {
270 // try again with the remaining stuff
271 unsigned char const *new_ptr = ptr;
272 new_ptr += items_flush * size;
273 // report the directly flushed items as written plus the remaining stuff
274 return items_flush + cxBufferWrite(new_ptr, size, items_keep, buffer);
276 // all items have been flushed - report them as written
279 } else if (flush_pos == 0) {
280 // nothing could be flushed at all, we immediately give up without writing any data
283 // we were partially successful, we shift left and try again
284 cxBufferShiftLeft(buffer, flush_pos);
285 return cxBufferWrite(ptr, size, nitems, buffer);
288 memcpy(buffer->bytes + buffer->pos, ptr, len);
290 if (buffer->pos > buffer->size) {
291 buffer->size = buffer->pos;
303 unsigned char const ch = c;
304 if (cxBufferWrite(&ch, 1, 1, buffer) == 1) {
311 size_t cxBufferPutString(
315 return cxBufferWrite(str, 1, strlen(str), buffer);
325 if (cx_szmul(size, nitems, &len)) {
328 if (buffer->pos + len > buffer->size) {
329 len = buffer->size - buffer->pos;
330 if (size > 1) len -= len % size;
337 memcpy(ptr, buffer->bytes + buffer->pos, len);
343 int cxBufferGet(CxBuffer *buffer) {
344 if (cxBufferEof(buffer)) {
347 int c = buffer->bytes[buffer->pos];
353 int cxBufferShiftLeft(
357 if (shift >= buffer->size) {
358 buffer->pos = buffer->size = 0;
360 memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift);
361 buffer->size -= shift;
363 if (buffer->pos >= shift) {
364 buffer->pos -= shift;
372 int cxBufferShiftRight(
376 size_t req_capacity = buffer->size + shift;
379 // auto extend buffer, if required and enabled
380 if (buffer->capacity < req_capacity) {
381 if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) {
382 if (cxBufferMinimumCapacity(buffer, req_capacity)) {
385 movebytes = buffer->size;
387 movebytes = buffer->capacity - shift;
390 movebytes = buffer->size;
393 memmove(buffer->bytes + shift, buffer->bytes, movebytes);
394 buffer->size = shift + movebytes;
396 buffer->pos += shift;
397 if (buffer->pos > buffer->size) {
398 buffer->pos = buffer->size;
409 return cxBufferShiftLeft(buffer, (size_t) (-shift));
410 } else if (shift > 0) {
411 return cxBufferShiftRight(buffer, (size_t) shift);