Fri, 12 Dec 2025 12:28:32 +0100
remove old UCX2 properties
| 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/allocator.h" | |
| 30 | ||
| 440 | 31 | #include <errno.h> |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
32 | #include <string.h> |
| 440 | 33 | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
34 | #ifdef _WIN32 |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
35 | #include <Windows.h> |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
36 | #include <sysinfoapi.h> |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
37 | unsigned long cx_system_page_size(void) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
38 | static unsigned long ps = 0; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
39 | if (ps == 0) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
40 | SYSTEM_INFO sysinfo; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
41 | GetSystemInfo(&sysinfo); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
42 | ps = (unsigned long) sysinfo.dwPageSize; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
43 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
44 | return ps; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
45 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
46 | #else |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
47 | #include <unistd.h> |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
48 | unsigned long cx_system_page_size(void) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
49 | static unsigned long ps = 0; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
50 | if (ps == 0) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
51 | long sc = sysconf(_SC_PAGESIZE); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
52 | if (sc < 0) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
53 | // fallback for systems which do not report a value here |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
54 | ps = 4096; // LCOV_EXCL_LINE |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
55 | } else { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
56 | ps = (unsigned long) sc; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
57 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
58 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
59 | return ps; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
60 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
61 | #endif |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
62 | |
| 174 | 63 | static void *cx_malloc_stdlib( |
| 440 | 64 | cx_attr_unused void *d, |
| 174 | 65 | size_t n |
| 66 | ) { | |
| 67 | return malloc(n); | |
| 68 | } | |
| 69 | ||
| 70 | static void *cx_realloc_stdlib( | |
| 440 | 71 | cx_attr_unused void *d, |
| 174 | 72 | void *mem, |
| 73 | size_t n | |
| 74 | ) { | |
| 75 | return realloc(mem, n); | |
| 76 | } | |
| 77 | ||
| 78 | static void *cx_calloc_stdlib( | |
| 440 | 79 | cx_attr_unused void *d, |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
80 | size_t nmemb, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
81 | size_t size |
| 174 | 82 | ) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
83 | return calloc(nmemb, size); |
| 174 | 84 | } |
| 85 | ||
| 86 | static void cx_free_stdlib( | |
| 440 | 87 | cx_attr_unused void *d, |
| 174 | 88 | void *mem |
| 89 | ) { | |
| 90 | free(mem); | |
| 91 | } | |
| 92 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
93 | static cx_allocator_class cx_stdlib_allocator_class = { |
| 174 | 94 | cx_malloc_stdlib, |
| 95 | cx_realloc_stdlib, | |
| 96 | cx_calloc_stdlib, | |
| 97 | cx_free_stdlib | |
| 98 | }; | |
| 99 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
100 | struct cx_allocator_s cx_stdlib_allocator = { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
101 | &cx_stdlib_allocator_class, |
| 174 | 102 | NULL |
| 103 | }; | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
104 | const CxAllocator * const cxStdlibAllocator = &cx_stdlib_allocator; |
| 845 | 105 | const CxAllocator * cxDefaultAllocator = &cx_stdlib_allocator; |
| 174 | 106 | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
107 | int cx_reallocate_( |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
108 | void **mem, |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
109 | size_t n |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
110 | ) { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
111 | if (n == 0) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
112 | free(*mem); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
113 | *mem = NULL; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
114 | return 0; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
115 | } |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
116 | void *nmem = realloc(*mem, n); |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
117 | if (nmem == NULL) { |
| 440 | 118 | return 1; // LCOV_EXCL_LINE |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
119 | } else { |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
120 | *mem = nmem; |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
121 | return 0; |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
122 | } |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
123 | } |
|
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
124 | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
125 | int cx_reallocatearray_( |
| 440 | 126 | void **mem, |
| 127 | size_t nmemb, | |
| 128 | size_t size | |
| 129 | ) { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
130 | if (nmemb == 0 || size == 0) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
131 | free(*mem); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
132 | *mem = NULL; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
133 | return 0; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
134 | } |
| 440 | 135 | size_t n; |
| 136 | if (cx_szmul(nmemb, size, &n)) { | |
| 137 | errno = EOVERFLOW; | |
| 138 | return 1; | |
| 139 | } else { | |
| 140 | void *nmem = realloc(*mem, n); | |
| 141 | if (nmem == NULL) { | |
| 142 | return 1; // LCOV_EXCL_LINE | |
| 143 | } else { | |
| 144 | *mem = nmem; | |
| 145 | return 0; | |
| 146 | } | |
| 147 | } | |
| 148 | } | |
| 149 | ||
| 174 | 150 | // IMPLEMENTATION OF HIGH LEVEL API |
| 151 | ||
| 152 | void *cxMalloc( | |
| 324 | 153 | const CxAllocator *allocator, |
| 174 | 154 | size_t n |
| 155 | ) { | |
| 156 | return allocator->cl->malloc(allocator->data, n); | |
| 157 | } | |
| 158 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
159 | void *cxZalloc( |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
160 | const CxAllocator *allocator, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
161 | size_t n |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
162 | ) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
163 | void *mem = allocator->cl->malloc(allocator->data, n); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
164 | if (mem != NULL) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
165 | memset(mem, 0, n); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
166 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
167 | return mem; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
168 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
169 | |
| 174 | 170 | void *cxRealloc( |
| 324 | 171 | const CxAllocator *allocator, |
| 174 | 172 | void *mem, |
| 173 | size_t n | |
| 174 | ) { | |
| 175 | return allocator->cl->realloc(allocator->data, mem, n); | |
| 176 | } | |
| 177 | ||
| 440 | 178 | void *cxReallocArray( |
| 179 | const CxAllocator *allocator, | |
| 180 | void *mem, | |
| 181 | size_t nmemb, | |
| 182 | size_t size | |
| 183 | ) { | |
| 184 | size_t n; | |
| 185 | if (cx_szmul(nmemb, size, &n)) { | |
| 186 | errno = EOVERFLOW; | |
| 187 | return NULL; | |
| 188 | } else { | |
| 189 | return allocator->cl->realloc(allocator->data, mem, n); | |
| 190 | } | |
| 191 | } | |
| 192 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
193 | int cxReallocate_( |
| 324 | 194 | const CxAllocator *allocator, |
| 174 | 195 | void **mem, |
| 196 | size_t n | |
| 197 | ) { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
198 | if (n == 0) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
199 | cxFree(allocator, *mem); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
200 | *mem = NULL; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
201 | return 0; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
202 | } |
| 174 | 203 | void *nmem = allocator->cl->realloc(allocator->data, *mem, n); |
| 204 | if (nmem == NULL) { | |
| 440 | 205 | return 1; // LCOV_EXCL_LINE |
| 206 | } else { | |
| 207 | *mem = nmem; | |
| 208 | return 0; | |
| 209 | } | |
| 210 | } | |
| 211 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
212 | int cxReallocateArray_( |
| 440 | 213 | const CxAllocator *allocator, |
| 214 | void **mem, | |
| 215 | size_t nmemb, | |
| 216 | size_t size | |
| 217 | ) { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
218 | if (nmemb == 0 || size == 0) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
219 | cxFree(allocator, *mem); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
220 | *mem = NULL; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
221 | return 0; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
222 | } |
| 440 | 223 | void *nmem = cxReallocArray(allocator, *mem, nmemb, size); |
| 224 | if (nmem == NULL) { | |
| 225 | return 1; // LCOV_EXCL_LINE | |
| 174 | 226 | } else { |
| 227 | *mem = nmem; | |
| 228 | return 0; | |
| 229 | } | |
| 230 | } | |
| 231 | ||
| 232 | void *cxCalloc( | |
| 324 | 233 | const CxAllocator *allocator, |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
234 | size_t nmemb, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
235 | size_t size |
| 174 | 236 | ) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
237 | return allocator->cl->calloc(allocator->data, nmemb, size); |
| 174 | 238 | } |
| 239 | ||
| 240 | void cxFree( | |
| 324 | 241 | const CxAllocator *allocator, |
| 174 | 242 | void *mem |
| 243 | ) { | |
| 244 | allocator->cl->free(allocator->data, mem); | |
| 245 | } | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
246 | |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
247 | void cxFreeDefault(void *mem) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
248 | cxDefaultAllocator->cl->free(cxDefaultAllocator->data, mem); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
845
diff
changeset
|
249 | } |