src/cx/buffer.h

changeset 985
68754c7de906
parent 948
036b431e70ee
equal deleted inserted replaced
984:e8f354a25ac8 985:68754c7de906
49 #include "common.h" 49 #include "common.h"
50 #include "allocator.h" 50 #include "allocator.h"
51 51
52 #include <sys/types.h> 52 #include <sys/types.h>
53 53
54 #ifdef __cplusplus 54 #ifdef __cplusplus
55 extern "C" { 55 extern "C" {
56 #endif 56 #endif
57 57
58 /** 58 /**
59 * No buffer features enabled (all flags cleared). 59 * No buffer features enabled (all flags cleared).
153 * @param allocator the allocator this buffer shall use for automatic 153 * @param allocator the allocator this buffer shall use for automatic
154 * memory management. If \c NULL, the default heap allocator will be used. 154 * memory management. If \c NULL, the default heap allocator will be used.
155 * @param flags buffer features (see cx_buffer_s.flags) 155 * @param flags buffer features (see cx_buffer_s.flags)
156 * @return zero on success, non-zero if a required allocation failed 156 * @return zero on success, non-zero if a required allocation failed
157 */ 157 */
158 __attribute__((__nonnull__(1))) 158 cx_attr_nonnull_arg(1)
159 int cxBufferInit( 159 int cxBufferInit(
160 CxBuffer *buffer, 160 CxBuffer *buffer,
161 void *space, 161 void *space,
162 size_t capacity, 162 size_t capacity,
163 const CxAllocator *allocator, 163 const CxAllocator *allocator,
164 int flags 164 int flags
165 ); 165 );
166
167 /**
168 * Destroys the buffer contents.
169 *
170 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled.
171 * If you want to free the memory of the entire buffer, use cxBufferFree().
172 *
173 * @param buffer the buffer which contents shall be destroyed
174 * @see cxBufferInit()
175 */
176 cx_attr_nonnull
177 void cxBufferDestroy(CxBuffer *buffer);
178
179 /**
180 * Deallocates the buffer.
181 *
182 * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys
183 * the contents. If you \em only want to destroy the contents, use cxBufferDestroy().
184 *
185 * \remark As with all free() functions, this accepts \c NULL arguments in which
186 * case it does nothing.
187 *
188 * @param buffer the buffer to deallocate
189 * @see cxBufferCreate()
190 */
191 void cxBufferFree(CxBuffer *buffer);
166 192
167 /** 193 /**
168 * Allocates and initializes a fresh buffer. 194 * Allocates and initializes a fresh buffer.
169 * 195 *
170 * \note You may provide \c NULL as argument for \p space. 196 * \note You may provide \c NULL as argument for \p space.
177 * @param allocator the allocator to use for allocating the structure and the automatic 203 * @param allocator the allocator to use for allocating the structure and the automatic
178 * memory management within the buffer. If \c NULL, the default heap allocator will be used. 204 * memory management within the buffer. If \c NULL, the default heap allocator will be used.
179 * @param flags buffer features (see cx_buffer_s.flags) 205 * @param flags buffer features (see cx_buffer_s.flags)
180 * @return a pointer to the buffer on success, \c NULL if a required allocation failed 206 * @return a pointer to the buffer on success, \c NULL if a required allocation failed
181 */ 207 */
208 cx_attr_malloc
209 cx_attr_dealloc(cxBufferFree, 1)
210 cx_attr_nodiscard
182 CxBuffer *cxBufferCreate( 211 CxBuffer *cxBufferCreate(
183 void *space, 212 void *space,
184 size_t capacity, 213 size_t capacity,
185 const CxAllocator *allocator, 214 const CxAllocator *allocator,
186 int flags 215 int flags
187 ); 216 );
188
189 /**
190 * Destroys the buffer contents.
191 *
192 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled.
193 * If you want to free the memory of the entire buffer, use cxBufferFree().
194 *
195 * @param buffer the buffer which contents shall be destroyed
196 * @see cxBufferInit()
197 */
198 __attribute__((__nonnull__))
199 void cxBufferDestroy(CxBuffer *buffer);
200
201 /**
202 * Deallocates the buffer.
203 *
204 * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys
205 * the contents. If you \em only want to destroy the contents, use cxBufferDestroy().
206 *
207 * @param buffer the buffer to deallocate
208 * @see cxBufferCreate()
209 */
210 __attribute__((__nonnull__))
211 void cxBufferFree(CxBuffer *buffer);
212 217
213 /** 218 /**
214 * Shifts the contents of the buffer by the given offset. 219 * Shifts the contents of the buffer by the given offset.
215 * 220 *
216 * If the offset is positive, the contents are shifted to the right. 221 * If the offset is positive, the contents are shifted to the right.
241 * 246 *
242 * @param buffer the buffer 247 * @param buffer the buffer
243 * @param shift the shift offset (negative means left shift) 248 * @param shift the shift offset (negative means left shift)
244 * @return 0 on success, non-zero if a required auto-extension fails 249 * @return 0 on success, non-zero if a required auto-extension fails
245 */ 250 */
246 __attribute__((__nonnull__)) 251 cx_attr_nonnull
247 int cxBufferShift( 252 int cxBufferShift(
248 CxBuffer *buffer, 253 CxBuffer *buffer,
249 off_t shift 254 off_t shift
250 ); 255 );
251 256
256 * @param buffer the buffer 261 * @param buffer the buffer
257 * @param shift the shift offset 262 * @param shift the shift offset
258 * @return 0 on success, non-zero if a required auto-extension fails 263 * @return 0 on success, non-zero if a required auto-extension fails
259 * @see cxBufferShift() 264 * @see cxBufferShift()
260 */ 265 */
261 __attribute__((__nonnull__)) 266 cx_attr_nonnull
262 int cxBufferShiftRight( 267 int cxBufferShiftRight(
263 CxBuffer *buffer, 268 CxBuffer *buffer,
264 size_t shift 269 size_t shift
265 ); 270 );
266 271
274 * @param buffer the buffer 279 * @param buffer the buffer
275 * @param shift the positive shift offset 280 * @param shift the positive shift offset
276 * @return always zero 281 * @return always zero
277 * @see cxBufferShift() 282 * @see cxBufferShift()
278 */ 283 */
279 __attribute__((__nonnull__)) 284 cx_attr_nonnull
280 int cxBufferShiftLeft( 285 int cxBufferShiftLeft(
281 CxBuffer *buffer, 286 CxBuffer *buffer,
282 size_t shift 287 size_t shift
283 ); 288 );
284 289
300 * @param offset position offset relative to \p whence 305 * @param offset position offset relative to \p whence
301 * @param whence one of \c SEEK_SET, \c SEEK_CUR or \c SEEK_END 306 * @param whence one of \c SEEK_SET, \c SEEK_CUR or \c SEEK_END
302 * @return 0 on success, non-zero if the position is invalid 307 * @return 0 on success, non-zero if the position is invalid
303 * 308 *
304 */ 309 */
305 __attribute__((__nonnull__)) 310 cx_attr_nonnull
306 int cxBufferSeek( 311 int cxBufferSeek(
307 CxBuffer *buffer, 312 CxBuffer *buffer,
308 off_t offset, 313 off_t offset,
309 int whence 314 int whence
310 ); 315 );
316 * If you do not need that, you can use the faster cxBufferReset(). 321 * If you do not need that, you can use the faster cxBufferReset().
317 * 322 *
318 * @param buffer the buffer to be cleared 323 * @param buffer the buffer to be cleared
319 * @see cxBufferReset() 324 * @see cxBufferReset()
320 */ 325 */
321 __attribute__((__nonnull__)) 326 cx_attr_nonnull
322 void cxBufferClear(CxBuffer *buffer); 327 void cxBufferClear(CxBuffer *buffer);
323 328
324 /** 329 /**
325 * Resets the buffer by resetting the position and size to zero. 330 * Resets the buffer by resetting the position and size to zero.
326 * 331 *
328 * reset of the buffer, use cxBufferClear(). 333 * reset of the buffer, use cxBufferClear().
329 * 334 *
330 * @param buffer the buffer to be cleared 335 * @param buffer the buffer to be cleared
331 * @see cxBufferClear() 336 * @see cxBufferClear()
332 */ 337 */
333 __attribute__((__nonnull__)) 338 cx_attr_nonnull
334 void cxBufferReset(CxBuffer *buffer); 339 void cxBufferReset(CxBuffer *buffer);
335 340
336 /** 341 /**
337 * Tests, if the buffer position has exceeded the buffer size. 342 * Tests, if the buffer position has exceeded the buffer size.
338 * 343 *
339 * @param buffer the buffer to test 344 * @param buffer the buffer to test
340 * @return non-zero, if the current buffer position has exceeded the last 345 * @return non-zero, if the current buffer position has exceeded the last
341 * byte of the buffer's contents. 346 * byte of the buffer's contents.
342 */ 347 */
343 __attribute__((__nonnull__)) 348 cx_attr_nonnull
349 cx_attr_nodiscard
344 int cxBufferEof(const CxBuffer *buffer); 350 int cxBufferEof(const CxBuffer *buffer);
345 351
346 352
347 /** 353 /**
348 * Ensures that the buffer has a minimum capacity. 354 * Ensures that the buffer has a minimum capacity.
351 * 357 *
352 * @param buffer the buffer 358 * @param buffer the buffer
353 * @param capacity the minimum required capacity for this buffer 359 * @param capacity the minimum required capacity for this buffer
354 * @return 0 on success or a non-zero value on failure 360 * @return 0 on success or a non-zero value on failure
355 */ 361 */
356 __attribute__((__nonnull__)) 362 cx_attr_nonnull
357 int cxBufferMinimumCapacity( 363 int cxBufferMinimumCapacity(
358 CxBuffer *buffer, 364 CxBuffer *buffer,
359 size_t capacity 365 size_t capacity
360 ); 366 );
361 367
383 * @param size the length of one element 389 * @param size the length of one element
384 * @param nitems the element count 390 * @param nitems the element count
385 * @param buffer the CxBuffer to write to 391 * @param buffer the CxBuffer to write to
386 * @return the total count of elements written 392 * @return the total count of elements written
387 */ 393 */
388 __attribute__((__nonnull__)) 394 cx_attr_nonnull
389 size_t cxBufferWrite( 395 size_t cxBufferWrite(
390 const void *ptr, 396 const void *ptr,
391 size_t size, 397 size_t size,
392 size_t nitems, 398 size_t nitems,
393 CxBuffer *buffer 399 CxBuffer *buffer
404 * @param size the length of one element 410 * @param size the length of one element
405 * @param nitems the element count 411 * @param nitems the element count
406 * @param buffer the CxBuffer to read from 412 * @param buffer the CxBuffer to read from
407 * @return the total number of elements read 413 * @return the total number of elements read
408 */ 414 */
409 __attribute__((__nonnull__)) 415 cx_attr_nonnull
410 size_t cxBufferRead( 416 size_t cxBufferRead(
411 void *ptr, 417 void *ptr,
412 size_t size, 418 size_t size,
413 size_t nitems, 419 size_t nitems,
414 CxBuffer *buffer 420 CxBuffer *buffer
427 * @param buffer the buffer to write to 433 * @param buffer the buffer to write to
428 * @param c the character to write 434 * @param c the character to write
429 * @return the byte that has bean written or \c EOF when the end of the stream is 435 * @return the byte that has bean written or \c EOF when the end of the stream is
430 * reached and automatic extension is not enabled or not possible 436 * reached and automatic extension is not enabled or not possible
431 */ 437 */
432 __attribute__((__nonnull__)) 438 cx_attr_nonnull
433 int cxBufferPut( 439 int cxBufferPut(
434 CxBuffer *buffer, 440 CxBuffer *buffer,
435 int c 441 int c
436 ); 442 );
437 443
440 * 446 *
441 * @param buffer the buffer 447 * @param buffer the buffer
442 * @param str the zero-terminated string 448 * @param str the zero-terminated string
443 * @return the number of bytes written 449 * @return the number of bytes written
444 */ 450 */
445 __attribute__((__nonnull__)) 451 cx_attr_nonnull
452 cx_attr_cstr_arg(2)
446 size_t cxBufferPutString( 453 size_t cxBufferPutString(
447 CxBuffer *buffer, 454 CxBuffer *buffer,
448 const char *str 455 const char *str
449 ); 456 );
450 457
454 * The current position of the buffer is increased after a successful read. 461 * The current position of the buffer is increased after a successful read.
455 * 462 *
456 * @param buffer the buffer to read from 463 * @param buffer the buffer to read from
457 * @return the character or \c EOF, if the end of the buffer is reached 464 * @return the character or \c EOF, if the end of the buffer is reached
458 */ 465 */
459 __attribute__((__nonnull__)) 466 cx_attr_nonnull
460 int cxBufferGet(CxBuffer *buffer); 467 int cxBufferGet(CxBuffer *buffer);
461 468
462 #ifdef __cplusplus 469 #ifdef __cplusplus
463 } 470 }
464 #endif 471 #endif

mercurial