#include "cx/allocator.h"
__attribute__((__malloc__, __alloc_size__(
2)))
static void *cx_malloc_stdlib(
__attribute__((__unused__))
void *d,
size_t n
) {
return malloc(n);
}
__attribute__((__warn_unused_result__, __alloc_size__(
3)))
static void *cx_realloc_stdlib(
__attribute__((__unused__))
void *d,
void *mem,
size_t n
) {
return realloc(mem, n);
}
__attribute__((__malloc__, __alloc_size__(
2,
3)))
static void *cx_calloc_stdlib(
__attribute__((__unused__))
void *d,
size_t nelem,
size_t n
) {
return calloc(nelem, n);
}
__attribute__((__nonnull__))
static void cx_free_stdlib(
__attribute__((__unused__))
void *d,
void *mem
) {
free(mem);
}
static cx_allocator_class cx_default_allocator_class = {
cx_malloc_stdlib,
cx_realloc_stdlib,
cx_calloc_stdlib,
cx_free_stdlib
};
struct cx_allocator_s cx_default_allocator = {
&cx_default_allocator_class,
NULL
};
CxAllocator *cxDefaultAllocator = &cx_default_allocator;
void *cxMalloc(
CxAllocator
const *allocator,
size_t n
) {
return allocator->cl->malloc(allocator->data, n);
}
void *cxRealloc(
CxAllocator
const *allocator,
void *mem,
size_t n
) {
return allocator->cl->realloc(allocator->data, mem, n);
}
int cxReallocate(
CxAllocator
const *allocator,
void **mem,
size_t n
) {
void *nmem = allocator->cl->realloc(allocator->data, *mem, n);
if (nmem ==
NULL) {
return 1;
}
else {
*mem = nmem;
return 0;
}
}
void *cxCalloc(
CxAllocator
const *allocator,
size_t nelem,
size_t n
) {
return allocator->cl->calloc(allocator->data, nelem, n);
}
void cxFree(
CxAllocator
const *allocator,
void *mem
) {
allocator->cl->free(allocator->data, mem);
}