src/ucx/cx/buffer.h

Sun, 30 Nov 2025 18:25:55 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 30 Nov 2025 18:25:55 +0100
changeset 645
0c85c4cd0dd8
parent 621
956c03c25edd
permissions
-rw-r--r--

update ucx to version 3.2

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

mercurial