| 29 #include "cx/allocator.h" |
29 #include "cx/allocator.h" |
| 30 |
30 |
| 31 #include <errno.h> |
31 #include <errno.h> |
| 32 #include <string.h> |
32 #include <string.h> |
| 33 |
33 |
| |
34 #ifdef _WIN32 |
| |
35 #include <Windows.h> |
| |
36 #include <sysinfoapi.h> |
| |
37 unsigned long cx_system_page_size(void) { |
| |
38 static unsigned long ps = 0; |
| |
39 if (ps == 0) { |
| |
40 SYSTEM_INFO sysinfo; |
| |
41 GetSystemInfo(&sysinfo); |
| |
42 ps = (unsigned long) sysinfo.dwPageSize; |
| |
43 } |
| |
44 return ps; |
| |
45 } |
| |
46 #else |
| |
47 #include <unistd.h> |
| |
48 unsigned long cx_system_page_size(void) { |
| |
49 static unsigned long ps = 0; |
| |
50 if (ps == 0) { |
| |
51 long sc = sysconf(_SC_PAGESIZE); |
| |
52 if (sc < 0) { |
| |
53 // fallback for systems which do not report a value here |
| |
54 ps = 4096; // LCOV_EXCL_LINE |
| |
55 } else { |
| |
56 ps = (unsigned long) sc; |
| |
57 } |
| |
58 } |
| |
59 return ps; |
| |
60 } |
| |
61 #endif |
| |
62 |
| 34 static void *cx_malloc_stdlib( |
63 static void *cx_malloc_stdlib( |
| 35 cx_attr_unused void *d, |
64 cx_attr_unused void *d, |
| 36 size_t n |
65 size_t n |
| 37 ) { |
66 ) { |
| 38 return malloc(n); |
67 return malloc(n); |
| 154 int cxReallocate_( |
193 int cxReallocate_( |
| 155 const CxAllocator *allocator, |
194 const CxAllocator *allocator, |
| 156 void **mem, |
195 void **mem, |
| 157 size_t n |
196 size_t n |
| 158 ) { |
197 ) { |
| |
198 if (n == 0) { |
| |
199 cxFree(allocator, *mem); |
| |
200 *mem = NULL; |
| |
201 return 0; |
| |
202 } |
| 159 void *nmem = allocator->cl->realloc(allocator->data, *mem, n); |
203 void *nmem = allocator->cl->realloc(allocator->data, *mem, n); |
| 160 if (nmem == NULL) { |
204 if (nmem == NULL) { |
| 161 return 1; // LCOV_EXCL_LINE |
205 return 1; // LCOV_EXCL_LINE |
| 162 } else { |
206 } else { |
| 163 *mem = nmem; |
207 *mem = nmem; |
| 169 const CxAllocator *allocator, |
213 const CxAllocator *allocator, |
| 170 void **mem, |
214 void **mem, |
| 171 size_t nmemb, |
215 size_t nmemb, |
| 172 size_t size |
216 size_t size |
| 173 ) { |
217 ) { |
| |
218 if (nmemb == 0 || size == 0) { |
| |
219 cxFree(allocator, *mem); |
| |
220 *mem = NULL; |
| |
221 return 0; |
| |
222 } |
| 174 void *nmem = cxReallocArray(allocator, *mem, nmemb, size); |
223 void *nmem = cxReallocArray(allocator, *mem, nmemb, size); |
| 175 if (nmem == NULL) { |
224 if (nmem == NULL) { |
| 176 return 1; // LCOV_EXCL_LINE |
225 return 1; // LCOV_EXCL_LINE |
| 177 } else { |
226 } else { |
| 178 *mem = nmem; |
227 *mem = nmem; |