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 #endif
int atoi (char const *s); int atoi (char const *s);
void * calloc (size_t nmemb, size_t size);
void exit (int); void exit (int);
void free (void *ptr); void free (void *ptr);
char* getenv (char const* s); char* getenv (char const* s);

View file

@ -19,6 +19,7 @@
*/ */
#include <libc-mes.c> #include <libc-mes.c>
#include <setjmp.h> #include <setjmp.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
@ -248,3 +249,25 @@ vsnprintf (char *str, size_t size, char const *format, va_list ap)
{ {
return 0; 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/>. * along with Mes. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdarg.h>
#include <stdlib.h>
int g_stdin = 0; int g_stdin = 0;
char **g_environment; char **g_environment;
int _env (); int _env ();
@ -303,28 +306,24 @@ atoi (char const *s)
return i * sign; return i * sign;
} }
//void *g_malloc_base = 0; char *g_brk = 0;
char *g_malloc_base = 0;
//void * void *
int * malloc (size_t size)
malloc (int size)
{ {
//void *p = brk (0); if (!g_brk) g_brk = brk (0);
char *p = 0; char *p = g_brk;
p = brk (0); if (size < 0 || brk (g_brk + size) == -1)
if (!g_malloc_base) g_malloc_base = p; return 0;
brk (p+size); g_brk += size;
return p; return p;
} }
//void * void *
int * realloc (void *p, int size)
//realloc (void *p, int size)
realloc (int *p, int size)
{ {
brk (g_malloc_base + size); brk (g_brk + size);
return g_malloc_base; return p;
} }
int int
@ -334,7 +333,6 @@ strncmp (char const* a, char const* b, int length)
return *a - *b; return *a - *b;
} }
char **g_environment;
char * char *
getenv (char const* s) getenv (char const* s)
{ {
@ -348,8 +346,6 @@ getenv (char const* s)
return 0; return 0;
} }
#include <stdarg.h>
int int
vprintf (char const* format, va_list ap) vprintf (char const* format, va_list ap)
{ {