src/cx/buffer.h

Mon, 18 Dec 2023 14:14:47 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Dec 2023 14:14:47 +0100
changeset 759
475335643af4
parent 757
49ceea78fce7
child 761
61d5197d612b
permissions
-rw-r--r--

increase version number to 3.1

remove per-file version information
from Doxygen output

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 /**
universe@483 30 * \file buffer.h
universe@483 31 *
universe@483 32 * \brief Advanced buffer implementation.
universe@483 33 *
universe@483 34 * Instances of CxBuffer can be used to read from or to write to like one
universe@483 35 * would do with a stream.
universe@483 36 *
universe@483 37 * Some features for convenient use of the buffer
universe@483 38 * can be enabled. See the documentation of the macro constants for more
universe@483 39 * information.
universe@483 40 *
universe@483 41 * \author Mike Becker
universe@483 42 * \author Olaf Wintermann
universe@483 43 * \copyright 2-Clause BSD License
universe@483 44 */
universe@483 45
universe@483 46 #ifndef UCX_BUFFER_H
universe@484 47 #define UCX_BUFFER_H
universe@483 48
universe@484 49 #include "common.h"
universe@501 50 #include "allocator.h"
universe@483 51
universe@483 52 #ifdef __cplusplus
universe@483 53 extern "C" {
universe@483 54 #endif
universe@483 55
universe@483 56 /**
universe@483 57 * No buffer features enabled (all flags cleared).
universe@483 58 */
universe@483 59 #define CX_BUFFER_DEFAULT 0x00
universe@483 60
universe@483 61 /**
universe@483 62 * If this flag is enabled, the buffer will automatically free its contents when destroyed.
universe@483 63 */
universe@483 64 #define CX_BUFFER_FREE_CONTENTS 0x01
universe@483 65
universe@483 66 /**
universe@483 67 * If this flag is enabled, the buffer will automatically extends its capacity.
universe@483 68 */
universe@483 69 #define CX_BUFFER_AUTO_EXTEND 0x02
universe@483 70
universe@483 71 /** Structure for the UCX buffer data. */
universe@483 72 typedef struct {
universe@483 73 /** A pointer to the buffer contents. */
universe@483 74 union {
universe@483 75 /**
universe@483 76 * Data is interpreted as text.
universe@483 77 */
universe@483 78 char *space;
universe@483 79 /**
universe@483 80 * Data is interpreted as binary.
universe@483 81 */
universe@483 82 unsigned char *bytes;
universe@483 83 };
universe@501 84 /** The allocator to use for automatic memory management. */
universe@529 85 CxAllocator const *allocator;
universe@483 86 /** Current position of the buffer. */
universe@483 87 size_t pos;
universe@483 88 /** Current capacity (i.e. maximum size) of the buffer. */
universe@483 89 size_t capacity;
universe@483 90 /** Current size of the buffer content. */
universe@483 91 size_t size;
universe@483 92 /**
universe@539 93 * The buffer may not extend beyond this threshold before starting to flush.
universe@539 94 * Default is \c SIZE_MAX (flushing disabled when auto extension is enabled).
universe@539 95 */
universe@539 96 size_t flush_threshold;
universe@539 97 /**
universe@539 98 * The block size for the elements to flush.
universe@539 99 * Default is 4096 bytes.
universe@539 100 */
universe@539 101 size_t flush_blksize;
universe@539 102 /**
universe@539 103 * The maximum number of blocks to flush in one cycle.
universe@539 104 * Zero disables flushing entirely (this is the default).
universe@539 105 * Set this to \c SIZE_MAX to flush the entire buffer.
universe@539 106 *
universe@539 107 * @attention if the maximum number of blocks multiplied with the block size
universe@539 108 * is smaller than the expected contents written to this buffer within one write
universe@539 109 * operation, multiple flush cycles are performed after that write.
universe@539 110 * That means the total number of blocks flushed after one write to this buffer may
universe@539 111 * be larger than \c flush_blkmax.
universe@539 112 */
universe@539 113 size_t flush_blkmax;
universe@539 114
universe@539 115 /**
universe@539 116 * The write function used for flushing.
universe@539 117 * If NULL, the flushed content gets discarded.
universe@539 118 */
universe@545 119 cx_write_func flush_func;
universe@539 120
universe@539 121 /**
universe@541 122 * The target for \c flush_func.
universe@541 123 */
universe@541 124 void *flush_target;
universe@541 125
universe@541 126 /**
universe@483 127 * Flag register for buffer features.
universe@485 128 * @see #CX_BUFFER_DEFAULT
universe@485 129 * @see #CX_BUFFER_FREE_CONTENTS
universe@485 130 * @see #CX_BUFFER_AUTO_EXTEND
universe@483 131 */
universe@483 132 int flags;
universe@483 133 } cx_buffer_s;
universe@483 134
universe@483 135 /**
universe@483 136 * UCX buffer.
universe@483 137 */
universe@500 138 typedef cx_buffer_s CxBuffer;
universe@483 139
universe@483 140 /**
universe@501 141 * Initializes a fresh buffer.
universe@483 142 *
universe@483 143 * \note You may provide \c NULL as argument for \p space.
universe@483 144 * Then this function will allocate the space and enforce
universe@483 145 * the #CX_BUFFER_FREE_CONTENTS flag.
universe@483 146 *
universe@501 147 * @param buffer the buffer to initialize
universe@501 148 * @param space pointer to the memory area, or \c NULL to allocate
universe@483 149 * new memory
universe@485 150 * @param capacity the capacity of the buffer
universe@673 151 * @param allocator the allocator this buffer shall use for automatic
universe@673 152 * memory management. If \c NULL, the default heap allocator will be used.
universe@485 153 * @param flags buffer features (see cx_buffer_s.flags)
universe@501 154 * @return zero on success, non-zero if a required allocation failed
universe@483 155 */
universe@673 156 __attribute__((__nonnull__(1)))
universe@501 157 int cxBufferInit(
universe@501 158 CxBuffer *buffer,
universe@483 159 void *space,
universe@483 160 size_t capacity,
universe@529 161 CxAllocator const *allocator,
universe@483 162 int flags
universe@483 163 );
universe@483 164
universe@483 165 /**
universe@683 166 * Allocates and initializes a fresh buffer.
universe@683 167 *
universe@683 168 * \note You may provide \c NULL as argument for \p space.
universe@683 169 * Then this function will allocate the space and enforce
universe@683 170 * the #CX_BUFFER_FREE_CONTENTS flag.
universe@683 171 *
universe@683 172 * @param space pointer to the memory area, or \c NULL to allocate
universe@683 173 * new memory
universe@683 174 * @param capacity the capacity of the buffer
universe@683 175 * @param allocator the allocator to use for allocating the structure and the automatic
universe@683 176 * memory management within the buffer. If \c NULL, the default heap allocator will be used.
universe@683 177 * @param flags buffer features (see cx_buffer_s.flags)
universe@683 178 * @return a pointer to the buffer on success, \c NULL if a required allocation failed
universe@683 179 */
universe@683 180 CxBuffer *cxBufferCreate(
universe@683 181 void *space,
universe@683 182 size_t capacity,
universe@683 183 CxAllocator const *allocator,
universe@683 184 int flags
universe@683 185 );
universe@683 186
universe@683 187 /**
universe@501 188 * Destroys the buffer contents.
universe@483 189 *
universe@501 190 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled.
universe@683 191 * If you want to free the memory of the entire buffer, use cxBufferFree().
universe@483 192 *
universe@501 193 * @param buffer the buffer which contents shall be destroyed
universe@683 194 * @see cxBufferInit()
universe@483 195 */
universe@529 196 __attribute__((__nonnull__))
universe@500 197 void cxBufferDestroy(CxBuffer *buffer);
universe@483 198
universe@483 199 /**
universe@683 200 * Deallocates the buffer.
universe@683 201 *
universe@683 202 * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys
universe@683 203 * the contents. If you \em only want to destroy the contents, use cxBufferDestroy().
universe@683 204 *
universe@683 205 * @param buffer the buffer to deallocate
universe@683 206 * @see cxBufferCreate()
universe@683 207 */
universe@683 208 __attribute__((__nonnull__))
universe@683 209 void cxBufferFree(CxBuffer *buffer);
universe@683 210
universe@683 211 /**
universe@483 212 * Shifts the contents of the buffer by the given offset.
universe@483 213 *
universe@483 214 * If the offset is positive, the contents are shifted to the right.
universe@483 215 * If auto extension is enabled, the buffer grows, if necessary.
universe@483 216 * In case the auto extension fails, this function returns a non-zero value and
universe@483 217 * no contents are changed.
universe@483 218 * If auto extension is disabled, the contents that do not fit into the buffer
universe@483 219 * are discarded.
universe@483 220 *
universe@483 221 * If the offset is negative, the contents are shifted to the left where the
universe@483 222 * first \p shift bytes are discarded.
universe@483 223 * The new size of the buffer is the old size minus the absolute shift value.
universe@483 224 * If this value is larger than the buffer size, the buffer is emptied (but
universe@483 225 * not cleared, see the security note below).
universe@483 226 *
universe@483 227 * The buffer position gets shifted alongside with the content but is kept
universe@483 228 * within the boundaries of the buffer.
universe@483 229 *
universe@483 230 * \note For situations where \c off_t is not large enough, there are specialized cxBufferShiftLeft() and
universe@483 231 * cxBufferShiftRight() functions using a \c size_t as parameter type.
universe@483 232 *
universe@485 233 * \attention
universe@485 234 * Security Note: The shifting operation does \em not erase the previously occupied memory cells.
universe@485 235 * But you can easily do that manually, e.g. by calling
universe@483 236 * <code>memset(buffer->bytes, 0, shift)</code> for a right shift or
universe@531 237 * <code>memset(buffer->bytes + buffer->size, 0, buffer->capacity - buffer->size)</code>
universe@483 238 * for a left shift.
universe@483 239 *
universe@485 240 * @param buffer the buffer
universe@485 241 * @param shift the shift offset (negative means left shift)
universe@485 242 * @return 0 on success, non-zero if a required auto-extension fails
universe@483 243 */
universe@529 244 __attribute__((__nonnull__))
universe@483 245 int cxBufferShift(
universe@500 246 CxBuffer *buffer,
universe@483 247 off_t shift
universe@483 248 );
universe@483 249
universe@483 250 /**
universe@483 251 * Shifts the buffer to the right.
universe@483 252 * See cxBufferShift() for details.
universe@483 253 *
universe@485 254 * @param buffer the buffer
universe@485 255 * @param shift the shift offset
universe@485 256 * @return 0 on success, non-zero if a required auto-extension fails
universe@485 257 * @see cxBufferShift()
universe@483 258 */
universe@529 259 __attribute__((__nonnull__))
universe@483 260 int cxBufferShiftRight(
universe@500 261 CxBuffer *buffer,
universe@483 262 size_t shift
universe@483 263 );
universe@483 264
universe@483 265 /**
universe@483 266 * Shifts the buffer to the left.
universe@483 267 * See cxBufferShift() for details.
universe@483 268 *
universe@483 269 * \note Since a left shift cannot fail due to memory allocation problems, this
universe@483 270 * function always returns zero.
universe@483 271 *
universe@485 272 * @param buffer the buffer
universe@485 273 * @param shift the positive shift offset
universe@485 274 * @return always zero
universe@485 275 * @see cxBufferShift()
universe@483 276 */
universe@529 277 __attribute__((__nonnull__))
universe@483 278 int cxBufferShiftLeft(
universe@500 279 CxBuffer *buffer,
universe@483 280 size_t shift
universe@483 281 );
universe@483 282
universe@483 283
universe@483 284 /**
universe@483 285 * Moves the position of the buffer.
universe@483 286 *
universe@483 287 * The new position is relative to the \p whence argument.
universe@483 288 *
universe@483 289 * \li \c SEEK_SET marks the start of the buffer.
universe@483 290 * \li \c SEEK_CUR marks the current position.
universe@483 291 * \li \c SEEK_END marks the end of the buffer.
universe@483 292 *
universe@483 293 * With an offset of zero, this function sets the buffer position to zero
universe@483 294 * (\c SEEK_SET), the buffer size (\c SEEK_END) or leaves the buffer position
universe@483 295 * unchanged (\c SEEK_CUR).
universe@483 296 *
universe@485 297 * @param buffer the buffer
universe@485 298 * @param offset position offset relative to \p whence
universe@485 299 * @param whence one of \c SEEK_SET, \c SEEK_CUR or \c SEEK_END
universe@485 300 * @return 0 on success, non-zero if the position is invalid
universe@483 301 *
universe@483 302 */
universe@529 303 __attribute__((__nonnull__))
universe@483 304 int cxBufferSeek(
universe@500 305 CxBuffer *buffer,
universe@483 306 off_t offset,
universe@483 307 int whence
universe@483 308 );
universe@483 309
universe@483 310 /**
universe@483 311 * Clears the buffer by resetting the position and deleting the data.
universe@483 312 *
universe@483 313 * The data is deleted by zeroing it with a call to memset().
universe@483 314 *
universe@485 315 * @param buffer the buffer to be cleared
universe@483 316 */
universe@529 317 __attribute__((__nonnull__))
universe@529 318 void cxBufferClear(CxBuffer *buffer);
universe@483 319
universe@483 320 /**
universe@757 321 * Tests, if the buffer position has exceeded the buffer size.
universe@483 322 *
universe@485 323 * @param buffer the buffer to test
universe@485 324 * @return non-zero, if the current buffer position has exceeded the last
universe@757 325 * byte of the buffer's contents.
universe@483 326 */
universe@529 327 __attribute__((__nonnull__))
universe@529 328 int cxBufferEof(CxBuffer const *buffer);
universe@483 329
universe@483 330
universe@483 331 /**
universe@483 332 * Ensures that the buffer has a minimum capacity.
universe@483 333 *
universe@483 334 * If the current capacity is not sufficient, the buffer will be extended.
universe@483 335 *
universe@485 336 * @param buffer the buffer
universe@485 337 * @param capacity the minimum required capacity for this buffer
universe@485 338 * @return 0 on success or a non-zero value on failure
universe@483 339 */
universe@529 340 __attribute__((__nonnull__))
universe@483 341 int cxBufferMinimumCapacity(
universe@500 342 CxBuffer *buffer,
universe@483 343 size_t capacity
universe@483 344 );
universe@483 345
universe@483 346 /**
universe@483 347 * Writes data to a CxBuffer.
universe@483 348 *
universe@567 349 * If flushing is enabled and the buffer needs to flush, the data is flushed to
universe@567 350 * the target until the target signals that it cannot take more data by
universe@567 351 * returning zero via the respective write function. In that case, the remaining
universe@567 352 * data in this buffer is shifted to the beginning of this buffer so that the
universe@567 353 * newly available space can be used to append as much data as possible. This
universe@567 354 * function only stops writing more elements, when the flush target and this
universe@567 355 * buffer are both incapable of taking more data or all data has been written.
universe@567 356 * The number returned by this function is the total number of elements that
universe@567 357 * could be written during the process. It does not necessarily mean that those
universe@567 358 * elements are still in this buffer, because some of them could have also be
universe@567 359 * flushed already.
universe@567 360 *
universe@567 361 * If automatic flushing is not enabled, the position of the buffer is increased
universe@567 362 * by the number of bytes written.
universe@483 363 *
universe@483 364 * \note The signature is compatible with the fwrite() family of functions.
universe@483 365 *
universe@485 366 * @param ptr a pointer to the memory area containing the bytes to be written
universe@485 367 * @param size the length of one element
universe@485 368 * @param nitems the element count
universe@485 369 * @param buffer the CxBuffer to write to
universe@537 370 * @return the total count of elements written
universe@483 371 */
universe@529 372 __attribute__((__nonnull__))
universe@483 373 size_t cxBufferWrite(
universe@489 374 void const *ptr,
universe@483 375 size_t size,
universe@483 376 size_t nitems,
universe@500 377 CxBuffer *buffer
universe@483 378 );
universe@483 379
universe@483 380 /**
universe@483 381 * Reads data from a CxBuffer.
universe@483 382 *
universe@483 383 * The position of the buffer is increased by the number of bytes read.
universe@483 384 *
universe@483 385 * \note The signature is compatible with the fread() family of functions.
universe@483 386 *
universe@485 387 * @param ptr a pointer to the memory area where to store the read data
universe@485 388 * @param size the length of one element
universe@485 389 * @param nitems the element count
universe@485 390 * @param buffer the CxBuffer to read from
universe@485 391 * @return the total number of elements read
universe@483 392 */
universe@529 393 __attribute__((__nonnull__))
universe@483 394 size_t cxBufferRead(
universe@483 395 void *ptr,
universe@483 396 size_t size,
universe@483 397 size_t nitems,
universe@500 398 CxBuffer *buffer
universe@483 399 );
universe@483 400
universe@483 401 /**
universe@483 402 * Writes a character to a buffer.
universe@483 403 *
universe@483 404 * The least significant byte of the argument is written to the buffer. If the
universe@483 405 * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled,
universe@483 406 * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature is
universe@483 407 * disabled or buffer extension fails, \c EOF is returned.
universe@483 408 *
universe@483 409 * On successful write, the position of the buffer is increased.
universe@483 410 *
universe@485 411 * @param buffer the buffer to write to
universe@485 412 * @param c the character to write
universe@485 413 * @return the byte that has bean written or \c EOF when the end of the stream is
universe@483 414 * reached and automatic extension is not enabled or not possible
universe@483 415 */
universe@529 416 __attribute__((__nonnull__))
universe@483 417 int cxBufferPut(
universe@500 418 CxBuffer *buffer,
universe@483 419 int c
universe@483 420 );
universe@483 421
universe@483 422 /**
universe@529 423 * Writes a string to a buffer.
universe@529 424 *
universe@529 425 * @param buffer the buffer
universe@529 426 * @param str the zero-terminated string
universe@529 427 * @return the number of bytes written
universe@529 428 */
universe@529 429 __attribute__((__nonnull__))
universe@529 430 size_t cxBufferPutString(
universe@529 431 CxBuffer *buffer,
universe@529 432 const char *str
universe@529 433 );
universe@529 434
universe@529 435 /**
universe@483 436 * Gets a character from a buffer.
universe@483 437 *
universe@483 438 * The current position of the buffer is increased after a successful read.
universe@483 439 *
universe@485 440 * @param buffer the buffer to read from
universe@485 441 * @return the character or \c EOF, if the end of the buffer is reached
universe@483 442 */
universe@529 443 __attribute__((__nonnull__))
universe@500 444 int cxBufferGet(CxBuffer *buffer);
universe@483 445
universe@483 446 #ifdef __cplusplus
universe@483 447 }
universe@483 448 #endif
universe@483 449
universe@628 450 #endif // UCX_BUFFER_H

mercurial