Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/allocator/guards.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test_cleanup(void)
static int
test_class_malloc(void)
{
BFDEV_CLASS(bfdev_malloc, ptr)(TEST_SIZE);
BFDEV_CLASS(bfdev_malloc, ptr)(NULL, TEST_SIZE);

if (!ptr)
return 1;
Expand All @@ -37,7 +37,7 @@ test_class_malloc(void)
static int
test_class_zalloc(void)
{
BFDEV_CLASS(bfdev_zalloc, ptr)(TEST_SIZE);
BFDEV_CLASS(bfdev_zalloc, ptr)(NULL, TEST_SIZE);

if (!ptr)
return 1;
Expand Down
50 changes: 44 additions & 6 deletions include/bfdev/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,59 @@ bfdev_realloc_array(const bfdev_alloc_t *alloc,
return bfdev_realloc(alloc, block, size * nr);
}

static inline void *
bfdev_malloc_with_class(const bfdev_alloc_t *alloc, bfdev_size_t size)
{
const bfdev_alloc_t **palloc;

palloc = bfdev_malloc(BFDEV_NULL, sizeof(*palloc) + size);
if (bfdev_unlikely(!palloc))
return BFDEV_NULL;
*palloc = alloc;

return (void *)(palloc + 1);
}

static inline void *
bfdev_zalloc_with_class(const bfdev_alloc_t *alloc, bfdev_size_t size)
{
const bfdev_alloc_t **palloc;

palloc = bfdev_zalloc(BFDEV_NULL, sizeof(*palloc) + size);
if (bfdev_unlikely(!palloc))
return BFDEV_NULL;
*palloc = alloc;

return (void *)(palloc + 1);
}

static inline void
bfdev_free_with_class(void *block)
{
const bfdev_alloc_t **palloc;

if (bfdev_unlikely(!block))
return;

palloc = (const bfdev_alloc_t **)block - 1;
bfdev_free(*palloc, palloc);
}

BFDEV_DEFINE_CLEAN(bfdev_free, void *,
if (!BFDEV_IS_INVAL(_T))
bfdev_free(BFDEV_NULL, _T);
)

BFDEV_DEFINE_CLASS(bfdev_malloc, void *,
bfdev_malloc(BFDEV_NULL, size),
bfdev_free(BFDEV_NULL, _T),
bfdev_size_t size
bfdev_malloc_with_class(alloc, size),
bfdev_free_with_class(_T),
const bfdev_alloc_t *alloc, bfdev_size_t size
)

BFDEV_DEFINE_CLASS(bfdev_zalloc, void *,
bfdev_zalloc(BFDEV_NULL, size),
bfdev_free(BFDEV_NULL, _T),
bfdev_size_t size
bfdev_zalloc_with_class(alloc, size),
bfdev_free_with_class(_T),
const bfdev_alloc_t *alloc, bfdev_size_t size
)

BFDEV_END_DECLS
Expand Down
Loading