]> git.ozlabs.org Git - yaboot.git/blob - lib/malloc.c
Add preliminary support for Windtunnel PowerMacs to ofpath
[yaboot.git] / lib / malloc.c
1 /*  malloc.c - Dumb memory allocation routines
2  *  
3  *  Copyright (C) 1997 Paul Mackerras
4  *                1996 Maurizio Plaza
5  *                1996 Jakub Jelinek
6  *
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.
11  *
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.
16  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  */
21
22 #include "types.h"
23 #include "stddef.h"
24
25 /* Imported functions */
26 extern void prom_printf (char *fmt, ...);
27
28 static char *malloc_ptr = 0;
29 static char *malloc_top = 0;
30 static char *last_alloc = 0;
31
32 void malloc_init(void *bottom, unsigned long size)
33 {
34         malloc_ptr = bottom;
35         malloc_top = bottom + size;
36 }
37
38 void malloc_dispose(void)
39 {
40         malloc_ptr = 0;
41         last_alloc = 0;
42 }
43
44 void *malloc (unsigned int size)
45 {
46     char *caddr;
47
48     if (!malloc_ptr)
49         return NULL;
50     if ((malloc_ptr + size + sizeof(int)) > malloc_top) {
51         prom_printf("malloc failed\n");
52         return NULL;
53     }
54     *(int *)malloc_ptr = size;
55     caddr = malloc_ptr + sizeof(int);
56     malloc_ptr += size + sizeof(int);
57     last_alloc = caddr;
58     malloc_ptr = (char *) ((((unsigned int) malloc_ptr) + 3) & (~3));
59     return caddr;
60 }
61
62 void *realloc(void *ptr, unsigned int size)
63 {
64     char *caddr, *oaddr = ptr;
65
66     if (!malloc_ptr)
67         return NULL;
68     if (oaddr == last_alloc) {
69         if (oaddr + size > malloc_top) {
70                 prom_printf("realloc failed\n");
71                 return NULL;
72         }
73         *(int *)(oaddr - sizeof(int)) = size;
74         malloc_ptr = oaddr + size;
75         return oaddr;
76     }
77     caddr = malloc(size);
78     if (caddr != 0 && oaddr != 0)
79         memcpy(caddr, oaddr, *(int *)(oaddr - sizeof(int)));
80     return caddr;
81 }
82
83 void free (void *m)
84 {
85     if (!malloc_ptr)
86         return;
87     if (m == last_alloc)
88         malloc_ptr = (char *) last_alloc - sizeof(int);
89 }
90
91 void mark (void **ptr)
92 {
93     if (!malloc_ptr)
94         return;
95     *ptr = (void *) malloc_ptr;
96 }
97
98 void release (void *ptr)
99 {
100     if (!malloc_ptr)
101         return;
102     malloc_ptr = (char *) ptr;
103 }
104
105 char *strdup(char const *str)
106 {
107     char *p = malloc(strlen(str) + 1);
108     if (p)
109          strcpy(p, str);
110     return p;
111 }