ucx/cx/buffer.h

changeset 31
287484519844
parent 30
d33eaaec15da
equal deleted inserted replaced
30:d33eaaec15da 31:287484519844
46 #ifndef UCX_BUFFER_H 46 #ifndef UCX_BUFFER_H
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 #include "string.h"
51 52
52 #ifdef __cplusplus 53 #ifdef __cplusplus
53 extern "C" { 54 extern "C" {
54 #endif 55 #endif
55 56
85 * After performing the copy, the flag is automatically cleared. 86 * 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 * 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 * buffers automatically admit the auto-extend flag when initialized with copy-on-extend enabled.
88 */ 89 */
89 #define CX_BUFFER_COPY_ON_EXTEND 0x08 90 #define CX_BUFFER_COPY_ON_EXTEND 0x08
91
92 /**
93 * If this flag is enabled, the buffer will never free its contents regardless of #CX_BUFFER_FREE_CONTENTS.
94 *
95 * This is useful, for example, when you want to keep a pointer to the data after destroying the buffer.
96 */
97 #define CX_BUFFER_DO_NOT_FREE 0x10
90 98
91 /** 99 /**
92 * Function pointer for cxBufferWrite that is compatible with cx_write_func. 100 * Function pointer for cxBufferWrite that is compatible with cx_write_func.
93 * @see cx_write_func 101 * @see cx_write_func
94 */ 102 */
158 * the #CX_BUFFER_FREE_CONTENTS flag. In that case, specifying 166 * the #CX_BUFFER_FREE_CONTENTS flag. In that case, specifying
159 * copy-on-write should be avoided, because the allocated 167 * copy-on-write should be avoided, because the allocated
160 * space will be leaking after the copy-on-write operation. 168 * space will be leaking after the copy-on-write operation.
161 * 169 *
162 * @param buffer the buffer to initialize 170 * @param buffer the buffer to initialize
171 * @param allocator the allocator this buffer shall use for automatic
172 * memory management
173 * (if @c NULL, the cxDefaultAllocator will be used)
163 * @param space pointer to the memory area, or @c NULL to allocate 174 * @param space pointer to the memory area, or @c NULL to allocate
164 * new memory 175 * new memory
165 * @param capacity the capacity of the buffer 176 * @param capacity the capacity of the buffer
166 * @param allocator the allocator this buffer shall use for automatic
167 * memory management
168 * (if @c NULL, the cxDefaultAllocator will be used)
169 * @param flags buffer features (see cx_buffer_s.flags) 177 * @param flags buffer features (see cx_buffer_s.flags)
170 * @return zero on success, non-zero if a required allocation failed 178 * @return zero on success, non-zero if a required allocation failed
171 */ 179 */
172 cx_attr_nonnull_arg(1) 180 cx_attr_nonnull_arg(1)
173 CX_EXPORT int cxBufferInit(CxBuffer *buffer, void *space, size_t capacity, 181 CX_EXPORT int cxBufferInit(CxBuffer *buffer, const CxAllocator *allocator,
174 const CxAllocator *allocator, int flags); 182 void *space, size_t capacity, int flags);
175 183
176 /** 184 /**
177 * Destroys the buffer contents. 185 * Destroys the buffer contents.
178 * 186 *
179 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled. 187 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled.
209 * 217 *
210 * @note You may provide @c NULL as the argument for @p space. 218 * @note You may provide @c NULL as the argument for @p space.
211 * Then this function will allocate the space and enforce 219 * Then this function will allocate the space and enforce
212 * the #CX_BUFFER_FREE_CONTENTS flag. 220 * the #CX_BUFFER_FREE_CONTENTS flag.
213 * 221 *
222 * @param allocator the allocator to use for allocating the structure and the automatic
223 * memory management within the buffer
224 * (if @c NULL, the cxDefaultAllocator will be used)
214 * @param space pointer to the memory area, or @c NULL to allocate 225 * @param space pointer to the memory area, or @c NULL to allocate
215 * new memory 226 * new memory
216 * @param capacity the capacity of the buffer 227 * @param capacity the capacity of the buffer
217 * @param allocator the allocator to use for allocating the structure and the automatic
218 * memory management within the buffer
219 * (if @c NULL, the cxDefaultAllocator will be used)
220 * @param flags buffer features (see cx_buffer_s.flags) 228 * @param flags buffer features (see cx_buffer_s.flags)
221 * @return a pointer to the buffer on success, @c NULL if a required allocation failed 229 * @return a pointer to the buffer on success, @c NULL if a required allocation failed
222 */ 230 */
223 cx_attr_malloc cx_attr_dealloc(cxBufferFree, 1) cx_attr_nodiscard 231 cx_attr_malloc cx_attr_dealloc(cxBufferFree, 1) cx_attr_nodiscard
224 CX_EXPORT CxBuffer *cxBufferCreate(void *space, size_t capacity, 232 CX_EXPORT CxBuffer *cxBufferCreate(const CxAllocator *allocator, void *space,
225 const CxAllocator *allocator, int flags); 233 size_t capacity, int flags);
226 234
227 /** 235 /**
228 * Shifts the contents of the buffer by the given offset. 236 * Shifts the contents of the buffer by the given offset.
229 * 237 *
230 * If the offset is positive, the contents are shifted to the right. 238 * If the offset is positive, the contents are shifted to the right.
536 CX_EXPORT int cxBufferPut(CxBuffer *buffer, int c); 544 CX_EXPORT int cxBufferPut(CxBuffer *buffer, int c);
537 545
538 /** 546 /**
539 * Writes a terminating zero to a buffer at the current position. 547 * Writes a terminating zero to a buffer at the current position.
540 * 548 *
541 * If successful, sets the size to the current position and advances 549 * If successful, also sets the size to the current position and shrinks the buffer.
542 * the position by one.
543 * 550 *
544 * The purpose of this function is to have the written data ready to be used as 551 * The purpose of this function is to have the written data ready to be used as
545 * a C string with the buffer's size being the length of that string. 552 * a C string with the buffer's size being the length of that string.
546 * 553 *
547 * @param buffer the buffer to write to 554 * @param buffer the buffer to write to
548 * @return zero, if the terminator could be written, non-zero otherwise 555 * @return zero, if the terminator could be written, non-zero otherwise
556 * @see cxBufferShrink()
549 */ 557 */
550 cx_attr_nonnull 558 cx_attr_nonnull
551 CX_EXPORT int cxBufferTerminate(CxBuffer *buffer); 559 CX_EXPORT int cxBufferTerminate(CxBuffer *buffer);
552 560
553 /** 561 /**
554 * Writes a string to a buffer. 562 * Internal function - do not use.
555 * 563 *
556 * This is a convenience function for <code>cxBufferWrite(str, 1, strlen(str), buffer)</code>. 564 * @param buffer the buffer
557 * 565 * @param str the string
558 * @param buffer the buffer
559 * @param str the zero-terminated string
560 * @return the number of bytes written 566 * @return the number of bytes written
561 */ 567 * @see cxBufferPutString()
562 cx_attr_nonnull cx_attr_cstr_arg(2) 568 */
563 CX_EXPORT size_t cxBufferPutString(CxBuffer *buffer, const char *str); 569 cx_attr_nonnull
570 CX_EXPORT size_t cx_buffer_put_string(CxBuffer *buffer, cxstring str);
571
572 /**
573 * Writes a string to a buffer with cxBufferWrite().
574 *
575 * @param buffer (@c CxBuffer*) the buffer
576 * @param str (any string) the zero-terminated string
577 * @return (@c size_t) the number of bytes written
578 * @see cxBufferWrite()
579 * @see cx_strcast()
580 */
581 #define cxBufferPutString(buffer, str) cx_buffer_put_string(buffer, cx_strcast(str))
582
583 /**
584 * Internal function - do not use.
585 *
586 * @param buffer the buffer
587 * @param str the string
588 * @return the number of bytes written
589 * @see cxBufferPutString()
590 */
591 cx_attr_nonnull
592 CX_EXPORT size_t cx_buffer_append_string(CxBuffer *buffer, cxstring str);
593
594 /**
595 * Appends a string to a buffer with cxBufferAppend().
596 *
597 * @param buffer (@c CxBuffer*) the buffer
598 * @param str (any string) the zero-terminated string
599 * @return (@c size_t) the number of bytes written
600 * @see cxBufferAppend()
601 * @see cx_strcast()
602 */
603 #define cxBufferAppendString(buffer, str) cx_buffer_append_string(buffer, cx_strcast(str))
564 604
565 /** 605 /**
566 * Gets a character from a buffer. 606 * Gets a character from a buffer.
567 * 607 *
568 * The current position of the buffer is increased after a successful read. 608 * The current position of the buffer is increased after a successful read.

mercurial