1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 #ifndef UCX_ALLOCATOR_H
34 #define UCX_ALLOCATOR_H
35
36 #include "common.h"
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42
43
44
45 typedef struct {
46
47
48
49 void *(*malloc)(
void *data,
size_t n);
50
51
52
53
54 void *(*realloc)(
void *data,
void *mem,
size_t n);
55
56
57
58
59 void *(*calloc)(
void *data,
size_t nmemb,
size_t size);
60
61
62
63
64 void (*free)(
void *data,
void *mem);
65 } cx_allocator_class;
66
67
68
69
70 struct cx_allocator_s {
71
72
73
74 cx_allocator_class *cl;
75
76
77
78 void *data;
79 };
80
81
82
83
84 typedef struct cx_allocator_s CxAllocator;
85
86
87
88
89 CX_EXPORT extern const CxAllocator *
const cxStdlibAllocator;
90
91
92
93
94
95 CX_EXPORT extern const CxAllocator * cxDefaultAllocator;
96
97
98
99
100
101
102
103
104
105
106
107 typedef void (*cx_destructor_func)(
void *memory);
108
109
110
111
112
113
114
115
116
117
118
119
120 typedef void (*cx_destructor_func2)(
void *data,
void *memory);
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 typedef void*(cx_clone_func)(
void *target,
const void *source,
146 const CxAllocator *allocator,
void *data);
147
148
149
150
151
152
153
154
155
156 cx_attr_nodiscard
157 CX_EXPORT unsigned long cx_system_page_size(
void);
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 cx_attr_nonnull cx_attr_nodiscard
175 CX_EXPORT int cx_reallocate_(
void **mem,
size_t n);
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 cx_attr_nonnull cx_attr_nodiscard
197 CX_EXPORT int cx_reallocatearray_(
void **mem,
size_t nmemb,
size_t size);
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214 #define cx_reallocate(mem, n) cx_reallocate_((
void**)(mem), n)
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 #define cx_reallocatearray(mem, nmemb, size) \
235 cx_reallocatearray_((
void**)(mem), nmemb, size)
236
237
238
239
240
241
242
243 #define cx_zalloc(n) calloc(
1, n)
244
245
246
247
248
249
250
251
252
253 cx_attr_nonnull_arg(
1)
254 CX_EXPORT void cxFree(
const CxAllocator *allocator,
void *mem);
255
256
257
258
259
260
261
262
263 cx_attr_nodiscard cx_attr_nonnull
264 cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(
2)
265 CX_EXPORT void *cxMalloc(
const CxAllocator *allocator,
size_t n);
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280 cx_attr_nodiscard cx_attr_nonnull_arg(
1)
281 cx_attr_dealloc_ucx cx_attr_allocsize(
3)
282 CX_EXPORT void *cxRealloc(
const CxAllocator *allocator,
void *mem,
size_t n);
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 cx_attr_nodiscard cx_attr_nonnull_arg(
1)
303 cx_attr_dealloc_ucx cx_attr_allocsize(
3,
4)
304 CX_EXPORT void *cxReallocArray(
const CxAllocator *allocator,
305 void *mem,
size_t nmemb,
size_t size);
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323 cx_attr_nodiscard cx_attr_nonnull
324 CX_EXPORT int cxReallocate_(
const CxAllocator *allocator,
void **mem,
size_t n);
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342 #define cxReallocate(allocator, mem, n) \
343 cxReallocate_(allocator, (
void**)(mem), n)
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364 cx_attr_nodiscard cx_attr_nonnull
365 CX_EXPORT int cxReallocateArray_(
const CxAllocator *allocator,
366 void **mem,
size_t nmemb,
size_t size);
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387 #define cxReallocateArray(allocator, mem, nmemb, size) \
388 cxReallocateArray_(allocator, (
void**) (mem), nmemb, size)
389
390
391
392
393
394
395
396
397
398 cx_attr_nonnull_arg(
1) cx_attr_nodiscard
399 cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(
2,
3)
400 CX_EXPORT void *cxCalloc(
const CxAllocator *allocator,
size_t nmemb,
size_t size);
401
402
403
404
405
406
407
408
409 cx_attr_nodiscard cx_attr_nonnull
410 cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(
2)
411 CX_EXPORT void *cxZalloc(
const CxAllocator *allocator,
size_t n);
412
413
414
415
416 #define cxMallocDefault(...) cxMalloc(cxDefaultAllocator,
__VA_ARGS__)
417
418
419
420 #define cxZallocDefault(...) cxZalloc(cxDefaultAllocator,
__VA_ARGS__)
421
422
423
424 #define cxCallocDefault(...) cxCalloc(cxDefaultAllocator,
__VA_ARGS__)
425
426
427
428 #define cxReallocDefault(...) cxRealloc(cxDefaultAllocator,
__VA_ARGS__)
429
430
431
432 #define cxReallocateDefault(...) cxReallocate(cxDefaultAllocator,
__VA_ARGS__)
433
434
435
436 #define cxReallocateArrayDefault(...) cxReallocateArray(cxDefaultAllocator,
__VA_ARGS__)
437
438
439
440 #define cxReallocArrayDefault(...) cxReallocArray(cxDefaultAllocator,
__VA_ARGS__)
441
442
443
444 CX_EXPORT void cxFreeDefault(
void *mem);
445
446 #ifdef __cplusplus
447 }
448 #endif
449
450 #endif
451