#ifndef UCX_ALLOCATOR_H
#define UCX_ALLOCATOR_H
#include "common.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
void *(*malloc)(
void *data,
size_t n);
void *(*realloc)(
void *data,
void *mem,
size_t n);
void *(*calloc)(
void *data,
size_t nmemb,
size_t size);
void (*free)(
void *data,
void *mem);
} cx_allocator_class;
struct cx_allocator_s {
cx_allocator_class *cl;
void *data;
};
typedef struct cx_allocator_s CxAllocator;
CX_EXPORT extern const CxAllocator *
const cxStdlibAllocator;
CX_EXPORT extern const CxAllocator * cxDefaultAllocator;
typedef void (*cx_destructor_func)(
void *memory);
typedef void (*cx_destructor_func2)(
void *data,
void *memory);
typedef void*(cx_clone_func)(
void *target,
const void *source,
const CxAllocator *allocator,
void *data);
cx_attr_nodiscard
CX_EXPORT unsigned long cx_system_page_size(
void);
cx_attr_nonnull cx_attr_nodiscard
CX_EXPORT int cx_reallocate_(
void **mem,
size_t n);
cx_attr_nonnull cx_attr_nodiscard
CX_EXPORT int cx_reallocatearray_(
void **mem,
size_t nmemb,
size_t size);
#define cx_reallocate(mem, n) cx_reallocate_((
void**)(mem), n)
#define cx_reallocatearray(mem, nmemb, size) \
cx_reallocatearray_((
void**)(mem), nmemb, size)
#define cx_zalloc(n) calloc(
1, n)
cx_attr_nonnull_arg(
1)
CX_EXPORT void cxFree(
const CxAllocator *allocator,
void *mem);
cx_attr_nodiscard cx_attr_nonnull
cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(
2)
CX_EXPORT void *cxMalloc(
const CxAllocator *allocator,
size_t n);
cx_attr_nodiscard cx_attr_nonnull_arg(
1)
cx_attr_dealloc_ucx cx_attr_allocsize(
3)
CX_EXPORT void *cxRealloc(
const CxAllocator *allocator,
void *mem,
size_t n);
cx_attr_nodiscard cx_attr_nonnull_arg(
1)
cx_attr_dealloc_ucx cx_attr_allocsize(
3,
4)
CX_EXPORT void *cxReallocArray(
const CxAllocator *allocator,
void *mem,
size_t nmemb,
size_t size);
cx_attr_nodiscard cx_attr_nonnull
CX_EXPORT int cxReallocate_(
const CxAllocator *allocator,
void **mem,
size_t n);
#define cxReallocate(allocator, mem, n) \
cxReallocate_(allocator, (
void**)(mem), n)
cx_attr_nodiscard cx_attr_nonnull
CX_EXPORT int cxReallocateArray_(
const CxAllocator *allocator,
void **mem,
size_t nmemb,
size_t size);
#define cxReallocateArray(allocator, mem, nmemb, size) \
cxReallocateArray_(allocator, (
void**) (mem), nmemb, size)
cx_attr_nonnull_arg(
1) cx_attr_nodiscard
cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(
2,
3)
CX_EXPORT void *cxCalloc(
const CxAllocator *allocator,
size_t nmemb,
size_t size);
cx_attr_nodiscard cx_attr_nonnull
cx_attr_malloc cx_attr_dealloc_ucx cx_attr_allocsize(
2)
CX_EXPORT void *cxZalloc(
const CxAllocator *allocator,
size_t n);
#define cxMallocDefault(...) cxMalloc(cxDefaultAllocator,
__VA_ARGS__)
#define cxZallocDefault(...) cxZalloc(cxDefaultAllocator,
__VA_ARGS__)
#define cxCallocDefault(...) cxCalloc(cxDefaultAllocator,
__VA_ARGS__)
#define cxReallocDefault(...) cxRealloc(cxDefaultAllocator,
__VA_ARGS__)
#define cxReallocateDefault(...) cxReallocate(cxDefaultAllocator,
__VA_ARGS__)
#define cxReallocateArrayDefault(...) cxReallocateArray(cxDefaultAllocator,
__VA_ARGS__)
#define cxReallocArrayDefault(...) cxReallocArray(cxDefaultAllocator,
__VA_ARGS__)
CX_EXPORT void cxFreeDefault(
void *mem);
#ifdef __cplusplus
}
#endif
#endif