mescc: Tinycc support: calloc,malloc,realloc.

* mlibc/libc-mes+tcc.c (calloc,malloc,realloc): New function.
This commit is contained in:
Jan Nieuwenhuizen 2017-07-25 20:55:45 +02:00
parent b7cc9d375d
commit 62e7809725
3 changed files with 39 additions and 19 deletions

View file

@ -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);

View file

@ -19,6 +19,7 @@
*/
#include <libc-mes.c>
#include <setjmp.h>
#include <stdarg.h>
#include <stdio.h>
@ -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;
}

View file

@ -18,6 +18,9 @@
* along with Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <stdlib.h>
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 <stdarg.h>
int
vprintf (char const* format, va_list ap)
{