make cutil work without stdlib via CMake option NOSTDLIB=ON, using internal implementations
This commit is contained in:
parent
c11dd9aede
commit
f744ec0f35
5 changed files with 37 additions and 23 deletions
|
|
@ -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)
|
||||
32
cutil.c
32
cutil.c
|
|
@ -1,24 +1,28 @@
|
|||
#include "cutil.h"
|
||||
#include "endian.h"
|
||||
#include <string.h>
|
||||
|
||||
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 <stdlib.h>
|
||||
# include <string.h>
|
||||
|
||||
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()
|
||||
|
|
|
|||
11
cutil.h
11
cutil.h
|
|
@ -2,20 +2,13 @@
|
|||
#define __CUTIL_H
|
||||
|
||||
#include "cutypes.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
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*);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
4
test.c
4
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;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue