| 14 /* the buffer may automatically double its size on write operations */ |
14 /* the buffer may automatically double its size on write operations */ |
| 15 #define UCX_BUFFER_AUTOEXTEND 0x02 |
15 #define UCX_BUFFER_AUTOEXTEND 0x02 |
| 16 |
16 |
| 17 /* the user shall not modify values, but may get the latest pointer */ |
17 /* the user shall not modify values, but may get the latest pointer */ |
| 18 typedef struct { |
18 typedef struct { |
| 19 void *space; |
19 char *space; |
| 20 size_t pos; |
20 size_t pos; |
| |
21 size_t capacity; |
| 21 size_t size; |
22 size_t size; |
| 22 int flags; |
23 int flags; |
| 23 } UcxBuffer; |
24 } UcxBuffer; |
| 24 |
25 |
| 25 /* if space is NULL, new space is allocated and the autofree flag is enforced */ |
26 /* if space is NULL, new space is allocated and the autofree flag is enforced */ |
| 26 UcxBuffer *ucx_buffer_new(void *space, size_t length, int flags); |
27 UcxBuffer *ucx_buffer_new(void *space, size_t size, int flags); |
| 27 void ucx_buffer_free(UcxBuffer* buffer); |
28 void ucx_buffer_free(UcxBuffer* buffer); |
| 28 |
29 |
| 29 /* |
30 /* |
| 30 * the autofree flag is enforced for the new buffer |
31 * the autofree flag is enforced for the new buffer |
| 31 * if length is zero, the whole remaining buffer shall be extracted |
32 * if length is zero, the whole remaining buffer shall be extracted |
| 32 * the position of the new buffer is set to zero |
33 * the position of the new buffer is set to zero |
| 33 */ |
34 */ |
| 34 UcxBuffer *restrict ucx_buffer_extract(UcxBuffer *restrict src, |
35 UcxBuffer* ucx_buffer_extract(UcxBuffer *src, |
| 35 size_t start, size_t length, int flags); |
36 size_t start, size_t length, int flags); |
| 36 #define ucx_buffer_clone(src,flags) \ |
37 #define ucx_buffer_clone(src,flags) \ |
| 37 ucx_buffer_extract(src, 0, 0, flags) |
38 ucx_buffer_extract(src, 0, 0, flags) |
| 38 |
39 |
| 39 /* |
40 /* |
| 49 * |
50 * |
| 50 */ |
51 */ |
| 51 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence); |
52 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence); |
| 52 |
53 |
| 53 /* |
54 /* |
| 54 * returns non-zero, iff the current buffer position has exceeded the last |
55 * returns non-zero, if the current buffer position has exceeded the last |
| 55 * available byte of the underlying buffer |
56 * available byte of the underlying buffer |
| 56 * |
57 * |
| 57 */ |
58 */ |
| 58 int ucx_buffer_eof(UcxBuffer *buffer); |
59 int ucx_buffer_eof(UcxBuffer *buffer); |
| 59 |
60 |
| 60 size_t ucx_bufio(void *d, size_t s, size_t n, UcxBuffer* b, _Bool read); |
61 |
| |
62 int ucx_buffere_extend(UcxBuffer *buffer, size_t len); |
| |
63 |
| |
64 size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, |
| |
65 UcxBuffer *buffer); |
| |
66 |
| |
67 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, |
| |
68 UcxBuffer *buffer); |
| |
69 |
| 61 /* when autoextend is enabled, ensure you get the latest pointer to the data */ |
70 /* when autoextend is enabled, ensure you get the latest pointer to the data */ |
| 62 #define ucx_buffer_write(data, itemsize, nitems, buffer) \ |
71 //define ucx_buffer_write(data, itemsize, nitems, buffer) \ |
| 63 ucx_bufio(data, itemsize, nitems, buffer, 0) |
72 // ucx_bufio(data, itemsize, nitems, buffer, 0) |
| 64 #define ucx_buffer_read(data, itemsize, nitems, buffer) \ |
73 //define ucx_buffer_read(data, itemsize, nitems, buffer) \ |
| 65 ucx_bufio(data, itemsize, nitems, buffer, 1) |
74 // ucx_bufio(data, itemsize, nitems, buffer, 1) |
| 66 int ucx_buffer_putc(UcxBuffer *b, int c); |
75 int ucx_buffer_putc(UcxBuffer *b, int c); |
| 67 int ucx_buffer_getc(UcxBuffer *b); |
76 int ucx_buffer_getc(UcxBuffer *b); |
| |
77 |
| |
78 |
| |
79 /* |
| |
80 * copies all bytes from s1 to s2 |
| |
81 * uses the read function r to read from s1 und writes the data using the |
| |
82 * write function w to s2 |
| |
83 * returns the number of bytes copied |
| |
84 */ |
| |
85 size_t ucx_buffer_generic_copy(void *s1, void *s2, read_func r, write_func w, |
| |
86 size_t bufsize); |
| |
87 |
| |
88 |
| |
89 #define UCX_DEFAULT_BUFFER_SIZE 0x4000000 |
| |
90 |
| |
91 #define ucx_buffer_copy(s1,s2,r,w) \ |
| |
92 ucx_buffer_generic_copy(s1, s2, (read_func)r, (write_func)w, \ |
| |
93 UCX_DEFAULT_BUFFER_SIZE) |
| 68 |
94 |
| 69 #ifdef __cplusplus |
95 #ifdef __cplusplus |
| 70 } |
96 } |
| 71 #endif |
97 #endif |
| 72 |
98 |