Mon, 06 Jan 2025 22:22:55 +0100
update ucx, toolkit
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | /** |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
30 | * @file buffer.h |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | * |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
32 | * @brief Advanced buffer implementation. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | * Instances of CxBuffer can be used to read from or to write to like one |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | * would do with a stream. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | * Some features for convenient use of the buffer |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | * can be enabled. See the documentation of the macro constants for more |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | * information. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | * |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
41 | * @author Mike Becker |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
42 | * @author Olaf Wintermann |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
43 | * @copyright 2-Clause BSD License |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | */ |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | #ifndef UCX_BUFFER_H |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | #define UCX_BUFFER_H |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49 | #include "common.h" |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | #include "allocator.h" |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
52 | #ifdef __cplusplus |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | extern "C" { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54 | #endif |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
55 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
57 | * No buffer features enabled (all flags cleared). |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
58 | */ |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
59 | #define CX_BUFFER_DEFAULT 0x00 |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
61 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62 | * If this flag is enabled, the buffer will automatically free its contents when destroyed. |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
63 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
64 | * Do NOT set this flag together with #CX_BUFFER_COPY_ON_WRITE. It will be automatically |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
65 | * set when the copy-on-write operations is performed. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | */ |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | #define CX_BUFFER_FREE_CONTENTS 0x01 |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | /** |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
70 | * If this flag is enabled, the buffer will automatically extend its capacity. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | */ |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | #define CX_BUFFER_AUTO_EXTEND 0x02 |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
74 | /** |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
75 | * If this flag is enabled, the buffer will allocate new memory when written to. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
76 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
77 | * The current contents of the buffer will be copied to the new memory and the flag |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
78 | * will be cleared while the #CX_BUFFER_FREE_CONTENTS flag will be set automatically. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
79 | */ |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
80 | #define CX_BUFFER_COPY_ON_WRITE 0x04 |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
81 | |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
82 | /** |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
83 | * If this flag is enabled, the buffer will copy its contents to a new memory area on reallocation. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
84 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
85 | * After performing the copy, the flag is automatically cleared. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
86 | * This flag has no effect on buffers which do not have #CX_BUFFER_AUTO_EXTEND set, which is why |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
87 | * buffers automatically admit the auto-extend flag when initialized with copy-on-extend enabled. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
88 | */ |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
89 | #define CX_BUFFER_COPY_ON_EXTEND 0x08 |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
90 | |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
91 | /** |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
92 | * Configuration for automatic flushing. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
93 | */ |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
94 | struct cx_buffer_flush_config_s { |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
95 | /** |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
96 | * The buffer may not extend beyond this threshold before starting to flush. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
97 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
98 | * Only used when the buffer uses #CX_BUFFER_AUTO_EXTEND. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
99 | * The threshold will be the maximum capacity the buffer is extended to |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
100 | * before flushing. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
101 | */ |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
102 | size_t threshold; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
103 | /** |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
104 | * The block size for the elements to flush. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
105 | */ |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
106 | size_t blksize; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
107 | /** |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
108 | * The maximum number of blocks to flush in one cycle. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
109 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
110 | * @attention while it is guaranteed that cxBufferFlush() will not flush |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
111 | * more blocks, this is not necessarily the case for cxBufferWrite(). |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
112 | * After performing a flush cycle, cxBufferWrite() will retry the write |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
113 | * operation and potentially trigger another flush cycle, until the |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
114 | * flush target accepts no more data. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
115 | */ |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
116 | size_t blkmax; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
117 | |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
118 | /** |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
119 | * The target for write function. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
120 | */ |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
121 | void *target; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
122 | |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
123 | /** |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
124 | * The write-function used for flushing. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
125 | * If NULL, the flushed content gets discarded. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
126 | */ |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
127 | cx_write_func wfunc; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
128 | }; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
129 | |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
130 | /** |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
131 | * Type alais for the flush configuration struct. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
132 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
133 | * @code |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
134 | * struct cx_buffer_flush_config_s { |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
135 | * size_t threshold; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
136 | * size_t blksize; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
137 | * size_t blkmax; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
138 | * void *target; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
139 | * cx_write_func wfunc; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
140 | * }; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
141 | * @endcode |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
142 | */ |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
143 | typedef struct cx_buffer_flush_config_s CxBufferFlushConfig; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
144 | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
145 | /** Structure for the UCX buffer data. */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
146 | struct cx_buffer_s { |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
147 | /** A pointer to the buffer contents. */ |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
148 | union { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
149 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
150 | * Data is interpreted as text. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
151 | */ |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
152 | char *space; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
153 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
154 | * Data is interpreted as binary. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
155 | */ |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
156 | unsigned char *bytes; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
157 | }; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
158 | /** The allocator to use for automatic memory management. */ |
49
2f71f4ee247a
update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
159 | const CxAllocator *allocator; |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
160 | /** |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
161 | * Optional flush configuration |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
162 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
163 | * @see cxBufferEnableFlushing() |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
164 | */ |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
165 | CxBufferFlushConfig* flush; |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
166 | /** Current position of the buffer. */ |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
167 | size_t pos; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
168 | /** Current capacity (i.e. maximum size) of the buffer. */ |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
169 | size_t capacity; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
170 | /** Current size of the buffer content. */ |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
171 | size_t size; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
172 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
173 | * Flag register for buffer features. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
174 | * @see #CX_BUFFER_DEFAULT |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
175 | * @see #CX_BUFFER_FREE_CONTENTS |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
176 | * @see #CX_BUFFER_AUTO_EXTEND |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
177 | * @see #CX_BUFFER_COPY_ON_WRITE |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178 | */ |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
179 | int flags; |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
180 | }; |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
181 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
182 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
183 | * UCX buffer. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
184 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
185 | typedef struct cx_buffer_s CxBuffer; |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
186 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
187 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
188 | * Initializes a fresh buffer. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
189 | * |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
190 | * You may also provide a read-only @p space, in which case |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
191 | * you will need to cast the pointer, and you should set the |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
192 | * #CX_BUFFER_COPY_ON_WRITE flag. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
193 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
194 | * You need to set the size manually after initialization, if |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
195 | * you provide @p space which already contains data. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
196 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
197 | * When you specify stack memory as @p space and decide to use |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
198 | * the auto-extension feature, you @em must use the |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
199 | * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
200 | * #CX_BUFFER_AUTO_EXTEND flag. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
201 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
202 | * @note You may provide @c NULL as argument for @p space. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
203 | * Then this function will allocate the space and enforce |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
204 | * the #CX_BUFFER_FREE_CONTENTS flag. In that case, specifying |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
205 | * copy-on-write should be avoided, because the allocated |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
206 | * space will be leaking after the copy-on-write operation. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
207 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
208 | * @param buffer the buffer to initialize |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
209 | * @param space pointer to the memory area, or @c NULL to allocate |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
210 | * new memory |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
211 | * @param capacity the capacity of the buffer |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
212 | * @param allocator the allocator this buffer shall use for automatic |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
213 | * memory management |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
214 | * (if @c NULL, a default stdlib allocator will be used) |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
215 | * @param flags buffer features (see cx_buffer_s.flags) |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
216 | * @return zero on success, non-zero if a required allocation failed |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
217 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
218 | cx_attr_nonnull_arg(1) |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
219 | int cxBufferInit( |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
220 | CxBuffer *buffer, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
221 | void *space, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
222 | size_t capacity, |
49
2f71f4ee247a
update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
223 | const CxAllocator *allocator, |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
224 | int flags |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
225 | ); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
226 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
227 | /** |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
228 | * Configures the buffer for flushing. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
229 | * |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
230 | * Flushing can happen automatically when data is written |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
231 | * to the buffer (see cxBufferWrite()) or manually when |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
232 | * cxBufferFlush() is called. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
233 | * |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
234 | * @param buffer the buffer |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
235 | * @param config the flush configuration |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
236 | * @retval zero success |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
237 | * @retval non-zero failure |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
238 | * @see cxBufferFlush() |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
239 | * @see cxBufferWrite() |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
240 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
241 | cx_attr_nonnull |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
242 | int cxBufferEnableFlushing( |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
243 | CxBuffer *buffer, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
244 | CxBufferFlushConfig config |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
245 | ); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
246 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
247 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
248 | * Destroys the buffer contents. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
249 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
250 | * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
251 | * If you want to free the memory of the entire buffer, use cxBufferFree(). |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
252 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
253 | * @param buffer the buffer which contents shall be destroyed |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
254 | * @see cxBufferInit() |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
255 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
256 | cx_attr_nonnull |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
257 | void cxBufferDestroy(CxBuffer *buffer); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
258 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
259 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
260 | * Deallocates the buffer. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
261 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
262 | * If the #CX_BUFFER_FREE_CONTENTS feature is enabled, this function also destroys |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
263 | * the contents. If you @em only want to destroy the contents, use cxBufferDestroy(). |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
264 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
265 | * @remark As with all free() functions, this accepts @c NULL arguments in which |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
266 | * case it does nothing. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
267 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
268 | * @param buffer the buffer to deallocate |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
269 | * @see cxBufferCreate() |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
270 | */ |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
271 | void cxBufferFree(CxBuffer *buffer); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
272 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
273 | /** |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
274 | * Allocates and initializes a fresh buffer. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
275 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
276 | * You may also provide a read-only @p space, in which case |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
277 | * you will need to cast the pointer, and you should set the |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
278 | * #CX_BUFFER_COPY_ON_WRITE flag. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
279 | * When you specify stack memory as @p space and decide to use |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
280 | * the auto-extension feature, you @em must use the |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
281 | * #CX_BUFFER_COPY_ON_EXTEND flag, instead of the |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
282 | * #CX_BUFFER_AUTO_EXTEND flag. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
283 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
284 | * @note You may provide @c NULL as argument for @p space. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
285 | * Then this function will allocate the space and enforce |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
286 | * the #CX_BUFFER_FREE_CONTENTS flag. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
287 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
288 | * @param space pointer to the memory area, or @c NULL to allocate |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
289 | * new memory |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
290 | * @param capacity the capacity of the buffer |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
291 | * @param allocator the allocator to use for allocating the structure and the automatic |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
292 | * memory management within the buffer |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
293 | * (if @c NULL, a default stdlib allocator will be used) |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
294 | * @param flags buffer features (see cx_buffer_s.flags) |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
295 | * @return a pointer to the buffer on success, @c NULL if a required allocation failed |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
296 | */ |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
297 | cx_attr_malloc |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
298 | cx_attr_dealloc(cxBufferFree, 1) |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
299 | cx_attr_nodiscard |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
300 | CxBuffer *cxBufferCreate( |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
301 | void *space, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
302 | size_t capacity, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
303 | const CxAllocator *allocator, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
304 | int flags |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
305 | ); |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
306 | |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
307 | /** |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
308 | * Shifts the contents of the buffer by the given offset. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
309 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
310 | * If the offset is positive, the contents are shifted to the right. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
311 | * If auto extension is enabled, the buffer grows, if necessary. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
312 | * In case the auto extension fails, this function returns a non-zero value and |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
313 | * no contents are changed. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
314 | * If auto extension is disabled, the contents that do not fit into the buffer |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
315 | * are discarded. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
316 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
317 | * If the offset is negative, the contents are shifted to the left where the |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
318 | * first @p shift bytes are discarded. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
319 | * The new size of the buffer is the old size minus the absolute shift value. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
320 | * If this value is larger than the buffer size, the buffer is emptied (but |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
321 | * not cleared, see the security note below). |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
322 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
323 | * The buffer position gets shifted alongside with the content but is kept |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
324 | * within the boundaries of the buffer. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
325 | * |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
326 | * @note For situations where @c off_t is not large enough, there are specialized cxBufferShiftLeft() and |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
327 | * cxBufferShiftRight() functions using a @c size_t as parameter type. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
328 | * |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
329 | * @attention |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
330 | * Security Note: The shifting operation does @em not erase the previously occupied memory cells. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
331 | * But you can easily do that manually, e.g. by calling |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
332 | * <code>memset(buffer->bytes, 0, shift)</code> for a right shift or |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
333 | * <code>memset(buffer->bytes + buffer->size, 0, buffer->capacity - buffer->size)</code> |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
334 | * for a left shift. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
335 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
336 | * @param buffer the buffer |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
337 | * @param shift the shift offset (negative means left shift) |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
338 | * @retval zero success |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
339 | * @retval non-zero if a required auto-extension or copy-on-write fails |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
340 | * @see cxBufferShiftLeft() |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
341 | * @see cxBufferShiftRight() |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
342 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
343 | cx_attr_nonnull |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
344 | int cxBufferShift( |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
345 | CxBuffer *buffer, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
346 | off_t shift |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
347 | ); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
348 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
349 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
350 | * Shifts the buffer to the right. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
351 | * See cxBufferShift() for details. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
352 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
353 | * @param buffer the buffer |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
354 | * @param shift the shift offset |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
355 | * @retval zero success |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
356 | * @retval non-zero if a required auto-extension or copy-on-write fails |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
357 | * @see cxBufferShift() |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
358 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
359 | cx_attr_nonnull |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
360 | int cxBufferShiftRight( |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
361 | CxBuffer *buffer, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
362 | size_t shift |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
363 | ); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
364 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
365 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
366 | * Shifts the buffer to the left. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
367 | * See cxBufferShift() for details. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
368 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
369 | * @param buffer the buffer |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
370 | * @param shift the positive shift offset |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
371 | * @retval zero success |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
372 | * @retval non-zero if the buffer uses copy-on-write and the allocation fails |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
373 | * @see cxBufferShift() |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
374 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
375 | cx_attr_nonnull |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
376 | int cxBufferShiftLeft( |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
377 | CxBuffer *buffer, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
378 | size_t shift |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
379 | ); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
380 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
381 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
382 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
383 | * Moves the position of the buffer. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
384 | * |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
385 | * The new position is relative to the @p whence argument. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
386 | * |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
387 | * @li @c SEEK_SET marks the start of the buffer. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
388 | * @li @c SEEK_CUR marks the current position. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
389 | * @li @c SEEK_END marks the end of the buffer. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
390 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
391 | * With an offset of zero, this function sets the buffer position to zero |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
392 | * (@c SEEK_SET), the buffer size (@c SEEK_END) or leaves the buffer position |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
393 | * unchanged (@c SEEK_CUR). |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
394 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
395 | * @param buffer the buffer |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
396 | * @param offset position offset relative to @p whence |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
397 | * @param whence one of @c SEEK_SET, @c SEEK_CUR or @c SEEK_END |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
398 | * @retval zero success |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
399 | * @retval non-zero if the position is invalid |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
400 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
401 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
402 | cx_attr_nonnull |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
403 | int cxBufferSeek( |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
404 | CxBuffer *buffer, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
405 | off_t offset, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
406 | int whence |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
407 | ); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
408 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
409 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
410 | * Clears the buffer by resetting the position and deleting the data. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
411 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
412 | * The data is deleted by zeroing it with a call to memset(). |
2
fbdfaacc4182
update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
413 | * If you do not need that, you can use the faster cxBufferReset(). |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
414 | * |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
415 | * @note If the #CX_BUFFER_COPY_ON_WRITE flag is set, this function |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
416 | * will not erase the data and behave exactly as cxBufferReset(). |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
417 | * |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
418 | * @param buffer the buffer to be cleared |
2
fbdfaacc4182
update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
419 | * @see cxBufferReset() |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
420 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
421 | cx_attr_nonnull |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
422 | void cxBufferClear(CxBuffer *buffer); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
423 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
424 | /** |
2
fbdfaacc4182
update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
425 | * Resets the buffer by resetting the position and size to zero. |
fbdfaacc4182
update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
426 | * |
fbdfaacc4182
update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
427 | * The data in the buffer is not deleted. If you need a safe |
fbdfaacc4182
update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
428 | * reset of the buffer, use cxBufferClear(). |
fbdfaacc4182
update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
429 | * |
fbdfaacc4182
update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
430 | * @param buffer the buffer to be cleared |
fbdfaacc4182
update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
431 | * @see cxBufferClear() |
fbdfaacc4182
update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
432 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
433 | cx_attr_nonnull |
2
fbdfaacc4182
update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
434 | void cxBufferReset(CxBuffer *buffer); |
fbdfaacc4182
update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
435 | |
fbdfaacc4182
update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
436 | /** |
fbdfaacc4182
update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
437 | * Tests, if the buffer position has exceeded the buffer size. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
438 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
439 | * @param buffer the buffer to test |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
440 | * @retval true if the current buffer position has exceeded the last |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
441 | * byte of the buffer's contents |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
442 | * @retval false otherwise |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
443 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
444 | cx_attr_nonnull |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
445 | cx_attr_nodiscard |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
446 | bool cxBufferEof(const CxBuffer *buffer); |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
447 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
448 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
449 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
450 | * Ensures that the buffer has a minimum capacity. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
451 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
452 | * If the current capacity is not sufficient, the buffer will be extended. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
453 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
454 | * @param buffer the buffer |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
455 | * @param capacity the minimum required capacity for this buffer |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
456 | * @retval zero the capacity was already sufficient or successfully increased |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
457 | * @retval non-zero on allocation failure |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
458 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
459 | cx_attr_nonnull |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
460 | int cxBufferMinimumCapacity( |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
461 | CxBuffer *buffer, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
462 | size_t capacity |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
463 | ); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
464 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
465 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
466 | * Writes data to a CxBuffer. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
467 | * |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
468 | * If automatic flushing is not enabled, the data is simply written into the |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
469 | * buffer at the current position and the position of the buffer is increased |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
470 | * by the number of bytes written. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
471 | * |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
472 | * If flushing is enabled and the buffer needs to flush, the data is flushed to |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
473 | * the target until the target signals that it cannot take more data by |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
474 | * returning zero via the respective write function. In that case, the remaining |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
475 | * data in this buffer is shifted to the beginning of this buffer so that the |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
476 | * newly available space can be used to append as much data as possible. This |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
477 | * function only stops writing more elements, when the flush target and this |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
478 | * buffer are both incapable of taking more data or all data has been written. |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
479 | * If number of items that shall be written is larger than the buffer can hold, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
480 | * the first items from @c ptr are directly relayed to the flush target, if |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
481 | * possible. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
482 | * The number returned by this function is only the number of elements from |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
483 | * @c ptr that could be written to either the flush target or the buffer. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
484 | * |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
485 | * @note The signature is compatible with the fwrite() family of functions. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
486 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
487 | * @param ptr a pointer to the memory area containing the bytes to be written |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
488 | * @param size the length of one element |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
489 | * @param nitems the element count |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
490 | * @param buffer the CxBuffer to write to |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
491 | * @return the total count of elements written |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
492 | * @see cxBufferAppend() |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
493 | * @see cxBufferRead() |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
494 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
495 | cx_attr_nonnull |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
496 | size_t cxBufferWrite( |
49
2f71f4ee247a
update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
497 | const void *ptr, |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
498 | size_t size, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
499 | size_t nitems, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
500 | CxBuffer *buffer |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
501 | ); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
502 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
503 | /** |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
504 | * Appends data to a CxBuffer. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
505 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
506 | * The data is always appended to current data within the buffer, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
507 | * regardless of the current position. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
508 | * This is especially useful when the buffer is primarily meant for reading |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
509 | * while additional data is added to the buffer occasionally. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
510 | * Consequently, the position of the buffer is unchanged after this operation. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
511 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
512 | * @note The signature is compatible with the fwrite() family of functions. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
513 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
514 | * @param ptr a pointer to the memory area containing the bytes to be written |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
515 | * @param size the length of one element |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
516 | * @param nitems the element count |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
517 | * @param buffer the CxBuffer to write to |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
518 | * @return the total count of elements written |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
519 | * @see cxBufferWrite() |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
520 | * @see cxBufferRead() |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
521 | */ |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
522 | cx_attr_nonnull |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
523 | size_t cxBufferAppend( |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
524 | const void *ptr, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
525 | size_t size, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
526 | size_t nitems, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
527 | CxBuffer *buffer |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
528 | ); |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
529 | |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
530 | /** |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
531 | * Performs a single flush-run on the specified buffer. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
532 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
533 | * Does nothing when the position in the buffer is zero. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
534 | * Otherwise, the data until the current position minus |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
535 | * one is considered for flushing. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
536 | * Note carefully that flushing will never exceed the |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
537 | * current @em position, even when the size of the |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
538 | * buffer is larger than the current position. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
539 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
540 | * One flush run will try to flush @c blkmax many |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
541 | * blocks of size @c blksize until either the @p buffer |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
542 | * has no more data to flush or the write function |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
543 | * used for flushing returns zero. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
544 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
545 | * The buffer is shifted left for that many bytes |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
546 | * the flush operation has successfully flushed. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
547 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
548 | * @par Example 1 |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
549 | * Assume you have a buffer with size 340 and you are |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
550 | * at position 200. The flush configuration is |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
551 | * @c blkmax=4 and @c blksize=64 . |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
552 | * Assume that the entire flush operation is successful. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
553 | * All 200 bytes on the left hand-side from the current |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
554 | * position are written. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
555 | * That means, the size of the buffer is now 140 and the |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
556 | * position is zero. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
557 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
558 | * @par Example 2 |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
559 | * Same as Example 1, but now the @c blkmax is 1. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
560 | * The size of the buffer is now 276 and the position is 136. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
561 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
562 | * @par Example 3 |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
563 | * Same as Example 1, but now assume the flush target |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
564 | * only accepts 100 bytes before returning zero. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
565 | * That means, the flush operations manages to flush |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
566 | * one complete block and one partial block, ending |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
567 | * up with a buffer with size 240 and position 100. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
568 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
569 | * @remark Just returns zero when flushing was not enabled with |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
570 | * cxBufferEnableFlushing(). |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
571 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
572 | * @remark When the buffer uses copy-on-write, the memory |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
573 | * is copied first, before attempting any flush. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
574 | * This is, however, considered an erroneous use of the |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
575 | * buffer, because it does not make much sense to put |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
576 | * readonly data into an UCX buffer for flushing, instead |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
577 | * of writing it directly to the target. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
578 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
579 | * @param buffer the buffer |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
580 | * @return the number of successfully flushed bytes |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
581 | * @see cxBufferEnableFlushing() |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
582 | */ |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
583 | cx_attr_nonnull |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
584 | size_t cxBufferFlush(CxBuffer *buffer); |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
585 | |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
586 | /** |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
587 | * Reads data from a CxBuffer. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
588 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
589 | * The position of the buffer is increased by the number of bytes read. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
590 | * |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
591 | * @note The signature is compatible with the fread() family of functions. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
592 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
593 | * @param ptr a pointer to the memory area where to store the read data |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
594 | * @param size the length of one element |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
595 | * @param nitems the element count |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
596 | * @param buffer the CxBuffer to read from |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
597 | * @return the total number of elements read |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
598 | * @see cxBufferWrite() |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
599 | * @see cxBufferAppend() |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
600 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
601 | cx_attr_nonnull |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
602 | size_t cxBufferRead( |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
603 | void *ptr, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
604 | size_t size, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
605 | size_t nitems, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
606 | CxBuffer *buffer |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
607 | ); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
608 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
609 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
610 | * Writes a character to a buffer. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
611 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
612 | * The least significant byte of the argument is written to the buffer. If the |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
613 | * end of the buffer is reached and #CX_BUFFER_AUTO_EXTEND feature is enabled, |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
614 | * the buffer capacity is extended by cxBufferMinimumCapacity(). If the feature |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
615 | * is disabled or buffer extension fails, @c EOF is returned. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
616 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
617 | * On successful write, the position of the buffer is increased. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
618 | * |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
619 | * If you just want to write a null-terminator at the current position, you |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
620 | * should use cxBufferTerminate() instead. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
621 | * |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
622 | * @param buffer the buffer to write to |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
623 | * @param c the character to write |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
624 | * @return the byte that has been written or @c EOF when the end of the stream is |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
625 | * reached and automatic extension is not enabled or not possible |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
626 | * @see cxBufferTerminate() |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
627 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
628 | cx_attr_nonnull |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
629 | int cxBufferPut( |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
630 | CxBuffer *buffer, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
631 | int c |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
632 | ); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
633 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
634 | /** |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
635 | * Writes a terminating zero to a buffer at the current position. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
636 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
637 | * On successful write, @em neither the position @em nor the size of the buffer is |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
638 | * increased. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
639 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
640 | * The purpose of this function is to have the written data ready to be used as |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
641 | * a C string. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
642 | * |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
643 | * @param buffer the buffer to write to |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
644 | * @return zero, if the terminator could be written, non-zero otherwise |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
645 | */ |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
646 | cx_attr_nonnull |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
647 | int cxBufferTerminate(CxBuffer *buffer); |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
648 | |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
649 | /** |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
650 | * Writes a string to a buffer. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
651 | * |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
652 | * This is a convenience function for <code>cxBufferWrite(str, 1, strlen(str), buffer)</code>. |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
653 | * |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
654 | * @param buffer the buffer |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
655 | * @param str the zero-terminated string |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
656 | * @return the number of bytes written |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
657 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
658 | cx_attr_nonnull |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
659 | cx_attr_cstr_arg(2) |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
660 | size_t cxBufferPutString( |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
661 | CxBuffer *buffer, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
662 | const char *str |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
663 | ); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
664 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
665 | /** |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
666 | * Gets a character from a buffer. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
667 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
668 | * The current position of the buffer is increased after a successful read. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
669 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
670 | * @param buffer the buffer to read from |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
671 | * @return the character or @c EOF, if the end of the buffer is reached |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
672 | */ |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
673 | cx_attr_nonnull |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
674 | int cxBufferGet(CxBuffer *buffer); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
675 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
676 | #ifdef __cplusplus |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
677 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
678 | #endif |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
679 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
680 | #endif // UCX_BUFFER_H |