X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=lib%2Fmalloc.c;h=b21490cdd95411eb6b7d2beefb3260a1bfdf15f3;hb=94b9f91a346ceed386faf284ad2e549922a1a385;hp=81d7717b32eb23ff410dc6c7077b32853d993386;hpb=6084bb9a372a5fb9fa3e63a26c1770036c31883d;p=yaboot.git diff --git a/lib/malloc.c b/lib/malloc.c index 81d7717..b21490c 100644 --- a/lib/malloc.c +++ b/lib/malloc.c @@ -23,6 +23,10 @@ #include "stddef.h" #include "string.h" +/* Copied from asm-generic/errno-base.h */ +#define ENOMEM 12 /* Out of memory */ +#define EINVAL 22 /* Invalid argument */ + /* Imported functions */ extern void prom_printf (char *fmt, ...); @@ -60,6 +64,56 @@ void *malloc (unsigned int size) return caddr; } +/* Do not fall back to the malloc above as posix_memalign is needed by + * external libraries not yaboot */ +int posix_memalign(void **memptr, size_t alignment, size_t size) +{ + char *caddr; + /* size of allocation including the alignment */ + size_t alloc_size; + + if (!malloc_ptr) + return EINVAL; + + /* Minimal aligment is sizeof(void *) */ + if (alignment < sizeof(void*)) + alignment = sizeof(void*); + + /* Check for valid alignment and power of 2 */ + if ((alignment % sizeof(void*) != 0) || ((alignment-1)&alignment)) + return EINVAL; + + if (size == 0) { + *memptr=NULL; + return 0; + } + + caddr = (char*)( + (size_t)((malloc_ptr + sizeof(int))+(alignment-1)) & + (~(alignment-1)) + ); + + alloc_size = size + (caddr - (malloc_ptr+sizeof(int))); + + if ((malloc_ptr + alloc_size + sizeof(int)) > malloc_top) + return ENOMEM; + + *(int *)(caddr - sizeof(int)) = size; + malloc_ptr += alloc_size + sizeof(int); + last_alloc = caddr; + malloc_ptr = (char *) ((((unsigned int) malloc_ptr) + 3) & (~3)); + *memptr=(void*)caddr; + + return 0; +} + +void *calloc(size_t nmemb, size_t size) +{ + unsigned char *p = malloc(nmemb * size); + memset(p, 0x0, nmemb * size); + return p; +} + void *realloc(void *ptr, unsigned int size) { char *caddr, *oaddr = ptr;