From f744ec0f35579f6a3d2ee69191f174d087ad29ca Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Tue, 17 May 2022 01:57:18 +0300 Subject: [PATCH] make cutil work without stdlib via CMake option NOSTDLIB=ON, using internal implementations --- CMakeLists.txt | 6 ++++++ cutil.c | 32 ++++++++++++++++++-------------- cutil.h | 11 ++--------- cutypes.h | 7 +++++++ test.c | 4 ++++ 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 84f112a..8329c05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,5 +24,11 @@ add_library(cutil STATIC ${SOURCES} ${HEADERS} "arch/${CMAKE_SYSTEM_PROCESSOR}.S" ) +option(NOSTDLIB "compile cutil without stdlib" ON) +if (NOSTDLIB) + target_compile_definitions(cutil PRIVATE CUTIL_NOSTDLIB) + target_compile_options(cutil PRIVATE -nostdlib) +endif() + add_executable(test test.c) target_link_libraries(test cutil) \ No newline at end of file diff --git a/cutil.c b/cutil.c index 0128301..c79c133 100644 --- a/cutil.c +++ b/cutil.c @@ -1,24 +1,28 @@ #include "cutil.h" #include "endian.h" -#include -void* (*cu_malloc)(size_t); -void* (*cu_realloc)(void*, size_t); -void (*cu_free)(void*) = CUTIL_FREE; -void* (*cu_memset)(void*,int,size_t); -void* (*cu_memcpy)(void*,const void*,size_t); -void* (*cu_memmove)(void*,const void*,size_t); +#ifdef CUTIL_NOSTDLIB +void* (*cu_malloc)(size_t) = NULL; +void* (*cu_realloc)(void*, size_t) = NULL; +void (*cu_free)(void*) = NULL; +void* (*cu_memset)(void*,int,size_t) = _cu_memset; +void* (*cu_memcpy)(void*,const void*,size_t) = _cu_memcpy; +void* (*cu_memmove)(void*,const void*,size_t) = _cu_memmove; +#else +# include +# include + +void* (*cu_malloc)(size_t) = malloc; +void* (*cu_realloc)(void*, size_t) = realloc; +void (*cu_free)(void*) = free; +void* (*cu_memset)(void*,int,size_t) = memset; +void* (*cu_memcpy)(void*,const void*,size_t) = memcpy; +void* (*cu_memmove)(void*,const void*,size_t) = memmove; +#endif void cutil_init() { cu_endian_init(); - - cu_malloc = CUTIL_MALLOC; - cu_realloc = CUTIL_REALLOC; - cu_free = CUTIL_FREE; - cu_memset = CUTIL_MEMSET; - cu_memcpy = CUTIL_MEMCPY; - cu_memmove = CUTIL_MEMMOVE; } void cutil_exit() diff --git a/cutil.h b/cutil.h index 01cdc8d..2a9ec8e 100644 --- a/cutil.h +++ b/cutil.h @@ -2,20 +2,13 @@ #define __CUTIL_H #include "cutypes.h" -#include + extern void* _cu_memset(void* dst, int val, size_t size); extern void* _cu_memcpy(void* dst, const void* src, size_t size); -extern void* _cu_memmove(void* dst, void* src, size_t size); +extern void* _cu_memmove(void* dst, const void* src, size_t size); void __cu_memmove(void* dst, void* src, size_t size); -#define CUTIL_MALLOC malloc -#define CUTIL_REALLOC realloc -#define CUTIL_FREE free -#define CUTIL_MEMSET memset -#define CUTIL_MEMCPY memcpy -#define CUTIL_MEMMOVE memmove - extern void* (*cu_malloc)(size_t); extern void* (*cu_realloc)(void*, size_t); extern void (*cu_free)(void*); diff --git a/cutypes.h b/cutypes.h index af8d242..94be2a7 100644 --- a/cutypes.h +++ b/cutypes.h @@ -128,4 +128,11 @@ typedef void* cu_ptr; #define cu_align(value, align) (((value / align) + !!(value % align))*align) +#ifdef CUTIL_NOSTDLIB +# define NULL (void*)0 +# define ZERO 0 +typedef uword size_t; +typedef iword ssize_t; +#endif + #endif \ No newline at end of file diff --git a/test.c b/test.c index cf41c9b..d7a5c43 100644 --- a/test.c +++ b/test.c @@ -48,6 +48,10 @@ void va_test(int fixed, ...) int main() { + cu_malloc = malloc; + cu_realloc = realloc; + cu_free = free; + cutil_init(); printf("[list]\n"); list_t list;