Sun, 01 May 2022 11:54:10 +0200
optimization for buffer write easy cases
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@543 | 143 | // optimize for easy case |
universe@543 | 144 | if (size == 1 && (buffer->capacity - buffer->pos) >= nitems) { |
universe@543 | 145 | memcpy(buffer->bytes + buffer->pos, ptr, nitems); |
universe@543 | 146 | buffer->pos += nitems; |
universe@543 | 147 | if (buffer->pos > buffer->size) { |
universe@543 | 148 | buffer->size = buffer->pos; |
universe@543 | 149 | } |
universe@543 | 150 | return nitems; |
universe@543 | 151 | } |
universe@543 | 152 | |
universe@483 | 153 | size_t len; |
universe@483 | 154 | if (cx_szmul(size, nitems, &len)) { |
universe@483 | 155 | return 0; |
universe@483 | 156 | } |
universe@483 | 157 | size_t required = buffer->pos + len; |
universe@483 | 158 | if (buffer->pos > required) { |
universe@483 | 159 | return 0; |
universe@483 | 160 | } |
universe@483 | 161 | |
universe@539 | 162 | bool perform_flush = false; |
universe@483 | 163 | if (required > buffer->capacity) { |
universe@539 | 164 | if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND && required) { |
universe@539 | 165 | if (buffer->flush_blkmax > 0 && required > buffer->flush_threshold) { |
universe@539 | 166 | perform_flush = true; |
universe@539 | 167 | } else { |
universe@539 | 168 | if (cxBufferMinimumCapacity(buffer, required)) { |
universe@539 | 169 | return 0; |
universe@539 | 170 | } |
universe@483 | 171 | } |
universe@483 | 172 | } else { |
universe@539 | 173 | if (buffer->flush_blkmax > 0) { |
universe@539 | 174 | perform_flush = true; |
universe@539 | 175 | } else { |
universe@539 | 176 | // truncate data to be written, if we can neither extend nor flush |
universe@539 | 177 | len = buffer->capacity - buffer->pos; |
universe@539 | 178 | if (size > 1) { |
universe@539 | 179 | len -= len % size; |
universe@539 | 180 | } |
universe@540 | 181 | nitems = len / size; |
universe@483 | 182 | } |
universe@483 | 183 | } |
universe@483 | 184 | } |
universe@483 | 185 | |
universe@483 | 186 | if (len == 0) { |
universe@483 | 187 | return len; |
universe@483 | 188 | } |
universe@483 | 189 | |
universe@539 | 190 | if (perform_flush) { |
universe@539 | 191 | // TODO: implement flushing |
universe@539 | 192 | // (1) determine how many bytes to flush (use flushmax = blkmax * blksize) |
universe@539 | 193 | // (2) if len is larger than the number computed in (1) we need more flush cycles, compute how many |
universe@539 | 194 | // (3) determine how many bytes from the buffer shall be flushed |
universe@539 | 195 | // (4) if something remains in the buffer, shift the buffer to the left |
universe@539 | 196 | // (4a) if buffer was shifted, append the new data to the buffer |
universe@539 | 197 | // (4b) if the buffer was flushed entirely AND the new data also fits into flushmax, |
universe@539 | 198 | // directly write the new data to the flush sink |
universe@539 | 199 | return 0; // remove this after implementation |
universe@539 | 200 | } else { |
universe@539 | 201 | memcpy(buffer->bytes + buffer->pos, ptr, len); |
universe@539 | 202 | buffer->pos += len; |
universe@539 | 203 | if (buffer->pos > buffer->size) { |
universe@539 | 204 | buffer->size = buffer->pos; |
universe@539 | 205 | } |
universe@483 | 206 | } |
universe@483 | 207 | |
universe@539 | 208 | return nitems; |
universe@483 | 209 | } |
universe@483 | 210 | |
universe@538 | 211 | int cxBufferPut( |
universe@538 | 212 | CxBuffer *buffer, |
universe@538 | 213 | int c |
universe@538 | 214 | ) { |
universe@538 | 215 | c &= 0xFF; |
universe@538 | 216 | unsigned char const ch = c; |
universe@538 | 217 | if (cxBufferWrite(&ch, 1, 1, buffer) == 1) { |
universe@538 | 218 | return c; |
universe@538 | 219 | } else { |
universe@538 | 220 | return EOF; |
universe@538 | 221 | } |
universe@538 | 222 | } |
universe@538 | 223 | |
universe@538 | 224 | size_t cxBufferPutString( |
universe@538 | 225 | CxBuffer *buffer, |
universe@538 | 226 | const char *str |
universe@538 | 227 | ) { |
universe@538 | 228 | return cxBufferWrite(str, 1, strlen(str), buffer); |
universe@538 | 229 | } |
universe@538 | 230 | |
universe@483 | 231 | size_t cxBufferRead( |
universe@483 | 232 | void *ptr, |
universe@483 | 233 | size_t size, |
universe@483 | 234 | size_t nitems, |
universe@500 | 235 | CxBuffer *buffer |
universe@483 | 236 | ) { |
universe@483 | 237 | size_t len; |
universe@483 | 238 | if (cx_szmul(size, nitems, &len)) { |
universe@483 | 239 | return 0; |
universe@483 | 240 | } |
universe@483 | 241 | if (buffer->pos + len > buffer->size) { |
universe@483 | 242 | len = buffer->size - buffer->pos; |
universe@483 | 243 | if (size > 1) len -= len % size; |
universe@483 | 244 | } |
universe@483 | 245 | |
universe@483 | 246 | if (len <= 0) { |
universe@483 | 247 | return len; |
universe@483 | 248 | } |
universe@483 | 249 | |
universe@483 | 250 | memcpy(ptr, buffer->bytes + buffer->pos, len); |
universe@483 | 251 | buffer->pos += len; |
universe@483 | 252 | |
universe@483 | 253 | return len / size; |
universe@483 | 254 | } |
universe@483 | 255 | |
universe@500 | 256 | int cxBufferGet(CxBuffer *buffer) { |
universe@483 | 257 | if (cxBufferEof(buffer)) { |
universe@483 | 258 | return EOF; |
universe@483 | 259 | } else { |
universe@483 | 260 | int c = buffer->bytes[buffer->pos]; |
universe@483 | 261 | buffer->pos++; |
universe@483 | 262 | return c; |
universe@483 | 263 | } |
universe@483 | 264 | } |
universe@483 | 265 | |
universe@483 | 266 | int cxBufferShiftLeft( |
universe@500 | 267 | CxBuffer *buffer, |
universe@483 | 268 | size_t shift |
universe@483 | 269 | ) { |
universe@483 | 270 | if (shift >= buffer->size) { |
universe@483 | 271 | buffer->pos = buffer->size = 0; |
universe@483 | 272 | } else { |
universe@483 | 273 | memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift); |
universe@483 | 274 | buffer->size -= shift; |
universe@483 | 275 | |
universe@483 | 276 | if (buffer->pos >= shift) { |
universe@483 | 277 | buffer->pos -= shift; |
universe@483 | 278 | } else { |
universe@483 | 279 | buffer->pos = 0; |
universe@483 | 280 | } |
universe@483 | 281 | } |
universe@483 | 282 | return 0; |
universe@483 | 283 | } |
universe@483 | 284 | |
universe@483 | 285 | int cxBufferShiftRight( |
universe@500 | 286 | CxBuffer *buffer, |
universe@483 | 287 | size_t shift |
universe@483 | 288 | ) { |
universe@483 | 289 | size_t req_capacity = buffer->size + shift; |
universe@483 | 290 | size_t movebytes; |
universe@483 | 291 | |
universe@483 | 292 | // auto extend buffer, if required and enabled |
universe@483 | 293 | if (buffer->capacity < req_capacity) { |
universe@483 | 294 | if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) { |
universe@483 | 295 | if (cxBufferMinimumCapacity(buffer, req_capacity)) { |
universe@483 | 296 | return 1; |
universe@483 | 297 | } |
universe@483 | 298 | movebytes = buffer->size; |
universe@483 | 299 | } else { |
universe@483 | 300 | movebytes = buffer->capacity - shift; |
universe@483 | 301 | } |
universe@483 | 302 | } else { |
universe@483 | 303 | movebytes = buffer->size; |
universe@483 | 304 | } |
universe@483 | 305 | |
universe@483 | 306 | memmove(buffer->bytes + shift, buffer->bytes, movebytes); |
universe@483 | 307 | buffer->size = shift + movebytes; |
universe@483 | 308 | |
universe@483 | 309 | buffer->pos += shift; |
universe@483 | 310 | if (buffer->pos > buffer->size) { |
universe@483 | 311 | buffer->pos = buffer->size; |
universe@483 | 312 | } |
universe@483 | 313 | |
universe@483 | 314 | return 0; |
universe@483 | 315 | } |
universe@483 | 316 | |
universe@483 | 317 | int cxBufferShift( |
universe@500 | 318 | CxBuffer *buffer, |
universe@483 | 319 | off_t shift |
universe@483 | 320 | ) { |
universe@483 | 321 | if (shift < 0) { |
universe@483 | 322 | return cxBufferShiftLeft(buffer, (size_t) (-shift)); |
universe@483 | 323 | } else if (shift > 0) { |
universe@483 | 324 | return cxBufferShiftRight(buffer, (size_t) shift); |
universe@483 | 325 | } else { |
universe@483 | 326 | return 0; |
universe@483 | 327 | } |
universe@483 | 328 | } |