29 #ifndef UCX_ALLOCATOR_H |
29 #ifndef UCX_ALLOCATOR_H |
30 #define UCX_ALLOCATOR_H |
30 #define UCX_ALLOCATOR_H |
31 |
31 |
32 #include <stdlib.h> |
32 #include <stdlib.h> |
33 |
33 |
|
34 /* LOW LEVEL API */ |
|
35 |
34 typedef void* cx_allocator; |
36 typedef void* cx_allocator; |
35 typedef void*(*cx_malloc_func)(cx_allocator a, size_t n); |
37 typedef void*(*cx_malloc_func)(cx_allocator a, size_t n); |
36 typedef void*(*cx_realloc_func)(cx_allocator a, void* mem, size_t n); |
38 typedef void*(*cx_realloc_func)(cx_allocator a, void* mem, size_t n); |
37 typedef void*(*cx_calloc_func)(cx_allocator a, size_t nelem, size_t n); |
39 typedef void*(*cx_calloc_func)(cx_allocator a, size_t nelem, size_t n); |
38 typedef void(*cx_free_func)(cx_allocator a, void* mem); |
40 typedef void(*cx_free_func)(cx_allocator a, void* mem); |
40 void* cx_malloc_stdlib(cx_allocator a, size_t n); |
42 void* cx_malloc_stdlib(cx_allocator a, size_t n); |
41 void* cx_realloc_stdlib(cx_allocator a, void* mem, size_t n); |
43 void* cx_realloc_stdlib(cx_allocator a, void* mem, size_t n); |
42 void* cx_calloc_stdlib(cx_allocator a, size_t nelem, size_t n); |
44 void* cx_calloc_stdlib(cx_allocator a, size_t nelem, size_t n); |
43 void cx_free_stdlib(cx_allocator a, void* mem); |
45 void cx_free_stdlib(cx_allocator a, void* mem); |
44 |
46 |
45 struct cx_allocator_s { |
47 struct cx_allocator_class { |
46 cx_malloc_func malloc; |
48 cx_malloc_func malloc; |
47 cx_realloc_func realloc; |
49 cx_realloc_func realloc; |
48 cx_calloc_func calloc; |
50 cx_calloc_func calloc; |
49 cx_free_func free; |
51 cx_free_func free; |
|
52 }; |
|
53 |
|
54 /* HIGH LEVEL API */ |
|
55 |
|
56 struct cx_allocator_s { |
|
57 struct cx_allocator_class allocatorClass; |
50 void* data; |
58 void* data; |
51 }; |
59 }; |
52 typedef struct cx_allocator_s* CxAllocator; |
60 typedef struct cx_allocator_s* CxAllocator; |
53 |
61 |
54 extern CxAllocator cxDefaultAllocator; |
62 extern CxAllocator cxDefaultAllocator; |
55 |
63 |
56 #define cxMalloc(a, n) (a)->malloc((a), n) |
64 void* cxMalloc(CxAllocator allocator, size_t n); |
57 #define cxRealloc(a, mem, n) (a)->realloc((a), mem, n) |
65 void* cxRealloc(CxAllocator allocator, void* mem, size_t n); |
58 #define cxCalloc(a, nelem, n) (a)->calloc((a), nelem, n) |
66 void* cxCalloc(CxAllocator allocator, size_t nelem, size_t n); |
59 #define cxFree(a, mem) (a)->free((a), mem) |
67 void cxFree(CxAllocator allocator, void* mem); |
60 |
68 |
61 #endif /* UCX_ALLOCATOR_H */ |
69 #endif /* UCX_ALLOCATOR_H */ |