2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
32 * \brief Advanced buffer implementation.
34 * Instances of CxBuffer can be used to read from or to write to like one
35 * would do with a stream.
37 * Some features for convenient use of the buffer
38 * can be enabled. See the documentation of the macro constants for more
42 * \author Olaf Wintermann
44 * \copyright 2-Clause BSD License
51 #include "allocator.h"
58 * No buffer features enabled (all flags cleared).
60 #define CX_BUFFER_DEFAULT 0x00
63 * If this flag is enabled, the buffer will automatically free its contents when destroyed.
65 #define CX_BUFFER_FREE_CONTENTS 0x01
68 * If this flag is enabled, the buffer will automatically extends its capacity.
70 #define CX_BUFFER_AUTO_EXTEND 0x02
72 /** Structure for the UCX buffer data. */
74 /** A pointer to the buffer contents. */
77 * Data is interpreted as text.
81 * Data is interpreted as binary.
85 /** The allocator to use for automatic memory management. */
86 CxAllocator const *allocator;
87 /** Current position of the buffer. */
89 /** Current capacity (i.e. maximum size) of the buffer. */
91 /** Current size of the buffer content. */
94 * The buffer may not extend beyond this threshold before starting to flush.
95 * Default is \c SIZE_MAX (flushing disabled when auto extension is enabled).
97 size_t flush_threshold;
99 * The block size for the elements to flush.
100 * Default is 4096 bytes.
102 size_t flush_blksize;
104 * The maximum number of blocks to flush in one cycle.
105 * Zero disables flushing entirely (this is the default).
106 * Set this to \c SIZE_MAX to flush the entire buffer.
108 * @attention if the maximum number of blocks multiplied with the block size
109 * is smaller than the expected contents written to this buffer within one write
110 * operation, multiple flush cycles are performed after that write.
111 * That means the total number of blocks flushed after one write to this buffer may
112 * be larger than \c flush_blkmax.
117 * The write function used for flushing.
118 * If NULL, the flushed content gets discarded.
120 cx_write_func flush_func;
123 * The target for \c flush_func.
128 * Flag register for buffer features.
129 * @see #CX_BUFFER_DEFAULT
130 * @see #CX_BUFFER_FREE_CONTENTS
131 * @see #CX_BUFFER_AUTO_EXTEND
139 typedef cx_buffer_s CxBuffer;
142 * Initializes a fresh buffer.
144 * \note You may provide \c NULL as argument for \p space.
145 * Then this function will allocate the space and enforce
146 * the #CX_BUFFER_FREE_CONTENTS flag.
148 * @param buffer the buffer to initialize
149 * @param space pointer to the memory area, or \c NULL to allocate
151 * @param capacity the capacity of the buffer
152 * @param allocator the allocator this buffer shall use for automatic
153 * memory management. If \c NULL, the default heap allocator will be used.
154 * @param flags buffer features (see cx_buffer_s.flags)
155 * @return zero on success, non-zero if a required allocation failed
157 __attribute__((__nonnull__(1)))
162 CxAllocator const *allocator,
167 * Destroys the buffer contents.
169 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled.
171 * @param buffer the buffer which contents shall be destroyed
173 __attribute__((__nonnull__))
174 void cxBufferDestroy(CxBuffer *buffer);
177 * Shifts the contents of the buffer by the given offset.
179 * If the offset is positive, the contents are shifted to the right.
180 * If auto extension is enabled, the buffer grows, if necessary.
181 * In case the auto extension fails, this function returns a non-zero value and
182 * no contents are changed.
183 * If auto extension is disabled, the contents that do not fit into the buffer
186 * If the offset is negative, the contents are shifted to the left where the
187 * first \p shift bytes are discarded.
188 * The new size of the buffer is the old size minus the absolute shift value.
189 * If this value is larger than the buffer size, the buffer is emptied (but
190 * not cleared, see the security note below).
192 * The buffer position gets shifted alongside with the content but is kept
193 * within the boundaries of the buffer.
195 * \note For situations where \c off_t is not large enough, there are specialized cxBufferShiftLeft() and
196 * cxBufferShiftRight() functions using a \c size_t as parameter type.
199 * Security Note: The shifting operation does \em not erase the previously occupied memory cells.
200 * But you can easily do that manually, e.g. by calling
201 * <code>memset(buffer->bytes, 0, shift)</code> for a right shift or
202 * <code>memset(buffer->bytes + buffer->size, 0, buffer->capacity - buffer->size)</code>
205 * @param buffer the buffer
206 * @param shift the shift offset (negative means left shift)
207 * @return 0 on success, non-zero if a required auto-extension fails
209 __attribute__((__nonnull__))
216 * Shifts the buffer to the right.
217 * See cxBufferShift() for details.
219 * @param buffer the buffer
220 * @param shift the shift offset
221 * @return 0 on success, non-zero if a required auto-extension fails
222 * @see cxBufferShift()
224 __attribute__((__nonnull__))
225 int cxBufferShiftRight(
231 * Shifts the buffer to the left.
232 * See cxBufferShift() for details.
234 * \note Since a left shift cannot fail due to memory allocation problems, this
235 * function always returns zero.
237 * @param buffer the buffer
238 * @param shift the positive shift offset
239 * @return always zero
240 * @see cxBufferShift()
242 __attribute__((__nonnull__))
243 int cxBufferShiftLeft(
250 * Moves the position of the buffer.
252 * The new position is relative to the \p whence argument.
254 * \li \c SEEK_SET marks the start of the buffer.
255 * \li \c SEEK_CUR marks the current position.
256 * \li \c SEEK_END marks the end of the buffer.
258 * With an offset of zero, this function sets the buffer position to zero
259 * (\c SEEK_SET), the buffer size (\c SEEK_END) or leaves the buffer position
260 * unchanged (\c SEEK_CUR).
262 * @param buffer the buffer
263 * @param offset position offset relative to \p whence
264 * @param whence one of \c SEEK_SET, \c SEEK_CUR or \c SEEK_END
265 * @return 0 on success, non-zero if the position is invalid
268 __attribute__((__nonnull__))
276 * Clears the buffer by resetting the position and deleting the data.
278 * The data is deleted by zeroing it with a call to memset().
280 * @param buffer the buffer to be cleared
282 __attribute__((__nonnull__))
283 void cxBufferClear(CxBuffer *buffer);
286 * Tests, if the buffer position has exceeded the buffer capacity.
288 * @param buffer the buffer to test
289 * @return non-zero, if the current buffer position has exceeded the last
290 * available byte of the buffer.
292 __attribute__((__nonnull__))
293 int cxBufferEof(CxBuffer const *buffer);
297 * Ensures that the buffer has a minimum capacity.
299 * If the current capacity is not sufficient, the buffer will be extended.
301 * @param buffer the buffer
302 * @param capacity the minimum required capacity for this buffer
303 * @return 0 on success or a non-zero value on failure
305 __attribute__((__nonnull__))
306 int cxBufferMinimumCapacity(
312 * Writes data to a CxBuffer.
314 * If flushing is enabled and the buffer needs to flush, the data is flushed to
315 * the target until the target signals that it cannot take more data by
316 * returning zero via the respective write function. In that case, the remaining
317 * data in this buffer is shifted to the beginning of this buffer so that the
318 * newly available space can be used to append as much data as possible. This
319 * function only stops writing more elements, when the flush target and this
320 * buffer are both incapable of taking more data or all data has been written.
321 * The number returned by this function is the total number of elements that
322 * could be written during the process. It does not necessarily mean that those
323 * elements are still in this buffer, because some of them could have also be
326 * If automatic flushing is not enabled, the position of the buffer is increased
327 * by the number of bytes written.
329 * \note The signature is compatible with the fwrite() family of functions.
331 * @param ptr a pointer to the memory area containing the bytes to be written
332 * @param size the length of one element
333 * @param nitems the element count
334 * @param buffer the CxBuffer to write to
335 * @return the total count of elements written
337 __attribute__((__nonnull__))
338 size_t cxBufferWrite(
346 * Reads data from a CxBuffer.
348 * The position of the buffer is increased by the number of bytes read.
350 * \note The signature is compatible with the fread() family of functions.
352 * @param ptr a pointer to the memory area where to store the read data
353 * @param size the length of one element
354 * @param nitems the element count
355 * @param buffer the CxBuffer to read from
356 * @return the total number of elements read
358 __attribute__((__nonnull__))
367 * Writes a character to a buffer.
369 * The least significant byte of the argument is written to the buffer. If the
370 * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled,
371 * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature is
372 * disabled or buffer extension fails, \c EOF is returned.
374 * On successful write, the position of the buffer is increased.
376 * @param buffer the buffer to write to
377 * @param c the character to write
378 * @return the byte that has bean written or \c EOF when the end of the stream is
379 * reached and automatic extension is not enabled or not possible
381 __attribute__((__nonnull__))
388 * Writes a string to a buffer.
390 * @param buffer the buffer
391 * @param str the zero-terminated string
392 * @return the number of bytes written
394 __attribute__((__nonnull__))
395 size_t cxBufferPutString(
401 * Gets a character from a buffer.
403 * The current position of the buffer is increased after a successful read.
405 * @param buffer the buffer to read from
406 * @return the character or \c EOF, if the end of the buffer is reached
408 __attribute__((__nonnull__))
409 int cxBufferGet(CxBuffer *buffer);
415 #endif // UCX_BUFFER_H