Add static/non-static option for guf_alloc_libc.h
This commit is contained in:
parent
894d9e8fc4
commit
776c4a8899
@ -27,10 +27,10 @@ else ()
|
|||||||
set(DBG_FLAGS /fsanitize=address)
|
set(DBG_FLAGS /fsanitize=address)
|
||||||
endif ()
|
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)
|
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)
|
target_include_directories(libguf_test PRIVATE src src/test)
|
||||||
|
|
||||||
set_target_properties(libguf_example libguf_test PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
|
set_target_properties(libguf_example libguf_test PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
|
||||||
|
|||||||
@ -2,6 +2,12 @@
|
|||||||
is parametrized: no
|
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
|
#ifndef GUF_ALLOC_LIBC_H
|
||||||
#define GUF_ALLOC_LIBC_H
|
#define GUF_ALLOC_LIBC_H
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
@ -14,7 +20,19 @@ typedef struct guf_libc_alloc_ctx {
|
|||||||
bool zero_init;
|
bool zero_init;
|
||||||
} guf_libc_alloc_ctx;
|
} 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_ASSERT_RELEASE(size >= 0);
|
||||||
guf_libc_alloc_ctx *alloc_ctx = (guf_libc_alloc_ctx*)ctx;
|
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;
|
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(ptr);
|
||||||
GUF_ASSERT_RELEASE(old_size >= 0 && new_size >= 0);
|
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;
|
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);
|
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);
|
GUF_ASSERT_RELEASE(a);
|
||||||
a->alloc = guf_libc_alloc;
|
a->alloc = guf_libc_alloc;
|
||||||
@ -84,12 +102,17 @@ static inline guf_allocator *guf_libc_allocator_init(guf_allocator *a, guf_libc_
|
|||||||
return a;
|
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_allocator a;
|
||||||
guf_libc_allocator_init(&a, ctx);
|
guf_libc_allocator_init(&a, ctx);
|
||||||
return a;
|
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
|
||||||
|
|||||||
@ -3,9 +3,11 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#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_alloc_libc.h"
|
||||||
|
|
||||||
#include "guf_cstr.h"
|
#include "guf_cstr.h"
|
||||||
#include "guf_linalg.h"
|
#include "guf_linalg.h"
|
||||||
#include "guf_utils.h"
|
#include "guf_utils.h"
|
||||||
|
|||||||
2
src/test/impls/alloc_libc_impl.c
Normal file
2
src/test/impls/alloc_libc_impl.c
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#define GUF_ALLOC_LIBC_IMPL
|
||||||
|
#include "guf_alloc_libc.h"
|
||||||
Loading…
x
Reference in New Issue
Block a user