ucx/cx/buffer.h

changeset 11
0aa8cbd7912e
parent 0
1a157da63d7c
child 16
04c9f8d8f03b
equal deleted inserted replaced
10:80f9d007cb52 11:0aa8cbd7912e
47 #define UCX_BUFFER_H 47 #define UCX_BUFFER_H
48 48
49 #include "common.h" 49 #include "common.h"
50 #include "allocator.h" 50 #include "allocator.h"
51 51
52 #ifdef __cplusplus 52 #ifdef __cplusplus
53 extern "C" { 53 extern "C" {
54 #endif 54 #endif
55 55
56 /** 56 /**
57 * No buffer features enabled (all flags cleared). 57 * No buffer features enabled (all flags cleared).
58 */ 58 */
59 #define CX_BUFFER_DEFAULT 0x00 59 #define CX_BUFFER_DEFAULT 0x00
60 60
61 /** 61 /**
62 * If this flag is enabled, the buffer will automatically free its contents when destroyed. 62 * If this flag is enabled, the buffer will automatically free its contents when destroyed.
63 *
64 * Do NOT set this flag together with #CX_BUFFER_COPY_ON_WRITE. It will be automatically
65 * set when the copy-on-write operations is performed.
63 */ 66 */
64 #define CX_BUFFER_FREE_CONTENTS 0x01 67 #define CX_BUFFER_FREE_CONTENTS 0x01
65 68
66 /** 69 /**
67 * If this flag is enabled, the buffer will automatically extends its capacity. 70 * If this flag is enabled, the buffer will automatically extend its capacity.
68 */ 71 */
69 #define CX_BUFFER_AUTO_EXTEND 0x02 72 #define CX_BUFFER_AUTO_EXTEND 0x02
73
74 /**
75 * If this flag is enabled, the buffer will allocate new memory when written to.
76 *
77 * The current contents of the buffer will be copied to the new memory and the flag
78 * will be cleared while the #CX_BUFFER_FREE_CONTENTS flag will be set automatically.
79 */
80 #define CX_BUFFER_COPY_ON_WRITE 0x04
81
82 /**
83 * If this flag is enabled, the buffer will copy its contents to a new memory area on reallocation.
84 *
85 * After performing the copy, the flag is automatically cleared.
86 * This flag has no effect on buffers which do not have #CX_BUFFER_AUTO_EXTEND set, which is why
87 * buffers automatically admit the auto-extend flag when initialized with copy-on-extend enabled.
88 */
89 #define CX_BUFFER_COPY_ON_EXTEND 0x08
70 90
71 /** Structure for the UCX buffer data. */ 91 /** Structure for the UCX buffer data. */
72 typedef struct { 92 typedef struct {
73 /** A pointer to the buffer contents. */ 93 /** A pointer to the buffer contents. */
74 union { 94 union {
80 * Data is interpreted as binary. 100 * Data is interpreted as binary.
81 */ 101 */
82 unsigned char *bytes; 102 unsigned char *bytes;
83 }; 103 };
84 /** The allocator to use for automatic memory management. */ 104 /** The allocator to use for automatic memory management. */
85 CxAllocator const *allocator; 105 const CxAllocator *allocator;
86 /** Current position of the buffer. */ 106 /** Current position of the buffer. */
87 size_t pos; 107 size_t pos;
88 /** Current capacity (i.e. maximum size) of the buffer. */ 108 /** Current capacity (i.e. maximum size) of the buffer. */
89 size_t capacity; 109 size_t capacity;
90 /** Current size of the buffer content. */ 110 /** Current size of the buffer content. */
126 /** 146 /**
127 * Flag register for buffer features. 147 * Flag register for buffer features.
128 * @see #CX_BUFFER_DEFAULT 148 * @see #CX_BUFFER_DEFAULT
129 * @see #CX_BUFFER_FREE_CONTENTS 149 * @see #CX_BUFFER_FREE_CONTENTS
130 * @see #CX_BUFFER_AUTO_EXTEND 150 * @see #CX_BUFFER_AUTO_EXTEND
151 * @see #CX_BUFFER_COPY_ON_WRITE
131 */ 152 */
132 int flags; 153 int flags;
133 } cx_buffer_s; 154 } cx_buffer_s;
134 155
135 /** 156 /**
138 typedef cx_buffer_s CxBuffer; 159 typedef cx_buffer_s CxBuffer;
139 160
140 /** 161 /**
141 * Initializes a fresh buffer. 162 * Initializes a fresh buffer.
142 * 163 *
164 * You may also provide a read-only \p space, in which case
165 * you will need to cast the pointer, and you should set the
166 * #CX_BUFFER_COPY_ON_WRITE flag.
167 *
168 * You need to set the size manually after initialization, if
169 * you provide \p space which already contains data.
170 *
171 * When you specify stack memory as \p space and decide to use
172 * the auto-extension feature, you \em must use the
173 * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the
174 * #CX_BUFFER_AUTO_EXTEND flag.
175 *
143 * \note You may provide \c NULL as argument for \p space. 176 * \note You may provide \c NULL as argument for \p space.
144 * Then this function will allocate the space and enforce 177 * Then this function will allocate the space and enforce
145 * the #CX_BUFFER_FREE_CONTENTS flag. 178 * the #CX_BUFFER_FREE_CONTENTS flag. In that case, specifying
179 * copy-on-write should be avoided, because the allocated
180 * space will be leaking after the copy-on-write operation.
146 * 181 *
147 * @param buffer the buffer to initialize 182 * @param buffer the buffer to initialize
148 * @param space pointer to the memory area, or \c NULL to allocate 183 * @param space pointer to the memory area, or \c NULL to allocate
149 * new memory 184 * new memory
150 * @param capacity the capacity of the buffer 185 * @param capacity the capacity of the buffer
151 * @param allocator the allocator this buffer shall use for automatic 186 * @param allocator the allocator this buffer shall use for automatic
152 * memory management. If \c NULL, the default heap allocator will be used. 187 * memory management
188 * (if \c NULL, a default stdlib allocator will be used)
153 * @param flags buffer features (see cx_buffer_s.flags) 189 * @param flags buffer features (see cx_buffer_s.flags)
154 * @return zero on success, non-zero if a required allocation failed 190 * @return zero on success, non-zero if a required allocation failed
155 */ 191 */
156 __attribute__((__nonnull__(1))) 192 cx_attr_nonnull_arg(1)
157 int cxBufferInit( 193 int cxBufferInit(
158 CxBuffer *buffer, 194 CxBuffer *buffer,
159 void *space, 195 void *space,
160 size_t capacity, 196 size_t capacity,
161 CxAllocator const *allocator, 197 const CxAllocator *allocator,
162 int flags 198 int flags
163 ); 199 );
164 200
165 /** 201 /**
202 * Destroys the buffer contents.
203 *
204 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled.
205 * If you want to free the memory of the entire buffer, use cxBufferFree().
206 *
207 * @param buffer the buffer which contents shall be destroyed
208 * @see cxBufferInit()
209 */
210 cx_attr_nonnull
211 void cxBufferDestroy(CxBuffer *buffer);
212
213 /**
214 * Deallocates the buffer.
215 *
216 * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys
217 * the contents. If you \em only want to destroy the contents, use cxBufferDestroy().
218 *
219 * \remark As with all free() functions, this accepts \c NULL arguments in which
220 * case it does nothing.
221 *
222 * @param buffer the buffer to deallocate
223 * @see cxBufferCreate()
224 */
225 void cxBufferFree(CxBuffer *buffer);
226
227 /**
166 * Allocates and initializes a fresh buffer. 228 * Allocates and initializes a fresh buffer.
229 *
230 * You may also provide a read-only \p space, in which case
231 * you will need to cast the pointer, and you should set the
232 * #CX_BUFFER_COPY_ON_WRITE flag.
233 * When you specify stack memory as \p space and decide to use
234 * the auto-extension feature, you \em must use the
235 * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the
236 * #CX_BUFFER_AUTO_EXTEND flag.
167 * 237 *
168 * \note You may provide \c NULL as argument for \p space. 238 * \note You may provide \c NULL as argument for \p space.
169 * Then this function will allocate the space and enforce 239 * Then this function will allocate the space and enforce
170 * the #CX_BUFFER_FREE_CONTENTS flag. 240 * the #CX_BUFFER_FREE_CONTENTS flag.
171 * 241 *
172 * @param space pointer to the memory area, or \c NULL to allocate 242 * @param space pointer to the memory area, or \c NULL to allocate
173 * new memory 243 * new memory
174 * @param capacity the capacity of the buffer 244 * @param capacity the capacity of the buffer
175 * @param allocator the allocator to use for allocating the structure and the automatic 245 * @param allocator the allocator to use for allocating the structure and the automatic
176 * memory management within the buffer. If \c NULL, the default heap allocator will be used. 246 * memory management within the buffer
247 * (if \c NULL, a default stdlib allocator will be used)
177 * @param flags buffer features (see cx_buffer_s.flags) 248 * @param flags buffer features (see cx_buffer_s.flags)
178 * @return a pointer to the buffer on success, \c NULL if a required allocation failed 249 * @return a pointer to the buffer on success, \c NULL if a required allocation failed
179 */ 250 */
251 cx_attr_malloc
252 cx_attr_dealloc(cxBufferFree, 1)
253 cx_attr_nodiscard
180 CxBuffer *cxBufferCreate( 254 CxBuffer *cxBufferCreate(
181 void *space, 255 void *space,
182 size_t capacity, 256 size_t capacity,
183 CxAllocator const *allocator, 257 const CxAllocator *allocator,
184 int flags 258 int flags
185 ); 259 );
186
187 /**
188 * Destroys the buffer contents.
189 *
190 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled.
191 * If you want to free the memory of the entire buffer, use cxBufferFree().
192 *
193 * @param buffer the buffer which contents shall be destroyed
194 * @see cxBufferInit()
195 */
196 __attribute__((__nonnull__))
197 void cxBufferDestroy(CxBuffer *buffer);
198
199 /**
200 * Deallocates the buffer.
201 *
202 * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys
203 * the contents. If you \em only want to destroy the contents, use cxBufferDestroy().
204 *
205 * @param buffer the buffer to deallocate
206 * @see cxBufferCreate()
207 */
208 __attribute__((__nonnull__))
209 void cxBufferFree(CxBuffer *buffer);
210 260
211 /** 261 /**
212 * Shifts the contents of the buffer by the given offset. 262 * Shifts the contents of the buffer by the given offset.
213 * 263 *
214 * If the offset is positive, the contents are shifted to the right. 264 * If the offset is positive, the contents are shifted to the right.
237 * <code>memset(buffer->bytes + buffer->size, 0, buffer->capacity - buffer->size)</code> 287 * <code>memset(buffer->bytes + buffer->size, 0, buffer->capacity - buffer->size)</code>
238 * for a left shift. 288 * for a left shift.
239 * 289 *
240 * @param buffer the buffer 290 * @param buffer the buffer
241 * @param shift the shift offset (negative means left shift) 291 * @param shift the shift offset (negative means left shift)
242 * @return 0 on success, non-zero if a required auto-extension fails 292 * @return 0 on success, non-zero if a required auto-extension or copy-on-write fails
243 */ 293 */
244 __attribute__((__nonnull__)) 294 cx_attr_nonnull
245 int cxBufferShift( 295 int cxBufferShift(
246 CxBuffer *buffer, 296 CxBuffer *buffer,
247 off_t shift 297 off_t shift
248 ); 298 );
249 299
251 * Shifts the buffer to the right. 301 * Shifts the buffer to the right.
252 * See cxBufferShift() for details. 302 * See cxBufferShift() for details.
253 * 303 *
254 * @param buffer the buffer 304 * @param buffer the buffer
255 * @param shift the shift offset 305 * @param shift the shift offset
256 * @return 0 on success, non-zero if a required auto-extension fails 306 * @return 0 on success, non-zero if a required auto-extension or copy-on-write fails
257 * @see cxBufferShift() 307 * @see cxBufferShift()
258 */ 308 */
259 __attribute__((__nonnull__)) 309 cx_attr_nonnull
260 int cxBufferShiftRight( 310 int cxBufferShiftRight(
261 CxBuffer *buffer, 311 CxBuffer *buffer,
262 size_t shift 312 size_t shift
263 ); 313 );
264 314
265 /** 315 /**
266 * Shifts the buffer to the left. 316 * Shifts the buffer to the left.
267 * See cxBufferShift() for details. 317 * See cxBufferShift() for details.
268 * 318 *
269 * \note Since a left shift cannot fail due to memory allocation problems, this
270 * function always returns zero.
271 *
272 * @param buffer the buffer 319 * @param buffer the buffer
273 * @param shift the positive shift offset 320 * @param shift the positive shift offset
274 * @return always zero 321 * @return usually zero, except the buffer uses copy-on-write and the allocation fails
275 * @see cxBufferShift() 322 * @see cxBufferShift()
276 */ 323 */
277 __attribute__((__nonnull__)) 324 cx_attr_nonnull
278 int cxBufferShiftLeft( 325 int cxBufferShiftLeft(
279 CxBuffer *buffer, 326 CxBuffer *buffer,
280 size_t shift 327 size_t shift
281 ); 328 );
282 329
298 * @param offset position offset relative to \p whence 345 * @param offset position offset relative to \p whence
299 * @param whence one of \c SEEK_SET, \c SEEK_CUR or \c SEEK_END 346 * @param whence one of \c SEEK_SET, \c SEEK_CUR or \c SEEK_END
300 * @return 0 on success, non-zero if the position is invalid 347 * @return 0 on success, non-zero if the position is invalid
301 * 348 *
302 */ 349 */
303 __attribute__((__nonnull__)) 350 cx_attr_nonnull
304 int cxBufferSeek( 351 int cxBufferSeek(
305 CxBuffer *buffer, 352 CxBuffer *buffer,
306 off_t offset, 353 off_t offset,
307 int whence 354 int whence
308 ); 355 );
311 * Clears the buffer by resetting the position and deleting the data. 358 * Clears the buffer by resetting the position and deleting the data.
312 * 359 *
313 * The data is deleted by zeroing it with a call to memset(). 360 * The data is deleted by zeroing it with a call to memset().
314 * If you do not need that, you can use the faster cxBufferReset(). 361 * If you do not need that, you can use the faster cxBufferReset().
315 * 362 *
363 * \note If the #CX_BUFFER_COPY_ON_WRITE flag is set, this function
364 * will not erase the data and behave exactly as cxBufferReset().
365 *
316 * @param buffer the buffer to be cleared 366 * @param buffer the buffer to be cleared
317 * @see cxBufferReset() 367 * @see cxBufferReset()
318 */ 368 */
319 __attribute__((__nonnull__)) 369 cx_attr_nonnull
320 void cxBufferClear(CxBuffer *buffer); 370 void cxBufferClear(CxBuffer *buffer);
321 371
322 /** 372 /**
323 * Resets the buffer by resetting the position and size to zero. 373 * Resets the buffer by resetting the position and size to zero.
324 * 374 *
326 * reset of the buffer, use cxBufferClear(). 376 * reset of the buffer, use cxBufferClear().
327 * 377 *
328 * @param buffer the buffer to be cleared 378 * @param buffer the buffer to be cleared
329 * @see cxBufferClear() 379 * @see cxBufferClear()
330 */ 380 */
331 __attribute__((__nonnull__)) 381 cx_attr_nonnull
332 void cxBufferReset(CxBuffer *buffer); 382 void cxBufferReset(CxBuffer *buffer);
333 383
334 /** 384 /**
335 * Tests, if the buffer position has exceeded the buffer size. 385 * Tests, if the buffer position has exceeded the buffer size.
336 * 386 *
337 * @param buffer the buffer to test 387 * @param buffer the buffer to test
338 * @return non-zero, if the current buffer position has exceeded the last 388 * @return true, if the current buffer position has exceeded the last
339 * byte of the buffer's contents. 389 * byte of the buffer's contents.
340 */ 390 */
341 __attribute__((__nonnull__)) 391 cx_attr_nonnull
342 int cxBufferEof(CxBuffer const *buffer); 392 cx_attr_nodiscard
393 bool cxBufferEof(const CxBuffer *buffer);
343 394
344 395
345 /** 396 /**
346 * Ensures that the buffer has a minimum capacity. 397 * Ensures that the buffer has a minimum capacity.
347 * 398 *
349 * 400 *
350 * @param buffer the buffer 401 * @param buffer the buffer
351 * @param capacity the minimum required capacity for this buffer 402 * @param capacity the minimum required capacity for this buffer
352 * @return 0 on success or a non-zero value on failure 403 * @return 0 on success or a non-zero value on failure
353 */ 404 */
354 __attribute__((__nonnull__)) 405 cx_attr_nonnull
355 int cxBufferMinimumCapacity( 406 int cxBufferMinimumCapacity(
356 CxBuffer *buffer, 407 CxBuffer *buffer,
357 size_t capacity 408 size_t capacity
358 ); 409 );
359 410
381 * @param size the length of one element 432 * @param size the length of one element
382 * @param nitems the element count 433 * @param nitems the element count
383 * @param buffer the CxBuffer to write to 434 * @param buffer the CxBuffer to write to
384 * @return the total count of elements written 435 * @return the total count of elements written
385 */ 436 */
386 __attribute__((__nonnull__)) 437 cx_attr_nonnull
387 size_t cxBufferWrite( 438 size_t cxBufferWrite(
388 void const *ptr, 439 const void *ptr,
440 size_t size,
441 size_t nitems,
442 CxBuffer *buffer
443 );
444
445 /**
446 * Appends data to a CxBuffer.
447 *
448 * The data is always appended to current data within the buffer,
449 * regardless of the current position.
450 * This is especially useful when the buffer is primarily meant for reading
451 * while additional data is added to the buffer occasionally.
452 * Consequently, the position of the buffer is unchanged after this operation.
453 *
454 * \note The signature is compatible with the fwrite() family of functions.
455 *
456 * @param ptr a pointer to the memory area containing the bytes to be written
457 * @param size the length of one element
458 * @param nitems the element count
459 * @param buffer the CxBuffer to write to
460 * @return the total count of elements written
461 * @see cxBufferWrite()
462 */
463 cx_attr_nonnull
464 size_t cxBufferAppend(
465 const void *ptr,
389 size_t size, 466 size_t size,
390 size_t nitems, 467 size_t nitems,
391 CxBuffer *buffer 468 CxBuffer *buffer
392 ); 469 );
393 470
402 * @param size the length of one element 479 * @param size the length of one element
403 * @param nitems the element count 480 * @param nitems the element count
404 * @param buffer the CxBuffer to read from 481 * @param buffer the CxBuffer to read from
405 * @return the total number of elements read 482 * @return the total number of elements read
406 */ 483 */
407 __attribute__((__nonnull__)) 484 cx_attr_nonnull
408 size_t cxBufferRead( 485 size_t cxBufferRead(
409 void *ptr, 486 void *ptr,
410 size_t size, 487 size_t size,
411 size_t nitems, 488 size_t nitems,
412 CxBuffer *buffer 489 CxBuffer *buffer
415 /** 492 /**
416 * Writes a character to a buffer. 493 * Writes a character to a buffer.
417 * 494 *
418 * The least significant byte of the argument is written to the buffer. If the 495 * The least significant byte of the argument is written to the buffer. If the
419 * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled, 496 * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled,
420 * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature is 497 * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature
421 * disabled or buffer extension fails, \c EOF is returned. 498 * is disabled or buffer extension fails, \c EOF is returned.
422 * 499 *
423 * On successful write, the position of the buffer is increased. 500 * On successful write, the position of the buffer is increased.
424 * 501 *
425 * @param buffer the buffer to write to 502 * @param buffer the buffer to write to
426 * @param c the character to write 503 * @param c the character to write
427 * @return the byte that has bean written or \c EOF when the end of the stream is 504 * @return the byte that has been written or \c EOF when the end of the stream is
428 * reached and automatic extension is not enabled or not possible 505 * reached and automatic extension is not enabled or not possible
429 */ 506 */
430 __attribute__((__nonnull__)) 507 cx_attr_nonnull
431 int cxBufferPut( 508 int cxBufferPut(
432 CxBuffer *buffer, 509 CxBuffer *buffer,
433 int c 510 int c
434 ); 511 );
512
513 /**
514 * Writes a terminating zero to a buffer.
515 *
516 * On successful write, \em neither the position \em nor the size of the buffer is
517 * increased.
518 *
519 * The purpose of this function is to have the written data ready to be used as
520 * a C string.
521 *
522 * @param buffer the buffer to write to
523 * @return zero, if the terminator could be written, non-zero otherwise
524 */
525 cx_attr_nonnull
526 int cxBufferTerminate(CxBuffer *buffer);
435 527
436 /** 528 /**
437 * Writes a string to a buffer. 529 * Writes a string to a buffer.
438 * 530 *
439 * @param buffer the buffer 531 * @param buffer the buffer
440 * @param str the zero-terminated string 532 * @param str the zero-terminated string
441 * @return the number of bytes written 533 * @return the number of bytes written
442 */ 534 */
443 __attribute__((__nonnull__)) 535 cx_attr_nonnull
536 cx_attr_cstr_arg(2)
444 size_t cxBufferPutString( 537 size_t cxBufferPutString(
445 CxBuffer *buffer, 538 CxBuffer *buffer,
446 const char *str 539 const char *str
447 ); 540 );
448 541
452 * The current position of the buffer is increased after a successful read. 545 * The current position of the buffer is increased after a successful read.
453 * 546 *
454 * @param buffer the buffer to read from 547 * @param buffer the buffer to read from
455 * @return the character or \c EOF, if the end of the buffer is reached 548 * @return the character or \c EOF, if the end of the buffer is reached
456 */ 549 */
457 __attribute__((__nonnull__)) 550 cx_attr_nonnull
458 int cxBufferGet(CxBuffer *buffer); 551 int cxBufferGet(CxBuffer *buffer);
459 552
460 #ifdef __cplusplus 553 #ifdef __cplusplus
461 } 554 }
462 #endif 555 #endif

mercurial