ucx/cx/buffer.h

changeset 888
af685cc9d623
parent 854
1c8401ece69e
equal deleted inserted replaced
877:b60487c3ec36 888:af685cc9d623
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.
72 #define CX_BUFFER_AUTO_EXTEND 0x02 72 #define CX_BUFFER_AUTO_EXTEND 0x02
73 73
74 /** 74 /**
75 * If this flag is enabled, the buffer will allocate new memory when written to. 75 * If this flag is enabled, the buffer will allocate new memory when written to.
76 * 76 *
77 * The current contents of the buffer will be copied to the new memory and the flag 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. 78 * will be cleared while the #CX_BUFFER_FREE_CONTENTS flag will be set automatically.
79 */ 79 */
80 #define CX_BUFFER_COPY_ON_WRITE 0x04 80 #define CX_BUFFER_COPY_ON_WRITE 0x04
81 81
82 /** 82 /**
125 * flush target accepts no more data. 125 * flush target accepts no more data.
126 */ 126 */
127 size_t blkmax; 127 size_t blkmax;
128 128
129 /** 129 /**
130 * The target for write function. 130 * The target for the write function.
131 */ 131 */
132 void *target; 132 void *target;
133 133
134 /** 134 /**
135 * The write-function used for flushing. 135 * The write-function used for flushing.
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 *
220 * @param space pointer to the memory area, or @c NULL to allocate 220 * @param space pointer to the memory area, or @c NULL to allocate
221 * new memory 221 * new memory
222 * @param capacity the capacity of the buffer 222 * @param capacity the capacity of the buffer
223 * @param allocator the allocator this buffer shall use for automatic 223 * @param allocator the allocator this buffer shall use for automatic
224 * memory management 224 * memory management
225 * (if @c NULL, a default stdlib allocator will be used) 225 * (if @c NULL, the cxDefaultAllocator will be used)
226 * @param flags buffer features (see cx_buffer_s.flags) 226 * @param flags buffer features (see cx_buffer_s.flags)
227 * @return zero on success, non-zero if a required allocation failed 227 * @return zero on success, non-zero if a required allocation failed
228 */ 228 */
229 cx_attr_nonnull_arg(1) 229 cx_attr_nonnull_arg(1)
230 cx_attr_export 230 CX_EXPORT int cxBufferInit(CxBuffer *buffer, void *space, size_t capacity,
231 int cxBufferInit( 231 const CxAllocator *allocator, int flags);
232 CxBuffer *buffer,
233 void *space,
234 size_t capacity,
235 const CxAllocator *allocator,
236 int flags
237 );
238 232
239 /** 233 /**
240 * Configures the buffer for flushing. 234 * Configures the buffer for flushing.
241 * 235 *
242 * Flushing can happen automatically when data is written 236 * Flushing can happen automatically when data is written
249 * @retval non-zero failure 243 * @retval non-zero failure
250 * @see cxBufferFlush() 244 * @see cxBufferFlush()
251 * @see cxBufferWrite() 245 * @see cxBufferWrite()
252 */ 246 */
253 cx_attr_nonnull 247 cx_attr_nonnull
254 cx_attr_export 248 CX_EXPORT int cxBufferEnableFlushing(CxBuffer *buffer, CxBufferFlushConfig config);
255 int cxBufferEnableFlushing(
256 CxBuffer *buffer,
257 CxBufferFlushConfig config
258 );
259 249
260 /** 250 /**
261 * Destroys the buffer contents. 251 * Destroys the buffer contents.
262 * 252 *
263 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled. 253 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled.
265 * 255 *
266 * @param buffer the buffer which contents shall be destroyed 256 * @param buffer the buffer which contents shall be destroyed
267 * @see cxBufferInit() 257 * @see cxBufferInit()
268 */ 258 */
269 cx_attr_nonnull 259 cx_attr_nonnull
270 cx_attr_export 260 CX_EXPORT void cxBufferDestroy(CxBuffer *buffer);
271 void cxBufferDestroy(CxBuffer *buffer);
272 261
273 /** 262 /**
274 * Deallocates the buffer. 263 * Deallocates the buffer.
275 * 264 *
276 * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys 265 * 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(). 266 * the contents. If you @em only want to destroy the contents, use cxBufferDestroy().
278 * 267 *
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 268 * @param buffer the buffer to deallocate
283 * @see cxBufferCreate() 269 * @see cxBufferCreate()
284 */ 270 */
285 cx_attr_export 271 CX_EXPORT void cxBufferFree(CxBuffer *buffer);
286 void cxBufferFree(CxBuffer *buffer);
287 272
288 /** 273 /**
289 * Allocates and initializes a fresh buffer. 274 * Allocates and initializes a fresh buffer.
290 * 275 *
291 * You may also provide a read-only @p space, in which case 276 * You may also provide a read-only @p space, in which case
294 * When you specify stack memory as @p space and decide to use 279 * When you specify stack memory as @p space and decide to use
295 * the auto-extension feature, you @em must use the 280 * the auto-extension feature, you @em must use the
296 * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the 281 * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the
297 * #CX_BUFFER_AUTO_EXTEND flag. 282 * #CX_BUFFER_AUTO_EXTEND flag.
298 * 283 *
299 * @note You may provide @c NULL as argument for @p space. 284 * @note You may provide @c NULL as the argument for @p space.
300 * Then this function will allocate the space and enforce 285 * Then this function will allocate the space and enforce
301 * the #CX_BUFFER_FREE_CONTENTS flag. 286 * the #CX_BUFFER_FREE_CONTENTS flag.
302 * 287 *
303 * @param space pointer to the memory area, or @c NULL to allocate 288 * @param space pointer to the memory area, or @c NULL to allocate
304 * new memory 289 * new memory
305 * @param capacity the capacity of the buffer 290 * @param capacity the capacity of the buffer
306 * @param allocator the allocator to use for allocating the structure and the automatic 291 * @param allocator the allocator to use for allocating the structure and the automatic
307 * memory management within the buffer 292 * memory management within the buffer
308 * (if @c NULL, a default stdlib allocator will be used) 293 * (if @c NULL, the cxDefaultAllocator will be used)
309 * @param flags buffer features (see cx_buffer_s.flags) 294 * @param flags buffer features (see cx_buffer_s.flags)
310 * @return a pointer to the buffer on success, @c NULL if a required allocation failed 295 * @return a pointer to the buffer on success, @c NULL if a required allocation failed
311 */ 296 */
312 cx_attr_malloc 297 cx_attr_malloc cx_attr_dealloc(cxBufferFree, 1) cx_attr_nodiscard
313 cx_attr_dealloc(cxBufferFree, 1) 298 CX_EXPORT CxBuffer *cxBufferCreate(void *space, size_t capacity,
314 cx_attr_nodiscard 299 const CxAllocator *allocator, int flags);
315 cx_attr_export
316 CxBuffer *cxBufferCreate(
317 void *space,
318 size_t capacity,
319 const CxAllocator *allocator,
320 int flags
321 );
322 300
323 /** 301 /**
324 * Shifts the contents of the buffer by the given offset. 302 * Shifts the contents of the buffer by the given offset.
325 * 303 *
326 * If the offset is positive, the contents are shifted to the right. 304 * If the offset is positive, the contents are shifted to the right.
327 * If auto extension is enabled, the buffer grows, if necessary. 305 * 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 306 * In case the auto extension fails, this function returns a non-zero value and
329 * no contents are changed. 307 * no contents are changed.
330 * If auto extension is disabled, the contents that do not fit into the buffer 308 * When the auto extension is disabled, the contents that do not fit into the
331 * are discarded. 309 * buffer are discarded.
332 * 310 *
333 * If the offset is negative, the contents are shifted to the left where the 311 * If the offset is negative, the contents are shifted to the left where the
334 * first @p shift bytes are discarded. 312 * first @p shift bytes are discarded.
335 * The new size of the buffer is the old size minus the absolute shift value. 313 * 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 314 * If this value is larger than the buffer size, the buffer is emptied (but
337 * not cleared, see the security note below). 315 * not cleared, see the security note below).
338 * 316 *
339 * The buffer position gets shifted alongside with the content but is kept 317 * The buffer position gets shifted alongside the content but is kept
340 * within the boundaries of the buffer. 318 * within the boundaries of the buffer.
341 * 319 *
342 * @note For situations where @c off_t is not large enough, there are specialized cxBufferShiftLeft() and 320 * @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. 321 * cxBufferShiftRight() functions using a @c size_t as the parameter type.
344 * 322 *
345 * @attention 323 * @attention
346 * Security Note: The shifting operation does @em not erase the previously occupied memory cells. 324 * 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 325 * But you can do that manually by calling
348 * <code>memset(buffer->bytes, 0, shift)</code> for a right shift or 326 * <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> 327 * <code>memset(buffer->bytes + buffer->size, 0, buffer->capacity - buffer->size)</code>
350 * for a left shift. 328 * for a left shift.
351 * 329 *
352 * @param buffer the buffer 330 * @param buffer the buffer
355 * @retval non-zero if a required auto-extension or copy-on-write fails 333 * @retval non-zero if a required auto-extension or copy-on-write fails
356 * @see cxBufferShiftLeft() 334 * @see cxBufferShiftLeft()
357 * @see cxBufferShiftRight() 335 * @see cxBufferShiftRight()
358 */ 336 */
359 cx_attr_nonnull 337 cx_attr_nonnull
360 cx_attr_export 338 CX_EXPORT int cxBufferShift(CxBuffer *buffer, off_t shift);
361 int cxBufferShift(
362 CxBuffer *buffer,
363 off_t shift
364 );
365 339
366 /** 340 /**
367 * Shifts the buffer to the right. 341 * Shifts the buffer to the right.
368 * See cxBufferShift() for details. 342 * See cxBufferShift() for details.
369 * 343 *
372 * @retval zero success 346 * @retval zero success
373 * @retval non-zero if a required auto-extension or copy-on-write fails 347 * @retval non-zero if a required auto-extension or copy-on-write fails
374 * @see cxBufferShift() 348 * @see cxBufferShift()
375 */ 349 */
376 cx_attr_nonnull 350 cx_attr_nonnull
377 cx_attr_export 351 CX_EXPORT int cxBufferShiftRight(CxBuffer *buffer, size_t shift);
378 int cxBufferShiftRight(
379 CxBuffer *buffer,
380 size_t shift
381 );
382 352
383 /** 353 /**
384 * Shifts the buffer to the left. 354 * Shifts the buffer to the left.
385 * See cxBufferShift() for details. 355 * See cxBufferShift() for details.
386 * 356 *
389 * @retval zero success 359 * @retval zero success
390 * @retval non-zero if the buffer uses copy-on-write and the allocation fails 360 * @retval non-zero if the buffer uses copy-on-write and the allocation fails
391 * @see cxBufferShift() 361 * @see cxBufferShift()
392 */ 362 */
393 cx_attr_nonnull 363 cx_attr_nonnull
394 cx_attr_export 364 CX_EXPORT int cxBufferShiftLeft(CxBuffer *buffer, size_t shift);
395 int cxBufferShiftLeft(
396 CxBuffer *buffer,
397 size_t shift
398 );
399 365
400 366
401 /** 367 /**
402 * Moves the position of the buffer. 368 * Moves the position of the buffer.
403 * 369 *
417 * @retval zero success 383 * @retval zero success
418 * @retval non-zero if the position is invalid 384 * @retval non-zero if the position is invalid
419 * 385 *
420 */ 386 */
421 cx_attr_nonnull 387 cx_attr_nonnull
422 cx_attr_export 388 CX_EXPORT int cxBufferSeek(CxBuffer *buffer, off_t offset, int whence);
423 int cxBufferSeek(
424 CxBuffer *buffer,
425 off_t offset,
426 int whence
427 );
428 389
429 /** 390 /**
430 * Clears the buffer by resetting the position and deleting the data. 391 * Clears the buffer by resetting the position and deleting the data.
431 * 392 *
432 * The data is deleted by zeroing it with a call to memset(). 393 * The data is deleted by zeroing it with a call to memset().
437 * 398 *
438 * @param buffer the buffer to be cleared 399 * @param buffer the buffer to be cleared
439 * @see cxBufferReset() 400 * @see cxBufferReset()
440 */ 401 */
441 cx_attr_nonnull 402 cx_attr_nonnull
442 cx_attr_export 403 CX_EXPORT void cxBufferClear(CxBuffer *buffer);
443 void cxBufferClear(CxBuffer *buffer);
444 404
445 /** 405 /**
446 * Resets the buffer by resetting the position and size to zero. 406 * Resets the buffer by resetting the position and size to zero.
447 * 407 *
448 * The data in the buffer is not deleted. If you need a safe 408 * The data in the buffer is not deleted. If you need a safe
450 * 410 *
451 * @param buffer the buffer to be cleared 411 * @param buffer the buffer to be cleared
452 * @see cxBufferClear() 412 * @see cxBufferClear()
453 */ 413 */
454 cx_attr_nonnull 414 cx_attr_nonnull
455 cx_attr_export 415 CX_EXPORT void cxBufferReset(CxBuffer *buffer);
456 void cxBufferReset(CxBuffer *buffer);
457 416
458 /** 417 /**
459 * Tests, if the buffer position has exceeded the buffer size. 418 * Tests, if the buffer position has exceeded the buffer size.
460 * 419 *
461 * @param buffer the buffer to test 420 * @param buffer the buffer to test
462 * @retval true if the current buffer position has exceeded the last 421 * @retval true if the current buffer position has exceeded the last
463 * byte of the buffer's contents 422 * byte of the buffer's contents
464 * @retval false otherwise 423 * @retval false otherwise
465 */ 424 */
466 cx_attr_nonnull 425 cx_attr_nonnull cx_attr_nodiscard
467 cx_attr_nodiscard 426 CX_EXPORT bool cxBufferEof(const CxBuffer *buffer);
468 cx_attr_export
469 bool cxBufferEof(const CxBuffer *buffer);
470 427
471 428
472 /** 429 /**
473 * Ensures that the buffer has a minimum capacity. 430 * Ensures that the buffer has a minimum capacity.
474 * 431 *
475 * If the current capacity is not sufficient, the buffer will be extended. 432 * If the current capacity is not sufficient, the buffer will be extended.
433 *
434 * The new capacity will be a power of two until the system's page size is reached.
435 * Then, the new capacity will be a multiple of the page size.
476 * 436 *
477 * @param buffer the buffer 437 * @param buffer the buffer
478 * @param capacity the minimum required capacity for this buffer 438 * @param capacity the minimum required capacity for this buffer
479 * @retval zero the capacity was already sufficient or successfully increased 439 * @retval zero the capacity was already sufficient or successfully increased
480 * @retval non-zero on allocation failure 440 * @retval non-zero on allocation failure
481 */ 441 * @see cxBufferShrink()
482 cx_attr_nonnull 442 */
483 cx_attr_export 443 cx_attr_nonnull
484 int cxBufferMinimumCapacity( 444 CX_EXPORT int cxBufferMinimumCapacity(CxBuffer *buffer, size_t capacity);
485 CxBuffer *buffer, 445
486 size_t capacity 446 /**
487 ); 447 * Shrinks the capacity of the buffer to fit its current size.
448 *
449 * If @p reserve is larger than zero, the buffer is shrunk to its size plus
450 * the number of reserved bytes.
451 *
452 * If the current capacity is not larger than the size plus the reserved bytes,
453 * nothing happens.
454 *
455 * If the #CX_BUFFER_COPY_ON_WRITE or #CX_BUFFER_COPY_ON_EXTEND flag is set,
456 * this function does nothing.
457 *
458 * @param buffer the buffer
459 * @param reserve the number of bytes that shall remain reserved
460 * @see cxBufferMinimumCapacity()
461 */
462 cx_attr_nonnull
463 CX_EXPORT void cxBufferShrink(CxBuffer *buffer, size_t reserve);
488 464
489 /** 465 /**
490 * Writes data to a CxBuffer. 466 * Writes data to a CxBuffer.
491 * 467 *
492 * If automatic flushing is not enabled, the data is simply written into the 468 * If automatic flushing is not enabled, the data is simply written into the
493 * buffer at the current position and the position of the buffer is increased 469 * buffer at the current position, and the position of the buffer is increased
494 * by the number of bytes written. 470 * by the number of bytes written.
495 * 471 *
496 * If flushing is enabled and the buffer needs to flush, the data is flushed to 472 * If flushing is enabled and the buffer needs to flush, the data is flushed to
497 * the target until the target signals that it cannot take more data by 473 * the target until the target signals that it cannot take more data by
498 * returning zero via the respective write function. In that case, the remaining 474 * returning zero via the respective write function. In that case, the remaining
499 * data in this buffer is shifted to the beginning of this buffer so that the 475 * data in this buffer is shifted to the beginning of this buffer so that the
500 * newly available space can be used to append as much data as possible. 476 * newly available space can be used to append as much data as possible.
501 * 477 *
502 * This function only stops writing more elements, when the flush target and this 478 * This function only stops writing more elements when the flush target and this
503 * buffer are both incapable of taking more data or all data has been written. 479 * buffer are both incapable of taking more data or all data has been written.
504 * 480 *
505 * If, after flushing, the number of items that shall be written still exceeds 481 * If, after flushing, the number of items that shall be written still exceeds
506 * the capacity or flush threshold, this function tries to write all items directly 482 * the capacity or flush threshold, this function tries to write all items directly
507 * to the flush target, if possible. 483 * to the flush target, if possible.
508 * 484 *
509 * The number returned by this function is the number of elements from 485 * 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 486 * @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 487 * That means it does @em not include the number of items that were already in
512 * in were flushed during the process). 488 * the buffer and were also flushed during the process.
513 * 489 *
514 * @attention 490 * @attention
515 * When @p size is larger than one and the contents of the buffer are not aligned 491 * 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 492 * with @p size, flushing stops after all complete items have been flushed, leaving
517 * the mis-aligned part in the buffer. 493 * the misaligned part in the buffer.
518 * Afterward, this function only writes as many items as possible to the buffer. 494 * Afterward, this function only writes as many items as possible to the buffer.
519 * 495 *
520 * @note The signature is compatible with the fwrite() family of functions. 496 * @note The signature is compatible with the fwrite() family of functions.
521 * 497 *
522 * @param ptr a pointer to the memory area containing the bytes to be written 498 * @param ptr a pointer to the memory area containing the bytes to be written
526 * @return the total count of elements written 502 * @return the total count of elements written
527 * @see cxBufferAppend() 503 * @see cxBufferAppend()
528 * @see cxBufferRead() 504 * @see cxBufferRead()
529 */ 505 */
530 cx_attr_nonnull 506 cx_attr_nonnull
531 cx_attr_export 507 CX_EXPORT size_t cxBufferWrite(const void *ptr, size_t size,
532 size_t cxBufferWrite( 508 size_t nitems, CxBuffer *buffer);
533 const void *ptr,
534 size_t size,
535 size_t nitems,
536 CxBuffer *buffer
537 );
538 509
539 /** 510 /**
540 * Appends data to a CxBuffer. 511 * Appends data to a CxBuffer.
541 * 512 *
542 * The data is always appended to current data within the buffer, 513 * The data is always appended to current data within the buffer,
554 * @return the total count of elements written 525 * @return the total count of elements written
555 * @see cxBufferWrite() 526 * @see cxBufferWrite()
556 * @see cxBufferRead() 527 * @see cxBufferRead()
557 */ 528 */
558 cx_attr_nonnull 529 cx_attr_nonnull
559 cx_attr_export 530 CX_EXPORT size_t cxBufferAppend(const void *ptr, size_t size,
560 size_t cxBufferAppend( 531 size_t nitems, CxBuffer *buffer);
561 const void *ptr,
562 size_t size,
563 size_t nitems,
564 CxBuffer *buffer
565 );
566 532
567 /** 533 /**
568 * Performs a single flush-run on the specified buffer. 534 * Performs a single flush-run on the specified buffer.
569 * 535 *
570 * Does nothing when the position in the buffer is zero. 536 * Does nothing when the position in the buffer is zero.
585 * @par Example 1 551 * @par Example 1
586 * Assume you have a buffer with size 340 and you are 552 * Assume you have a buffer with size 340 and you are
587 * at position 200. The flush configuration is 553 * at position 200. The flush configuration is
588 * @c blkmax=4 and @c blksize=64 . 554 * @c blkmax=4 and @c blksize=64 .
589 * Assume that the entire flush operation is successful. 555 * Assume that the entire flush operation is successful.
590 * All 200 bytes on the left hand-side from the current 556 * All 200 bytes on the left-hand-side from the current
591 * position are written. 557 * position are written.
592 * That means, the size of the buffer is now 140 and the 558 * That means the size of the buffer is now 140 and the
593 * position is zero. 559 * position is zero.
594 * 560 *
595 * @par Example 2 561 * @par Example 2
596 * Same as Example 1, but now the @c blkmax is 1. 562 * Same as Example 1, but now the @c blkmax is 1.
597 * The size of the buffer is now 276 and the position is 136. 563 * The size of the buffer is now 276, and the position is 136.
598 * 564 *
599 * @par Example 3 565 * @par Example 3
600 * Same as Example 1, but now assume the flush target 566 * Same as Example 1, but now assume the flush target
601 * only accepts 100 bytes before returning zero. 567 * only accepts 100 bytes before returning zero.
602 * That means, the flush operations manages to flush 568 * That means the flush operation manages to flush
603 * one complete block and one partial block, ending 569 * one complete block and one partial block, ending
604 * up with a buffer with size 240 and position 100. 570 * up with a buffer with size 240 and position 100.
605 * 571 *
606 * @remark Just returns zero when flushing was not enabled with 572 * @remark Just returns zero when flushing was not enabled with
607 * cxBufferEnableFlushing(). 573 * cxBufferEnableFlushing().
608 * 574 *
609 * @remark When the buffer uses copy-on-write, the memory 575 * @remark When the buffer uses copy-on-write, the memory
610 * is copied first, before attempting any flush. 576 * is copied first, before attempting any flush.
611 * This is, however, considered an erroneous use of the 577 * This is, however, considered an erroneous use of the
612 * buffer, because it does not make much sense to put 578 * buffer because it makes little sense to put
613 * readonly data into an UCX buffer for flushing, instead 579 * readonly data into an UCX buffer for flushing instead
614 * of writing it directly to the target. 580 * of writing it directly to the target.
615 * 581 *
616 * @param buffer the buffer 582 * @param buffer the buffer
617 * @return the number of successfully flushed bytes 583 * @return the number of successfully flushed bytes
618 * @see cxBufferEnableFlushing() 584 * @see cxBufferEnableFlushing()
619 */ 585 */
620 cx_attr_nonnull 586 cx_attr_nonnull
621 cx_attr_export 587 CX_EXPORT size_t cxBufferFlush(CxBuffer *buffer);
622 size_t cxBufferFlush(CxBuffer *buffer);
623 588
624 /** 589 /**
625 * Reads data from a CxBuffer. 590 * Reads data from a CxBuffer.
626 * 591 *
627 * The position of the buffer is increased by the number of bytes read. 592 * The position of the buffer is increased by the number of bytes read.
635 * @return the total number of elements read 600 * @return the total number of elements read
636 * @see cxBufferWrite() 601 * @see cxBufferWrite()
637 * @see cxBufferAppend() 602 * @see cxBufferAppend()
638 */ 603 */
639 cx_attr_nonnull 604 cx_attr_nonnull
640 cx_attr_export 605 CX_EXPORT size_t cxBufferRead(void *ptr, size_t size,
641 size_t cxBufferRead( 606 size_t nitems, CxBuffer *buffer);
642 void *ptr,
643 size_t size,
644 size_t nitems,
645 CxBuffer *buffer
646 );
647 607
648 /** 608 /**
649 * Writes a character to a buffer. 609 * Writes a character to a buffer.
650 * 610 *
651 * The least significant byte of the argument is written to the buffer. If the 611 * The least significant byte of the argument is written to the buffer. If the
652 * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled, 612 * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled,
653 * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature 613 * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature
654 * is disabled or buffer extension fails, @c EOF is returned. 614 * is disabled or the buffer extension fails, @c EOF is returned.
655 * 615 *
656 * On successful write, the position of the buffer is increased. 616 * On successful writing, the position of the buffer is increased.
657 * 617 *
658 * If you just want to write a null-terminator at the current position, you 618 * If you just want to write a null-terminator at the current position, you
659 * should use cxBufferTerminate() instead. 619 * should use cxBufferTerminate() instead.
660 * 620 *
661 * @param buffer the buffer to write to 621 * @param buffer the buffer to write to
662 * @param c the character to write 622 * @param c the character to write
663 * @return the byte that has been written or @c EOF when the end of the stream is 623 * @return the byte that has been written or @c EOF when the end of the stream is
664 * reached and automatic extension is not enabled or not possible 624 * reached, and automatic extension is not enabled or not possible
665 * @see cxBufferTerminate() 625 * @see cxBufferTerminate()
666 */ 626 */
667 cx_attr_nonnull 627 cx_attr_nonnull
668 cx_attr_export 628 CX_EXPORT int cxBufferPut(CxBuffer *buffer, int c);
669 int cxBufferPut(
670 CxBuffer *buffer,
671 int c
672 );
673 629
674 /** 630 /**
675 * Writes a terminating zero to a buffer at the current position. 631 * Writes a terminating zero to a buffer at the current position.
676 * 632 *
677 * On successful write, @em neither the position @em nor the size of the buffer is 633 * If successful, sets the size to the current position and advances the position by one.
678 * increased.
679 * 634 *
680 * The purpose of this function is to have the written data ready to be used as 635 * The purpose of this function is to have the written data ready to be used as
681 * a C string. 636 * a C string with the buffer's size being the length of that string.
682 * 637 *
683 * @param buffer the buffer to write to 638 * @param buffer the buffer to write to
684 * @return zero, if the terminator could be written, non-zero otherwise 639 * @return zero, if the terminator could be written, non-zero otherwise
685 */ 640 */
686 cx_attr_nonnull 641 cx_attr_nonnull
687 cx_attr_export 642 CX_EXPORT int cxBufferTerminate(CxBuffer *buffer);
688 int cxBufferTerminate(CxBuffer *buffer);
689 643
690 /** 644 /**
691 * Writes a string to a buffer. 645 * Writes a string to a buffer.
692 * 646 *
693 * This is a convenience function for <code>cxBufferWrite(str, 1, strlen(str), buffer)</code>. 647 * This is a convenience function for <code>cxBufferWrite(str, 1, strlen(str), buffer)</code>.
694 * 648 *
695 * @param buffer the buffer 649 * @param buffer the buffer
696 * @param str the zero-terminated string 650 * @param str the zero-terminated string
697 * @return the number of bytes written 651 * @return the number of bytes written
698 */ 652 */
699 cx_attr_nonnull 653 cx_attr_nonnull cx_attr_cstr_arg(2)
700 cx_attr_cstr_arg(2) 654 CX_EXPORT size_t cxBufferPutString(CxBuffer *buffer, const char *str);
701 cx_attr_export
702 size_t cxBufferPutString(
703 CxBuffer *buffer,
704 const char *str
705 );
706 655
707 /** 656 /**
708 * Gets a character from a buffer. 657 * Gets a character from a buffer.
709 * 658 *
710 * The current position of the buffer is increased after a successful read. 659 * The current position of the buffer is increased after a successful read.
711 * 660 *
712 * @param buffer the buffer to read from 661 * @param buffer the buffer to read from
713 * @return the character or @c EOF, if the end of the buffer is reached 662 * @return the character or @c EOF, if the end of the buffer is reached
714 */ 663 */
715 cx_attr_nonnull 664 cx_attr_nonnull
716 cx_attr_export 665 CX_EXPORT int cxBufferGet(CxBuffer *buffer);
717 int cxBufferGet(CxBuffer *buffer);
718 666
719 #ifdef __cplusplus 667 #ifdef __cplusplus
720 } 668 }
721 #endif 669 #endif
722 670

mercurial