1 /* Dumb memory allocation routines
3 Copyright (C) 1997 Paul Mackerras
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 /* Imported functions */
25 extern void prom_printf (char *fmt, ...);
27 static char *malloc_ptr = 0;
28 static char *malloc_top = 0;
29 static char *last_alloc = 0;
31 void malloc_init(void *bottom, unsigned long size)
34 malloc_top = bottom + size;
37 void malloc_dispose(void)
43 void *malloc (unsigned int size)
49 if ((malloc_ptr + size + sizeof(int)) > malloc_top) {
50 prom_printf("malloc failed\n");
53 *(int *)malloc_ptr = size;
54 caddr = malloc_ptr + sizeof(int);
55 malloc_ptr += size + sizeof(int);
57 malloc_ptr = (char *) ((((unsigned int) malloc_ptr) + 3) & (~3));
61 void *realloc(void *ptr, unsigned int size)
63 char *caddr, *oaddr = ptr;
67 if (oaddr == last_alloc) {
68 if (oaddr + size > malloc_top) {
69 prom_printf("realloc failed\n");
72 *(int *)(oaddr - sizeof(int)) = size;
73 malloc_ptr = oaddr + size;
77 if (caddr != 0 && oaddr != 0)
78 memcpy(caddr, oaddr, *(int *)(oaddr - sizeof(int)));
87 malloc_ptr = (char *) last_alloc - sizeof(int);
90 void mark (void **ptr)
94 *ptr = (void *) malloc_ptr;
97 void release (void *ptr)
101 malloc_ptr = (char *) ptr;
104 char *strdup(char const *str)
106 char *p = malloc(strlen(str) + 1);