]> git.ozlabs.org Git - yaboot.git/blob - lib/malloc.c
Makefile: add cscope target
[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 /* Copied from asm-generic/errno-base.h */
27 #define ENOMEM          12      /* Out of memory */
28 #define EINVAL          22      /* Invalid argument */
29
30 /* Imported functions */
31 extern void prom_printf (char *fmt, ...);
32
33 static char *malloc_ptr = 0;
34 static char *malloc_top = 0;
35 static char *last_alloc = 0;
36
37 void malloc_init(void *bottom, unsigned long size)
38 {
39         malloc_ptr = bottom;
40         malloc_top = bottom + size;
41 }
42
43 void malloc_dispose(void)
44 {
45         malloc_ptr = 0;
46         last_alloc = 0;
47 }
48
49 void *malloc (unsigned int size)
50 {
51     char *caddr;
52
53     if (!malloc_ptr)
54         return NULL;
55     if ((malloc_ptr + size + sizeof(int)) > malloc_top) {
56         prom_printf("malloc failed\n");
57         return NULL;
58     }
59     *(int *)malloc_ptr = size;
60     caddr = malloc_ptr + sizeof(int);
61     malloc_ptr += size + sizeof(int);
62     last_alloc = caddr;
63     malloc_ptr = (char *) ((((unsigned int) malloc_ptr) + 3) & (~3));
64     return caddr;
65 }
66
67 /* Do not fall back to the malloc above as posix_memalign is needed by
68  * external libraries not yaboot */
69 int posix_memalign(void **memptr, size_t alignment, size_t size)
70 {
71     char *caddr;
72     /* size of allocation including the alignment */
73     size_t alloc_size;
74
75     if (!malloc_ptr)
76         return EINVAL;
77
78     /* Minimal aligment is sizeof(void *) */
79     if (alignment < sizeof(void*))
80         alignment = sizeof(void*);
81
82     /* Check for valid alignment and power of 2 */
83     if ((alignment % sizeof(void*) != 0) || ((alignment-1)&alignment))
84         return EINVAL;
85
86     if (size == 0) {
87         *memptr=NULL;
88         return 0;
89     }
90
91     caddr = (char*)(
92              (size_t)((malloc_ptr + sizeof(int))+(alignment-1)) &
93              (~(alignment-1))
94             );
95
96     alloc_size = size + (caddr - (malloc_ptr+sizeof(int)));
97
98     if ((malloc_ptr + alloc_size + sizeof(int)) > malloc_top)
99         return ENOMEM;
100
101     *(int *)(caddr - sizeof(int)) = size;
102     malloc_ptr += alloc_size + sizeof(int);
103     last_alloc = caddr;
104     malloc_ptr = (char *) ((((unsigned int) malloc_ptr) + 3) & (~3));
105     *memptr=(void*)caddr;
106
107     return 0;
108 }
109
110 void *calloc(size_t nmemb, size_t size)
111 {
112         unsigned char *p = malloc(nmemb * size);
113         memset(p, 0x0, nmemb * size);
114         return p;
115 }
116
117 void *realloc(void *ptr, unsigned int size)
118 {
119     char *caddr, *oaddr = ptr;
120
121     if (!malloc_ptr)
122         return NULL;
123     if (oaddr == last_alloc) {
124         if (oaddr + size > malloc_top) {
125                 prom_printf("realloc failed\n");
126                 return NULL;
127         }
128         *(int *)(oaddr - sizeof(int)) = size;
129         malloc_ptr = oaddr + size;
130         return oaddr;
131     }
132     caddr = malloc(size);
133     if (caddr != 0 && oaddr != 0)
134         memcpy(caddr, oaddr, *(int *)(oaddr - sizeof(int)));
135     return caddr;
136 }
137
138 void free (void *m)
139 {
140     if (!malloc_ptr)
141         return;
142     if (m == last_alloc)
143         malloc_ptr = (char *) last_alloc - sizeof(int);
144 }
145
146 void mark (void **ptr)
147 {
148     if (!malloc_ptr)
149         return;
150     *ptr = (void *) malloc_ptr;
151 }
152
153 void release (void *ptr)
154 {
155     if (!malloc_ptr)
156         return;
157     malloc_ptr = (char *) ptr;
158 }
159
160 char *strdup(char const *str)
161 {
162     char *p = malloc(strlen(str) + 1);
163     if (p)
164          strcpy(p, str);
165     return p;
166 }