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);
96 if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) {
100 if (npos >= buffer->size) {
109 void cxBufferClear(CxBuffer *buffer) {
110 memset(buffer->bytes, 0, buffer->size);
115 int cxBufferEof(CxBuffer const *buffer) {
116 return buffer->pos >= buffer->size;
119 int cxBufferMinimumCapacity(
123 if (newcap <= buffer->capacity) {
127 if (cxReallocate(buffer->allocator,
128 (void **) &buffer->bytes, newcap) == 0) {
129 buffer->capacity = newcap;
137 * Helps flushing data to the flush target of a buffer.
139 * @param buffer the buffer containing the config
140 * @param space the data to flush
141 * @param size the element size
142 * @param nitems the number of items
143 * @return the number of items flushed
145 static size_t cx_buffer_write_flush_helper(
147 unsigned char const *space,
152 size_t remaining = nitems;
153 size_t max_items = buffer->flush_blksize / size;
154 while (remaining > 0) {
155 size_t items = remaining > max_items ? max_items : remaining;
156 size_t flushed = buffer->flush_func(
159 buffer->flush_target);
161 pos += (flushed * size);
162 remaining -= flushed;
164 // if no bytes can be flushed out anymore, we give up
168 return nitems - remaining;
171 size_t cxBufferWrite(
177 // optimize for easy case
178 if (size == 1 && (buffer->capacity - buffer->pos) >= nitems) {
179 memcpy(buffer->bytes + buffer->pos, ptr, nitems);
180 buffer->pos += nitems;
181 if (buffer->pos > buffer->size) {
182 buffer->size = buffer->pos;
188 size_t nitems_out = nitems;
189 if (cx_szmul(size, nitems, &len)) {
192 size_t required = buffer->pos + len;
193 if (buffer->pos > required) {
197 bool perform_flush = false;
198 if (required > buffer->capacity) {
199 if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND && required) {
200 if (buffer->flush_blkmax > 0 && required > buffer->flush_threshold) {
201 perform_flush = true;
203 if (cxBufferMinimumCapacity(buffer, required)) {
208 if (buffer->flush_blkmax > 0) {
209 perform_flush = true;
211 // truncate data to be written, if we can neither extend nor flush
212 len = buffer->capacity - buffer->pos;
216 nitems_out = len / size;
227 if (cx_szmul(buffer->flush_blkmax, buffer->flush_blksize, &flush_max)) {
230 size_t flush_pos = buffer->flush_func == NULL || buffer->flush_target == NULL
232 : cx_buffer_write_flush_helper(buffer, buffer->bytes, 1, buffer->pos);
233 if (flush_pos == buffer->pos) {
234 // entire buffer has been flushed, we can reset
235 buffer->size = buffer->pos = 0;
237 size_t items_flush; // how many items can also be directly flushed
238 size_t items_keep; // how many items have to be written to the buffer
240 items_flush = flush_max >= required ? nitems : (flush_max - flush_pos) / size;
241 if (items_flush > 0) {
242 items_flush = cx_buffer_write_flush_helper(buffer, ptr, size, items_flush / size);
243 // in case we could not flush everything, keep the rest
245 items_keep = nitems - items_flush;
246 if (items_keep > 0) {
247 // try again with the remaining stuff
248 unsigned char const *new_ptr = ptr;
249 new_ptr += items_flush * size;
250 // report the directly flushed items as written plus the remaining stuff
251 return items_flush + cxBufferWrite(new_ptr, size, items_keep, buffer);
253 // all items have been flushed - report them as written
256 } else if (flush_pos == 0) {
257 // nothing could be flushed at all, we immediately give up without writing any data
260 // we were partially successful, we shift left and try again
261 cxBufferShiftLeft(buffer, flush_pos);
262 return cxBufferWrite(ptr, size, nitems, buffer);
265 memcpy(buffer->bytes + buffer->pos, ptr, len);
267 if (buffer->pos > buffer->size) {
268 buffer->size = buffer->pos;
280 unsigned char const ch = c;
281 if (cxBufferWrite(&ch, 1, 1, buffer) == 1) {
288 size_t cxBufferPutString(
292 return cxBufferWrite(str, 1, strlen(str), buffer);
302 if (cx_szmul(size, nitems, &len)) {
305 if (buffer->pos + len > buffer->size) {
306 len = buffer->size - buffer->pos;
307 if (size > 1) len -= len % size;
314 memcpy(ptr, buffer->bytes + buffer->pos, len);
320 int cxBufferGet(CxBuffer *buffer) {
321 if (cxBufferEof(buffer)) {
324 int c = buffer->bytes[buffer->pos];
330 int cxBufferShiftLeft(
334 if (shift >= buffer->size) {
335 buffer->pos = buffer->size = 0;
337 memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift);
338 buffer->size -= shift;
340 if (buffer->pos >= shift) {
341 buffer->pos -= shift;
349 int cxBufferShiftRight(
353 size_t req_capacity = buffer->size + shift;
356 // auto extend buffer, if required and enabled
357 if (buffer->capacity < req_capacity) {
358 if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) {
359 if (cxBufferMinimumCapacity(buffer, req_capacity)) {
362 movebytes = buffer->size;
364 movebytes = buffer->capacity - shift;
367 movebytes = buffer->size;
370 memmove(buffer->bytes + shift, buffer->bytes, movebytes);
371 buffer->size = shift + movebytes;
373 buffer->pos += shift;
374 if (buffer->pos > buffer->size) {
375 buffer->pos = buffer->size;
386 return cxBufferShiftLeft(buffer, (size_t) (-shift));
387 } else if (shift > 0) {
388 return cxBufferShiftRight(buffer, (size_t) shift);