src/cx/buffer.h

Mon, 31 Jan 2022 17:15:59 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 31 Jan 2022 17:15:59 +0100
changeset 501
9a08f5e515cc
parent 500
eb9e7bd40a8e
child 529
814d51173f20
permissions
-rw-r--r--

add allocator support to CxBuffer

Also change how the buffer itself is allocated and destroyed.

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 * \version 3.0
universe@483 44 * \copyright 2-Clause BSD License
universe@483 45 */
universe@483 46
universe@483 47 #ifndef UCX_BUFFER_H
universe@484 48 #define UCX_BUFFER_H
universe@483 49
universe@484 50 #include "common.h"
universe@501 51 #include "allocator.h"
universe@483 52 #include <stdio.h>
universe@483 53
universe@483 54 #ifdef __cplusplus
universe@483 55 extern "C" {
universe@483 56 #endif
universe@483 57
universe@483 58 /**
universe@483 59 * No buffer features enabled (all flags cleared).
universe@483 60 */
universe@483 61 #define CX_BUFFER_DEFAULT 0x00
universe@483 62
universe@483 63 /**
universe@483 64 * If this flag is enabled, the buffer will automatically free its contents when destroyed.
universe@483 65 */
universe@483 66 #define CX_BUFFER_FREE_CONTENTS 0x01
universe@483 67
universe@483 68 /**
universe@483 69 * If this flag is enabled, the buffer will automatically extends its capacity.
universe@483 70 */
universe@483 71 #define CX_BUFFER_AUTO_EXTEND 0x02
universe@483 72
universe@483 73 /** Structure for the UCX buffer data. */
universe@483 74 typedef struct {
universe@483 75 /** A pointer to the buffer contents. */
universe@483 76 union {
universe@483 77 /**
universe@483 78 * Data is interpreted as text.
universe@483 79 */
universe@483 80 char *space;
universe@483 81 /**
universe@483 82 * Data is interpreted as binary.
universe@483 83 */
universe@483 84 unsigned char *bytes;
universe@483 85 };
universe@501 86 /** The allocator to use for automatic memory management. */
universe@501 87 CxAllocator *allocator;
universe@483 88 /** Current position of the buffer. */
universe@483 89 size_t pos;
universe@483 90 /** Current capacity (i.e. maximum size) of the buffer. */
universe@483 91 size_t capacity;
universe@483 92 /** Current size of the buffer content. */
universe@483 93 size_t size;
universe@483 94 /**
universe@483 95 * Flag register for buffer features.
universe@485 96 * @see #CX_BUFFER_DEFAULT
universe@485 97 * @see #CX_BUFFER_FREE_CONTENTS
universe@485 98 * @see #CX_BUFFER_AUTO_EXTEND
universe@483 99 */
universe@483 100 int flags;
universe@483 101 } cx_buffer_s;
universe@483 102
universe@483 103 /**
universe@483 104 * UCX buffer.
universe@483 105 */
universe@500 106 typedef cx_buffer_s CxBuffer;
universe@483 107
universe@483 108 /**
universe@501 109 * Initializes a fresh buffer.
universe@483 110 *
universe@483 111 * \note You may provide \c NULL as argument for \p space.
universe@483 112 * Then this function will allocate the space and enforce
universe@483 113 * the #CX_BUFFER_FREE_CONTENTS flag.
universe@483 114 *
universe@501 115 * @param buffer the buffer to initialize
universe@501 116 * @param space pointer to the memory area, or \c NULL to allocate
universe@483 117 * new memory
universe@485 118 * @param capacity the capacity of the buffer
universe@501 119 * @param allocator the allocator this buffer shall use for automatic memory management
universe@485 120 * @param flags buffer features (see cx_buffer_s.flags)
universe@501 121 * @return zero on success, non-zero if a required allocation failed
universe@483 122 */
universe@501 123 int cxBufferInit(
universe@501 124 CxBuffer *buffer,
universe@483 125 void *space,
universe@483 126 size_t capacity,
universe@501 127 CxAllocator *allocator,
universe@483 128 int flags
universe@483 129 );
universe@483 130
universe@483 131 /**
universe@501 132 * Destroys the buffer contents.
universe@483 133 *
universe@501 134 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled.
universe@483 135 *
universe@501 136 * @param buffer the buffer which contents shall be destroyed
universe@483 137 */
universe@500 138 void cxBufferDestroy(CxBuffer *buffer);
universe@483 139
universe@483 140 /**
universe@483 141 * Creates a new buffer and fills it with content copied from another buffer.
universe@483 142 *
universe@483 143 * \note The #CX_BUFFER_FREE_CONTENTS feature is enforced for the new buffer.
universe@483 144 *
universe@485 145 * @param src the source buffer
universe@485 146 * @param start the start position of extraction
universe@485 147 * @param length the count of bytes to extract (must not be zero)
universe@485 148 * @param flags features for the new buffer (#CX_BUFFER_FREE_CONTENTS will always be enabled)
universe@485 149 * @return a new buffer containing the extraction
universe@483 150 */
universe@500 151 CxBuffer *cxBufferExtract(
universe@500 152 CxBuffer *src,
universe@483 153 size_t start,
universe@483 154 size_t length,
universe@483 155 int flags
universe@483 156 );
universe@483 157
universe@483 158 /**
universe@483 159 * A shorthand macro for copying an entire buffer.
universe@483 160 *
universe@485 161 * @param src the source buffer
universe@485 162 * @param flags features for the new buffer (#CX_BUFFER_FREE_CONTENTS will always be enabled)
universe@485 163 * @return a new buffer with the copied content
universe@483 164 */
universe@483 165 #define cxBufferClone(src, flags) cxBufferExtract(src, 0, (src)->capacity, flags)
universe@483 166
universe@483 167
universe@483 168 /**
universe@483 169 * Shifts the contents of the buffer by the given offset.
universe@483 170 *
universe@483 171 * If the offset is positive, the contents are shifted to the right.
universe@483 172 * If auto extension is enabled, the buffer grows, if necessary.
universe@483 173 * In case the auto extension fails, this function returns a non-zero value and
universe@483 174 * no contents are changed.
universe@483 175 * If auto extension is disabled, the contents that do not fit into the buffer
universe@483 176 * are discarded.
universe@483 177 *
universe@483 178 * If the offset is negative, the contents are shifted to the left where the
universe@483 179 * first \p shift bytes are discarded.
universe@483 180 * The new size of the buffer is the old size minus the absolute shift value.
universe@483 181 * If this value is larger than the buffer size, the buffer is emptied (but
universe@483 182 * not cleared, see the security note below).
universe@483 183 *
universe@483 184 * The buffer position gets shifted alongside with the content but is kept
universe@483 185 * within the boundaries of the buffer.
universe@483 186 *
universe@483 187 * \note For situations where \c off_t is not large enough, there are specialized cxBufferShiftLeft() and
universe@483 188 * cxBufferShiftRight() functions using a \c size_t as parameter type.
universe@483 189 *
universe@485 190 * \attention
universe@485 191 * Security Note: The shifting operation does \em not erase the previously occupied memory cells.
universe@485 192 * But you can easily do that manually, e.g. by calling
universe@483 193 * <code>memset(buffer->bytes, 0, shift)</code> for a right shift or
universe@483 194 * <code>memset(buffer->size, 0, buffer->capacity - buffer->size)</code>
universe@483 195 * for a left shift.
universe@483 196 *
universe@485 197 * @param buffer the buffer
universe@485 198 * @param shift the shift offset (negative means left shift)
universe@485 199 * @return 0 on success, non-zero if a required auto-extension fails
universe@483 200 */
universe@483 201 int cxBufferShift(
universe@500 202 CxBuffer *buffer,
universe@483 203 off_t shift
universe@483 204 );
universe@483 205
universe@483 206 /**
universe@483 207 * Shifts the buffer to the right.
universe@483 208 * See cxBufferShift() for details.
universe@483 209 *
universe@485 210 * @param buffer the buffer
universe@485 211 * @param shift the shift offset
universe@485 212 * @return 0 on success, non-zero if a required auto-extension fails
universe@485 213 * @see cxBufferShift()
universe@483 214 */
universe@483 215 int cxBufferShiftRight(
universe@500 216 CxBuffer *buffer,
universe@483 217 size_t shift
universe@483 218 );
universe@483 219
universe@483 220 /**
universe@483 221 * Shifts the buffer to the left.
universe@483 222 * See cxBufferShift() for details.
universe@483 223 *
universe@483 224 * \note Since a left shift cannot fail due to memory allocation problems, this
universe@483 225 * function always returns zero.
universe@483 226 *
universe@485 227 * @param buffer the buffer
universe@485 228 * @param shift the positive shift offset
universe@485 229 * @return always zero
universe@485 230 * @see cxBufferShift()
universe@483 231 */
universe@483 232 int cxBufferShiftLeft(
universe@500 233 CxBuffer *buffer,
universe@483 234 size_t shift
universe@483 235 );
universe@483 236
universe@483 237
universe@483 238 /**
universe@483 239 * Moves the position of the buffer.
universe@483 240 *
universe@483 241 * The new position is relative to the \p whence argument.
universe@483 242 *
universe@483 243 * \li \c SEEK_SET marks the start of the buffer.
universe@483 244 * \li \c SEEK_CUR marks the current position.
universe@483 245 * \li \c SEEK_END marks the end of the buffer.
universe@483 246 *
universe@483 247 * With an offset of zero, this function sets the buffer position to zero
universe@483 248 * (\c SEEK_SET), the buffer size (\c SEEK_END) or leaves the buffer position
universe@483 249 * unchanged (\c SEEK_CUR).
universe@483 250 *
universe@485 251 * @param buffer the buffer
universe@485 252 * @param offset position offset relative to \p whence
universe@485 253 * @param whence one of \c SEEK_SET, \c SEEK_CUR or \c SEEK_END
universe@485 254 * @return 0 on success, non-zero if the position is invalid
universe@483 255 *
universe@483 256 */
universe@483 257 int cxBufferSeek(
universe@500 258 CxBuffer *buffer,
universe@483 259 off_t offset,
universe@483 260 int whence
universe@483 261 );
universe@483 262
universe@483 263 /**
universe@483 264 * Clears the buffer by resetting the position and deleting the data.
universe@483 265 *
universe@483 266 * The data is deleted by zeroing it with a call to memset().
universe@483 267 *
universe@485 268 * @param buffer the buffer to be cleared
universe@483 269 */
universe@483 270 #define cxBufferClear(buffer) memset((buffer)->bytes, 0, (buffer)->size); \
universe@483 271 (buffer)->size = 0; (buffer)->pos = 0;
universe@483 272
universe@483 273 /**
universe@483 274 * Tests, if the buffer position has exceeded the buffer capacity.
universe@483 275 *
universe@485 276 * @param buffer the buffer to test
universe@485 277 * @return non-zero, if the current buffer position has exceeded the last
universe@483 278 * available byte of the buffer.
universe@483 279 */
universe@500 280 int cxBufferEof(CxBuffer *buffer);
universe@483 281
universe@483 282
universe@483 283 /**
universe@483 284 * Ensures that the buffer has a minimum capacity.
universe@483 285 *
universe@483 286 * If the current capacity is not sufficient, the buffer will be extended.
universe@483 287 *
universe@485 288 * @param buffer the buffer
universe@485 289 * @param capacity the minimum required capacity for this buffer
universe@485 290 * @return 0 on success or a non-zero value on failure
universe@483 291 */
universe@483 292 int cxBufferMinimumCapacity(
universe@500 293 CxBuffer *buffer,
universe@483 294 size_t capacity
universe@483 295 );
universe@483 296
universe@483 297 /**
universe@483 298 * Writes data to a CxBuffer.
universe@483 299 *
universe@483 300 * The position of the buffer is increased by the number of bytes written.
universe@483 301 *
universe@483 302 * \note The signature is compatible with the fwrite() family of functions.
universe@483 303 *
universe@485 304 * @param ptr a pointer to the memory area containing the bytes to be written
universe@485 305 * @param size the length of one element
universe@485 306 * @param nitems the element count
universe@485 307 * @param buffer the CxBuffer to write to
universe@485 308 * @return the total count of bytes written
universe@483 309 */
universe@483 310 size_t cxBufferWrite(
universe@489 311 void const *ptr,
universe@483 312 size_t size,
universe@483 313 size_t nitems,
universe@500 314 CxBuffer *buffer
universe@483 315 );
universe@483 316
universe@483 317 /**
universe@483 318 * Reads data from a CxBuffer.
universe@483 319 *
universe@483 320 * The position of the buffer is increased by the number of bytes read.
universe@483 321 *
universe@483 322 * \note The signature is compatible with the fread() family of functions.
universe@483 323 *
universe@485 324 * @param ptr a pointer to the memory area where to store the read data
universe@485 325 * @param size the length of one element
universe@485 326 * @param nitems the element count
universe@485 327 * @param buffer the CxBuffer to read from
universe@485 328 * @return the total number of elements read
universe@483 329 */
universe@483 330 size_t cxBufferRead(
universe@483 331 void *ptr,
universe@483 332 size_t size,
universe@483 333 size_t nitems,
universe@500 334 CxBuffer *buffer
universe@483 335 );
universe@483 336
universe@483 337 /**
universe@483 338 * Writes a character to a buffer.
universe@483 339 *
universe@483 340 * The least significant byte of the argument is written to the buffer. If the
universe@483 341 * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled,
universe@483 342 * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature is
universe@483 343 * disabled or buffer extension fails, \c EOF is returned.
universe@483 344 *
universe@483 345 * On successful write, the position of the buffer is increased.
universe@483 346 *
universe@485 347 * @param buffer the buffer to write to
universe@485 348 * @param c the character to write
universe@485 349 * @return the byte that has bean written or \c EOF when the end of the stream is
universe@483 350 * reached and automatic extension is not enabled or not possible
universe@483 351 */
universe@483 352 int cxBufferPut(
universe@500 353 CxBuffer *buffer,
universe@483 354 int c
universe@483 355 );
universe@483 356
universe@483 357 /**
universe@483 358 * Gets a character from a buffer.
universe@483 359 *
universe@483 360 * The current position of the buffer is increased after a successful read.
universe@483 361 *
universe@485 362 * @param buffer the buffer to read from
universe@485 363 * @return the character or \c EOF, if the end of the buffer is reached
universe@483 364 */
universe@500 365 int cxBufferGet(CxBuffer *buffer);
universe@483 366
universe@483 367 /**
universe@483 368 * Writes a string to a buffer.
universe@483 369 *
universe@485 370 * @param buffer the buffer
universe@485 371 * @param str the zero-terminated string
universe@485 372 * @return the number of bytes written
universe@483 373 */
universe@483 374 size_t cxBufferPutString(
universe@500 375 CxBuffer *buffer,
universe@483 376 const char *str
universe@483 377 );
universe@483 378
universe@483 379 #ifdef __cplusplus
universe@483 380 }
universe@483 381 #endif
universe@483 382
universe@483 383 #endif /* UCX_BUFFER_H */

mercurial