ucx/cx/buffer.h

changeset 471
063a9f29098c
parent 440
7c4b9cba09ca
equal deleted inserted replaced
470:118e2386d5b4 471:063a9f29098c
85 * After performing the copy, the flag is automatically cleared. 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 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. 87 * buffers automatically admit the auto-extend flag when initialized with copy-on-extend enabled.
88 */ 88 */
89 #define CX_BUFFER_COPY_ON_EXTEND 0x08 89 #define CX_BUFFER_COPY_ON_EXTEND 0x08
90
91 /**
92 * Function pointer for cxBufferWrite that is compatible with cx_write_func.
93 * @see cx_write_func
94 */
95 #define cxBufferWriteFunc ((cx_write_func) cxBufferWrite)
96 /**
97 * Function pointer for cxBufferRead that is compatible with cx_read_func.
98 * @see cx_read_func
99 */
100 #define cxBufferReadFunc ((cx_read_func) cxBufferRead)
90 101
91 /** 102 /**
92 * Configuration for automatic flushing. 103 * Configuration for automatic flushing.
93 */ 104 */
94 struct cx_buffer_flush_config_s { 105 struct cx_buffer_flush_config_s {
126 */ 137 */
127 cx_write_func wfunc; 138 cx_write_func wfunc;
128 }; 139 };
129 140
130 /** 141 /**
131 * Type alais for the flush configuration struct. 142 * Type alias for the flush configuration struct.
132 * 143 *
133 * @code 144 * @code
134 * struct cx_buffer_flush_config_s { 145 * struct cx_buffer_flush_config_s {
135 * size_t threshold; 146 * size_t threshold;
136 * size_t blksize; 147 * size_t blksize;
160 /** 171 /**
161 * Optional flush configuration 172 * Optional flush configuration
162 * 173 *
163 * @see cxBufferEnableFlushing() 174 * @see cxBufferEnableFlushing()
164 */ 175 */
165 CxBufferFlushConfig* flush; 176 CxBufferFlushConfig *flush;
166 /** Current position of the buffer. */ 177 /** Current position of the buffer. */
167 size_t pos; 178 size_t pos;
168 /** Current capacity (i.e. maximum size) of the buffer. */ 179 /** Current capacity (i.e. maximum size) of the buffer. */
169 size_t capacity; 180 size_t capacity;
170 /** Current size of the buffer content. */ 181 /** Current size of the buffer content. */
214 * (if @c NULL, a default stdlib allocator will be used) 225 * (if @c NULL, a default stdlib allocator will be used)
215 * @param flags buffer features (see cx_buffer_s.flags) 226 * @param flags buffer features (see cx_buffer_s.flags)
216 * @return zero on success, non-zero if a required allocation failed 227 * @return zero on success, non-zero if a required allocation failed
217 */ 228 */
218 cx_attr_nonnull_arg(1) 229 cx_attr_nonnull_arg(1)
230 cx_attr_export
219 int cxBufferInit( 231 int cxBufferInit(
220 CxBuffer *buffer, 232 CxBuffer *buffer,
221 void *space, 233 void *space,
222 size_t capacity, 234 size_t capacity,
223 const CxAllocator *allocator, 235 const CxAllocator *allocator,
237 * @retval non-zero failure 249 * @retval non-zero failure
238 * @see cxBufferFlush() 250 * @see cxBufferFlush()
239 * @see cxBufferWrite() 251 * @see cxBufferWrite()
240 */ 252 */
241 cx_attr_nonnull 253 cx_attr_nonnull
254 cx_attr_export
242 int cxBufferEnableFlushing( 255 int cxBufferEnableFlushing(
243 CxBuffer *buffer, 256 CxBuffer *buffer,
244 CxBufferFlushConfig config 257 CxBufferFlushConfig config
245 ); 258 );
246 259
252 * 265 *
253 * @param buffer the buffer which contents shall be destroyed 266 * @param buffer the buffer which contents shall be destroyed
254 * @see cxBufferInit() 267 * @see cxBufferInit()
255 */ 268 */
256 cx_attr_nonnull 269 cx_attr_nonnull
270 cx_attr_export
257 void cxBufferDestroy(CxBuffer *buffer); 271 void cxBufferDestroy(CxBuffer *buffer);
258 272
259 /** 273 /**
260 * Deallocates the buffer. 274 * Deallocates the buffer.
261 * 275 *
266 * case it does nothing. 280 * case it does nothing.
267 * 281 *
268 * @param buffer the buffer to deallocate 282 * @param buffer the buffer to deallocate
269 * @see cxBufferCreate() 283 * @see cxBufferCreate()
270 */ 284 */
285 cx_attr_export
271 void cxBufferFree(CxBuffer *buffer); 286 void cxBufferFree(CxBuffer *buffer);
272 287
273 /** 288 /**
274 * Allocates and initializes a fresh buffer. 289 * Allocates and initializes a fresh buffer.
275 * 290 *
295 * @return a pointer to the buffer on success, @c NULL if a required allocation failed 310 * @return a pointer to the buffer on success, @c NULL if a required allocation failed
296 */ 311 */
297 cx_attr_malloc 312 cx_attr_malloc
298 cx_attr_dealloc(cxBufferFree, 1) 313 cx_attr_dealloc(cxBufferFree, 1)
299 cx_attr_nodiscard 314 cx_attr_nodiscard
315 cx_attr_export
300 CxBuffer *cxBufferCreate( 316 CxBuffer *cxBufferCreate(
301 void *space, 317 void *space,
302 size_t capacity, 318 size_t capacity,
303 const CxAllocator *allocator, 319 const CxAllocator *allocator,
304 int flags 320 int flags
339 * @retval non-zero if a required auto-extension or copy-on-write fails 355 * @retval non-zero if a required auto-extension or copy-on-write fails
340 * @see cxBufferShiftLeft() 356 * @see cxBufferShiftLeft()
341 * @see cxBufferShiftRight() 357 * @see cxBufferShiftRight()
342 */ 358 */
343 cx_attr_nonnull 359 cx_attr_nonnull
360 cx_attr_export
344 int cxBufferShift( 361 int cxBufferShift(
345 CxBuffer *buffer, 362 CxBuffer *buffer,
346 off_t shift 363 off_t shift
347 ); 364 );
348 365
355 * @retval zero success 372 * @retval zero success
356 * @retval non-zero if a required auto-extension or copy-on-write fails 373 * @retval non-zero if a required auto-extension or copy-on-write fails
357 * @see cxBufferShift() 374 * @see cxBufferShift()
358 */ 375 */
359 cx_attr_nonnull 376 cx_attr_nonnull
377 cx_attr_export
360 int cxBufferShiftRight( 378 int cxBufferShiftRight(
361 CxBuffer *buffer, 379 CxBuffer *buffer,
362 size_t shift 380 size_t shift
363 ); 381 );
364 382
371 * @retval zero success 389 * @retval zero success
372 * @retval non-zero if the buffer uses copy-on-write and the allocation fails 390 * @retval non-zero if the buffer uses copy-on-write and the allocation fails
373 * @see cxBufferShift() 391 * @see cxBufferShift()
374 */ 392 */
375 cx_attr_nonnull 393 cx_attr_nonnull
394 cx_attr_export
376 int cxBufferShiftLeft( 395 int cxBufferShiftLeft(
377 CxBuffer *buffer, 396 CxBuffer *buffer,
378 size_t shift 397 size_t shift
379 ); 398 );
380 399
398 * @retval zero success 417 * @retval zero success
399 * @retval non-zero if the position is invalid 418 * @retval non-zero if the position is invalid
400 * 419 *
401 */ 420 */
402 cx_attr_nonnull 421 cx_attr_nonnull
422 cx_attr_export
403 int cxBufferSeek( 423 int cxBufferSeek(
404 CxBuffer *buffer, 424 CxBuffer *buffer,
405 off_t offset, 425 off_t offset,
406 int whence 426 int whence
407 ); 427 );
417 * 437 *
418 * @param buffer the buffer to be cleared 438 * @param buffer the buffer to be cleared
419 * @see cxBufferReset() 439 * @see cxBufferReset()
420 */ 440 */
421 cx_attr_nonnull 441 cx_attr_nonnull
442 cx_attr_export
422 void cxBufferClear(CxBuffer *buffer); 443 void cxBufferClear(CxBuffer *buffer);
423 444
424 /** 445 /**
425 * Resets the buffer by resetting the position and size to zero. 446 * Resets the buffer by resetting the position and size to zero.
426 * 447 *
429 * 450 *
430 * @param buffer the buffer to be cleared 451 * @param buffer the buffer to be cleared
431 * @see cxBufferClear() 452 * @see cxBufferClear()
432 */ 453 */
433 cx_attr_nonnull 454 cx_attr_nonnull
455 cx_attr_export
434 void cxBufferReset(CxBuffer *buffer); 456 void cxBufferReset(CxBuffer *buffer);
435 457
436 /** 458 /**
437 * Tests, if the buffer position has exceeded the buffer size. 459 * Tests, if the buffer position has exceeded the buffer size.
438 * 460 *
441 * byte of the buffer's contents 463 * byte of the buffer's contents
442 * @retval false otherwise 464 * @retval false otherwise
443 */ 465 */
444 cx_attr_nonnull 466 cx_attr_nonnull
445 cx_attr_nodiscard 467 cx_attr_nodiscard
468 cx_attr_export
446 bool cxBufferEof(const CxBuffer *buffer); 469 bool cxBufferEof(const CxBuffer *buffer);
447 470
448 471
449 /** 472 /**
450 * Ensures that the buffer has a minimum capacity. 473 * Ensures that the buffer has a minimum capacity.
455 * @param capacity the minimum required capacity for this buffer 478 * @param capacity the minimum required capacity for this buffer
456 * @retval zero the capacity was already sufficient or successfully increased 479 * @retval zero the capacity was already sufficient or successfully increased
457 * @retval non-zero on allocation failure 480 * @retval non-zero on allocation failure
458 */ 481 */
459 cx_attr_nonnull 482 cx_attr_nonnull
483 cx_attr_export
460 int cxBufferMinimumCapacity( 484 int cxBufferMinimumCapacity(
461 CxBuffer *buffer, 485 CxBuffer *buffer,
462 size_t capacity 486 size_t capacity
463 ); 487 );
464 488
471 * 495 *
472 * If flushing is enabled and the buffer needs to flush, the data is flushed to 496 * If flushing is enabled and the buffer needs to flush, the data is flushed to
473 * the target until the target signals that it cannot take more data by 497 * the target until the target signals that it cannot take more data by
474 * returning zero via the respective write function. In that case, the remaining 498 * returning zero via the respective write function. In that case, the remaining
475 * data in this buffer is shifted to the beginning of this buffer so that the 499 * data in this buffer is shifted to the beginning of this buffer so that the
476 * newly available space can be used to append as much data as possible. This 500 * newly available space can be used to append as much data as possible.
477 * function only stops writing more elements, when the flush target and this 501 *
502 * This function only stops writing more elements, when the flush target and this
478 * buffer are both incapable of taking more data or all data has been written. 503 * buffer are both incapable of taking more data or all data has been written.
479 * If number of items that shall be written is larger than the buffer can hold, 504 *
480 * the first items from @c ptr are directly relayed to the flush target, if 505 * If, after flushing, the number of items that shall be written still exceeds
481 * possible. 506 * the capacity or flush threshold, this function tries to write all items directly
482 * The number returned by this function is only the number of elements from 507 * to the flush target, if possible.
483 * @c ptr that could be written to either the flush target or the buffer. 508 *
509 * The number returned by this function is the number of elements from
510 * @c ptr that could be written to either the flush target or the buffer
511 * (so it does not include the number of items that had been already in the buffer
512 * in were flushed during the process).
513 *
514 * @attention
515 * When @p size is larger than one and the contents of the buffer are not aligned
516 * with @p size, flushing stops after all complete items have been flushed, leaving
517 * the mis-aligned part in the buffer.
518 * Afterward, this function only writes as many items as possible to the buffer.
484 * 519 *
485 * @note The signature is compatible with the fwrite() family of functions. 520 * @note The signature is compatible with the fwrite() family of functions.
486 * 521 *
487 * @param ptr a pointer to the memory area containing the bytes to be written 522 * @param ptr a pointer to the memory area containing the bytes to be written
488 * @param size the length of one element 523 * @param size the length of one element
491 * @return the total count of elements written 526 * @return the total count of elements written
492 * @see cxBufferAppend() 527 * @see cxBufferAppend()
493 * @see cxBufferRead() 528 * @see cxBufferRead()
494 */ 529 */
495 cx_attr_nonnull 530 cx_attr_nonnull
531 cx_attr_export
496 size_t cxBufferWrite( 532 size_t cxBufferWrite(
497 const void *ptr, 533 const void *ptr,
498 size_t size, 534 size_t size,
499 size_t nitems, 535 size_t nitems,
500 CxBuffer *buffer 536 CxBuffer *buffer
518 * @return the total count of elements written 554 * @return the total count of elements written
519 * @see cxBufferWrite() 555 * @see cxBufferWrite()
520 * @see cxBufferRead() 556 * @see cxBufferRead()
521 */ 557 */
522 cx_attr_nonnull 558 cx_attr_nonnull
559 cx_attr_export
523 size_t cxBufferAppend( 560 size_t cxBufferAppend(
524 const void *ptr, 561 const void *ptr,
525 size_t size, 562 size_t size,
526 size_t nitems, 563 size_t nitems,
527 CxBuffer *buffer 564 CxBuffer *buffer
579 * @param buffer the buffer 616 * @param buffer the buffer
580 * @return the number of successfully flushed bytes 617 * @return the number of successfully flushed bytes
581 * @see cxBufferEnableFlushing() 618 * @see cxBufferEnableFlushing()
582 */ 619 */
583 cx_attr_nonnull 620 cx_attr_nonnull
621 cx_attr_export
584 size_t cxBufferFlush(CxBuffer *buffer); 622 size_t cxBufferFlush(CxBuffer *buffer);
585 623
586 /** 624 /**
587 * Reads data from a CxBuffer. 625 * Reads data from a CxBuffer.
588 * 626 *
597 * @return the total number of elements read 635 * @return the total number of elements read
598 * @see cxBufferWrite() 636 * @see cxBufferWrite()
599 * @see cxBufferAppend() 637 * @see cxBufferAppend()
600 */ 638 */
601 cx_attr_nonnull 639 cx_attr_nonnull
640 cx_attr_export
602 size_t cxBufferRead( 641 size_t cxBufferRead(
603 void *ptr, 642 void *ptr,
604 size_t size, 643 size_t size,
605 size_t nitems, 644 size_t nitems,
606 CxBuffer *buffer 645 CxBuffer *buffer
624 * @return the byte that has been written or @c EOF when the end of the stream is 663 * @return the byte that has been written or @c EOF when the end of the stream is
625 * reached and automatic extension is not enabled or not possible 664 * reached and automatic extension is not enabled or not possible
626 * @see cxBufferTerminate() 665 * @see cxBufferTerminate()
627 */ 666 */
628 cx_attr_nonnull 667 cx_attr_nonnull
668 cx_attr_export
629 int cxBufferPut( 669 int cxBufferPut(
630 CxBuffer *buffer, 670 CxBuffer *buffer,
631 int c 671 int c
632 ); 672 );
633 673
642 * 682 *
643 * @param buffer the buffer to write to 683 * @param buffer the buffer to write to
644 * @return zero, if the terminator could be written, non-zero otherwise 684 * @return zero, if the terminator could be written, non-zero otherwise
645 */ 685 */
646 cx_attr_nonnull 686 cx_attr_nonnull
687 cx_attr_export
647 int cxBufferTerminate(CxBuffer *buffer); 688 int cxBufferTerminate(CxBuffer *buffer);
648 689
649 /** 690 /**
650 * Writes a string to a buffer. 691 * Writes a string to a buffer.
651 * 692 *
655 * @param str the zero-terminated string 696 * @param str the zero-terminated string
656 * @return the number of bytes written 697 * @return the number of bytes written
657 */ 698 */
658 cx_attr_nonnull 699 cx_attr_nonnull
659 cx_attr_cstr_arg(2) 700 cx_attr_cstr_arg(2)
701 cx_attr_export
660 size_t cxBufferPutString( 702 size_t cxBufferPutString(
661 CxBuffer *buffer, 703 CxBuffer *buffer,
662 const char *str 704 const char *str
663 ); 705 );
664 706
669 * 711 *
670 * @param buffer the buffer to read from 712 * @param buffer the buffer to read from
671 * @return the character or @c EOF, if the end of the buffer is reached 713 * @return the character or @c EOF, if the end of the buffer is reached
672 */ 714 */
673 cx_attr_nonnull 715 cx_attr_nonnull
716 cx_attr_export
674 int cxBufferGet(CxBuffer *buffer); 717 int cxBufferGet(CxBuffer *buffer);
675 718
676 #ifdef __cplusplus 719 #ifdef __cplusplus
677 } 720 }
678 #endif 721 #endif

mercurial