diff --git a/CMakeLists.txt b/CMakeLists.txt index 8251c7a..c76905a 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,10 +27,10 @@ else () set(DBG_FLAGS /fsanitize=address) endif () -add_executable(libguf_example src/test/example.c src/test/impls/str_impl.c src/test/impls/dict_impl.c src/test/impls/linalg_impl.c src/test/impls/alloc_tracker_impl.c) +add_executable(libguf_example src/test/example.c src/test/impls/str_impl.c src/test/impls/dict_impl.c src/test/impls/linalg_impl.c src/test/impls/alloc_tracker_impl.c ) target_include_directories(libguf_example PRIVATE src src/test) -add_executable(libguf_test src/test/test.cpp src/test/test_dbuf.cpp src/test/test_dict.cpp src/test/test_str.cpp src/test/test_ckdint.cpp src/test/test_utf8.cpp src/test/impls/init_impl.c src/test/impls/dbuf_impl.c src/test/impls/str_impl.c src/test/impls/dict_impl.c src/test/impls/rand_impl.c src/test/impls/sort_impl.c src/test/impls/linalg_impl.c src/test/impls/ckdint_impl.c src/test/impls/alloc_tracker_impl.c) +add_executable(libguf_test src/test/test.cpp src/test/test_dbuf.cpp src/test/test_dict.cpp src/test/test_str.cpp src/test/test_ckdint.cpp src/test/test_utf8.cpp src/test/impls/init_impl.c src/test/impls/dbuf_impl.c src/test/impls/str_impl.c src/test/impls/dict_impl.c src/test/impls/rand_impl.c src/test/impls/sort_impl.c src/test/impls/linalg_impl.c src/test/impls/ckdint_impl.c src/test/impls/alloc_tracker_impl.c src/test/impls/alloc_libc_impl.c ) target_include_directories(libguf_test PRIVATE src src/test) set_target_properties(libguf_example libguf_test PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX}) diff --git a/src/guf_alloc_libc.h b/src/guf_alloc_libc.h index bf521b6..f0142d2 100644 --- a/src/guf_alloc_libc.h +++ b/src/guf_alloc_libc.h @@ -2,6 +2,12 @@ is parametrized: no */ +#if defined(GUF_ALLOC_LIBC_IMPL_STATIC) + #define GUF_ALLOC_LIBC_KWRDS static inline +#else + #define GUF_ALLOC_LIBC_KWRDS +#endif + #ifndef GUF_ALLOC_LIBC_H #define GUF_ALLOC_LIBC_H #include @@ -14,7 +20,19 @@ typedef struct guf_libc_alloc_ctx { bool zero_init; } guf_libc_alloc_ctx; -static inline void *guf_libc_alloc(ptrdiff_t size, void *ctx) +#if !defined(GUF_ALLOC_LIBC_IMPL_STATIC) && !defined(GUF_ALLOC_LIBC_IMPL) + GUF_ALLOC_LIBC_KWRDS void *guf_libc_alloc(ptrdiff_t size, void *ctx); + GUF_ALLOC_LIBC_KWRDS void *guf_libc_realloc(void *ptr, ptrdiff_t old_size, ptrdiff_t new_size, void *ctx); + GUF_ALLOC_LIBC_KWRDS void *guf_libc_realloc(void *ptr, ptrdiff_t old_size, ptrdiff_t new_size, void *ctx); + GUF_ALLOC_LIBC_KWRDS void guf_libc_free(void *ptr, ptrdiff_t size, void *ctx); + + GUF_ALLOC_LIBC_KWRDS guf_allocator *guf_libc_allocator_init(guf_allocator *a, guf_libc_alloc_ctx *ctx); + GUF_ALLOC_LIBC_KWRDS guf_allocator guf_libc_allocator_new(guf_libc_alloc_ctx *ctx); +#endif + +#if defined(GUF_ALLOC_LIBC_IMPL_STATIC) || defined(GUF_ALLOC_LIBC_IMPL) + +GUF_ALLOC_LIBC_KWRDS void *guf_libc_alloc(ptrdiff_t size, void *ctx) { GUF_ASSERT_RELEASE(size >= 0); guf_libc_alloc_ctx *alloc_ctx = (guf_libc_alloc_ctx*)ctx; @@ -36,7 +54,7 @@ static inline void *guf_libc_alloc(ptrdiff_t size, void *ctx) return res; } -static inline void *guf_libc_realloc(void *ptr, ptrdiff_t old_size, ptrdiff_t new_size, void *ctx) +GUF_ALLOC_LIBC_KWRDS void *guf_libc_realloc(void *ptr, ptrdiff_t old_size, ptrdiff_t new_size, void *ctx) { GUF_ASSERT_RELEASE(ptr); GUF_ASSERT_RELEASE(old_size >= 0 && new_size >= 0); @@ -63,7 +81,7 @@ static inline void *guf_libc_realloc(void *ptr, ptrdiff_t old_size, ptrdiff_t ne return new_ptr; } -static inline void guf_libc_free(void *ptr, ptrdiff_t size, void *ctx) +GUF_ALLOC_LIBC_KWRDS void guf_libc_free(void *ptr, ptrdiff_t size, void *ctx) { free(ptr); @@ -74,7 +92,7 @@ static inline void guf_libc_free(void *ptr, ptrdiff_t size, void *ctx) } } -static inline guf_allocator *guf_libc_allocator_init(guf_allocator *a, guf_libc_alloc_ctx *ctx) +GUF_ALLOC_LIBC_KWRDS guf_allocator *guf_libc_allocator_init(guf_allocator *a, guf_libc_alloc_ctx *ctx) { GUF_ASSERT_RELEASE(a); a->alloc = guf_libc_alloc; @@ -84,12 +102,17 @@ static inline guf_allocator *guf_libc_allocator_init(guf_allocator *a, guf_libc_ return a; } -static inline guf_allocator guf_libc_allocator_new(guf_libc_alloc_ctx *ctx) +GUF_ALLOC_LIBC_KWRDS guf_allocator guf_libc_allocator_new(guf_libc_alloc_ctx *ctx) { guf_allocator a; guf_libc_allocator_init(&a, ctx); return a; } +#endif /* End impl */ -#endif +#endif /* End header-guard */ + +#undef GUF_ALLOC_LIBC_IMPL_STATIC +#undef GUF_ALLOC_LIBC_IMPL +#undef GUF_ALLOC_LIBC_KWRDS diff --git a/src/test/example.c b/src/test/example.c index b2488ef..5685519 100644 --- a/src/test/example.c +++ b/src/test/example.c @@ -3,9 +3,11 @@ #include #include -#include "guf_init.h" /* Must be included once */ +#include "guf_init.h" /* Must be included once (or compiled in a separate .c file and linked) */ +#define GUF_ALLOC_LIBC_IMPL_STATIC #include "guf_alloc_libc.h" + #include "guf_cstr.h" #include "guf_linalg.h" #include "guf_utils.h" diff --git a/src/test/impls/alloc_libc_impl.c b/src/test/impls/alloc_libc_impl.c new file mode 100644 index 0000000..813fd0e --- /dev/null +++ b/src/test/impls/alloc_libc_impl.c @@ -0,0 +1,2 @@ +#define GUF_ALLOC_LIBC_IMPL +#include "guf_alloc_libc.h"