ucx/buffer.h

Thu, 11 Oct 2012 11:42:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 11 Oct 2012 11:42:31 +0200
changeset 67
27e67e725d35
parent 64
16590c9c497c
child 69
fb59270b1de3
permissions
-rw-r--r--

added some qualifiers + removed pointer alias in mergesort

     1 #ifndef BUFFER_H
     2 #define	BUFFER_H
     4 #include <sys/types.h>
     5 #include <stdio.h>
     7 #ifdef	__cplusplus
     8 extern "C" {
     9 #endif
    11 #define UCX_BUFFER_DEFAULT      0x00
    12 #define UCX_BUFFER_AUTOFREE     0x01
    13 /* the buffer may automatically double its size on write operations */
    14 #define UCX_BUFFER_AUTOEXTEND   0x02
    16 /* the user shall not modify values, but may get the latest pointer */
    17 typedef struct {
    18     void *space;
    19     size_t pos;
    20     size_t size;
    21     int flags;
    22 } UcxBuffer;
    24 /* if space is NULL, new space is allocated and the autofree flag is enforced */
    25 UcxBuffer *ucx_buffer_new(void *space, size_t length, int flags);
    26 void ucx_buffer_free(UcxBuffer* buffer);
    28 /*
    29  * the autofree flag is enforced for the new buffer
    30  * if length is zero, the whole remaining buffer shall be extracted
    31  * the position of the new buffer is set to zero
    32  */
    33 UcxBuffer *restrict ucx_buffer_extract(UcxBuffer *restrict src,
    34         size_t start, size_t length, int flags);
    35 #define ucx_buffer_clone(src,flags) \
    36     ucx_buffer_extract(src, 0, 0, flags)
    38 /*
    39  * Moves the position of the buffer to a new position relative to whence.
    40  *
    41  * SEEK_SET marks the start of the buffer
    42  * SEEK_CUR marks the current position
    43  * SEEK_END marks the first 0-byte in the buffer
    44  *
    45  * ucx_memseek returns 0 on success and -1 if the new position is beyond the
    46  * bounds of the allocated buffer. In that case the position of the buffer
    47  * remains unchanged.
    48  *
    49  */
    50 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence);
    52 /*
    53  * returns non-zero, iff the current buffer position has exceeded the last
    54  * available byte of the underlying buffer
    55  *
    56  */
    57 int ucx_buffer_eof(UcxBuffer *buffer);
    59 size_t ucx_bufio(void *d, size_t s, size_t n, UcxBuffer* b, _Bool read);
    60 /* when autoextend is enabled, ensure you get the latest pointer to the data */
    61 #define ucx_buffer_write(data, itemsize, nitems, buffer) \
    62     ucx_bufio(data, itemsize, nitems, buffer, 0)
    63 #define ucx_buffer_read(data, itemsize, nitems, buffer) \
    64         ucx_bufio(data, itemsize, nitems, buffer, 1)
    65 int ucx_buffer_putc(UcxBuffer *b, int c);
    66 int ucx_buffer_getc(UcxBuffer *b);
    68 #ifdef	__cplusplus
    69 }
    70 #endif
    72 #endif	/* BUFFER_H */

mercurial