From: John Hodge Date: Mon, 26 Apr 2010 02:19:47 +0000 (+0800) Subject: Added calloc to libc X-Git-Tag: rel0.06~208 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=e61cffb0ef7221e18df8e67cba37a3ec45232275;p=tpg%2Facess2.git Added calloc to libc --- diff --git a/Usermode/Libraries/libc.so_src/heap.c b/Usermode/Libraries/libc.so_src/heap.c index 499d24c9..d0490707 100644 --- a/Usermode/Libraries/libc.so_src/heap.c +++ b/Usermode/Libraries/libc.so_src/heap.c @@ -4,6 +4,7 @@ heap.c - Heap Manager */ #include #include +#include #include "lib.h" // === Constants === @@ -14,7 +15,7 @@ heap.c - Heap Manager typedef unsigned int Uint; -//Typedefs +// === TYPES === typedef struct { Uint magic; Uint size; @@ -24,12 +25,13 @@ typedef struct { Uint magic; } heap_foot; -//Globals -void *_heap_start = NULL; -void *_heap_end = NULL; +// === LOCAL VARIABLES === +static void *_heap_start = NULL; +static void *_heap_end = NULL; -//Prototypes -EXPORT void *malloc(Uint bytes); +// === PROTOTYPES === +EXPORT void *malloc(size_t bytes); +EXPORT void *calloc(size_t bytes, size_t count); EXPORT void free(void *mem); EXPORT void *realloc(void *mem, Uint bytes); EXPORT void *sbrk(int increment); @@ -133,6 +135,20 @@ EXPORT void *malloc(size_t bytes) return (void*)(bestMatchAddr+sizeof(heap_head)); } +/** + * \fn EXPORT void *calloc(size_t bytes, size_t count) + * \brief Allocate and zero a block of memory + * \param __nmemb Number of memeber elements + * \param __size Size of one element + */ +EXPORT void *calloc(size_t __nmemb, size_t __size) +{ + void *ret = malloc(__size*__nmemb); + if(!ret) return NULL; + memset(ret, 0, __size*__nmemb); + return ret; +} + /** \fn EXPORT void free(void *mem) \brief Free previously allocated memory diff --git a/Usermode/include/stdlib.h b/Usermode/include/stdlib.h index 50cedb34..baf12c31 100644 --- a/Usermode/include/stdlib.h +++ b/Usermode/include/stdlib.h @@ -19,7 +19,7 @@ #define UNLOCK(_name) __asm__ __volatile__("lock andl $0, (%0)"::"D"(&_spinlock_##_name)) // --- StdLib --- -extern void _exit(int code); //NOTE: Also defined in acess/sys.h +extern void _exit(int code) __attribute__((noreturn)); //NOTE: Also defined in acess/sys.h extern int atoi(const char *ptr); extern void exit(int status) __attribute__((noreturn)); extern void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); @@ -29,8 +29,9 @@ extern char *getenv(const char *name); // --- Heap --- extern void free(void *mem); -extern void *malloc(unsigned int bytes); -extern void *realloc(void *oldPos, unsigned int bytes); +extern void *malloc(size_t bytes); +extern void *calloc(size_t __nmemb, size_t __size); +extern void *realloc(void *__ptr, size_t __size); extern int IsHeap(void *ptr); #ifndef SEEK_CUR