Fri, 17 Oct 2025 15:44:30 +0200
add support for selectable source list header rows (GTK)
| 174 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * | |
| 9 | * 1. Redistributions of source code must retain the above copyright | |
| 10 | * notice, this list of conditions and the following disclaimer. | |
| 11 | * | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer in the | |
| 14 | * documentation and/or other materials provided with the distribution. | |
| 15 | * | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | * POSSIBILITY OF SUCH DAMAGE. | |
| 27 | */ | |
| 28 | ||
| 29 | #include "cx/buffer.h" | |
| 30 | ||
| 31 | #include <stdio.h> | |
| 32 | #include <string.h> | |
| 440 | 33 | #include <errno.h> |
| 34 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
35 | #ifdef _WIN32 |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
36 | #include <Windows.h> |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
37 | #include <sysinfoapi.h> |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
38 | static unsigned long system_page_size() { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
39 | static unsigned long ps = 0; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
40 | if (ps == 0) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
41 | SYSTEM_INFO sysinfo; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
42 | GetSystemInfo(&sysinfo); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
43 | ps = sysinfo.dwPageSize; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
44 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
45 | return ps; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
46 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
47 | #define SYSTEM_PAGE_SIZE system_page_size() |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
48 | #else |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
49 | #include <unistd.h> |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
50 | #define SYSTEM_PAGE_SIZE sysconf(_SC_PAGESIZE) |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
51 | #endif |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
52 | |
| 440 | 53 | static int buffer_copy_on_write(CxBuffer* buffer) { |
| 54 | if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) return 0; | |
| 55 | void *newspace = cxMalloc(buffer->allocator, buffer->capacity); | |
| 56 | if (NULL == newspace) return -1; | |
| 57 | memcpy(newspace, buffer->space, buffer->size); | |
| 58 | buffer->space = newspace; | |
| 59 | buffer->flags &= ~CX_BUFFER_COPY_ON_WRITE; | |
| 60 | buffer->flags |= CX_BUFFER_FREE_CONTENTS; | |
| 61 | return 0; | |
| 62 | } | |
| 174 | 63 | |
| 64 | int cxBufferInit( | |
| 65 | CxBuffer *buffer, | |
| 66 | void *space, | |
| 67 | size_t capacity, | |
| 324 | 68 | const CxAllocator *allocator, |
| 174 | 69 | int flags |
| 70 | ) { | |
| 440 | 71 | if (allocator == NULL) { |
| 72 | allocator = cxDefaultAllocator; | |
| 73 | } | |
| 74 | if (flags & CX_BUFFER_COPY_ON_EXTEND) { | |
| 75 | flags |= CX_BUFFER_AUTO_EXTEND; | |
| 76 | } | |
| 174 | 77 | buffer->allocator = allocator; |
| 78 | buffer->flags = flags; | |
| 79 | if (!space) { | |
| 80 | buffer->bytes = cxMalloc(allocator, capacity); | |
| 81 | if (buffer->bytes == NULL) { | |
| 440 | 82 | return -1; // LCOV_EXCL_LINE |
| 174 | 83 | } |
| 84 | buffer->flags |= CX_BUFFER_FREE_CONTENTS; | |
| 85 | } else { | |
| 86 | buffer->bytes = space; | |
| 87 | } | |
| 88 | buffer->capacity = capacity; | |
| 89 | buffer->size = 0; | |
| 90 | buffer->pos = 0; | |
| 91 | ||
| 440 | 92 | buffer->flush = NULL; |
| 174 | 93 | |
| 94 | return 0; | |
| 95 | } | |
| 96 | ||
| 440 | 97 | int cxBufferEnableFlushing( |
| 98 | CxBuffer *buffer, | |
| 99 | CxBufferFlushConfig config | |
| 100 | ) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
101 | buffer->flush = cxMallocDefault(sizeof(CxBufferFlushConfig)); |
| 440 | 102 | if (buffer->flush == NULL) return -1; // LCOV_EXCL_LINE |
| 103 | memcpy(buffer->flush, &config, sizeof(CxBufferFlushConfig)); | |
| 104 | return 0; | |
| 105 | } | |
| 106 | ||
| 174 | 107 | void cxBufferDestroy(CxBuffer *buffer) { |
| 440 | 108 | if (buffer->flags & CX_BUFFER_FREE_CONTENTS) { |
| 174 | 109 | cxFree(buffer->allocator, buffer->bytes); |
| 110 | } | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
111 | cxFreeDefault(buffer->flush); |
| 440 | 112 | memset(buffer, 0, sizeof(CxBuffer)); |
| 174 | 113 | } |
| 114 | ||
| 115 | CxBuffer *cxBufferCreate( | |
| 116 | void *space, | |
| 117 | size_t capacity, | |
| 324 | 118 | const CxAllocator *allocator, |
| 174 | 119 | int flags |
| 120 | ) { | |
| 440 | 121 | if (allocator == NULL) { |
| 122 | allocator = cxDefaultAllocator; | |
| 123 | } | |
| 174 | 124 | CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer)); |
| 125 | if (buf == NULL) return NULL; | |
| 126 | if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) { | |
| 127 | return buf; | |
| 128 | } else { | |
| 440 | 129 | // LCOV_EXCL_START |
| 174 | 130 | cxFree(allocator, buf); |
| 131 | return NULL; | |
| 440 | 132 | // LCOV_EXCL_STOP |
| 174 | 133 | } |
| 134 | } | |
| 135 | ||
| 136 | void cxBufferFree(CxBuffer *buffer) { | |
| 440 | 137 | if (buffer == NULL) return; |
| 138 | const CxAllocator *allocator = buffer->allocator; | |
| 139 | cxBufferDestroy(buffer); | |
| 140 | cxFree(allocator, buffer); | |
| 174 | 141 | } |
| 142 | ||
| 143 | int cxBufferSeek( | |
| 144 | CxBuffer *buffer, | |
| 145 | off_t offset, | |
| 146 | int whence | |
| 147 | ) { | |
| 148 | size_t npos; | |
| 149 | switch (whence) { | |
| 150 | case SEEK_CUR: | |
| 151 | npos = buffer->pos; | |
| 152 | break; | |
| 153 | case SEEK_END: | |
| 154 | npos = buffer->size; | |
| 155 | break; | |
| 156 | case SEEK_SET: | |
| 157 | npos = 0; | |
| 158 | break; | |
| 159 | default: | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
160 | errno = EINVAL; |
| 174 | 161 | return -1; |
| 162 | } | |
| 163 | ||
| 164 | size_t opos = npos; | |
| 165 | npos += offset; | |
| 166 | ||
| 167 | if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
168 | // to be compliant with fseek() specification |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
169 | // we return EINVAL on underflow |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
170 | errno = EINVAL; |
| 174 | 171 | return -1; |
| 172 | } | |
| 173 | ||
| 440 | 174 | if (npos > buffer->size) { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
175 | // not compliant with fseek() specification |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
176 | // but this is the better behavior for CxBuffer |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
177 | errno = EINVAL; |
| 174 | 178 | return -1; |
| 179 | } else { | |
| 180 | buffer->pos = npos; | |
| 181 | return 0; | |
| 182 | } | |
| 183 | ||
| 184 | } | |
| 185 | ||
| 186 | void cxBufferClear(CxBuffer *buffer) { | |
| 440 | 187 | if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) { |
| 188 | memset(buffer->bytes, 0, buffer->size); | |
| 189 | } | |
| 174 | 190 | buffer->size = 0; |
| 191 | buffer->pos = 0; | |
| 192 | } | |
| 193 | ||
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
194 | void cxBufferReset(CxBuffer *buffer) { |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
195 | buffer->size = 0; |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
196 | buffer->pos = 0; |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
197 | } |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
198 | |
| 440 | 199 | bool cxBufferEof(const CxBuffer *buffer) { |
| 174 | 200 | return buffer->pos >= buffer->size; |
| 201 | } | |
| 202 | ||
| 203 | int cxBufferMinimumCapacity( | |
| 204 | CxBuffer *buffer, | |
| 205 | size_t newcap | |
| 206 | ) { | |
| 207 | if (newcap <= buffer->capacity) { | |
| 208 | return 0; | |
| 209 | } | |
| 210 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
211 | unsigned long pagesize = SYSTEM_PAGE_SIZE; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
212 | // if page size is larger than 64 KB - for some reason - truncate to 64 KB |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
213 | if (pagesize > 65536) pagesize = 65536; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
214 | if (newcap < pagesize) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
215 | // when smaller as one page, map to the next power of two |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
216 | newcap--; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
217 | newcap |= newcap >> 1; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
218 | newcap |= newcap >> 2; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
219 | newcap |= newcap >> 4; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
220 | // last operation only needed for pages larger 4096 bytes |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
221 | // but if/else would be more expensive than just doing this |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
222 | newcap |= newcap >> 8; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
223 | newcap++; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
224 | } else { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
225 | // otherwise, map to a multiple of the page size |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
226 | newcap -= newcap % pagesize; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
227 | newcap += pagesize; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
228 | // note: if newcap is already page aligned, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
229 | // this gives a full additional page (which is good) |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
230 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
231 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
232 | |
| 440 | 233 | const int force_copy_flags = CX_BUFFER_COPY_ON_WRITE | CX_BUFFER_COPY_ON_EXTEND; |
| 234 | if (buffer->flags & force_copy_flags) { | |
| 235 | void *newspace = cxMalloc(buffer->allocator, newcap); | |
| 236 | if (NULL == newspace) return -1; | |
| 237 | memcpy(newspace, buffer->space, buffer->size); | |
| 238 | buffer->space = newspace; | |
| 239 | buffer->capacity = newcap; | |
| 240 | buffer->flags &= ~force_copy_flags; | |
| 241 | buffer->flags |= CX_BUFFER_FREE_CONTENTS; | |
| 242 | return 0; | |
| 243 | } else if (cxReallocate(buffer->allocator, | |
| 174 | 244 | (void **) &buffer->bytes, newcap) == 0) { |
| 245 | buffer->capacity = newcap; | |
| 246 | return 0; | |
| 247 | } else { | |
| 440 | 248 | return -1; // LCOV_EXCL_LINE |
| 174 | 249 | } |
| 250 | } | |
| 251 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
252 | void cxBufferShrink( |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
253 | CxBuffer *buffer, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
254 | size_t reserve |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
255 | ) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
256 | // Ensure buffer is in a reallocatable state |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
257 | const int force_copy_flags = CX_BUFFER_COPY_ON_WRITE | CX_BUFFER_COPY_ON_EXTEND; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
258 | if (buffer->flags & force_copy_flags) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
259 | // do nothing when we are not allowed to reallocate |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
260 | return; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
261 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
262 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
263 | // calculate new capacity |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
264 | size_t newCapacity = buffer->size + reserve; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
265 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
266 | // If new capacity is smaller than current capacity, resize the buffer |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
267 | if (newCapacity < buffer->capacity) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
268 | if (0 == cxReallocate(buffer->allocator, &buffer->bytes, newCapacity)) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
269 | buffer->capacity = newCapacity; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
270 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
271 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
272 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
273 | |
| 440 | 274 | static size_t cx_buffer_flush_helper( |
| 275 | const CxBuffer *buffer, | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
276 | const unsigned char *src, |
| 174 | 277 | size_t size, |
| 278 | size_t nitems | |
| 279 | ) { | |
| 440 | 280 | // flush data from an arbitrary source |
| 281 | // does not need to be the buffer's contents | |
| 282 | size_t max_items = buffer->flush->blksize / size; | |
| 283 | size_t fblocks = 0; | |
| 284 | size_t flushed_total = 0; | |
| 285 | while (nitems > 0 && fblocks < buffer->flush->blkmax) { | |
| 286 | fblocks++; | |
| 287 | size_t items = nitems > max_items ? max_items : nitems; | |
| 288 | size_t flushed = buffer->flush->wfunc( | |
| 289 | src, size, items, buffer->flush->target); | |
| 174 | 290 | if (flushed > 0) { |
| 440 | 291 | flushed_total += flushed; |
| 292 | src += flushed * size; | |
| 293 | nitems -= flushed; | |
| 174 | 294 | } else { |
| 295 | // if no bytes can be flushed out anymore, we give up | |
| 296 | break; | |
| 297 | } | |
| 298 | } | |
| 440 | 299 | return flushed_total; |
| 300 | } | |
| 301 | ||
| 302 | static size_t cx_buffer_flush_impl(CxBuffer *buffer, size_t size) { | |
| 303 | // flush the current contents of the buffer | |
| 304 | unsigned char *space = buffer->bytes; | |
| 305 | size_t remaining = buffer->pos / size; | |
| 306 | size_t flushed_total = cx_buffer_flush_helper( | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
307 | buffer, space, size, remaining); |
| 440 | 308 | |
| 309 | // shift the buffer left after flushing | |
| 310 | // IMPORTANT: up to this point, copy on write must have been | |
| 311 | // performed already, because we can't do error handling here | |
| 312 | cxBufferShiftLeft(buffer, flushed_total*size); | |
| 313 | ||
| 314 | return flushed_total; | |
| 315 | } | |
| 316 | ||
| 317 | size_t cxBufferFlush(CxBuffer *buffer) { | |
| 318 | if (buffer_copy_on_write(buffer)) return 0; | |
| 319 | return cx_buffer_flush_impl(buffer, 1); | |
| 174 | 320 | } |
| 321 | ||
| 322 | size_t cxBufferWrite( | |
| 324 | 323 | const void *ptr, |
| 174 | 324 | size_t size, |
| 325 | size_t nitems, | |
| 326 | CxBuffer *buffer | |
| 327 | ) { | |
| 328 | // optimize for easy case | |
| 329 | if (size == 1 && (buffer->capacity - buffer->pos) >= nitems) { | |
| 440 | 330 | if (buffer_copy_on_write(buffer)) return 0; |
| 174 | 331 | memcpy(buffer->bytes + buffer->pos, ptr, nitems); |
| 332 | buffer->pos += nitems; | |
| 333 | if (buffer->pos > buffer->size) { | |
| 334 | buffer->size = buffer->pos; | |
| 335 | } | |
| 336 | return nitems; | |
| 337 | } | |
| 338 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
339 | size_t len, total_flushed = 0; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
340 | cx_buffer_write_retry: |
| 174 | 341 | if (cx_szmul(size, nitems, &len)) { |
| 440 | 342 | errno = EOVERFLOW; |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
343 | return total_flushed; |
| 440 | 344 | } |
| 345 | if (buffer->pos > SIZE_MAX - len) { | |
| 346 | errno = EOVERFLOW; | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
347 | return total_flushed; |
| 174 | 348 | } |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
349 | |
| 174 | 350 | size_t required = buffer->pos + len; |
| 351 | bool perform_flush = false; | |
| 352 | if (required > buffer->capacity) { | |
| 440 | 353 | if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
| 354 | if (buffer->flush != NULL && required > buffer->flush->threshold) { | |
| 174 | 355 | perform_flush = true; |
| 356 | } else { | |
| 357 | if (cxBufferMinimumCapacity(buffer, required)) { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
358 | return total_flushed; // LCOV_EXCL_LINE |
| 174 | 359 | } |
| 360 | } | |
| 361 | } else { | |
| 440 | 362 | if (buffer->flush != NULL) { |
| 174 | 363 | perform_flush = true; |
| 364 | } else { | |
| 440 | 365 | // truncate data, if we can neither extend nor flush |
| 174 | 366 | len = buffer->capacity - buffer->pos; |
| 367 | if (size > 1) { | |
| 368 | len -= len % size; | |
| 369 | } | |
| 440 | 370 | nitems = len / size; |
| 174 | 371 | } |
| 372 | } | |
| 373 | } | |
| 374 | ||
| 440 | 375 | // check here and not above because of possible truncation |
| 174 | 376 | if (len == 0) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
377 | return total_flushed; |
| 174 | 378 | } |
| 379 | ||
| 440 | 380 | // check if we need to copy |
| 381 | if (buffer_copy_on_write(buffer)) return 0; | |
| 174 | 382 | |
| 440 | 383 | // perform the operation |
| 384 | if (perform_flush) { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
385 | size_t items_flushed; |
| 440 | 386 | if (buffer->pos == 0) { |
| 387 | // if we don't have data in the buffer, but are instructed | |
| 388 | // to flush, it means that we are supposed to relay the data | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
389 | items_flushed = cx_buffer_flush_helper(buffer, ptr, size, nitems); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
390 | if (items_flushed == 0) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
391 | // we needed to relay data, but could not flush anything |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
392 | // i.e. we have to give up to avoid endless trying |
| 440 | 393 | return 0; |
| 174 | 394 | } |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
395 | nitems -= items_flushed; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
396 | total_flushed += items_flushed; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
397 | if (nitems > 0) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
398 | ptr = ((unsigned char*)ptr) + items_flushed * size; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
399 | goto cx_buffer_write_retry; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
400 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
401 | return total_flushed; |
| 440 | 402 | } else { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
403 | items_flushed = cx_buffer_flush_impl(buffer, size); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
404 | if (items_flushed == 0) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
405 | // flush target is full, let's try to truncate |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
406 | size_t remaining_space; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
407 | if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
408 | remaining_space = buffer->flush->threshold > buffer->pos |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
409 | ? buffer->flush->threshold - buffer->pos |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
410 | : 0; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
411 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
412 | remaining_space = buffer->capacity > buffer->pos |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
413 | ? buffer->capacity - buffer->pos |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
414 | : 0; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
415 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
416 | nitems = remaining_space / size; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
417 | if (nitems == 0) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
418 | return total_flushed; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
419 | } |
| 174 | 420 | } |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
421 | goto cx_buffer_write_retry; |
| 174 | 422 | } |
| 423 | } else { | |
| 424 | memcpy(buffer->bytes + buffer->pos, ptr, len); | |
| 425 | buffer->pos += len; | |
| 426 | if (buffer->pos > buffer->size) { | |
| 427 | buffer->size = buffer->pos; | |
| 428 | } | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
429 | return total_flushed + nitems; |
| 174 | 430 | } |
| 431 | } | |
| 432 | ||
| 440 | 433 | size_t cxBufferAppend( |
| 434 | const void *ptr, | |
| 435 | size_t size, | |
| 436 | size_t nitems, | |
| 437 | CxBuffer *buffer | |
| 438 | ) { | |
| 439 | size_t pos = buffer->pos; | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
440 | size_t append_pos = buffer->size; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
441 | buffer->pos = append_pos; |
| 440 | 442 | size_t written = cxBufferWrite(ptr, size, nitems, buffer); |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
443 | // the buffer might have been flushed |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
444 | // we must compute a possible delta for the position |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
445 | // expected: pos = append_pos + written |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
446 | // -> if this is not the case, there is a delta |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
447 | size_t delta = append_pos + written*size - buffer->pos; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
448 | if (delta > pos) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
449 | buffer->pos = 0; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
450 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
451 | buffer->pos = pos - delta; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
452 | } |
| 440 | 453 | return written; |
| 454 | } | |
| 455 | ||
| 174 | 456 | int cxBufferPut( |
| 457 | CxBuffer *buffer, | |
| 458 | int c | |
| 459 | ) { | |
| 460 | c &= 0xFF; | |
| 461 | unsigned char const ch = c; | |
| 462 | if (cxBufferWrite(&ch, 1, 1, buffer) == 1) { | |
| 463 | return c; | |
| 464 | } else { | |
| 465 | return EOF; | |
| 466 | } | |
| 467 | } | |
| 468 | ||
| 440 | 469 | int cxBufferTerminate(CxBuffer *buffer) { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
470 | if (0 == cxBufferPut(buffer, 0)) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
471 | buffer->size = buffer->pos - 1; |
| 440 | 472 | return 0; |
| 473 | } else { | |
| 474 | return -1; | |
| 475 | } | |
| 476 | } | |
| 477 | ||
| 174 | 478 | size_t cxBufferPutString( |
| 479 | CxBuffer *buffer, | |
| 480 | const char *str | |
| 481 | ) { | |
| 482 | return cxBufferWrite(str, 1, strlen(str), buffer); | |
| 483 | } | |
| 484 | ||
| 485 | size_t cxBufferRead( | |
| 486 | void *ptr, | |
| 487 | size_t size, | |
| 488 | size_t nitems, | |
| 489 | CxBuffer *buffer | |
| 490 | ) { | |
| 491 | size_t len; | |
| 492 | if (cx_szmul(size, nitems, &len)) { | |
| 440 | 493 | errno = EOVERFLOW; |
| 174 | 494 | return 0; |
| 495 | } | |
| 496 | if (buffer->pos + len > buffer->size) { | |
| 497 | len = buffer->size - buffer->pos; | |
| 498 | if (size > 1) len -= len % size; | |
| 499 | } | |
| 500 | ||
| 501 | if (len <= 0) { | |
| 502 | return len; | |
| 503 | } | |
| 504 | ||
| 505 | memcpy(ptr, buffer->bytes + buffer->pos, len); | |
| 506 | buffer->pos += len; | |
| 507 | ||
| 508 | return len / size; | |
| 509 | } | |
| 510 | ||
| 511 | int cxBufferGet(CxBuffer *buffer) { | |
| 512 | if (cxBufferEof(buffer)) { | |
| 513 | return EOF; | |
| 514 | } else { | |
| 515 | int c = buffer->bytes[buffer->pos]; | |
| 516 | buffer->pos++; | |
| 517 | return c; | |
| 518 | } | |
| 519 | } | |
| 520 | ||
| 521 | int cxBufferShiftLeft( | |
| 522 | CxBuffer *buffer, | |
| 523 | size_t shift | |
| 524 | ) { | |
| 525 | if (shift >= buffer->size) { | |
| 526 | buffer->pos = buffer->size = 0; | |
| 527 | } else { | |
| 440 | 528 | if (buffer_copy_on_write(buffer)) return -1; |
| 174 | 529 | memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift); |
| 530 | buffer->size -= shift; | |
| 531 | ||
| 532 | if (buffer->pos >= shift) { | |
| 533 | buffer->pos -= shift; | |
| 534 | } else { | |
| 535 | buffer->pos = 0; | |
| 536 | } | |
| 537 | } | |
| 538 | return 0; | |
| 539 | } | |
| 540 | ||
| 541 | int cxBufferShiftRight( | |
| 542 | CxBuffer *buffer, | |
| 543 | size_t shift | |
| 544 | ) { | |
| 440 | 545 | if (buffer->size > SIZE_MAX - shift) { |
| 546 | errno = EOVERFLOW; | |
| 547 | return -1; | |
| 548 | } | |
| 174 | 549 | size_t req_capacity = buffer->size + shift; |
| 550 | size_t movebytes; | |
| 551 | ||
| 552 | // auto extend buffer, if required and enabled | |
| 553 | if (buffer->capacity < req_capacity) { | |
| 440 | 554 | if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
| 174 | 555 | if (cxBufferMinimumCapacity(buffer, req_capacity)) { |
| 440 | 556 | return -1; // LCOV_EXCL_LINE |
| 174 | 557 | } |
| 558 | movebytes = buffer->size; | |
| 559 | } else { | |
| 560 | movebytes = buffer->capacity - shift; | |
| 561 | } | |
| 562 | } else { | |
| 563 | movebytes = buffer->size; | |
| 564 | } | |
| 565 | ||
| 440 | 566 | if (movebytes > 0) { |
| 567 | if (buffer_copy_on_write(buffer)) return -1; | |
| 568 | memmove(buffer->bytes + shift, buffer->bytes, movebytes); | |
| 569 | buffer->size = shift + movebytes; | |
| 570 | } | |
| 174 | 571 | |
| 572 | buffer->pos += shift; | |
| 573 | if (buffer->pos > buffer->size) { | |
| 574 | buffer->pos = buffer->size; | |
| 575 | } | |
| 576 | ||
| 577 | return 0; | |
| 578 | } | |
| 579 | ||
| 580 | int cxBufferShift( | |
| 581 | CxBuffer *buffer, | |
| 582 | off_t shift | |
| 583 | ) { | |
| 584 | if (shift < 0) { | |
| 585 | return cxBufferShiftLeft(buffer, (size_t) (-shift)); | |
| 586 | } else if (shift > 0) { | |
| 587 | return cxBufferShiftRight(buffer, (size_t) shift); | |
| 588 | } else { | |
| 589 | return 0; | |
| 590 | } | |
| 591 | } |