From 62e7809725146c8b647958bf751efef4041f7288 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Tue, 25 Jul 2017 20:55:45 +0200 Subject: [PATCH] mescc: Tinycc support: calloc,malloc,realloc. * mlibc/libc-mes+tcc.c (calloc,malloc,realloc): New function. --- mlibc/include/stdlib.h | 1 + mlibc/libc-mes+tcc.c | 23 +++++++++++++++++++++++ mlibc/libc-mes.c | 34 +++++++++++++++------------------- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/mlibc/include/stdlib.h b/mlibc/include/stdlib.h index 45eacc6c..a95883dd 100644 --- a/mlibc/include/stdlib.h +++ b/mlibc/include/stdlib.h @@ -34,6 +34,7 @@ typedef long size_t; #endif int atoi (char const *s); +void * calloc (size_t nmemb, size_t size); void exit (int); void free (void *ptr); char* getenv (char const* s); diff --git a/mlibc/libc-mes+tcc.c b/mlibc/libc-mes+tcc.c index e35875a9..8899bc5c 100644 --- a/mlibc/libc-mes+tcc.c +++ b/mlibc/libc-mes+tcc.c @@ -19,6 +19,7 @@ */ #include + #include #include #include @@ -248,3 +249,25 @@ vsnprintf (char *str, size_t size, char const *format, va_list ap) { return 0; } + +void * +calloc (size_t nmemb, size_t size) +{ + size_t count = nmemb * size; + void *p = malloc (count); + memset (p, 0, count); + return p; +} + +void * +realloc (void *ptr, size_t size) +{ + void *new = malloc (size); + if (ptr && new) + { + memcpy (new, ptr, size); + free (ptr); + } + return new; +} + diff --git a/mlibc/libc-mes.c b/mlibc/libc-mes.c index 259f1394..16eea931 100644 --- a/mlibc/libc-mes.c +++ b/mlibc/libc-mes.c @@ -18,6 +18,9 @@ * along with Mes. If not, see . */ +#include +#include + int g_stdin = 0; char **g_environment; int _env (); @@ -303,28 +306,24 @@ atoi (char const *s) return i * sign; } -//void *g_malloc_base = 0; -char *g_malloc_base = 0; +char *g_brk = 0; -//void * -int * -malloc (int size) +void * +malloc (size_t size) { - //void *p = brk (0); - char *p = 0; - p = brk (0); - if (!g_malloc_base) g_malloc_base = p; - brk (p+size); + if (!g_brk) g_brk = brk (0); + char *p = g_brk; + if (size < 0 || brk (g_brk + size) == -1) + return 0; + g_brk += size; return p; } -//void * -int * -//realloc (void *p, int size) -realloc (int *p, int size) +void * +realloc (void *p, int size) { - brk (g_malloc_base + size); - return g_malloc_base; + brk (g_brk + size); + return p; } int @@ -334,7 +333,6 @@ strncmp (char const* a, char const* b, int length) return *a - *b; } -char **g_environment; char * getenv (char const* s) { @@ -348,8 +346,6 @@ getenv (char const* s) return 0; } -#include - int vprintf (char const* format, va_list ap) {