27 */ |
27 */ |
28 |
28 |
29 #include "cx/allocator.h" |
29 #include "cx/allocator.h" |
30 |
30 |
31 #include <stdlib.h> |
31 #include <stdlib.h> |
32 #include <errno.h> |
|
33 |
32 |
34 void *cx_malloc_stdlib(void *unused, size_t n) { |
33 __attribute__((malloc)) |
|
34 void *cx_malloc_stdlib(__attribute__((unused)) void *d, size_t n) { |
35 return malloc(n); |
35 return malloc(n); |
36 } |
36 } |
37 |
37 |
38 void *cx_realloc_stdlib(void *unused, void *mem, size_t n) { |
38 void *cx_realloc_stdlib(__attribute__((unused)) void *d, void *mem, size_t n) { |
39 return realloc(mem, n); |
39 return realloc(mem, n); |
40 } |
40 } |
41 |
41 |
42 void *cx_calloc_stdlib(void *unused, size_t nelem, size_t n) { |
42 __attribute__((malloc)) |
|
43 void *cx_calloc_stdlib(__attribute__((unused)) void *d, size_t nelem, size_t n) { |
43 return calloc(nelem, n); |
44 return calloc(nelem, n); |
44 } |
45 } |
45 |
46 |
46 void cx_free_stdlib(void *unused, void *mem) { |
47 __attribute__((nonnull)) |
|
48 void cx_free_stdlib(__attribute__((unused)) void *d, void *mem) { |
47 free(mem); |
49 free(mem); |
48 } |
50 } |
49 |
51 |
50 cx_allocator_class cx_default_allocator_class = { |
52 cx_allocator_class cx_default_allocator_class = { |
51 cx_malloc_stdlib, |
53 cx_malloc_stdlib, |
58 &cx_default_allocator_class, |
60 &cx_default_allocator_class, |
59 NULL |
61 NULL |
60 }; |
62 }; |
61 CxAllocator cxDefaultAllocator = &cx_default_allocator; |
63 CxAllocator cxDefaultAllocator = &cx_default_allocator; |
62 |
64 |
|
65 /* IMPLEMENTATION OF HIGH LEVEL API */ |
|
66 |
63 void *cxMalloc(CxAllocator allocator, size_t n) { |
67 void *cxMalloc(CxAllocator allocator, size_t n) { |
64 return allocator->cl->malloc(allocator->data, n); |
68 return allocator->cl->malloc(allocator->data, n); |
65 } |
69 } |
66 |
70 |
67 void *cxRealloc(CxAllocator allocator, void *mem, size_t n) { |
71 void *cxRealloc(CxAllocator allocator, void *mem, size_t n) { |
68 return allocator->cl->realloc(allocator->data, mem, n); |
72 return allocator->cl->realloc(allocator->data, mem, n); |
69 } |
73 } |
70 |
74 |
71 int cxReallocate(CxAllocator allocator, void **mem, size_t n) { |
75 int cxReallocate(CxAllocator allocator, void **mem, size_t n) { |
72 if (mem == NULL) { |
|
73 errno = EINVAL; |
|
74 return 1; |
|
75 } |
|
76 void* nmem = allocator->cl->realloc(allocator->data, *mem, n); |
76 void* nmem = allocator->cl->realloc(allocator->data, *mem, n); |
77 if (nmem == NULL) { |
77 if (nmem == NULL) { |
78 return 1; |
78 return 1; |
79 } else { |
79 } else { |
80 *mem = nmem; |
80 *mem = nmem; |