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
6 changes: 6 additions & 0 deletions examples/allocator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ add_executable(allocator-simple simple.c)
target_link_libraries(allocator-simple bfdev)
add_test(allocator-simple allocator-simple)

add_executable(allocator-guards guards.c)
target_link_libraries(allocator-guards bfdev)
add_test(allocator-guards allocator-guards)

if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev")
install(FILES
simple.c
guards.c
DESTINATION
${CMAKE_INSTALL_DOCDIR}/examples/allocator
)

install(TARGETS
allocator-simple
allocator-guards
DESTINATION
${CMAKE_INSTALL_DOCDIR}/bin
)
Expand Down
60 changes: 60 additions & 0 deletions examples/allocator/guards.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright(c) 2025 John Sanpe <sanpeqf@gmail.com>
*/

#include <string.h>
#include <bfdev/allocator.h>
#include <bfdev/size.h>

#define TEST_SIZE BFDEV_SZ_64KiB

static int
test_cleanup(void)
{
BFDEV_CLEAN(bfdev_free) void *ptr;

ptr = bfdev_malloc(NULL, TEST_SIZE);
if (!ptr)
return 1;
bzero(ptr, TEST_SIZE);

return 0;
}

static int
test_class_malloc(void)
{
BFDEV_CLASS(bfdev_malloc, ptr)(TEST_SIZE);

if (!ptr)
return 1;
bzero(ptr, TEST_SIZE);

return 0;
}

static int
test_class_zalloc(void)
{
BFDEV_CLASS(bfdev_zalloc, ptr)(TEST_SIZE);

if (!ptr)
return 1;
bzero(ptr, TEST_SIZE);

return 0;
}

int
main(int argc, char const *argv[])
{
int retval;

if ((retval = test_cleanup()) ||
(retval = test_class_malloc()) ||
(retval = test_class_zalloc()))
return retval;

return 0;
}
12 changes: 12 additions & 0 deletions include/bfdev/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ BFDEV_DEFINE_CLEAN(bfdev_free, void *,
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_DEFINE_CLASS(bfdev_zalloc, void *,
bfdev_zalloc(BFDEV_NULL, size),
bfdev_free(BFDEV_NULL, _T),
bfdev_size_t size
)

BFDEV_END_DECLS

#endif /* _BFDEV_ALLOCATOR_H_ */
Loading