From c0ee8e368c324056741b2c4d35ee9e8daa8bd217 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 12 Aug 2025 17:31:44 +0800 Subject: [PATCH 1/2] feat allocator: added malloc zalloc class templates Signed-off-by: John Sanpe --- include/bfdev/allocator.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/bfdev/allocator.h b/include/bfdev/allocator.h index 02a86205..ee103699 100644 --- a/include/bfdev/allocator.h +++ b/include/bfdev/allocator.h @@ -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_ */ From d6a4306fa16f53c531ce1aa624df8563669767b5 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 12 Aug 2025 17:32:04 +0800 Subject: [PATCH 2/2] feat allocator: added malloc zalloc class templates examples Signed-off-by: John Sanpe --- examples/allocator/CMakeLists.txt | 6 ++++ examples/allocator/guards.c | 60 +++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 examples/allocator/guards.c diff --git a/examples/allocator/CMakeLists.txt b/examples/allocator/CMakeLists.txt index 99f7a7a1..2204df3e 100644 --- a/examples/allocator/CMakeLists.txt +++ b/examples/allocator/CMakeLists.txt @@ -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 ) diff --git a/examples/allocator/guards.c b/examples/allocator/guards.c new file mode 100644 index 00000000..6c7807f8 --- /dev/null +++ b/examples/allocator/guards.c @@ -0,0 +1,60 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2025 John Sanpe + */ + +#include +#include +#include + +#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; +}