26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
28 |
28 |
29 #include "cx/allocator.h" |
29 #include "cx/allocator.h" |
30 |
30 |
31 __attribute__((__malloc__, __alloc_size__(2))) |
31 #include <errno.h> |
|
32 |
32 static void *cx_malloc_stdlib( |
33 static void *cx_malloc_stdlib( |
33 __attribute__((__unused__)) void *d, |
34 cx_attr_unused void *d, |
34 size_t n |
35 size_t n |
35 ) { |
36 ) { |
36 return malloc(n); |
37 return malloc(n); |
37 } |
38 } |
38 |
39 |
39 __attribute__((__warn_unused_result__, __alloc_size__(3))) |
|
40 static void *cx_realloc_stdlib( |
40 static void *cx_realloc_stdlib( |
41 __attribute__((__unused__)) void *d, |
41 cx_attr_unused void *d, |
42 void *mem, |
42 void *mem, |
43 size_t n |
43 size_t n |
44 ) { |
44 ) { |
45 return realloc(mem, n); |
45 return realloc(mem, n); |
46 } |
46 } |
47 |
47 |
48 __attribute__((__malloc__, __alloc_size__(2, 3))) |
|
49 static void *cx_calloc_stdlib( |
48 static void *cx_calloc_stdlib( |
50 __attribute__((__unused__)) void *d, |
49 cx_attr_unused void *d, |
51 size_t nelem, |
50 size_t nelem, |
52 size_t n |
51 size_t n |
53 ) { |
52 ) { |
54 return calloc(nelem, n); |
53 return calloc(nelem, n); |
55 } |
54 } |
56 |
55 |
57 __attribute__((__nonnull__)) |
|
58 static void cx_free_stdlib( |
56 static void cx_free_stdlib( |
59 __attribute__((__unused__)) void *d, |
57 cx_attr_unused void *d, |
60 void *mem |
58 void *mem |
61 ) { |
59 ) { |
62 free(mem); |
60 free(mem); |
63 } |
61 } |
64 |
62 |
73 &cx_default_allocator_class, |
71 &cx_default_allocator_class, |
74 NULL |
72 NULL |
75 }; |
73 }; |
76 CxAllocator *cxDefaultAllocator = &cx_default_allocator; |
74 CxAllocator *cxDefaultAllocator = &cx_default_allocator; |
77 |
75 |
78 |
76 #undef cx_reallocate |
79 int cx_reallocate( |
77 int cx_reallocate( |
80 void **mem, |
78 void **mem, |
81 size_t n |
79 size_t n |
82 ) { |
80 ) { |
83 void *nmem = realloc(*mem, n); |
81 void *nmem = realloc(*mem, n); |
84 if (nmem == NULL) { |
82 if (nmem == NULL) { |
85 return 1; |
83 return 1; // LCOV_EXCL_LINE |
86 } else { |
84 } else { |
87 *mem = nmem; |
85 *mem = nmem; |
88 return 0; |
86 return 0; |
89 } |
87 } |
90 } |
88 } |
91 |
89 |
|
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 |
92 // IMPLEMENTATION OF HIGH LEVEL API |
111 // IMPLEMENTATION OF HIGH LEVEL API |
93 |
112 |
94 void *cxMalloc( |
113 void *cxMalloc( |
95 CxAllocator const *allocator, |
114 const CxAllocator *allocator, |
96 size_t n |
115 size_t n |
97 ) { |
116 ) { |
98 return allocator->cl->malloc(allocator->data, n); |
117 return allocator->cl->malloc(allocator->data, n); |
99 } |
118 } |
100 |
119 |
101 void *cxRealloc( |
120 void *cxRealloc( |
102 CxAllocator const *allocator, |
121 const CxAllocator *allocator, |
103 void *mem, |
122 void *mem, |
104 size_t n |
123 size_t n |
105 ) { |
124 ) { |
106 return allocator->cl->realloc(allocator->data, mem, n); |
125 return allocator->cl->realloc(allocator->data, mem, n); |
107 } |
126 } |
108 |
127 |
|
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 |
109 int cxReallocate( |
144 int cxReallocate( |
110 CxAllocator const *allocator, |
145 const CxAllocator *allocator, |
111 void **mem, |
146 void **mem, |
112 size_t n |
147 size_t n |
113 ) { |
148 ) { |
114 void *nmem = allocator->cl->realloc(allocator->data, *mem, n); |
149 void *nmem = allocator->cl->realloc(allocator->data, *mem, n); |
115 if (nmem == NULL) { |
150 if (nmem == NULL) { |
116 return 1; |
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 |
117 } else { |
168 } else { |
118 *mem = nmem; |
169 *mem = nmem; |
119 return 0; |
170 return 0; |
120 } |
171 } |
121 } |
172 } |
122 |
173 |
123 void *cxCalloc( |
174 void *cxCalloc( |
124 CxAllocator const *allocator, |
175 const CxAllocator *allocator, |
125 size_t nelem, |
176 size_t nelem, |
126 size_t n |
177 size_t n |
127 ) { |
178 ) { |
128 return allocator->cl->calloc(allocator->data, nelem, n); |
179 return allocator->cl->calloc(allocator->data, nelem, n); |
129 } |
180 } |
130 |
181 |
131 void cxFree( |
182 void cxFree( |
132 CxAllocator const *allocator, |
183 const CxAllocator *allocator, |
133 void *mem |
184 void *mem |
134 ) { |
185 ) { |
135 allocator->cl->free(allocator->data, mem); |
186 allocator->cl->free(allocator->data, mem); |
136 } |
187 } |