Root/
Source at commit 2740 created 8 years 11 months ago. By ifabio, sync with trunk. | |
---|---|
1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | /*␊ |
25 | * Copyright 1993 NeXT Computer, Inc.␊ |
26 | * All rights reserved.␊ |
27 | *␊ |
28 | * Sam's simple memory allocator.␊ |
29 | *␊ |
30 | */␊ |
31 | ␊ |
32 | #include "libsa.h"␊ |
33 | //#include "saio_internal.h" - Azi: needed for ZDEBUG (printf)␊ |
34 | #include "memory.h"␊ |
35 | ␊ |
36 | #define ZDEBUG 0 //Azi: booter doesn't load with this enabled; instant reboot at "boot1: ..."␊ |
37 | ␊ |
38 | #if ZDEBUG␊ |
39 | int zout;␊ |
40 | #endif␊ |
41 | ␊ |
42 | typedef struct {␊ |
43 | ␉char * start;␊ |
44 | ␉size_t size;␊ |
45 | } zmem;␊ |
46 | ␊ |
47 | static zmem * zalloced;␊ |
48 | static zmem * zavailable;␊ |
49 | static short availableNodes, allocedNodes, totalNodes;␊ |
50 | static char * zalloc_base;␊ |
51 | static char * zalloc_end;␊ |
52 | static void (*zerror)(char *, size_t, const char *, int);␊ |
53 | ␊ |
54 | static void zallocate(char * start,int size);␊ |
55 | static void zinsert(zmem * zp, int ndx);␊ |
56 | static void zdelete(zmem * zp, int ndx);␊ |
57 | static void zcoalesce(void);␊ |
58 | ␊ |
59 | #if ZDEBUG␊ |
60 | ␉size_t zalloced_size;␊ |
61 | #endif␊ |
62 | ␊ |
63 | #define ZALLOC_NODES␉32767 /* was 16384 */␊ |
64 | ␊ |
65 | static void malloc_error(char *addr, size_t size, const char *file, int line)␊ |
66 | {␊ |
67 | #ifdef i386␊ |
68 | ␉asm volatile ("hlt");␊ |
69 | #endif␊ |
70 | }␊ |
71 | ␊ |
72 | // define the block of memory that the allocator will use␊ |
73 | void malloc_init(char * start, int size, int nodes, void (*malloc_err_fn)(char *, size_t, const char *, int))␊ |
74 | {␊ |
75 | ␉zalloc_base = start ? start : (char *)ZALLOC_ADDR;␊ |
76 | ␉totalNodes = nodes ? nodes : ZALLOC_NODES;␊ |
77 | ␉zalloced = (zmem *) zalloc_base;␊ |
78 | ␉zavailable = (zmem *) zalloc_base + sizeof(zmem) * totalNodes;␊ |
79 | ␉zavailable[0].start = (char *)zavailable + sizeof(zmem) * totalNodes;␊ |
80 | ␊ |
81 | ␉if (size == 0)␊ |
82 | ␉{␊ |
83 | ␉␉size = ZALLOC_LEN;␊ |
84 | ␉}␊ |
85 | ␊ |
86 | ␉zavailable[0].size = size - (zavailable[0].start - zalloc_base);␊ |
87 | ␉zalloc_end = zalloc_base + size;␊ |
88 | ␉availableNodes = 1;␊ |
89 | ␉allocedNodes = 0;␊ |
90 | ␉zerror = malloc_err_fn ? malloc_err_fn : malloc_error;␊ |
91 | }␊ |
92 | ␊ |
93 | #define BEST_FIT 1␊ |
94 | ␊ |
95 | void * safe_malloc(size_t size, const char *file, int line)␊ |
96 | {␊ |
97 | ␉int i;␊ |
98 | #if BEST_FIT␊ |
99 | ␉int bestFit;␊ |
100 | ␉size_t smallestSize;␊ |
101 | #endif␊ |
102 | ␉char * ret = 0;␊ |
103 | ␊ |
104 | ␉if ( !zalloc_base )␊ |
105 | ␉{␊ |
106 | ␉␉// this used to follow the bss but some bios' corrupted it...␊ |
107 | ␉␉malloc_init((char *)ZALLOC_ADDR, ZALLOC_LEN, ZALLOC_NODES, malloc_error);␊ |
108 | ␉}␊ |
109 | ␊ |
110 | ␉size = ((size + 0xf) & ~0xf);␊ |
111 | ␊ |
112 | ␉if (size == 0)␊ |
113 | ␉{␊ |
114 | ␉␉if (zerror)␊ |
115 | ␉␉{␊ |
116 | ␉␉␉(*zerror)((char *)0xdeadbeef, 0, file, line);␊ |
117 | ␉}␊ |
118 | }␊ |
119 | #if BEST_FIT␊ |
120 | ␉smallestSize = 0;␊ |
121 | ␉bestFit = -1;␊ |
122 | #endif␊ |
123 | ␊ |
124 | ␉for (i = 0; i < availableNodes; i++)␊ |
125 | ␉{␊ |
126 | ␉␉// find node with equal size, or if not found,␊ |
127 | // then smallest node that fits.␊ |
128 | ␉␉if (zavailable[i].size == size)␊ |
129 | ␉␉{␊ |
130 | ␉␉␉zallocate(ret = zavailable[i].start, size);␊ |
131 | ␉␉␉zdelete(zavailable, i); availableNodes--;␊ |
132 | ␉␉␉goto done;␊ |
133 | ␉␉}␊ |
134 | #if BEST_FIT␊ |
135 | ␉␉else {␊ |
136 | ␉␉␉if ((zavailable[i].size > size) && ((smallestSize == 0) || (zavailable[i].size < smallestSize))) {␊ |
137 | ␉␉␉␉bestFit = i;␊ |
138 | ␉␉␉␉smallestSize = zavailable[i].size;␊ |
139 | ␉␉␉}␊ |
140 | ␉␉}␊ |
141 | #else␊ |
142 | ␉␉else if (zavailable[i].size > size)␊ |
143 | ␉␉{␊ |
144 | ␉␉␉zallocate(ret = zavailable[i].start, size);␊ |
145 | ␉␉␉zavailable[i].start += size;␊ |
146 | ␉␉␉zavailable[i].size -= size;␊ |
147 | ␉␉␉goto done;␊ |
148 | ␉␉}␊ |
149 | #endif␊ |
150 | ␉}␊ |
151 | #if BEST_FIT␊ |
152 | ␉if (bestFit != -1) {␊ |
153 | ␉␉zallocate(ret = zavailable[bestFit].start, size);␊ |
154 | ␉␉zavailable[bestFit].start += size;␊ |
155 | ␉␉zavailable[bestFit].size -= size;␊ |
156 | ␉}␊ |
157 | #endif␊ |
158 | ␊ |
159 | done:␊ |
160 | ␉if ((ret == 0) || (ret + size >= zalloc_end)) {␊ |
161 | ␉␉if (zerror)␊ |
162 | ␉␉{␊ |
163 | ␉␉␉(*zerror)(ret, size, file, line);␊ |
164 | ␉␉}␊ |
165 | ␉}␊ |
166 | ␊ |
167 | ␉if (ret != 0)␊ |
168 | ␉{␊ |
169 | ␉␉bzero(ret, size);␊ |
170 | ␉}␊ |
171 | #if ZDEBUG␊ |
172 | ␉zalloced_size += size;␊ |
173 | #endif␊ |
174 | ␉return (void *) ret;␊ |
175 | }␊ |
176 | ␊ |
177 | void free(void * pointer)␊ |
178 | {␊ |
179 | ␉unsigned long rp;␊ |
180 | ␉int i, found = 0;␊ |
181 | ␉size_t tsize = 0;␊ |
182 | ␉char * start = pointer;␊ |
183 | ␊ |
184 | #if i386 ␊ |
185 | ␉// Get return address of our caller,␊ |
186 | ␉// in case we have to report an error below.␊ |
187 | ␉asm volatile ("movl %%esp, %%eax\n\t"␊ |
188 | "subl $4, %%eax\n\t"␊ |
189 | "movl 0(%%eax), %%eax" : "=a" (rp));␊ |
190 | #else␊ |
191 | rp = 0;␊ |
192 | #endif␊ |
193 | ␊ |
194 | ␉if (!start)␊ |
195 | ␉{␊ |
196 | ␉␉return;␊ |
197 | ␉}␊ |
198 | ␊ |
199 | ␉for (i = 0; i < allocedNodes; i++)␊ |
200 | ␉{␊ |
201 | ␉␉if (zalloced[i].start == start)␊ |
202 | ␉␉{␊ |
203 | ␉␉␉tsize = zalloced[i].size;␊ |
204 | #if ZDEBUG␊ |
205 | ␉␉␉zout -= tsize;␊ |
206 | ␉␉␉printf(" zz out %d\n", zout);␊ |
207 | #endif␊ |
208 | ␉␉␉zdelete(zalloced, i); allocedNodes--;␊ |
209 | ␉␉␉found = 1;␊ |
210 | #if ZDEBUG␊ |
211 | ␉␉␉memset(pointer, 0x5A, tsize);␊ |
212 | #endif␊ |
213 | ␉␉␉break;␊ |
214 | ␉␉}␊ |
215 | ␉}␊ |
216 | ␉if (!found)␊ |
217 | ␉{␊ |
218 | ␉␉if (zerror)␊ |
219 | ␉␉{␊ |
220 | ␉␉␉(*zerror)(pointer, rp, "free", 0);␊ |
221 | ␉␉} else {␊ |
222 | ␉␉␉return;␊ |
223 | ␉␉}␊ |
224 | ␉}␊ |
225 | #if ZDEBUG␊ |
226 | zalloced_size -= tsize;␊ |
227 | #endif␊ |
228 | ␊ |
229 | ␉for (i = 0; i < availableNodes; i++)␊ |
230 | ␉{␊ |
231 | ␉␉if ((start + tsize) == zavailable[i].start) // merge it in␊ |
232 | ␉␉{␊ |
233 | ␉␉␉zavailable[i].start = start;␊ |
234 | ␉␉␉zavailable[i].size += tsize;␊ |
235 | ␉␉␉zcoalesce();␊ |
236 | ␉␉␉return;␊ |
237 | ␉␉}␊ |
238 | ␊ |
239 | ␉␉if ((i > 0) && (zavailable[i-1].start + zavailable[i-1].size == start))␊ |
240 | ␉␉{␊ |
241 | ␉␉␉zavailable[i-1].size += tsize;␊ |
242 | ␉␉␉zcoalesce();␊ |
243 | ␉␉␉return;␊ |
244 | ␉␉}␊ |
245 | ␊ |
246 | ␉␉if ((start + tsize) < zavailable[i].start)␊ |
247 | ␉␉{␊ |
248 | if (++availableNodes > totalNodes)␊ |
249 | ␉␉␉{␊ |
250 | ␉␉␉␉if (zerror)␊ |
251 | ␉␉␉␉{␊ |
252 | ␉␉␉␉␉(*zerror)((char *)0xf000f000, 0, "free", 0);␊ |
253 | ␉␉␉␉}␊ |
254 | ␉␉␉}␊ |
255 | ␉␉␉zinsert(zavailable, i); ␊ |
256 | ␉␉␉zavailable[i].start = start;␊ |
257 | ␉␉␉zavailable[i].size = tsize;␊ |
258 | ␉␉␉return;␊ |
259 | ␉␉}␊ |
260 | ␉}␊ |
261 | ␊ |
262 | ␉if (++availableNodes > totalNodes)␊ |
263 | ␉{␊ |
264 | ␉␉if (zerror)␊ |
265 | ␉␉{␊ |
266 | ␉␉␉(*zerror)((char *)0xf000f000, 1, "free", 0);␊ |
267 | ␉␉}␊ |
268 | ␉}␊ |
269 | ␉zavailable[i].start = start;␊ |
270 | ␉zavailable[i].size = tsize;␊ |
271 | ␉zcoalesce();␊ |
272 | ␊ |
273 | ␉return;␊ |
274 | }␊ |
275 | ␊ |
276 | static void␊ |
277 | zallocate(char * start,int size)␊ |
278 | {␊ |
279 | ␊ |
280 | #if ZDEBUG␊ |
281 | ␉zout += size;␊ |
282 | ␉printf(" alloc %d, total 0x%x\n",size,zout);␊ |
283 | #endif␊ |
284 | ␊ |
285 | ␉zalloced[allocedNodes].start = start;␊ |
286 | ␉zalloced[allocedNodes].size = size;␊ |
287 | ␊ |
288 | ␉if (++allocedNodes > totalNodes)␊ |
289 | ␉{␊ |
290 | ␉␉if (zerror)␊ |
291 | ␉␉{␊ |
292 | ␉␉␉(*zerror)((char *)0xf000f000, 2, "zallocate", 0);␊ |
293 | ␉␉}␊ |
294 | ␉};␊ |
295 | }␊ |
296 | ␊ |
297 | static void␊ |
298 | zinsert(zmem * zp, int ndx)␊ |
299 | {␊ |
300 | ␉int i;␊ |
301 | ␉zmem *z1, *z2;␊ |
302 | ␊ |
303 | ␉i = totalNodes-2;␊ |
304 | ␉z1 = zp + i;␊ |
305 | ␉z2 = z1 + 1;␊ |
306 | ␊ |
307 | ␉for (; i >= ndx; i--, z1--, z2--)␊ |
308 | ␉{␊ |
309 | ␉␉*z2 = *z1;␊ |
310 | ␉}␊ |
311 | }␊ |
312 | ␊ |
313 | static void␊ |
314 | zdelete(zmem * zp, int ndx)␊ |
315 | {␊ |
316 | ␉int i;␊ |
317 | ␉zmem *z1, *z2;␊ |
318 | ␊ |
319 | ␉z1 = zp + ndx;␊ |
320 | ␉z2 = z1 + 1;␊ |
321 | ␊ |
322 | ␉for (i = ndx; i < totalNodes - 1; i++, z1++, z2++)␊ |
323 | ␉{␊ |
324 | ␉␉*z1 = *z2;␊ |
325 | ␉}␊ |
326 | }␊ |
327 | ␊ |
328 | static void␊ |
329 | zcoalesce(void)␊ |
330 | {␊ |
331 | ␉int i;␊ |
332 | ␊ |
333 | ␉for (i = 0; i < availableNodes-1; i++)␊ |
334 | ␉{␊ |
335 | ␉␉if ( zavailable[i].start + zavailable[i].size == zavailable[i + 1].start )␊ |
336 | ␉␉{␊ |
337 | ␉␉␉zavailable[i].size += zavailable[i + 1].size;␊ |
338 | ␉␉␉zdelete(zavailable, i + 1); availableNodes--;␊ |
339 | ␉␉␉return;␊ |
340 | ␉␉}␊ |
341 | ␉}␉␊ |
342 | }␊ |
343 | ␊ |
344 | /* This is the simplest way possible. Should fix this. */␊ |
345 | void * realloc(void * start, size_t newsize)␊ |
346 | {␊ |
347 | void * newstart = safe_malloc(newsize, __FILE__, __LINE__);␊ |
348 | bcopy(start, newstart, newsize);␊ |
349 | free(start);␊ |
350 | return newstart;␊ |
351 | }␊ |
352 |