]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright 1993 NeXT Computer, Inc.
27 * All rights reserved.
29 * Sam's simple memory allocator.
33 * zalloc.c - malloc functions.
35 * Copyright (c) 1998-2003 Apple Computer, Inc.
42 #define ZALLOC_NODES 768
55 static int zalloc_addr
;
56 static int zalloc_len
;
57 static zmem
*zalloced
;
58 static zmem
*zavailable
;
59 static short availableNodes
, allocedNodes
, totalNodes
;
60 static char *zalloc_base
;
61 static void (*zerror
)();
63 static void zallocate(char *start
,int size
);
64 static void zinsert(zmem
*zp
, int ndx
);
65 static void zdelete(zmem
*zp
, int ndx
);
66 static void zcoalesce(void);
69 // define the block of memory that the allocator will use
70 void malloc_init(char *start
, int size
)
72 int nodes
= ZALLOC_NODES
;
74 zalloc_addr
= (int)start
;
79 zalloced
= (zmem
*)zalloc_base
;
80 start
+= sizeof(zmem
) * nodes
;
81 zavailable
= (zmem
*)start
;
82 start
+= sizeof(zmem
) * nodes
;
83 zavailable
[0].start
= start
;
84 zavailable
[0].size
= size
;
89 void malloc_error_init(void (*error_fn
)())
94 void * malloc(size_t size
)
99 extern char _DATA__end
;
102 // return error until malloc_init is called.
106 size
= ((size
+ 0xf) & ~0xf);
108 for (i
=0; i
<availableNodes
; i
++)
110 // uses first possible node, doesn't try to find best fit
111 if (zavailable
[i
].size
== size
)
113 zallocate(ret
= zavailable
[i
].start
, size
);
114 zdelete(zavailable
, i
); availableNodes
--;
117 else if (zavailable
[i
].size
> size
)
119 zallocate(ret
= zavailable
[i
].start
, size
);
120 zavailable
[i
].start
+= size
;
121 zavailable
[i
].size
-= size
;
127 /* if (ret + size >= (char*)_sp()) _stop("stack clobbered"); */
128 if ((ret
== 0) || (ret
+ size
>= (char *)(zalloc_addr
+ zalloc_len
)))
139 int i
, tsize
, found
= 0;
140 char *start
= pointer
;
143 // return until malloc_init is called.
149 for (i
=0; i
<allocedNodes
; i
++)
151 if (zalloced
[i
].start
== start
)
153 tsize
= zalloced
[i
].size
;
156 printf(" zz out %d\n",zout
);
158 zdelete(zalloced
, i
); allocedNodes
--;
165 for (i
=0; i
<availableNodes
; i
++)
167 if ((start
+tsize
) == zavailable
[i
].start
) // merge it in
169 zavailable
[i
].start
= start
;
170 zavailable
[i
].size
+= tsize
;
175 if (i
>0 && (zavailable
[i
-1].start
+ zavailable
[i
-1].size
==
178 zavailable
[i
-1].size
+= tsize
;
183 if ((start
+tsize
) < zavailable
[i
].start
)
185 zinsert(zavailable
, i
); availableNodes
++;
186 zavailable
[i
].start
= start
;
187 zavailable
[i
].size
= tsize
;
192 zavailable
[i
].start
= start
;
193 zavailable
[i
].size
= tsize
;
201 zallocate(char *start
,int size
)
205 printf(" alloc %d, total 0x%x\n",size
,zout
);
207 zalloced
[allocedNodes
].start
= start
;
208 zalloced
[allocedNodes
].size
= size
;
213 zinsert(zmem
*zp
, int ndx
)
222 for (; i
>= ndx
; i
--, z1
--, z2
--)
224 z2
->start
= z1
->start
;
230 zdelete(zmem
*zp
, int ndx
)
238 for (i
=ndx
; i
<totalNodes
-1; i
++, z1
++, z2
++)
240 z1
->start
= z2
->start
;
249 for (i
=0; i
<availableNodes
-1; i
++)
251 if (zavailable
[i
].start
+ zavailable
[i
].size
==
252 zavailable
[i
+1].start
)
254 zavailable
[i
].size
+= zavailable
[i
+1].size
;
255 zdelete(zavailable
, i
+1); availableNodes
--;
261 /* This is the simplest way possible. Should fix this. */
262 void *realloc(void *start
, size_t newsize
)
267 // return error until malloc_init is called.
271 newstart
= malloc(newsize
);
272 bcopy(start
, newstart
, newsize
);