]>
git.saurik.com Git - apple/bootx.git/blob - bootx.tproj/libclite.subproj/zalloc.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright 1993 NeXT Computer, Inc.
24 * All rights reserved.
26 * Sam's simple memory allocator.
30 * zalloc.c - malloc functions.
32 * Copyright (c) 1998-2000 Apple Computer, Inc.
39 #define ZALLOC_NODES 384
52 static int zalloc_addr
;
53 static int zalloc_len
;
54 static zmem
*zalloced
;
55 static zmem
*zavailable
;
56 static short availableNodes
, allocedNodes
, totalNodes
;
57 static char *zalloc_base
;
58 static void (*zerror
)();
60 static void zallocate(char *start
,int size
);
61 static void zinsert(zmem
*zp
, int ndx
);
62 static void zdelete(zmem
*zp
, int ndx
);
63 static void zcoalesce(void);
66 // define the block of memory that the allocator will use
67 void malloc_init(char *start
, int size
)
69 int nodes
= ZALLOC_NODES
;
71 zalloc_addr
= (int)start
;
76 zalloced
= (zmem
*)zalloc_base
;
77 start
+= sizeof(zmem
) * nodes
;
78 zavailable
= (zmem
*)start
;
79 start
+= sizeof(zmem
) * nodes
;
80 zavailable
[0].start
= start
;
81 zavailable
[0].size
= size
;
86 void malloc_error_init(void (*error_fn
)())
91 void * malloc(size_t size
)
96 extern char _DATA__end
;
99 // return error until malloc_init is called.
103 size
= ((size
+ 0xf) & ~0xf);
105 for (i
=0; i
<availableNodes
; i
++)
107 // uses first possible node, doesn't try to find best fit
108 if (zavailable
[i
].size
== size
)
110 zallocate(ret
= zavailable
[i
].start
, size
);
111 zdelete(zavailable
, i
); availableNodes
--;
114 else if (zavailable
[i
].size
> size
)
116 zallocate(ret
= zavailable
[i
].start
, size
);
117 zavailable
[i
].start
+= size
;
118 zavailable
[i
].size
-= size
;
124 /* if (ret + size >= (char*)_sp()) _stop("stack clobbered"); */
125 if ((ret
== 0) || (ret
+ size
>= (char *)(zalloc_addr
+ zalloc_len
)))
136 int i
, tsize
, found
= 0;
137 char *start
= pointer
;
140 // return until malloc_init is called.
146 for (i
=0; i
<allocedNodes
; i
++)
148 if (zalloced
[i
].start
== start
)
150 tsize
= zalloced
[i
].size
;
153 printf(" zz out %d\n",zout
);
155 zdelete(zalloced
, i
); allocedNodes
--;
162 for (i
=0; i
<availableNodes
; i
++)
164 if ((start
+tsize
) == zavailable
[i
].start
) // merge it in
166 zavailable
[i
].start
= start
;
167 zavailable
[i
].size
+= tsize
;
172 if (i
>0 && (zavailable
[i
-1].start
+ zavailable
[i
-1].size
==
175 zavailable
[i
-1].size
+= tsize
;
180 if ((start
+tsize
) < zavailable
[i
].start
)
182 zinsert(zavailable
, i
); availableNodes
++;
183 zavailable
[i
].start
= start
;
184 zavailable
[i
].size
= tsize
;
189 zavailable
[i
].start
= start
;
190 zavailable
[i
].size
= tsize
;
198 zallocate(char *start
,int size
)
202 printf(" alloc %d, total 0x%x\n",size
,zout
);
204 zalloced
[allocedNodes
].start
= start
;
205 zalloced
[allocedNodes
].size
= size
;
210 zinsert(zmem
*zp
, int ndx
)
219 for (; i
>= ndx
; i
--, z1
--, z2
--)
221 z2
->start
= z1
->start
;
227 zdelete(zmem
*zp
, int ndx
)
235 for (i
=ndx
; i
<totalNodes
-1; i
++, z1
++, z2
++)
237 z1
->start
= z2
->start
;
246 for (i
=0; i
<availableNodes
-1; i
++)
248 if (zavailable
[i
].start
+ zavailable
[i
].size
==
249 zavailable
[i
+1].start
)
251 zavailable
[i
].size
+= zavailable
[i
+1].size
;
252 zdelete(zavailable
, i
+1); availableNodes
--;
258 /* This is the simplest way possible. Should fix this. */
259 void *realloc(void *start
, size_t newsize
)
264 // return error until malloc_init is called.
268 newstart
= malloc(newsize
);
269 bcopy(start
, newstart
, newsize
);