libguf/src/guf_alloc.h
2025-12-21 17:30:13 +01:00

32 lines
1.0 KiB
C
Executable File

/*
is parametrized: no
*/
#ifndef GUF_ALLOC_H
#define GUF_ALLOC_H
#include "guf_common.h"
#include "guf_assert.h"
// A custom allocator interface as described in https://nullprogram.com/blog/2023/12/17/ (last-retrieved 2025-01-25)
typedef struct guf_allocator {
void *(*alloc)(ptrdiff_t size, void *ctx);
void *(*realloc)(void *ptr, ptrdiff_t old_size, ptrdiff_t new_size, void *ctx);
void (*free)(void *ptr, ptrdiff_t size, void *ctx);
void *ctx;
} guf_allocator;
// typedef enum guf_alloc_fn_type {
// GUF_ALLOC_FN_TYPE_ALLOC,
// GUF_ALLOC_FN_TYPE_REALLOC,
// GUF_ALLOC_FN_TYPE_FREE,
// } guf_alloc_fn_type;
/*
GUF_ALLOC_MAX_BYTES: Largest number of bytes an allocated buffer of elements of TYPE can have.
GUF_ALLOC_MAX_CAPACITY: Largest number of elements an allocated buffer of elements of TYPE can have.
*/
#define GUF_ALLOC_MAX_BYTES(TYPE) ( PTRDIFF_MAX - ( PTRDIFF_MAX % sizeof(TYPE) ) )
#define GUF_ALLOC_MAX_CAPACITY(TYPE) ( GUF_ALLOC_MAX_BYTES(TYPE) / sizeof(TYPE) )
#endif