universe@483: /*
universe@483: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@483: *
universe@483: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@483: *
universe@483: * Redistribution and use in source and binary forms, with or without
universe@483: * modification, are permitted provided that the following conditions are met:
universe@483: *
universe@483: * 1. Redistributions of source code must retain the above copyright
universe@483: * notice, this list of conditions and the following disclaimer.
universe@483: *
universe@483: * 2. Redistributions in binary form must reproduce the above copyright
universe@483: * notice, this list of conditions and the following disclaimer in the
universe@483: * documentation and/or other materials provided with the distribution.
universe@483: *
universe@483: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@483: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@483: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@483: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@483: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@483: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@483: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@483: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@483: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@483: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@483: * POSSIBILITY OF SUCH DAMAGE.
universe@483: */
universe@483:
universe@483: /**
universe@483: * \file buffer.h
universe@483: *
universe@483: * \brief Advanced buffer implementation.
universe@483: *
universe@483: * Instances of CxBuffer can be used to read from or to write to like one
universe@483: * would do with a stream.
universe@483: *
universe@483: * Some features for convenient use of the buffer
universe@483: * can be enabled. See the documentation of the macro constants for more
universe@483: * information.
universe@483: *
universe@483: * \author Mike Becker
universe@483: * \author Olaf Wintermann
universe@483: * \version 3.0
universe@483: * \copyright 2-Clause BSD License
universe@483: */
universe@483:
universe@483: #ifndef UCX_BUFFER_H
universe@484: #define UCX_BUFFER_H
universe@483:
universe@484: #include "common.h"
universe@501: #include "allocator.h"
universe@483:
universe@483: #ifdef __cplusplus
universe@483: extern "C" {
universe@483: #endif
universe@483:
universe@483: /**
universe@483: * No buffer features enabled (all flags cleared).
universe@483: */
universe@483: #define CX_BUFFER_DEFAULT 0x00
universe@483:
universe@483: /**
universe@483: * If this flag is enabled, the buffer will automatically free its contents when destroyed.
universe@483: */
universe@483: #define CX_BUFFER_FREE_CONTENTS 0x01
universe@483:
universe@483: /**
universe@483: * If this flag is enabled, the buffer will automatically extends its capacity.
universe@483: */
universe@483: #define CX_BUFFER_AUTO_EXTEND 0x02
universe@483:
universe@483: /** Structure for the UCX buffer data. */
universe@483: typedef struct {
universe@483: /** A pointer to the buffer contents. */
universe@483: union {
universe@483: /**
universe@483: * Data is interpreted as text.
universe@483: */
universe@483: char *space;
universe@483: /**
universe@483: * Data is interpreted as binary.
universe@483: */
universe@483: unsigned char *bytes;
universe@483: };
universe@501: /** The allocator to use for automatic memory management. */
universe@529: CxAllocator const *allocator;
universe@483: /** Current position of the buffer. */
universe@483: size_t pos;
universe@483: /** Current capacity (i.e. maximum size) of the buffer. */
universe@483: size_t capacity;
universe@483: /** Current size of the buffer content. */
universe@483: size_t size;
universe@483: /**
universe@539: * The buffer may not extend beyond this threshold before starting to flush.
universe@539: * Default is \c SIZE_MAX (flushing disabled when auto extension is enabled).
universe@539: */
universe@539: size_t flush_threshold;
universe@539: /**
universe@539: * The block size for the elements to flush.
universe@539: * Default is 4096 bytes.
universe@539: */
universe@539: size_t flush_blksize;
universe@539: /**
universe@539: * The maximum number of blocks to flush in one cycle.
universe@539: * Zero disables flushing entirely (this is the default).
universe@539: * Set this to \c SIZE_MAX to flush the entire buffer.
universe@539: *
universe@539: * @attention if the maximum number of blocks multiplied with the block size
universe@539: * is smaller than the expected contents written to this buffer within one write
universe@539: * operation, multiple flush cycles are performed after that write.
universe@539: * That means the total number of blocks flushed after one write to this buffer may
universe@539: * be larger than \c flush_blkmax.
universe@539: */
universe@539: size_t flush_blkmax;
universe@539:
universe@539: /**
universe@539: * The write function used for flushing.
universe@539: * If NULL, the flushed content gets discarded.
universe@539: */
universe@545: cx_write_func flush_func;
universe@539:
universe@539: /**
universe@541: * The target for \c flush_func.
universe@541: */
universe@541: void *flush_target;
universe@541:
universe@541: /**
universe@483: * Flag register for buffer features.
universe@485: * @see #CX_BUFFER_DEFAULT
universe@485: * @see #CX_BUFFER_FREE_CONTENTS
universe@485: * @see #CX_BUFFER_AUTO_EXTEND
universe@483: */
universe@483: int flags;
universe@483: } cx_buffer_s;
universe@483:
universe@483: /**
universe@483: * UCX buffer.
universe@483: */
universe@500: typedef cx_buffer_s CxBuffer;
universe@483:
universe@483: /**
universe@501: * Initializes a fresh buffer.
universe@483: *
universe@483: * \note You may provide \c NULL as argument for \p space.
universe@483: * Then this function will allocate the space and enforce
universe@483: * the #CX_BUFFER_FREE_CONTENTS flag.
universe@483: *
universe@501: * @param buffer the buffer to initialize
universe@501: * @param space pointer to the memory area, or \c NULL to allocate
universe@483: * new memory
universe@485: * @param capacity the capacity of the buffer
universe@501: * @param allocator the allocator this buffer shall use for automatic memory management
universe@485: * @param flags buffer features (see cx_buffer_s.flags)
universe@501: * @return zero on success, non-zero if a required allocation failed
universe@483: */
universe@529: __attribute__((__nonnull__(1, 4)))
universe@501: int cxBufferInit(
universe@501: CxBuffer *buffer,
universe@483: void *space,
universe@483: size_t capacity,
universe@529: CxAllocator const *allocator,
universe@483: int flags
universe@483: );
universe@483:
universe@483: /**
universe@501: * Destroys the buffer contents.
universe@483: *
universe@501: * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled.
universe@483: *
universe@501: * @param buffer the buffer which contents shall be destroyed
universe@483: */
universe@529: __attribute__((__nonnull__))
universe@500: void cxBufferDestroy(CxBuffer *buffer);
universe@483:
universe@483: /**
universe@483: * Shifts the contents of the buffer by the given offset.
universe@483: *
universe@483: * If the offset is positive, the contents are shifted to the right.
universe@483: * If auto extension is enabled, the buffer grows, if necessary.
universe@483: * In case the auto extension fails, this function returns a non-zero value and
universe@483: * no contents are changed.
universe@483: * If auto extension is disabled, the contents that do not fit into the buffer
universe@483: * are discarded.
universe@483: *
universe@483: * If the offset is negative, the contents are shifted to the left where the
universe@483: * first \p shift bytes are discarded.
universe@483: * The new size of the buffer is the old size minus the absolute shift value.
universe@483: * If this value is larger than the buffer size, the buffer is emptied (but
universe@483: * not cleared, see the security note below).
universe@483: *
universe@483: * The buffer position gets shifted alongside with the content but is kept
universe@483: * within the boundaries of the buffer.
universe@483: *
universe@483: * \note For situations where \c off_t is not large enough, there are specialized cxBufferShiftLeft() and
universe@483: * cxBufferShiftRight() functions using a \c size_t as parameter type.
universe@483: *
universe@485: * \attention
universe@485: * Security Note: The shifting operation does \em not erase the previously occupied memory cells.
universe@485: * But you can easily do that manually, e.g. by calling
universe@483: * memset(buffer->bytes, 0, shift)
for a right shift or
universe@531: * memset(buffer->bytes + buffer->size, 0, buffer->capacity - buffer->size)
universe@483: * for a left shift.
universe@483: *
universe@485: * @param buffer the buffer
universe@485: * @param shift the shift offset (negative means left shift)
universe@485: * @return 0 on success, non-zero if a required auto-extension fails
universe@483: */
universe@529: __attribute__((__nonnull__))
universe@483: int cxBufferShift(
universe@500: CxBuffer *buffer,
universe@483: off_t shift
universe@483: );
universe@483:
universe@483: /**
universe@483: * Shifts the buffer to the right.
universe@483: * See cxBufferShift() for details.
universe@483: *
universe@485: * @param buffer the buffer
universe@485: * @param shift the shift offset
universe@485: * @return 0 on success, non-zero if a required auto-extension fails
universe@485: * @see cxBufferShift()
universe@483: */
universe@529: __attribute__((__nonnull__))
universe@483: int cxBufferShiftRight(
universe@500: CxBuffer *buffer,
universe@483: size_t shift
universe@483: );
universe@483:
universe@483: /**
universe@483: * Shifts the buffer to the left.
universe@483: * See cxBufferShift() for details.
universe@483: *
universe@483: * \note Since a left shift cannot fail due to memory allocation problems, this
universe@483: * function always returns zero.
universe@483: *
universe@485: * @param buffer the buffer
universe@485: * @param shift the positive shift offset
universe@485: * @return always zero
universe@485: * @see cxBufferShift()
universe@483: */
universe@529: __attribute__((__nonnull__))
universe@483: int cxBufferShiftLeft(
universe@500: CxBuffer *buffer,
universe@483: size_t shift
universe@483: );
universe@483:
universe@483:
universe@483: /**
universe@483: * Moves the position of the buffer.
universe@483: *
universe@483: * The new position is relative to the \p whence argument.
universe@483: *
universe@483: * \li \c SEEK_SET marks the start of the buffer.
universe@483: * \li \c SEEK_CUR marks the current position.
universe@483: * \li \c SEEK_END marks the end of the buffer.
universe@483: *
universe@483: * With an offset of zero, this function sets the buffer position to zero
universe@483: * (\c SEEK_SET), the buffer size (\c SEEK_END) or leaves the buffer position
universe@483: * unchanged (\c SEEK_CUR).
universe@483: *
universe@485: * @param buffer the buffer
universe@485: * @param offset position offset relative to \p whence
universe@485: * @param whence one of \c SEEK_SET, \c SEEK_CUR or \c SEEK_END
universe@485: * @return 0 on success, non-zero if the position is invalid
universe@483: *
universe@483: */
universe@529: __attribute__((__nonnull__))
universe@483: int cxBufferSeek(
universe@500: CxBuffer *buffer,
universe@483: off_t offset,
universe@483: int whence
universe@483: );
universe@483:
universe@483: /**
universe@483: * Clears the buffer by resetting the position and deleting the data.
universe@483: *
universe@483: * The data is deleted by zeroing it with a call to memset().
universe@483: *
universe@485: * @param buffer the buffer to be cleared
universe@483: */
universe@529: __attribute__((__nonnull__))
universe@529: void cxBufferClear(CxBuffer *buffer);
universe@483:
universe@483: /**
universe@483: * Tests, if the buffer position has exceeded the buffer capacity.
universe@483: *
universe@485: * @param buffer the buffer to test
universe@485: * @return non-zero, if the current buffer position has exceeded the last
universe@483: * available byte of the buffer.
universe@483: */
universe@529: __attribute__((__nonnull__))
universe@529: int cxBufferEof(CxBuffer const *buffer);
universe@483:
universe@483:
universe@483: /**
universe@483: * Ensures that the buffer has a minimum capacity.
universe@483: *
universe@483: * If the current capacity is not sufficient, the buffer will be extended.
universe@483: *
universe@485: * @param buffer the buffer
universe@485: * @param capacity the minimum required capacity for this buffer
universe@485: * @return 0 on success or a non-zero value on failure
universe@483: */
universe@529: __attribute__((__nonnull__))
universe@483: int cxBufferMinimumCapacity(
universe@500: CxBuffer *buffer,
universe@483: size_t capacity
universe@483: );
universe@483:
universe@483: /**
universe@483: * Writes data to a CxBuffer.
universe@483: *
universe@567: * If flushing is enabled and the buffer needs to flush, the data is flushed to
universe@567: * the target until the target signals that it cannot take more data by
universe@567: * returning zero via the respective write function. In that case, the remaining
universe@567: * data in this buffer is shifted to the beginning of this buffer so that the
universe@567: * newly available space can be used to append as much data as possible. This
universe@567: * function only stops writing more elements, when the flush target and this
universe@567: * buffer are both incapable of taking more data or all data has been written.
universe@567: * The number returned by this function is the total number of elements that
universe@567: * could be written during the process. It does not necessarily mean that those
universe@567: * elements are still in this buffer, because some of them could have also be
universe@567: * flushed already.
universe@567: *
universe@567: * If automatic flushing is not enabled, the position of the buffer is increased
universe@567: * by the number of bytes written.
universe@483: *
universe@483: * \note The signature is compatible with the fwrite() family of functions.
universe@483: *
universe@485: * @param ptr a pointer to the memory area containing the bytes to be written
universe@485: * @param size the length of one element
universe@485: * @param nitems the element count
universe@485: * @param buffer the CxBuffer to write to
universe@537: * @return the total count of elements written
universe@483: */
universe@529: __attribute__((__nonnull__))
universe@483: size_t cxBufferWrite(
universe@489: void const *ptr,
universe@483: size_t size,
universe@483: size_t nitems,
universe@500: CxBuffer *buffer
universe@483: );
universe@483:
universe@483: /**
universe@483: * Reads data from a CxBuffer.
universe@483: *
universe@483: * The position of the buffer is increased by the number of bytes read.
universe@483: *
universe@483: * \note The signature is compatible with the fread() family of functions.
universe@483: *
universe@485: * @param ptr a pointer to the memory area where to store the read data
universe@485: * @param size the length of one element
universe@485: * @param nitems the element count
universe@485: * @param buffer the CxBuffer to read from
universe@485: * @return the total number of elements read
universe@483: */
universe@529: __attribute__((__nonnull__))
universe@483: size_t cxBufferRead(
universe@483: void *ptr,
universe@483: size_t size,
universe@483: size_t nitems,
universe@500: CxBuffer *buffer
universe@483: );
universe@483:
universe@483: /**
universe@483: * Writes a character to a buffer.
universe@483: *
universe@483: * The least significant byte of the argument is written to the buffer. If the
universe@483: * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled,
universe@483: * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature is
universe@483: * disabled or buffer extension fails, \c EOF is returned.
universe@483: *
universe@483: * On successful write, the position of the buffer is increased.
universe@483: *
universe@485: * @param buffer the buffer to write to
universe@485: * @param c the character to write
universe@485: * @return the byte that has bean written or \c EOF when the end of the stream is
universe@483: * reached and automatic extension is not enabled or not possible
universe@483: */
universe@529: __attribute__((__nonnull__))
universe@483: int cxBufferPut(
universe@500: CxBuffer *buffer,
universe@483: int c
universe@483: );
universe@483:
universe@483: /**
universe@529: * Writes a string to a buffer.
universe@529: *
universe@529: * @param buffer the buffer
universe@529: * @param str the zero-terminated string
universe@529: * @return the number of bytes written
universe@529: */
universe@529: __attribute__((__nonnull__))
universe@529: size_t cxBufferPutString(
universe@529: CxBuffer *buffer,
universe@529: const char *str
universe@529: );
universe@529:
universe@529: /**
universe@483: * Gets a character from a buffer.
universe@483: *
universe@483: * The current position of the buffer is increased after a successful read.
universe@483: *
universe@485: * @param buffer the buffer to read from
universe@485: * @return the character or \c EOF, if the end of the buffer is reached
universe@483: */
universe@529: __attribute__((__nonnull__))
universe@500: int cxBufferGet(CxBuffer *buffer);
universe@483:
universe@483: #ifdef __cplusplus
universe@483: }
universe@483: #endif
universe@483:
universe@628: #endif // UCX_BUFFER_H