Sun, 05 Jan 2025 22:00:39 +0100
update ucx
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> |
32 | ||
174 | 33 | static void *cx_malloc_stdlib( |
440 | 34 | cx_attr_unused void *d, |
174 | 35 | size_t n |
36 | ) { | |
37 | return malloc(n); | |
38 | } | |
39 | ||
40 | static void *cx_realloc_stdlib( | |
440 | 41 | cx_attr_unused void *d, |
174 | 42 | void *mem, |
43 | size_t n | |
44 | ) { | |
45 | return realloc(mem, n); | |
46 | } | |
47 | ||
48 | static void *cx_calloc_stdlib( | |
440 | 49 | cx_attr_unused void *d, |
174 | 50 | size_t nelem, |
51 | size_t n | |
52 | ) { | |
53 | return calloc(nelem, n); | |
54 | } | |
55 | ||
56 | static void cx_free_stdlib( | |
440 | 57 | cx_attr_unused void *d, |
174 | 58 | void *mem |
59 | ) { | |
60 | free(mem); | |
61 | } | |
62 | ||
63 | static cx_allocator_class cx_default_allocator_class = { | |
64 | cx_malloc_stdlib, | |
65 | cx_realloc_stdlib, | |
66 | cx_calloc_stdlib, | |
67 | cx_free_stdlib | |
68 | }; | |
69 | ||
70 | struct cx_allocator_s cx_default_allocator = { | |
71 | &cx_default_allocator_class, | |
72 | NULL | |
73 | }; | |
74 | CxAllocator *cxDefaultAllocator = &cx_default_allocator; | |
75 | ||
440 | 76 | #undef cx_reallocate |
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
77 | int cx_reallocate( |
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
78 | void **mem, |
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
79 | size_t n |
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
80 | ) { |
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
81 | void *nmem = realloc(*mem, n); |
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
82 | if (nmem == NULL) { |
440 | 83 | return 1; // LCOV_EXCL_LINE |
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
84 | } else { |
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
85 | *mem = nmem; |
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
86 | return 0; |
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
87 | } |
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
88 | } |
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
89 | |
440 | 90 | #undef cx_reallocatearray |
91 | int cx_reallocatearray( | |
92 | void **mem, | |
93 | size_t nmemb, | |
94 | size_t size | |
95 | ) { | |
96 | size_t n; | |
97 | if (cx_szmul(nmemb, size, &n)) { | |
98 | errno = EOVERFLOW; | |
99 | return 1; | |
100 | } else { | |
101 | void *nmem = realloc(*mem, n); | |
102 | if (nmem == NULL) { | |
103 | return 1; // LCOV_EXCL_LINE | |
104 | } else { | |
105 | *mem = nmem; | |
106 | return 0; | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
174 | 111 | // IMPLEMENTATION OF HIGH LEVEL API |
112 | ||
113 | void *cxMalloc( | |
324 | 114 | const CxAllocator *allocator, |
174 | 115 | size_t n |
116 | ) { | |
117 | return allocator->cl->malloc(allocator->data, n); | |
118 | } | |
119 | ||
120 | void *cxRealloc( | |
324 | 121 | const CxAllocator *allocator, |
174 | 122 | void *mem, |
123 | size_t n | |
124 | ) { | |
125 | return allocator->cl->realloc(allocator->data, mem, n); | |
126 | } | |
127 | ||
440 | 128 | void *cxReallocArray( |
129 | const CxAllocator *allocator, | |
130 | void *mem, | |
131 | size_t nmemb, | |
132 | size_t size | |
133 | ) { | |
134 | size_t n; | |
135 | if (cx_szmul(nmemb, size, &n)) { | |
136 | errno = EOVERFLOW; | |
137 | return NULL; | |
138 | } else { | |
139 | return allocator->cl->realloc(allocator->data, mem, n); | |
140 | } | |
141 | } | |
142 | ||
143 | #undef cxReallocate | |
174 | 144 | int cxReallocate( |
324 | 145 | const CxAllocator *allocator, |
174 | 146 | void **mem, |
147 | size_t n | |
148 | ) { | |
149 | void *nmem = allocator->cl->realloc(allocator->data, *mem, n); | |
150 | if (nmem == NULL) { | |
440 | 151 | return 1; // LCOV_EXCL_LINE |
152 | } else { | |
153 | *mem = nmem; | |
154 | return 0; | |
155 | } | |
156 | } | |
157 | ||
158 | #undef cxReallocateArray | |
159 | int cxReallocateArray( | |
160 | const CxAllocator *allocator, | |
161 | void **mem, | |
162 | size_t nmemb, | |
163 | size_t size | |
164 | ) { | |
165 | void *nmem = cxReallocArray(allocator, *mem, nmemb, size); | |
166 | if (nmem == NULL) { | |
167 | return 1; // LCOV_EXCL_LINE | |
174 | 168 | } else { |
169 | *mem = nmem; | |
170 | return 0; | |
171 | } | |
172 | } | |
173 | ||
174 | void *cxCalloc( | |
324 | 175 | const CxAllocator *allocator, |
174 | 176 | size_t nelem, |
177 | size_t n | |
178 | ) { | |
179 | return allocator->cl->calloc(allocator->data, nelem, n); | |
180 | } | |
181 | ||
182 | void cxFree( | |
324 | 183 | const CxAllocator *allocator, |
174 | 184 | void *mem |
185 | ) { | |
186 | allocator->cl->free(allocator->data, mem); | |
187 | } |