src/buffer.c

Sun, 01 May 2022 11:44:23 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 01 May 2022 11:44:23 +0200
changeset 541
67e078518935
parent 540
47e0f2237a94
child 542
45bcfd152f85
permissions
-rw-r--r--

#170 complete write tests for status quo

universe@483 1 /*
universe@483 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@483 3 *
universe@483 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@483 5 *
universe@483 6 * Redistribution and use in source and binary forms, with or without
universe@483 7 * modification, are permitted provided that the following conditions are met:
universe@483 8 *
universe@483 9 * 1. Redistributions of source code must retain the above copyright
universe@483 10 * notice, this list of conditions and the following disclaimer.
universe@483 11 *
universe@483 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@483 13 * notice, this list of conditions and the following disclaimer in the
universe@483 14 * documentation and/or other materials provided with the distribution.
universe@483 15 *
universe@483 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@483 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@483 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@483 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@483 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@483 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@483 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@483 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@483 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@483 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@483 26 * POSSIBILITY OF SUCH DAMAGE.
universe@483 27 */
universe@483 28
universe@483 29 #include "cx/buffer.h"
universe@483 30 #include "cx/utils.h"
universe@483 31
universe@483 32 #include <stdlib.h>
universe@530 33 #include <stdio.h>
universe@483 34 #include <string.h>
universe@539 35 #include <stdint.h>
universe@483 36
universe@501 37 int cxBufferInit(
universe@501 38 CxBuffer *buffer,
universe@483 39 void *space,
universe@483 40 size_t capacity,
universe@529 41 CxAllocator const *allocator,
universe@483 42 int flags
universe@483 43 ) {
universe@501 44 buffer->allocator = allocator;
universe@501 45 buffer->flags = flags;
universe@501 46 if (!space) {
universe@501 47 buffer->bytes = cxMalloc(allocator, capacity);
universe@501 48 if (buffer->bytes == NULL) {
universe@501 49 return 1;
universe@483 50 }
universe@501 51 buffer->flags |= CX_BUFFER_FREE_CONTENTS;
universe@501 52 } else {
universe@501 53 buffer->bytes = space;
universe@501 54 }
universe@501 55 buffer->capacity = capacity;
universe@501 56 buffer->size = 0;
universe@539 57 buffer->pos = 0;
universe@483 58
universe@539 59 buffer->flush_func = NULL;
universe@541 60 buffer->flush_target = NULL;
universe@539 61 buffer->flush_blkmax = 0;
universe@539 62 buffer->flush_blksize = 4096;
universe@539 63 buffer->flush_threshold = SIZE_MAX;
universe@483 64
universe@501 65 return 0;
universe@483 66 }
universe@483 67
universe@500 68 void cxBufferDestroy(CxBuffer *buffer) {
universe@483 69 if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS) {
universe@501 70 cxFree(buffer->allocator, buffer->bytes);
universe@483 71 }
universe@483 72 }
universe@483 73
universe@483 74 int cxBufferSeek(
universe@500 75 CxBuffer *buffer,
universe@483 76 off_t offset,
universe@483 77 int whence
universe@483 78 ) {
universe@483 79 size_t npos;
universe@483 80 switch (whence) {
universe@483 81 case SEEK_CUR:
universe@483 82 npos = buffer->pos;
universe@483 83 break;
universe@483 84 case SEEK_END:
universe@483 85 npos = buffer->size;
universe@483 86 break;
universe@483 87 case SEEK_SET:
universe@483 88 npos = 0;
universe@483 89 break;
universe@483 90 default:
universe@483 91 return -1;
universe@483 92 }
universe@483 93
universe@483 94 size_t opos = npos;
universe@483 95 npos += offset;
universe@483 96
universe@483 97 if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) {
universe@483 98 return -1;
universe@483 99 }
universe@483 100
universe@483 101 if (npos >= buffer->size) {
universe@483 102 return -1;
universe@483 103 } else {
universe@483 104 buffer->pos = npos;
universe@483 105 return 0;
universe@483 106 }
universe@483 107
universe@483 108 }
universe@483 109
universe@529 110 void cxBufferClear(CxBuffer *buffer) {
universe@529 111 memset(buffer->bytes, 0, buffer->size);
universe@529 112 buffer->size = 0;
universe@529 113 buffer->pos = 0;
universe@529 114 }
universe@529 115
universe@529 116 int cxBufferEof(CxBuffer const *buffer) {
universe@483 117 return buffer->pos >= buffer->size;
universe@483 118 }
universe@483 119
universe@483 120 int cxBufferMinimumCapacity(
universe@500 121 CxBuffer *buffer,
universe@532 122 size_t newcap
universe@483 123 ) {
universe@532 124 if (newcap <= buffer->capacity) {
universe@532 125 return 0;
universe@483 126 }
universe@483 127
universe@536 128 if (cxReallocate(buffer->allocator,
universe@536 129 (void **) &buffer->bytes, newcap) == 0) {
universe@483 130 buffer->capacity = newcap;
universe@533 131 return 0;
universe@483 132 } else {
universe@483 133 return -1;
universe@483 134 }
universe@483 135 }
universe@483 136
universe@483 137 size_t cxBufferWrite(
universe@489 138 void const *ptr,
universe@483 139 size_t size,
universe@483 140 size_t nitems,
universe@500 141 CxBuffer *buffer
universe@483 142 ) {
universe@539 143 // TODO: optimize for special case size == nitems == 1
universe@483 144 size_t len;
universe@483 145 if (cx_szmul(size, nitems, &len)) {
universe@483 146 return 0;
universe@483 147 }
universe@483 148 size_t required = buffer->pos + len;
universe@483 149 if (buffer->pos > required) {
universe@483 150 return 0;
universe@483 151 }
universe@483 152
universe@539 153 bool perform_flush = false;
universe@483 154 if (required > buffer->capacity) {
universe@539 155 if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND && required) {
universe@539 156 if (buffer->flush_blkmax > 0 && required > buffer->flush_threshold) {
universe@539 157 perform_flush = true;
universe@539 158 } else {
universe@539 159 if (cxBufferMinimumCapacity(buffer, required)) {
universe@539 160 return 0;
universe@539 161 }
universe@483 162 }
universe@483 163 } else {
universe@539 164 if (buffer->flush_blkmax > 0) {
universe@539 165 perform_flush = true;
universe@539 166 } else {
universe@539 167 // truncate data to be written, if we can neither extend nor flush
universe@539 168 len = buffer->capacity - buffer->pos;
universe@539 169 if (size > 1) {
universe@539 170 // TODO: this is bugged - it would only discard one element and not as many as required
universe@539 171 len -= len % size;
universe@539 172 }
universe@540 173 nitems = len / size;
universe@483 174 }
universe@483 175 }
universe@483 176 }
universe@483 177
universe@483 178 if (len == 0) {
universe@483 179 return len;
universe@483 180 }
universe@483 181
universe@539 182 if (perform_flush) {
universe@539 183 // TODO: implement flushing
universe@539 184 // (1) determine how many bytes to flush (use flushmax = blkmax * blksize)
universe@539 185 // (2) if len is larger than the number computed in (1) we need more flush cycles, compute how many
universe@539 186 // (3) determine how many bytes from the buffer shall be flushed
universe@539 187 // (4) if something remains in the buffer, shift the buffer to the left
universe@539 188 // (4a) if buffer was shifted, append the new data to the buffer
universe@539 189 // (4b) if the buffer was flushed entirely AND the new data also fits into flushmax,
universe@539 190 // directly write the new data to the flush sink
universe@539 191 return 0; // remove this after implementation
universe@539 192 } else {
universe@539 193 memcpy(buffer->bytes + buffer->pos, ptr, len);
universe@539 194 buffer->pos += len;
universe@539 195 if (buffer->pos > buffer->size) {
universe@539 196 buffer->size = buffer->pos;
universe@539 197 }
universe@483 198 }
universe@483 199
universe@539 200 return nitems;
universe@483 201 }
universe@483 202
universe@538 203 int cxBufferPut(
universe@538 204 CxBuffer *buffer,
universe@538 205 int c
universe@538 206 ) {
universe@538 207 c &= 0xFF;
universe@538 208 unsigned char const ch = c;
universe@538 209 if (cxBufferWrite(&ch, 1, 1, buffer) == 1) {
universe@538 210 return c;
universe@538 211 } else {
universe@538 212 return EOF;
universe@538 213 }
universe@538 214 }
universe@538 215
universe@538 216 size_t cxBufferPutString(
universe@538 217 CxBuffer *buffer,
universe@538 218 const char *str
universe@538 219 ) {
universe@538 220 return cxBufferWrite(str, 1, strlen(str), buffer);
universe@538 221 }
universe@538 222
universe@483 223 size_t cxBufferRead(
universe@483 224 void *ptr,
universe@483 225 size_t size,
universe@483 226 size_t nitems,
universe@500 227 CxBuffer *buffer
universe@483 228 ) {
universe@483 229 size_t len;
universe@483 230 if (cx_szmul(size, nitems, &len)) {
universe@483 231 return 0;
universe@483 232 }
universe@483 233 if (buffer->pos + len > buffer->size) {
universe@483 234 len = buffer->size - buffer->pos;
universe@483 235 if (size > 1) len -= len % size;
universe@483 236 }
universe@483 237
universe@483 238 if (len <= 0) {
universe@483 239 return len;
universe@483 240 }
universe@483 241
universe@483 242 memcpy(ptr, buffer->bytes + buffer->pos, len);
universe@483 243 buffer->pos += len;
universe@483 244
universe@483 245 return len / size;
universe@483 246 }
universe@483 247
universe@500 248 int cxBufferGet(CxBuffer *buffer) {
universe@483 249 if (cxBufferEof(buffer)) {
universe@483 250 return EOF;
universe@483 251 } else {
universe@483 252 int c = buffer->bytes[buffer->pos];
universe@483 253 buffer->pos++;
universe@483 254 return c;
universe@483 255 }
universe@483 256 }
universe@483 257
universe@483 258 int cxBufferShiftLeft(
universe@500 259 CxBuffer *buffer,
universe@483 260 size_t shift
universe@483 261 ) {
universe@483 262 if (shift >= buffer->size) {
universe@483 263 buffer->pos = buffer->size = 0;
universe@483 264 } else {
universe@483 265 memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift);
universe@483 266 buffer->size -= shift;
universe@483 267
universe@483 268 if (buffer->pos >= shift) {
universe@483 269 buffer->pos -= shift;
universe@483 270 } else {
universe@483 271 buffer->pos = 0;
universe@483 272 }
universe@483 273 }
universe@483 274 return 0;
universe@483 275 }
universe@483 276
universe@483 277 int cxBufferShiftRight(
universe@500 278 CxBuffer *buffer,
universe@483 279 size_t shift
universe@483 280 ) {
universe@483 281 size_t req_capacity = buffer->size + shift;
universe@483 282 size_t movebytes;
universe@483 283
universe@483 284 // auto extend buffer, if required and enabled
universe@483 285 if (buffer->capacity < req_capacity) {
universe@483 286 if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) {
universe@483 287 if (cxBufferMinimumCapacity(buffer, req_capacity)) {
universe@483 288 return 1;
universe@483 289 }
universe@483 290 movebytes = buffer->size;
universe@483 291 } else {
universe@483 292 movebytes = buffer->capacity - shift;
universe@483 293 }
universe@483 294 } else {
universe@483 295 movebytes = buffer->size;
universe@483 296 }
universe@483 297
universe@483 298 memmove(buffer->bytes + shift, buffer->bytes, movebytes);
universe@483 299 buffer->size = shift + movebytes;
universe@483 300
universe@483 301 buffer->pos += shift;
universe@483 302 if (buffer->pos > buffer->size) {
universe@483 303 buffer->pos = buffer->size;
universe@483 304 }
universe@483 305
universe@483 306 return 0;
universe@483 307 }
universe@483 308
universe@483 309 int cxBufferShift(
universe@500 310 CxBuffer *buffer,
universe@483 311 off_t shift
universe@483 312 ) {
universe@483 313 if (shift < 0) {
universe@483 314 return cxBufferShiftLeft(buffer, (size_t) (-shift));
universe@483 315 } else if (shift > 0) {
universe@483 316 return cxBufferShiftRight(buffer, (size_t) shift);
universe@483 317 } else {
universe@483 318 return 0;
universe@483 319 }
universe@483 320 }

mercurial