Sun, 07 Dec 2025 15:45:30 +0100
rename combobox to dropdown
| 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> |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
38 | static unsigned long system_page_size(void) { |
|
629
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 | #else |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
48 | #include <unistd.h> |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
49 | static unsigned long system_page_size(void) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
50 | static unsigned long ps = 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
51 | if (ps == 0) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
52 | long sc = sysconf(_SC_PAGESIZE); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
53 | if (sc < 0) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
54 | // fallback for systems which do not report a value here |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
55 | ps = 4096; // LCOV_EXCL_LINE |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
56 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
57 | ps = (unsigned long) sc; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
58 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
59 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
60 | return ps; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
61 | } |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
62 | #endif |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
63 | |
| 440 | 64 | static int buffer_copy_on_write(CxBuffer* buffer) { |
| 65 | if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) return 0; | |
| 66 | void *newspace = cxMalloc(buffer->allocator, buffer->capacity); | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
67 | if (NULL == newspace) return -1; // LCOV_EXCL_LINE |
| 440 | 68 | memcpy(newspace, buffer->space, buffer->size); |
| 69 | buffer->space = newspace; | |
| 70 | buffer->flags &= ~CX_BUFFER_COPY_ON_WRITE; | |
| 71 | buffer->flags |= CX_BUFFER_FREE_CONTENTS; | |
| 72 | return 0; | |
| 73 | } | |
| 174 | 74 | |
| 75 | int cxBufferInit( | |
| 76 | CxBuffer *buffer, | |
| 77 | void *space, | |
| 78 | size_t capacity, | |
| 324 | 79 | const CxAllocator *allocator, |
| 174 | 80 | int flags |
| 81 | ) { | |
| 440 | 82 | if (allocator == NULL) { |
| 83 | allocator = cxDefaultAllocator; | |
| 84 | } | |
| 85 | if (flags & CX_BUFFER_COPY_ON_EXTEND) { | |
| 86 | flags |= CX_BUFFER_AUTO_EXTEND; | |
| 87 | } | |
| 174 | 88 | buffer->allocator = allocator; |
| 89 | buffer->flags = flags; | |
| 90 | if (!space) { | |
| 91 | buffer->bytes = cxMalloc(allocator, capacity); | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
92 | if (buffer->bytes == NULL) return -1; // LCOV_EXCL_LINE |
| 174 | 93 | buffer->flags |= CX_BUFFER_FREE_CONTENTS; |
| 94 | } else { | |
| 95 | buffer->bytes = space; | |
| 96 | } | |
| 97 | buffer->capacity = capacity; | |
| 98 | buffer->size = 0; | |
| 99 | buffer->pos = 0; | |
| 100 | ||
| 440 | 101 | buffer->flush = NULL; |
| 174 | 102 | |
| 103 | return 0; | |
| 104 | } | |
| 105 | ||
| 440 | 106 | int cxBufferEnableFlushing( |
| 107 | CxBuffer *buffer, | |
| 108 | CxBufferFlushConfig config | |
| 109 | ) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
110 | buffer->flush = cxMallocDefault(sizeof(CxBufferFlushConfig)); |
| 440 | 111 | if (buffer->flush == NULL) return -1; // LCOV_EXCL_LINE |
| 112 | memcpy(buffer->flush, &config, sizeof(CxBufferFlushConfig)); | |
| 113 | return 0; | |
| 114 | } | |
| 115 | ||
| 174 | 116 | void cxBufferDestroy(CxBuffer *buffer) { |
| 440 | 117 | if (buffer->flags & CX_BUFFER_FREE_CONTENTS) { |
| 174 | 118 | cxFree(buffer->allocator, buffer->bytes); |
| 119 | } | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
120 | cxFreeDefault(buffer->flush); |
| 440 | 121 | memset(buffer, 0, sizeof(CxBuffer)); |
| 174 | 122 | } |
| 123 | ||
| 124 | CxBuffer *cxBufferCreate( | |
| 125 | void *space, | |
| 126 | size_t capacity, | |
| 324 | 127 | const CxAllocator *allocator, |
| 174 | 128 | int flags |
| 129 | ) { | |
| 440 | 130 | if (allocator == NULL) { |
| 131 | allocator = cxDefaultAllocator; | |
| 132 | } | |
| 174 | 133 | CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer)); |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
134 | if (buf == NULL) return NULL; // LCOV_EXCL_LINE |
| 174 | 135 | if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) { |
| 136 | return buf; | |
| 137 | } else { | |
| 440 | 138 | // LCOV_EXCL_START |
| 174 | 139 | cxFree(allocator, buf); |
| 140 | return NULL; | |
| 440 | 141 | // LCOV_EXCL_STOP |
| 174 | 142 | } |
| 143 | } | |
| 144 | ||
| 145 | void cxBufferFree(CxBuffer *buffer) { | |
| 440 | 146 | if (buffer == NULL) return; |
| 147 | const CxAllocator *allocator = buffer->allocator; | |
| 148 | cxBufferDestroy(buffer); | |
| 149 | cxFree(allocator, buffer); | |
| 174 | 150 | } |
| 151 | ||
| 152 | int cxBufferSeek( | |
| 153 | CxBuffer *buffer, | |
| 154 | off_t offset, | |
| 155 | int whence | |
| 156 | ) { | |
| 157 | size_t npos; | |
| 158 | switch (whence) { | |
| 159 | case SEEK_CUR: | |
| 160 | npos = buffer->pos; | |
| 161 | break; | |
| 162 | case SEEK_END: | |
| 163 | npos = buffer->size; | |
| 164 | break; | |
| 165 | case SEEK_SET: | |
| 166 | npos = 0; | |
| 167 | break; | |
| 168 | default: | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
169 | errno = EINVAL; |
| 174 | 170 | return -1; |
| 171 | } | |
| 172 | ||
| 173 | size_t opos = npos; | |
| 174 | npos += offset; | |
| 175 | ||
| 176 | if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
177 | // to be compliant with fseek() specification |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
178 | // we return EINVAL on underflow |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
179 | errno = EINVAL; |
| 174 | 180 | return -1; |
| 181 | } | |
| 182 | ||
| 440 | 183 | if (npos > buffer->size) { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
184 | // not compliant with fseek() specification |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
185 | // but this is the better behavior for CxBuffer |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
186 | errno = EINVAL; |
| 174 | 187 | return -1; |
| 188 | } else { | |
| 189 | buffer->pos = npos; | |
| 190 | return 0; | |
| 191 | } | |
| 192 | ||
| 193 | } | |
| 194 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
195 | size_t cxBufferPop(CxBuffer *buffer, size_t size, size_t nitems) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
196 | size_t len; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
197 | if (cx_szmul(size, nitems, &len)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
198 | // LCOV_EXCL_START |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
199 | errno = EOVERFLOW; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
200 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
201 | // LCOV_EXCL_STOP |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
202 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
203 | if (len == 0) return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
204 | if (len > buffer->size) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
205 | if (size == 1) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
206 | // simple case: everything can be discarded |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
207 | len = buffer->size; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
208 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
209 | // complicated case: misaligned bytes must stay |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
210 | size_t misalignment = buffer->size % size; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
211 | len = buffer->size - misalignment; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
212 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
213 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
214 | buffer->size -= len; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
215 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
216 | // adjust position, if required |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
217 | if (buffer->pos > buffer->size) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
218 | buffer->pos = buffer->size; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
219 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
220 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
221 | return len / size; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
222 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
223 | |
| 174 | 224 | void cxBufferClear(CxBuffer *buffer) { |
| 440 | 225 | if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) { |
| 226 | memset(buffer->bytes, 0, buffer->size); | |
| 227 | } | |
| 174 | 228 | buffer->size = 0; |
| 229 | buffer->pos = 0; | |
| 230 | } | |
| 231 | ||
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
232 | void cxBufferReset(CxBuffer *buffer) { |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
233 | buffer->size = 0; |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
234 | buffer->pos = 0; |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
235 | } |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
236 | |
| 440 | 237 | bool cxBufferEof(const CxBuffer *buffer) { |
| 174 | 238 | return buffer->pos >= buffer->size; |
| 239 | } | |
| 240 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
241 | int cxBufferReserve(CxBuffer *buffer, size_t newcap) { |
| 174 | 242 | if (newcap <= buffer->capacity) { |
| 243 | return 0; | |
| 244 | } | |
| 440 | 245 | const int force_copy_flags = CX_BUFFER_COPY_ON_WRITE | CX_BUFFER_COPY_ON_EXTEND; |
| 246 | if (buffer->flags & force_copy_flags) { | |
| 247 | void *newspace = cxMalloc(buffer->allocator, newcap); | |
| 248 | if (NULL == newspace) return -1; | |
| 249 | memcpy(newspace, buffer->space, buffer->size); | |
| 250 | buffer->space = newspace; | |
| 251 | buffer->capacity = newcap; | |
| 252 | buffer->flags &= ~force_copy_flags; | |
| 253 | buffer->flags |= CX_BUFFER_FREE_CONTENTS; | |
| 254 | return 0; | |
| 255 | } else if (cxReallocate(buffer->allocator, | |
| 174 | 256 | (void **) &buffer->bytes, newcap) == 0) { |
| 257 | buffer->capacity = newcap; | |
| 258 | return 0; | |
| 259 | } else { | |
| 440 | 260 | return -1; // LCOV_EXCL_LINE |
| 174 | 261 | } |
| 262 | } | |
| 263 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
264 | static size_t cx_buffer_calculate_minimum_capacity(size_t mincap) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
265 | unsigned long pagesize = system_page_size(); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
266 | // if page size is larger than 64 KB - for some reason - truncate to 64 KB |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
267 | if (pagesize > 65536) pagesize = 65536; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
268 | if (mincap < pagesize) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
269 | // when smaller as one page, map to the next power of two |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
270 | mincap--; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
271 | mincap |= mincap >> 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
272 | mincap |= mincap >> 2; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
273 | mincap |= mincap >> 4; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
274 | // last operation only needed for pages larger 4096 bytes |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
275 | // but if/else would be more expensive than just doing this |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
276 | mincap |= mincap >> 8; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
277 | mincap++; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
278 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
279 | // otherwise, map to a multiple of the page size |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
280 | mincap -= mincap % pagesize; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
281 | mincap += pagesize; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
282 | // note: if newcap is already page aligned, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
283 | // this gives a full additional page (which is good) |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
284 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
285 | return mincap; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
286 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
287 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
288 | int cxBufferMinimumCapacity(CxBuffer *buffer, size_t newcap) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
289 | if (newcap <= buffer->capacity) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
290 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
291 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
292 | newcap = cx_buffer_calculate_minimum_capacity(newcap); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
293 | return cxBufferReserve(buffer, newcap); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
294 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
295 | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
296 | void cxBufferShrink( |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
297 | CxBuffer *buffer, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
298 | size_t reserve |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
299 | ) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
300 | // Ensure buffer is in a reallocatable state |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
301 | 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
|
302 | if (buffer->flags & force_copy_flags) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
303 | // do nothing when we are not allowed to reallocate |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
304 | return; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
305 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
306 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
307 | // calculate new capacity |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
308 | size_t newCapacity = buffer->size + reserve; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
309 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
310 | // 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
|
311 | if (newCapacity < buffer->capacity) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
312 | if (0 == cxReallocate(buffer->allocator, &buffer->bytes, newCapacity)) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
313 | buffer->capacity = newCapacity; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
314 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
315 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
316 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
317 | |
| 440 | 318 | static size_t cx_buffer_flush_helper( |
| 319 | 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
|
320 | const unsigned char *src, |
| 174 | 321 | size_t size, |
| 322 | size_t nitems | |
| 323 | ) { | |
| 440 | 324 | // flush data from an arbitrary source |
| 325 | // does not need to be the buffer's contents | |
| 326 | size_t max_items = buffer->flush->blksize / size; | |
| 327 | size_t fblocks = 0; | |
| 328 | size_t flushed_total = 0; | |
| 329 | while (nitems > 0 && fblocks < buffer->flush->blkmax) { | |
| 330 | fblocks++; | |
| 331 | size_t items = nitems > max_items ? max_items : nitems; | |
| 332 | size_t flushed = buffer->flush->wfunc( | |
| 333 | src, size, items, buffer->flush->target); | |
| 174 | 334 | if (flushed > 0) { |
| 440 | 335 | flushed_total += flushed; |
| 336 | src += flushed * size; | |
| 337 | nitems -= flushed; | |
| 174 | 338 | } else { |
| 339 | // if no bytes can be flushed out anymore, we give up | |
| 340 | break; | |
| 341 | } | |
| 342 | } | |
| 440 | 343 | return flushed_total; |
| 344 | } | |
| 345 | ||
| 346 | static size_t cx_buffer_flush_impl(CxBuffer *buffer, size_t size) { | |
| 347 | // flush the current contents of the buffer | |
| 348 | unsigned char *space = buffer->bytes; | |
| 349 | size_t remaining = buffer->pos / size; | |
| 350 | 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
|
351 | buffer, space, size, remaining); |
| 440 | 352 | |
| 353 | // shift the buffer left after flushing | |
| 354 | // IMPORTANT: up to this point, copy on write must have been | |
| 355 | // performed already, because we can't do error handling here | |
| 356 | cxBufferShiftLeft(buffer, flushed_total*size); | |
| 357 | ||
| 358 | return flushed_total; | |
| 359 | } | |
| 360 | ||
| 361 | size_t cxBufferFlush(CxBuffer *buffer) { | |
| 362 | if (buffer_copy_on_write(buffer)) return 0; | |
| 363 | return cx_buffer_flush_impl(buffer, 1); | |
| 174 | 364 | } |
| 365 | ||
| 366 | size_t cxBufferWrite( | |
| 324 | 367 | const void *ptr, |
| 174 | 368 | size_t size, |
| 369 | size_t nitems, | |
| 370 | CxBuffer *buffer | |
| 371 | ) { | |
| 372 | // optimize for easy case | |
| 373 | if (size == 1 && (buffer->capacity - buffer->pos) >= nitems) { | |
| 440 | 374 | if (buffer_copy_on_write(buffer)) return 0; |
| 174 | 375 | memcpy(buffer->bytes + buffer->pos, ptr, nitems); |
| 376 | buffer->pos += nitems; | |
| 377 | if (buffer->pos > buffer->size) { | |
| 378 | buffer->size = buffer->pos; | |
| 379 | } | |
| 380 | return nitems; | |
| 381 | } | |
| 382 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
383 | 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
|
384 | cx_buffer_write_retry: |
| 174 | 385 | if (cx_szmul(size, nitems, &len)) { |
| 440 | 386 | 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
|
387 | return total_flushed; |
| 440 | 388 | } |
| 389 | if (buffer->pos > SIZE_MAX - len) { | |
| 390 | 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
|
391 | return total_flushed; |
| 174 | 392 | } |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
393 | |
| 174 | 394 | size_t required = buffer->pos + len; |
| 395 | bool perform_flush = false; | |
| 396 | if (required > buffer->capacity) { | |
| 440 | 397 | if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
398 | if (buffer->flush != NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
399 | size_t newcap = cx_buffer_calculate_minimum_capacity(required); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
400 | if (newcap > buffer->flush->threshold) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
401 | newcap = buffer->flush->threshold; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
402 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
403 | if (cxBufferReserve(buffer, newcap)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
404 | return total_flushed; // LCOV_EXCL_LINE |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
405 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
406 | if (required > newcap) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
407 | perform_flush = true; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
629
diff
changeset
|
408 | } |
| 174 | 409 | } else { |
| 410 | 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
|
411 | return total_flushed; // LCOV_EXCL_LINE |
| 174 | 412 | } |
| 413 | } | |
| 414 | } else { | |
| 440 | 415 | if (buffer->flush != NULL) { |
| 174 | 416 | perform_flush = true; |
| 417 | } else { | |
| 440 | 418 | // truncate data, if we can neither extend nor flush |
| 174 | 419 | len = buffer->capacity - buffer->pos; |
| 420 | if (size > 1) { | |
| 421 | len -= len % size; | |
| 422 | } | |
| 440 | 423 | nitems = len / size; |
| 174 | 424 | } |
| 425 | } | |
| 426 | } | |
| 427 | ||
| 440 | 428 | // check here and not above because of possible truncation |
| 174 | 429 | 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
|
430 | return total_flushed; |
| 174 | 431 | } |
| 432 | ||
| 440 | 433 | // check if we need to copy |
| 434 | if (buffer_copy_on_write(buffer)) return 0; | |
| 174 | 435 | |
| 440 | 436 | // perform the operation |
| 437 | 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
|
438 | size_t items_flushed; |
| 440 | 439 | if (buffer->pos == 0) { |
| 440 | // if we don't have data in the buffer, but are instructed | |
| 441 | // 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
|
442 | 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
|
443 | 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
|
444 | // 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
|
445 | // i.e. we have to give up to avoid endless trying |
| 440 | 446 | return 0; |
| 174 | 447 | } |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
448 | 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
|
449 | 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
|
450 | 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
|
451 | 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
|
452 | 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
|
453 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
454 | return total_flushed; |
| 440 | 455 | } 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
|
456 | 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
|
457 | 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
|
458 | // 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
|
459 | 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
|
460 | 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
|
461 | 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
|
462 | ? 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
|
463 | : 0; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
464 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
465 | 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
|
466 | ? 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
|
467 | : 0; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
468 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
469 | 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
|
470 | 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
|
471 | 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
|
472 | } |
| 174 | 473 | } |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
474 | goto cx_buffer_write_retry; |
| 174 | 475 | } |
| 476 | } else { | |
| 477 | memcpy(buffer->bytes + buffer->pos, ptr, len); | |
| 478 | buffer->pos += len; | |
| 479 | if (buffer->pos > buffer->size) { | |
| 480 | buffer->size = buffer->pos; | |
| 481 | } | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
482 | return total_flushed + nitems; |
| 174 | 483 | } |
| 484 | } | |
| 485 | ||
| 440 | 486 | size_t cxBufferAppend( |
| 487 | const void *ptr, | |
| 488 | size_t size, | |
| 489 | size_t nitems, | |
| 490 | CxBuffer *buffer | |
| 491 | ) { | |
| 492 | 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
|
493 | 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
|
494 | buffer->pos = append_pos; |
| 440 | 495 | 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
|
496 | // 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
|
497 | // 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
|
498 | // 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
|
499 | // -> 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
|
500 | 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
|
501 | 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
|
502 | 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
|
503 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
504 | 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
|
505 | } |
| 440 | 506 | return written; |
| 507 | } | |
| 508 | ||
| 174 | 509 | int cxBufferPut( |
| 510 | CxBuffer *buffer, | |
| 511 | int c | |
| 512 | ) { | |
| 513 | c &= 0xFF; | |
| 514 | unsigned char const ch = c; | |
| 515 | if (cxBufferWrite(&ch, 1, 1, buffer) == 1) { | |
| 516 | return c; | |
| 517 | } else { | |
| 518 | return EOF; | |
| 519 | } | |
| 520 | } | |
| 521 | ||
| 440 | 522 | int cxBufferTerminate(CxBuffer *buffer) { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
523 | if (0 == cxBufferPut(buffer, 0)) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
524 | buffer->size = buffer->pos - 1; |
| 440 | 525 | return 0; |
| 526 | } else { | |
| 527 | return -1; | |
| 528 | } | |
| 529 | } | |
| 530 | ||
| 174 | 531 | size_t cxBufferPutString( |
| 532 | CxBuffer *buffer, | |
| 533 | const char *str | |
| 534 | ) { | |
| 535 | return cxBufferWrite(str, 1, strlen(str), buffer); | |
| 536 | } | |
| 537 | ||
| 538 | size_t cxBufferRead( | |
| 539 | void *ptr, | |
| 540 | size_t size, | |
| 541 | size_t nitems, | |
| 542 | CxBuffer *buffer | |
| 543 | ) { | |
| 544 | size_t len; | |
| 545 | if (cx_szmul(size, nitems, &len)) { | |
| 440 | 546 | errno = EOVERFLOW; |
| 174 | 547 | return 0; |
| 548 | } | |
| 549 | if (buffer->pos + len > buffer->size) { | |
| 550 | len = buffer->size - buffer->pos; | |
| 551 | if (size > 1) len -= len % size; | |
| 552 | } | |
| 553 | ||
| 554 | if (len <= 0) { | |
| 555 | return len; | |
| 556 | } | |
| 557 | ||
| 558 | memcpy(ptr, buffer->bytes + buffer->pos, len); | |
| 559 | buffer->pos += len; | |
| 560 | ||
| 561 | return len / size; | |
| 562 | } | |
| 563 | ||
| 564 | int cxBufferGet(CxBuffer *buffer) { | |
| 565 | if (cxBufferEof(buffer)) { | |
| 566 | return EOF; | |
| 567 | } else { | |
| 568 | int c = buffer->bytes[buffer->pos]; | |
| 569 | buffer->pos++; | |
| 570 | return c; | |
| 571 | } | |
| 572 | } | |
| 573 | ||
| 574 | int cxBufferShiftLeft( | |
| 575 | CxBuffer *buffer, | |
| 576 | size_t shift | |
| 577 | ) { | |
| 578 | if (shift >= buffer->size) { | |
| 579 | buffer->pos = buffer->size = 0; | |
| 580 | } else { | |
| 440 | 581 | if (buffer_copy_on_write(buffer)) return -1; |
| 174 | 582 | memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift); |
| 583 | buffer->size -= shift; | |
| 584 | ||
| 585 | if (buffer->pos >= shift) { | |
| 586 | buffer->pos -= shift; | |
| 587 | } else { | |
| 588 | buffer->pos = 0; | |
| 589 | } | |
| 590 | } | |
| 591 | return 0; | |
| 592 | } | |
| 593 | ||
| 594 | int cxBufferShiftRight( | |
| 595 | CxBuffer *buffer, | |
| 596 | size_t shift | |
| 597 | ) { | |
| 440 | 598 | if (buffer->size > SIZE_MAX - shift) { |
| 599 | errno = EOVERFLOW; | |
| 600 | return -1; | |
| 601 | } | |
| 174 | 602 | size_t req_capacity = buffer->size + shift; |
| 603 | size_t movebytes; | |
| 604 | ||
| 605 | // auto extend buffer, if required and enabled | |
| 606 | if (buffer->capacity < req_capacity) { | |
| 440 | 607 | if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
| 174 | 608 | if (cxBufferMinimumCapacity(buffer, req_capacity)) { |
| 440 | 609 | return -1; // LCOV_EXCL_LINE |
| 174 | 610 | } |
| 611 | movebytes = buffer->size; | |
| 612 | } else { | |
| 613 | movebytes = buffer->capacity - shift; | |
| 614 | } | |
| 615 | } else { | |
| 616 | movebytes = buffer->size; | |
| 617 | } | |
| 618 | ||
| 440 | 619 | if (movebytes > 0) { |
| 620 | if (buffer_copy_on_write(buffer)) return -1; | |
| 621 | memmove(buffer->bytes + shift, buffer->bytes, movebytes); | |
| 622 | buffer->size = shift + movebytes; | |
| 623 | } | |
| 174 | 624 | |
| 625 | buffer->pos += shift; | |
| 626 | if (buffer->pos > buffer->size) { | |
| 627 | buffer->pos = buffer->size; | |
| 628 | } | |
| 629 | ||
| 630 | return 0; | |
| 631 | } | |
| 632 | ||
| 633 | int cxBufferShift( | |
| 634 | CxBuffer *buffer, | |
| 635 | off_t shift | |
| 636 | ) { | |
| 637 | if (shift < 0) { | |
| 638 | return cxBufferShiftLeft(buffer, (size_t) (-shift)); | |
| 639 | } else if (shift > 0) { | |
| 640 | return cxBufferShiftRight(buffer, (size_t) shift); | |
| 641 | } else { | |
| 642 | return 0; | |
| 643 | } | |
| 644 | } |