ucx/buffer.h

changeset 5
88625853ae74
parent 1
1bcaac272cdf
child 17
11dffb40cd91
equal deleted inserted replaced
4:ae5a98f0545c 5:88625853ae74
1 #ifndef BUFFER_H 1 /*
2 #define BUFFER_H 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef UCX_BUFFER_H
30 #define UCX_BUFFER_H
3 31
4 #include "ucx.h" 32 #include "ucx.h"
5 #include <sys/types.h> 33 #include <sys/types.h>
6 #include <stdio.h> 34 #include <stdio.h>
7 35
8 #ifdef __cplusplus 36 #ifdef __cplusplus
9 extern "C" { 37 extern "C" {
10 #endif 38 #endif
11 39
40 /* no autoextend or autofree behaviour */
12 #define UCX_BUFFER_DEFAULT 0x00 41 #define UCX_BUFFER_DEFAULT 0x00
42 /* the buffer shall free the occupied memory space */
13 #define UCX_BUFFER_AUTOFREE 0x01 43 #define UCX_BUFFER_AUTOFREE 0x01
14 /* the buffer may automatically double its size on write operations */ 44 /* the buffer may automatically double its size on write operations */
15 #define UCX_BUFFER_AUTOEXTEND 0x02 45 #define UCX_BUFFER_AUTOEXTEND 0x02
16 46
17 /* the user shall not modify values, but may get the latest pointer */ 47 /* the user shall not modify values, but may get the latest pointer */
42 * 72 *
43 * SEEK_SET marks the start of the buffer 73 * SEEK_SET marks the start of the buffer
44 * SEEK_CUR marks the current position 74 * SEEK_CUR marks the current position
45 * SEEK_END marks the first 0-byte in the buffer 75 * SEEK_END marks the first 0-byte in the buffer
46 * 76 *
47 * ucx_memseek returns 0 on success and -1 if the new position is beyond the 77 * ucx_buffer_seek returns 0 on success and -1 if the new position is beyond the
48 * bounds of the allocated buffer. In that case the position of the buffer 78 * bounds of the allocated buffer. In that case the position of the buffer
49 * remains unchanged. 79 * remains unchanged.
50 * 80 *
51 */ 81 */
52 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence); 82 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence);
83
84 #define ucx_buffer_clear(buffer) memset(buffer->space, 0, buffer->size); \
85 buffer->size = 0; buffer->pos = 0;
53 86
54 /* 87 /*
55 * returns non-zero, if the current buffer position has exceeded the last 88 * returns non-zero, if the current buffer position has exceeded the last
56 * available byte of the underlying buffer 89 * available byte of the underlying buffer
57 * 90 *
65 UcxBuffer *buffer); 98 UcxBuffer *buffer);
66 99
67 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, 100 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems,
68 UcxBuffer *buffer); 101 UcxBuffer *buffer);
69 102
70 /* when autoextend is enabled, ensure you get the latest pointer to the data */
71 //define ucx_buffer_write(data, itemsize, nitems, buffer) \
72 // ucx_bufio(data, itemsize, nitems, buffer, 0)
73 //define ucx_buffer_read(data, itemsize, nitems, buffer) \
74 // ucx_bufio(data, itemsize, nitems, buffer, 1)
75 int ucx_buffer_putc(UcxBuffer *b, int c); 103 int ucx_buffer_putc(UcxBuffer *b, int c);
76 int ucx_buffer_getc(UcxBuffer *b); 104 int ucx_buffer_getc(UcxBuffer *b);
77 105
78 106
79 /* 107 /*
83 * returns the number of bytes copied 111 * returns the number of bytes copied
84 */ 112 */
85 size_t ucx_buffer_generic_copy(void *s1, void *s2, read_func r, write_func w, 113 size_t ucx_buffer_generic_copy(void *s1, void *s2, read_func r, write_func w,
86 size_t bufsize); 114 size_t bufsize);
87 115
116 size_t ucx_buffer_generic_ncopy(void *s1, void *s2, read_func r, write_func w,
117 size_t bufsize, size_t n);
88 118
89 #define UCX_DEFAULT_BUFFER_SIZE 0x4000000 119 #define UCX_DEFAULT_BUFFER_SIZE 0x1000
90 120
91 #define ucx_buffer_copy(s1,s2,r,w) \ 121 #define ucx_buffer_copy(s1,s2,r,w) \
92 ucx_buffer_generic_copy(s1, s2, (read_func)r, (write_func)w, \ 122 ucx_buffer_generic_copy(s1, s2, (read_func)r, (write_func)w, \
93 UCX_DEFAULT_BUFFER_SIZE) 123 UCX_DEFAULT_BUFFER_SIZE)
94 124
125 #define ucx_buffer_ncopy(s1,s2,r,w, n) \
126 ucx_buffer_generic_ncopy(s1, s2, (read_func)r, (write_func)w, \
127 UCX_DEFAULT_BUFFER_SIZE, n)
128
95 #ifdef __cplusplus 129 #ifdef __cplusplus
96 } 130 }
97 #endif 131 #endif
98 132
99 #endif /* BUFFER_H */ 133 #endif /* UCX_BUFFER_H */
100 134

mercurial