]> git.ozlabs.org Git - yaboot.git/blob - lib/malloc.c
Allocate tftp temporary buffer from top of address space
[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 #include "string.h"
25
26 /* Imported functions */
27 extern void prom_printf (char *fmt, ...);
28
29 static char *malloc_ptr = 0;
30 static char *malloc_top = 0;
31 static char *last_alloc = 0;
32
33 void malloc_init(void *bottom, unsigned long size)
34 {
35         malloc_ptr = bottom;
36         malloc_top = bottom + size;
37 }
38
39 void malloc_dispose(void)
40 {
41         malloc_ptr = 0;
42         last_alloc = 0;
43 }
44
45 void *malloc (unsigned int size)
46 {
47     char *caddr;
48
49     if (!malloc_ptr)
50         return NULL;
51     if ((malloc_ptr + size + sizeof(int)) > malloc_top) {
52         prom_printf("malloc failed\n");
53         return NULL;
54     }
55     *(int *)malloc_ptr = size;
56     caddr = malloc_ptr + sizeof(int);
57     malloc_ptr += size + sizeof(int);
58     last_alloc = caddr;
59     malloc_ptr = (char *) ((((unsigned int) malloc_ptr) + 3) & (~3));
60     return caddr;
61 }
62
63 void *realloc(void *ptr, unsigned int size)
64 {
65     char *caddr, *oaddr = ptr;
66
67     if (!malloc_ptr)
68         return NULL;
69     if (oaddr == last_alloc) {
70         if (oaddr + size > malloc_top) {
71                 prom_printf("realloc failed\n");
72                 return NULL;
73         }
74         *(int *)(oaddr - sizeof(int)) = size;
75         malloc_ptr = oaddr + size;
76         return oaddr;
77     }
78     caddr = malloc(size);
79     if (caddr != 0 && oaddr != 0)
80         memcpy(caddr, oaddr, *(int *)(oaddr - sizeof(int)));
81     return caddr;
82 }
83
84 void free (void *m)
85 {
86     if (!malloc_ptr)
87         return;
88     if (m == last_alloc)
89         malloc_ptr = (char *) last_alloc - sizeof(int);
90 }
91
92 void mark (void **ptr)
93 {
94     if (!malloc_ptr)
95         return;
96     *ptr = (void *) malloc_ptr;
97 }
98
99 void release (void *ptr)
100 {
101     if (!malloc_ptr)
102         return;
103     malloc_ptr = (char *) ptr;
104 }
105
106 char *strdup(char const *str)
107 {
108     char *p = malloc(strlen(str) + 1);
109     if (p)
110          strcpy(p, str);
111     return p;
112 }