ucx/allocator.c

changeset 30
d33eaaec15da
parent 22
112b85020dc9
equal deleted inserted replaced
29:b8c826c720f3 30:d33eaaec15da
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);
77 106
78 int cx_reallocate_( 107 int cx_reallocate_(
79 void **mem, 108 void **mem,
80 size_t n 109 size_t n
81 ) { 110 ) {
111 if (n == 0) {
112 free(*mem);
113 *mem = NULL;
114 return 0;
115 }
82 void *nmem = realloc(*mem, n); 116 void *nmem = realloc(*mem, n);
83 if (nmem == NULL) { 117 if (nmem == NULL) {
84 return 1; // LCOV_EXCL_LINE 118 return 1; // LCOV_EXCL_LINE
85 } else { 119 } else {
86 *mem = nmem; 120 *mem = nmem;
91 int cx_reallocatearray_( 125 int cx_reallocatearray_(
92 void **mem, 126 void **mem,
93 size_t nmemb, 127 size_t nmemb,
94 size_t size 128 size_t size
95 ) { 129 ) {
130 if (nmemb == 0 || size == 0) {
131 free(*mem);
132 *mem = NULL;
133 return 0;
134 }
96 size_t n; 135 size_t n;
97 if (cx_szmul(nmemb, size, &n)) { 136 if (cx_szmul(nmemb, size, &n)) {
98 errno = EOVERFLOW; 137 errno = EOVERFLOW;
99 return 1; 138 return 1;
100 } else { 139 } else {
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;
192 const CxAllocator *allocator, 241 const CxAllocator *allocator,
193 void *mem 242 void *mem
194 ) { 243 ) {
195 allocator->cl->free(allocator->data, mem); 244 allocator->cl->free(allocator->data, mem);
196 } 245 }
246
247 void cxFreeDefault(void *mem) {
248 cxDefaultAllocator->cl->free(cxDefaultAllocator->data, mem);
249 }

mercurial