ucx/allocator.c

changeset 16
04c9f8d8f03b
parent 11
0aa8cbd7912e
child 21
5ea41679e15d
equal deleted inserted replaced
15:862ab606ee06 16:04c9f8d8f03b
28 28
29 #include "cx/allocator.h" 29 #include "cx/allocator.h"
30 30
31 #include <errno.h> 31 #include <errno.h>
32 32
33 __attribute__((__malloc__, __alloc_size__(2)))
34 static void *cx_malloc_stdlib( 33 static void *cx_malloc_stdlib(
35 cx_attr_unused void *d, 34 cx_attr_unused void *d,
36 size_t n 35 size_t n
37 ) { 36 ) {
38 return malloc(n); 37 return malloc(n);
39 } 38 }
40 39
41 __attribute__((__warn_unused_result__, __alloc_size__(3)))
42 static void *cx_realloc_stdlib( 40 static void *cx_realloc_stdlib(
43 cx_attr_unused void *d, 41 cx_attr_unused void *d,
44 void *mem, 42 void *mem,
45 size_t n 43 size_t n
46 ) { 44 ) {
47 return realloc(mem, n); 45 return realloc(mem, n);
48 } 46 }
49 47
50 __attribute__((__malloc__, __alloc_size__(2, 3)))
51 static void *cx_calloc_stdlib( 48 static void *cx_calloc_stdlib(
52 cx_attr_unused void *d, 49 cx_attr_unused void *d,
53 size_t nelem, 50 size_t nmemb,
54 size_t n 51 size_t size
55 ) { 52 ) {
56 return calloc(nelem, n); 53 return calloc(nmemb, size);
57 } 54 }
58 55
59 __attribute__((__nonnull__))
60 static void cx_free_stdlib( 56 static void cx_free_stdlib(
61 cx_attr_unused void *d, 57 cx_attr_unused void *d,
62 void *mem 58 void *mem
63 ) { 59 ) {
64 free(mem); 60 free(mem);
73 69
74 struct cx_allocator_s cx_default_allocator = { 70 struct cx_allocator_s cx_default_allocator = {
75 &cx_default_allocator_class, 71 &cx_default_allocator_class,
76 NULL 72 NULL
77 }; 73 };
78 CxAllocator *cxDefaultAllocator = &cx_default_allocator; 74 const CxAllocator * const cxDefaultAllocator = &cx_default_allocator;
79 75
80 #undef cx_reallocate 76 int cx_reallocate_(
81 int cx_reallocate(
82 void **mem, 77 void **mem,
83 size_t n 78 size_t n
84 ) { 79 ) {
85 void *nmem = realloc(*mem, n); 80 void *nmem = realloc(*mem, n);
86 if (nmem == NULL) { 81 if (nmem == NULL) {
89 *mem = nmem; 84 *mem = nmem;
90 return 0; 85 return 0;
91 } 86 }
92 } 87 }
93 88
94 #undef cx_reallocatearray 89 int cx_reallocatearray_(
95 int cx_reallocatearray(
96 void **mem, 90 void **mem,
97 size_t nmemb, 91 size_t nmemb,
98 size_t size 92 size_t size
99 ) { 93 ) {
100 size_t n; 94 size_t n;
142 } else { 136 } else {
143 return allocator->cl->realloc(allocator->data, mem, n); 137 return allocator->cl->realloc(allocator->data, mem, n);
144 } 138 }
145 } 139 }
146 140
147 #undef cxReallocate 141 int cxReallocate_(
148 int cxReallocate(
149 const CxAllocator *allocator, 142 const CxAllocator *allocator,
150 void **mem, 143 void **mem,
151 size_t n 144 size_t n
152 ) { 145 ) {
153 void *nmem = allocator->cl->realloc(allocator->data, *mem, n); 146 void *nmem = allocator->cl->realloc(allocator->data, *mem, n);
157 *mem = nmem; 150 *mem = nmem;
158 return 0; 151 return 0;
159 } 152 }
160 } 153 }
161 154
162 #undef cxReallocateArray 155 int cxReallocateArray_(
163 int cxReallocateArray(
164 const CxAllocator *allocator, 156 const CxAllocator *allocator,
165 void **mem, 157 void **mem,
166 size_t nmemb, 158 size_t nmemb,
167 size_t size 159 size_t size
168 ) { 160 ) {
175 } 167 }
176 } 168 }
177 169
178 void *cxCalloc( 170 void *cxCalloc(
179 const CxAllocator *allocator, 171 const CxAllocator *allocator,
180 size_t nelem, 172 size_t nmemb,
181 size_t n 173 size_t size
182 ) { 174 ) {
183 return allocator->cl->calloc(allocator->data, nelem, n); 175 return allocator->cl->calloc(allocator->data, nmemb, size);
184 } 176 }
185 177
186 void cxFree( 178 void cxFree(
187 const CxAllocator *allocator, 179 const CxAllocator *allocator,
188 void *mem 180 void *mem

mercurial