| 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 * |
63 * |
| 64 * Do NOT set this flag together with #CX_BUFFER_COPY_ON_WRITE. It will be automatically |
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. |
65 * set when the copy-on-write operation is performed. |
| 66 */ |
66 */ |
| 67 #define CX_BUFFER_FREE_CONTENTS 0x01 |
67 #define CX_BUFFER_FREE_CONTENTS 0x01 |
| 68 |
68 |
| 69 /** |
69 /** |
| 70 * If this flag is enabled, the buffer will automatically extend its capacity. |
70 * If this flag is enabled, the buffer will automatically extend its capacity. |
| 200 * |
200 * |
| 201 * You may also provide a read-only @p space, in which case |
201 * You may also provide a read-only @p space, in which case |
| 202 * you will need to cast the pointer, and you should set the |
202 * you will need to cast the pointer, and you should set the |
| 203 * #CX_BUFFER_COPY_ON_WRITE flag. |
203 * #CX_BUFFER_COPY_ON_WRITE flag. |
| 204 * |
204 * |
| 205 * You need to set the size manually after initialization, if |
205 * You need to set the size manually after initialization if |
| 206 * you provide @p space which already contains data. |
206 * you provide @p space which already contains data. |
| 207 * |
207 * |
| 208 * When you specify stack memory as @p space and decide to use |
208 * When you specify stack memory as @p space and decide to use |
| 209 * the auto-extension feature, you @em must use the |
209 * the auto-extension feature, you @em must use the |
| 210 * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the |
210 * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the |
| 211 * #CX_BUFFER_AUTO_EXTEND flag. |
211 * #CX_BUFFER_AUTO_EXTEND flag. |
| 212 * |
212 * |
| 213 * @note You may provide @c NULL as argument for @p space. |
213 * @note You may provide @c NULL as the argument for @p space. |
| 214 * Then this function will allocate the space and enforce |
214 * Then this function will allocate the space and enforce |
| 215 * the #CX_BUFFER_FREE_CONTENTS flag. In that case, specifying |
215 * the #CX_BUFFER_FREE_CONTENTS flag. In that case, specifying |
| 216 * copy-on-write should be avoided, because the allocated |
216 * copy-on-write should be avoided, because the allocated |
| 217 * space will be leaking after the copy-on-write operation. |
217 * space will be leaking after the copy-on-write operation. |
| 218 * |
218 * |
| 274 * Deallocates the buffer. |
274 * Deallocates the buffer. |
| 275 * |
275 * |
| 276 * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys |
276 * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys |
| 277 * the contents. If you @em only want to destroy the contents, use cxBufferDestroy(). |
277 * the contents. If you @em only want to destroy the contents, use cxBufferDestroy(). |
| 278 * |
278 * |
| 279 * @remark As with all free() functions, this accepts @c NULL arguments in which |
|
| 280 * case it does nothing. |
|
| 281 * |
|
| 282 * @param buffer the buffer to deallocate |
279 * @param buffer the buffer to deallocate |
| 283 * @see cxBufferCreate() |
280 * @see cxBufferCreate() |
| 284 */ |
281 */ |
| 285 cx_attr_export |
282 cx_attr_export |
| 286 void cxBufferFree(CxBuffer *buffer); |
283 void cxBufferFree(CxBuffer *buffer); |
| 294 * When you specify stack memory as @p space and decide to use |
291 * When you specify stack memory as @p space and decide to use |
| 295 * the auto-extension feature, you @em must use the |
292 * the auto-extension feature, you @em must use the |
| 296 * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the |
293 * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the |
| 297 * #CX_BUFFER_AUTO_EXTEND flag. |
294 * #CX_BUFFER_AUTO_EXTEND flag. |
| 298 * |
295 * |
| 299 * @note You may provide @c NULL as argument for @p space. |
296 * @note You may provide @c NULL as the argument for @p space. |
| 300 * Then this function will allocate the space and enforce |
297 * Then this function will allocate the space and enforce |
| 301 * the #CX_BUFFER_FREE_CONTENTS flag. |
298 * the #CX_BUFFER_FREE_CONTENTS flag. |
| 302 * |
299 * |
| 303 * @param space pointer to the memory area, or @c NULL to allocate |
300 * @param space pointer to the memory area, or @c NULL to allocate |
| 304 * new memory |
301 * new memory |
| 325 * |
322 * |
| 326 * If the offset is positive, the contents are shifted to the right. |
323 * If the offset is positive, the contents are shifted to the right. |
| 327 * If auto extension is enabled, the buffer grows, if necessary. |
324 * If auto extension is enabled, the buffer grows, if necessary. |
| 328 * In case the auto extension fails, this function returns a non-zero value and |
325 * In case the auto extension fails, this function returns a non-zero value and |
| 329 * no contents are changed. |
326 * no contents are changed. |
| 330 * If auto extension is disabled, the contents that do not fit into the buffer |
327 * When the auto extension is disabled, the contents that do not fit into the |
| 331 * are discarded. |
328 * buffer are discarded. |
| 332 * |
329 * |
| 333 * If the offset is negative, the contents are shifted to the left where the |
330 * If the offset is negative, the contents are shifted to the left where the |
| 334 * first @p shift bytes are discarded. |
331 * first @p shift bytes are discarded. |
| 335 * The new size of the buffer is the old size minus the absolute shift value. |
332 * The new size of the buffer is the old size minus the absolute shift value. |
| 336 * If this value is larger than the buffer size, the buffer is emptied (but |
333 * If this value is larger than the buffer size, the buffer is emptied (but |
| 337 * not cleared, see the security note below). |
334 * not cleared, see the security note below). |
| 338 * |
335 * |
| 339 * The buffer position gets shifted alongside with the content but is kept |
336 * The buffer position gets shifted alongside the content but is kept |
| 340 * within the boundaries of the buffer. |
337 * within the boundaries of the buffer. |
| 341 * |
338 * |
| 342 * @note For situations where @c off_t is not large enough, there are specialized cxBufferShiftLeft() and |
339 * @note For situations where @c off_t is not large enough, there are specialized cxBufferShiftLeft() and |
| 343 * cxBufferShiftRight() functions using a @c size_t as parameter type. |
340 * cxBufferShiftRight() functions using a @c size_t as the parameter type. |
| 344 * |
341 * |
| 345 * @attention |
342 * @attention |
| 346 * Security Note: The shifting operation does @em not erase the previously occupied memory cells. |
343 * Security Note: The shifting operation does @em not erase the previously occupied memory cells. |
| 347 * But you can easily do that manually, e.g. by calling |
344 * But you can do that manually by calling |
| 348 * <code>memset(buffer->bytes, 0, shift)</code> for a right shift or |
345 * <code>memset(buffer->bytes, 0, shift)</code> for a right shift or |
| 349 * <code>memset(buffer->bytes + buffer->size, 0, buffer->capacity - buffer->size)</code> |
346 * <code>memset(buffer->bytes + buffer->size, 0, buffer->capacity - buffer->size)</code> |
| 350 * for a left shift. |
347 * for a left shift. |
| 351 * |
348 * |
| 352 * @param buffer the buffer |
349 * @param buffer the buffer |
| 515 |
512 |
| 516 /** |
513 /** |
| 517 * Writes data to a CxBuffer. |
514 * Writes data to a CxBuffer. |
| 518 * |
515 * |
| 519 * If automatic flushing is not enabled, the data is simply written into the |
516 * If automatic flushing is not enabled, the data is simply written into the |
| 520 * buffer at the current position and the position of the buffer is increased |
517 * buffer at the current position, and the position of the buffer is increased |
| 521 * by the number of bytes written. |
518 * by the number of bytes written. |
| 522 * |
519 * |
| 523 * If flushing is enabled and the buffer needs to flush, the data is flushed to |
520 * If flushing is enabled and the buffer needs to flush, the data is flushed to |
| 524 * the target until the target signals that it cannot take more data by |
521 * the target until the target signals that it cannot take more data by |
| 525 * returning zero via the respective write function. In that case, the remaining |
522 * returning zero via the respective write function. In that case, the remaining |
| 526 * data in this buffer is shifted to the beginning of this buffer so that the |
523 * data in this buffer is shifted to the beginning of this buffer so that the |
| 527 * newly available space can be used to append as much data as possible. |
524 * newly available space can be used to append as much data as possible. |
| 528 * |
525 * |
| 529 * This function only stops writing more elements, when the flush target and this |
526 * This function only stops writing more elements when the flush target and this |
| 530 * buffer are both incapable of taking more data or all data has been written. |
527 * buffer are both incapable of taking more data or all data has been written. |
| 531 * |
528 * |
| 532 * If, after flushing, the number of items that shall be written still exceeds |
529 * If, after flushing, the number of items that shall be written still exceeds |
| 533 * the capacity or flush threshold, this function tries to write all items directly |
530 * the capacity or flush threshold, this function tries to write all items directly |
| 534 * to the flush target, if possible. |
531 * to the flush target, if possible. |
| 535 * |
532 * |
| 536 * The number returned by this function is the number of elements from |
533 * The number returned by this function is the number of elements from |
| 537 * @c ptr that could be written to either the flush target or the buffer |
534 * @c ptr that could be written to either the flush target or the buffer. |
| 538 * (so it does not include the number of items that had been already in the buffer |
535 * That means it does @em not include the number of items that were already in |
| 539 * in were flushed during the process). |
536 * the buffer and were also flushed during the process. |
| 540 * |
537 * |
| 541 * @attention |
538 * @attention |
| 542 * When @p size is larger than one and the contents of the buffer are not aligned |
539 * When @p size is larger than one and the contents of the buffer are not aligned |
| 543 * with @p size, flushing stops after all complete items have been flushed, leaving |
540 * with @p size, flushing stops after all complete items have been flushed, leaving |
| 544 * the mis-aligned part in the buffer. |
541 * the misaligned part in the buffer. |
| 545 * Afterward, this function only writes as many items as possible to the buffer. |
542 * Afterward, this function only writes as many items as possible to the buffer. |
| 546 * |
543 * |
| 547 * @note The signature is compatible with the fwrite() family of functions. |
544 * @note The signature is compatible with the fwrite() family of functions. |
| 548 * |
545 * |
| 549 * @param ptr a pointer to the memory area containing the bytes to be written |
546 * @param ptr a pointer to the memory area containing the bytes to be written |
| 612 * @par Example 1 |
609 * @par Example 1 |
| 613 * Assume you have a buffer with size 340 and you are |
610 * Assume you have a buffer with size 340 and you are |
| 614 * at position 200. The flush configuration is |
611 * at position 200. The flush configuration is |
| 615 * @c blkmax=4 and @c blksize=64 . |
612 * @c blkmax=4 and @c blksize=64 . |
| 616 * Assume that the entire flush operation is successful. |
613 * Assume that the entire flush operation is successful. |
| 617 * All 200 bytes on the left hand-side from the current |
614 * All 200 bytes on the left-hand-side from the current |
| 618 * position are written. |
615 * position are written. |
| 619 * That means, the size of the buffer is now 140 and the |
616 * That means the size of the buffer is now 140 and the |
| 620 * position is zero. |
617 * position is zero. |
| 621 * |
618 * |
| 622 * @par Example 2 |
619 * @par Example 2 |
| 623 * Same as Example 1, but now the @c blkmax is 1. |
620 * Same as Example 1, but now the @c blkmax is 1. |
| 624 * The size of the buffer is now 276 and the position is 136. |
621 * The size of the buffer is now 276, and the position is 136. |
| 625 * |
622 * |
| 626 * @par Example 3 |
623 * @par Example 3 |
| 627 * Same as Example 1, but now assume the flush target |
624 * Same as Example 1, but now assume the flush target |
| 628 * only accepts 100 bytes before returning zero. |
625 * only accepts 100 bytes before returning zero. |
| 629 * That means, the flush operations manages to flush |
626 * That means the flush operation manages to flush |
| 630 * one complete block and one partial block, ending |
627 * one complete block and one partial block, ending |
| 631 * up with a buffer with size 240 and position 100. |
628 * up with a buffer with size 240 and position 100. |
| 632 * |
629 * |
| 633 * @remark Just returns zero when flushing was not enabled with |
630 * @remark Just returns zero when flushing was not enabled with |
| 634 * cxBufferEnableFlushing(). |
631 * cxBufferEnableFlushing(). |
| 635 * |
632 * |
| 636 * @remark When the buffer uses copy-on-write, the memory |
633 * @remark When the buffer uses copy-on-write, the memory |
| 637 * is copied first, before attempting any flush. |
634 * is copied first, before attempting any flush. |
| 638 * This is, however, considered an erroneous use of the |
635 * This is, however, considered an erroneous use of the |
| 639 * buffer, because it does not make much sense to put |
636 * buffer because it makes little sense to put |
| 640 * readonly data into an UCX buffer for flushing, instead |
637 * readonly data into an UCX buffer for flushing instead |
| 641 * of writing it directly to the target. |
638 * of writing it directly to the target. |
| 642 * |
639 * |
| 643 * @param buffer the buffer |
640 * @param buffer the buffer |
| 644 * @return the number of successfully flushed bytes |
641 * @return the number of successfully flushed bytes |
| 645 * @see cxBufferEnableFlushing() |
642 * @see cxBufferEnableFlushing() |
| 676 * Writes a character to a buffer. |
673 * Writes a character to a buffer. |
| 677 * |
674 * |
| 678 * The least significant byte of the argument is written to the buffer. If the |
675 * The least significant byte of the argument is written to the buffer. If the |
| 679 * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled, |
676 * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled, |
| 680 * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature |
677 * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature |
| 681 * is disabled or buffer extension fails, @c EOF is returned. |
678 * is disabled or the buffer extension fails, @c EOF is returned. |
| 682 * |
679 * |
| 683 * On successful write, the position of the buffer is increased. |
680 * On successful writing, the position of the buffer is increased. |
| 684 * |
681 * |
| 685 * If you just want to write a null-terminator at the current position, you |
682 * If you just want to write a null-terminator at the current position, you |
| 686 * should use cxBufferTerminate() instead. |
683 * should use cxBufferTerminate() instead. |
| 687 * |
684 * |
| 688 * @param buffer the buffer to write to |
685 * @param buffer the buffer to write to |
| 689 * @param c the character to write |
686 * @param c the character to write |
| 690 * @return the byte that has been written or @c EOF when the end of the stream is |
687 * @return the byte that has been written or @c EOF when the end of the stream is |
| 691 * reached and automatic extension is not enabled or not possible |
688 * reached, and automatic extension is not enabled or not possible |
| 692 * @see cxBufferTerminate() |
689 * @see cxBufferTerminate() |
| 693 */ |
690 */ |
| 694 cx_attr_nonnull |
691 cx_attr_nonnull |
| 695 cx_attr_export |
692 cx_attr_export |
| 696 int cxBufferPut( |
693 int cxBufferPut( |