diff options
| author | Xavier Del Campo Romero <xavi92@disroot.org> | 2025-11-11 20:35:20 +0100 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi92@disroot.org> | 2025-11-11 20:55:58 +0100 |
| commit | 5ce25ae3b5d8666d373f7d7e336546ce8508c213 (patch) | |
| tree | 4aacb570241c1005590a47ac49adb4b75a8ffd8d /src/libc | |
| parent | f7ad4d9216b488f76ed4b3c8e423cd926e134b9d (diff) | |
| download | wnix-5ce25ae3b5d8666d373f7d7e336546ce8508c213.tar.gz | |
fixes
Diffstat (limited to 'src/libc')
| -rw-r--r-- | src/libc/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/libc/include/stdlib.h | 77 | ||||
| -rw-r--r-- | src/libc/src/ps1/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/libc/src/stdlib/calloc.c | 2 | ||||
| -rw-r--r-- | src/libc/src/stdlib/free.c | 2 | ||||
| -rw-r--r-- | src/libc/src/stdlib/malloc.c | 4 | ||||
| -rw-r--r-- | src/libc/src/stdlib/realloc.c | 21 | ||||
| -rw-r--r-- | src/libc/src/unistd/sbrk.c | 2 |
8 files changed, 98 insertions, 14 deletions
diff --git a/src/libc/CMakeLists.txt b/src/libc/CMakeLists.txt index 309e47a..a57be2a 100644 --- a/src/libc/CMakeLists.txt +++ b/src/libc/CMakeLists.txt @@ -21,11 +21,10 @@ set(src add_library(c ${src}) add_subdirectory(src) add_subdirectory(newlib) +target_compile_options(c PRIVATE -ffreestanding) target_include_directories(c PUBLIC include printf PRIVATE private_include tinyalloc) -target_compile_definitions(c PRIVATE TA_DISABLE_SPLIT TA_DISABLE_COMPACT) - if(NOT LIBC_FREESTANDING) target_link_libraries(c PRIVATE aio fs drv) endif() diff --git a/src/libc/include/stdlib.h b/src/libc/include/stdlib.h index 8f97ea3..2704fca 100644 --- a/src/libc/include/stdlib.h +++ b/src/libc/include/stdlib.h @@ -34,10 +34,10 @@ typedef struct int abs(int __v); long labs(long __v); long long llabs(long long __v); -void *malloc(size_t __n); -void *calloc(size_t __nemb, size_t __size); -void *realloc(void *__ptr, size_t __size); -void free(void *__p); +void *__malloc(size_t __n); +void *__calloc(size_t __nemb, size_t __size); +void *__realloc(void *__ptr, size_t __size); +void __free(void *__p); long strtol(const char *__s, char **__end, int __base); long long strtoll(const char *__s, char **__end, int __base); unsigned long strtoul(const char *__s, char **__end, int __base); @@ -45,4 +45,73 @@ unsigned long long strtoull(const char *__s, char **__end, int __base); void __abort(const char *__file, int __lineno); div_t div(int __numerator, int __denominator); +#if 1 +#define malloc(__n) __malloc(__n) +#define calloc(__n, __sz) __calloc(__n, __sz) +#define realloc(__p, __n) __realloc(__p, __n) +#define free(__p) __free(__p) +#else +#define RED "\x1b[31m" +#define GREEN "\x1b[32m" +#define YELLOW "\x1b[33m" +#define RESET "\x1b[0m" + +#define malloc(__n) ({void *__p; \ + Printf(GREEN "-> %s:%d (%s)", __FILE__, __LINE__, __func__); \ + __p = __malloc(__n); \ + Printf(", p=%p" RESET "\n", __p); \ + int Printf(const char *, ...); \ + size_t ta_num_free(); \ + size_t ta_num_used(); \ + size_t ta_num_fresh(); \ + Printf("%s: free=%lu, used=%lu, fresh=%lu\n", __func__, \ + (unsigned long)ta_num_free(), \ + (unsigned long)ta_num_used(), \ + (unsigned long)ta_num_fresh()); \ + __p;}) + +#define realloc(__p, __n) ({void *__np; \ + Printf(YELLOW "-> %s:%d (%s)", __FILE__, __LINE__, __func__); \ + __np = __realloc(__p, __n); \ + Printf(", np=%p" RESET "\n", __np); \ + int Printf(const char *, ...); \ + size_t ta_num_free(); \ +size_t ta_num_used(); \ +size_t ta_num_fresh(); \ + Printf("%s: free=%lu, used=%lu, fresh=%lu\n", __func__, \ + (unsigned long)ta_num_free(), \ + (unsigned long)ta_num_used(), \ + (unsigned long)ta_num_fresh()); \ + __np;}) + +#define calloc(__n, __sz) ({void *__p; \ + Printf(YELLOW "-> %s:%d (%s)", __FILE__, __LINE__, __func__); \ + __p = __calloc(__n, __sz); \ + Printf(", p=%p" RESET "\n", __p); \ + int Printf(const char *, ...); \ + size_t ta_num_free(); \ +size_t ta_num_used(); \ +size_t ta_num_fresh(); \ + Printf("%s: free=%lu, used=%lu, fresh=%lu\n", __func__, \ + (unsigned long)ta_num_free(), \ + (unsigned long)ta_num_used(), \ + (unsigned long)ta_num_fresh()); \ + __p;}) + +#define free(__p) ({ \ + int Printf(const char *, ...); \ + Printf(RED "<- %s:%d (%s), p=%p" RESET "\n", \ + __FILE__, __LINE__, __func__, __p); \ + size_t ta_num_free(); \ + size_t ta_num_used(); \ + size_t ta_num_fresh(); \ + __free(__p);\ + Printf("%s: free=%lu, used=%lu, fresh=%lu\n", \ + __func__, \ + (unsigned long)ta_num_free(), \ + (unsigned long)ta_num_used(), \ + (unsigned long)ta_num_fresh()); \ + }) +#endif + #endif diff --git a/src/libc/src/ps1/CMakeLists.txt b/src/libc/src/ps1/CMakeLists.txt index 54012df..1bab87a 100644 --- a/src/libc/src/ps1/CMakeLists.txt +++ b/src/libc/src/ps1/CMakeLists.txt @@ -20,4 +20,3 @@ target_sources(c PRIVATE ) target_link_libraries(c PRIVATE drv_ps1_bios) -target_compile_options(c PRIVATE -ffreestanding) diff --git a/src/libc/src/stdlib/calloc.c b/src/libc/src/stdlib/calloc.c index c5e1dee..1e331d1 100644 --- a/src/libc/src/stdlib/calloc.c +++ b/src/libc/src/stdlib/calloc.c @@ -20,7 +20,7 @@ #include <stddef.h> #include <string.h> -void *calloc(const size_t nemb, const size_t size) +void *__calloc(const size_t nemb, const size_t size) { const size_t n = nemb * size; void *const ret = malloc(n); diff --git a/src/libc/src/stdlib/free.c b/src/libc/src/stdlib/free.c index 1986c48..3916ff3 100644 --- a/src/libc/src/stdlib/free.c +++ b/src/libc/src/stdlib/free.c @@ -20,7 +20,7 @@ #include <libc/malloc.h> #include <stdlib.h> -void free(void *const ptr) +void __free(void *const ptr) { if (!ptr) return; diff --git a/src/libc/src/stdlib/malloc.c b/src/libc/src/stdlib/malloc.c index 81bdeb5..786f314 100644 --- a/src/libc/src/stdlib/malloc.c +++ b/src/libc/src/stdlib/malloc.c @@ -22,12 +22,12 @@ #include <errno.h> #include <stdint.h> -void *malloc(const size_t n) +void *__malloc(const size_t n) { if (!__malloc_ta_init) { /* README.md states 16 is a "good default value". */ - if (!ta_init(1024, 16, sizeof (intmax_t))) + if (!ta_init(300, 16, sizeof (intmax_t))) abort(); __malloc_ta_init = 1; diff --git a/src/libc/src/stdlib/realloc.c b/src/libc/src/stdlib/realloc.c index dabfc05..2b9c205 100644 --- a/src/libc/src/stdlib/realloc.c +++ b/src/libc/src/stdlib/realloc.c @@ -17,9 +17,26 @@ */ #include <tinyalloc.h> +#include <libc/malloc.h> +#include <errno.h> +#include <stdint.h> #include <stdlib.h> -void *realloc(void *const ptr, const size_t size) +void *__realloc(void *const ptr, const size_t size) { - return ta_realloc(ptr, size); + if (!__malloc_ta_init) + { + /* README.md states 16 is a "good default value". */ + if (!ta_init(1024, 16, sizeof (intmax_t))) + abort(); + + __malloc_ta_init = 1; + } + + void *const ret = ta_realloc(ptr, size); + + if (!ret) + errno = ENOMEM; + + return ret; } diff --git a/src/libc/src/unistd/sbrk.c b/src/libc/src/unistd/sbrk.c index 459d476..b41c192 100644 --- a/src/libc/src/unistd/sbrk.c +++ b/src/libc/src/unistd/sbrk.c @@ -31,7 +31,7 @@ void *sbrk(const intptr_t increment) if (base + increment >= __ram_end) return NULL; - void *p = base; + void *const p = base; base += increment; return p; |
