X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=lib%2Fmalloc.c;h=0121112a349059a922656074061a8d69685bb17a;hb=b1bdfaf8b8da39e0aa888643b1998a948f83e7dd;hp=45f1409e2218dd06f1743ae440df36c8cf31ca64;hpb=f4ebbd9f7ea23e3f0fcbe098754580c220894628;p=yaboot.git diff --git a/lib/malloc.c b/lib/malloc.c index 45f1409..0121112 100644 --- a/lib/malloc.c +++ b/lib/malloc.c @@ -1,25 +1,31 @@ -/* Dumb memory allocation routines - - Copyright (C) 1997 Paul Mackerras - 1996 Maurizio Plaza - 1996 Jakub Jelinek - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +/* malloc.c - Dumb memory allocation routines + * + * Copyright (C) 1997 Paul Mackerras + * 1996 Maurizio Plaza + * 1996 Jakub Jelinek + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ #include "types.h" #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, ...); @@ -58,6 +64,49 @@ 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 *realloc(void *ptr, unsigned int size) { char *caddr, *oaddr = ptr; @@ -101,10 +150,10 @@ void release (void *ptr) malloc_ptr = (char *) ptr; } -void *strdup(char *str) +char *strdup(char const *str) { char *p = malloc(strlen(str) + 1); - - strcpy(p, str); + if (p) + strcpy(p, str); return p; }