]>
git.saurik.com Git - apple/boot.git/blob - gen/libsa/zalloc.c
ea3b9a88678fdfbbf634999fc6f8225f83ec7d50
2 * Copyright (c) 1999 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.
48 static zmem
*zalloced
;
49 static zmem
*zavailable
;
50 static short availableNodes
, allocedNodes
, totalNodes
;
51 static char *zalloc_base
;
53 static void zallocate(char *start
,int size
);
54 static void zinsert(zmem
*zp
, int ndx
);
55 static void zdelete(zmem
*zp
, int ndx
);
56 static void zcoalesce(void);
59 // define the block of memory that the allocator will use
60 void malloc_init(char *start
, int size
, int nodes
)
64 zalloced
= (zmem
*)zalloc_base
;
65 start
+= sizeof(zmem
) * nodes
;
66 zavailable
= (zmem
*)start
;
67 start
+= sizeof(zmem
) * nodes
;
68 zavailable
[0].start
= start
;
69 zavailable
[0].size
= size
;
74 void * malloc(size_t size
)
79 extern char _DATA__end
;
83 // this used to follow the bss but some bios' corrupted it...
84 malloc_init((char *)ZALLOC_ADDR
, ZALLOC_LEN
, ZALLOC_NODES
);
87 size
= ((size
+ 0xf) & ~0xf);
89 for (i
=0; i
<availableNodes
; i
++)
91 // uses first possible node, doesn't try to find best fit
92 if (zavailable
[i
].size
== size
)
94 zallocate(ret
= zavailable
[i
].start
, size
);
95 zdelete(zavailable
, i
); availableNodes
--;
98 else if (zavailable
[i
].size
> size
)
100 zallocate(ret
= zavailable
[i
].start
, size
);
101 zavailable
[i
].start
+= size
;
102 zavailable
[i
].size
-= size
;
109 /* if (ret + size >= (char*)_sp()) _stop("stack clobbered"); */
110 if (ret
+ size
>= (char *)(ZALLOC_ADDR
+ ZALLOC_LEN
))
111 _stop("Out of memory");
121 int i
, tsize
, found
= 0;
122 char *start
= pointer
;
126 for (i
=0; i
<allocedNodes
; i
++)
128 if (zalloced
[i
].start
== start
)
130 tsize
= zalloced
[i
].size
;
133 printf(" zz out %d\n",zout
);
135 zdelete(zalloced
, i
); allocedNodes
--;
142 for (i
=0; i
<availableNodes
; i
++)
144 if ((start
+tsize
) == zavailable
[i
].start
) // merge it in
146 zavailable
[i
].start
= start
;
147 zavailable
[i
].size
+= tsize
;
152 if (i
>0 && (zavailable
[i
-1].start
+ zavailable
[i
-1].size
==
155 zavailable
[i
-1].size
+= tsize
;
160 if ((start
+tsize
) < zavailable
[i
].start
)
162 zinsert(zavailable
, i
); availableNodes
++;
163 zavailable
[i
].start
= start
;
164 zavailable
[i
].size
= tsize
;
169 zavailable
[i
].start
= start
;
170 zavailable
[i
].size
= tsize
;
178 zallocate(char *start
,int size
)
182 printf(" alloc %d, total 0x%x\n",size
,zout
);
184 zalloced
[allocedNodes
].start
= start
;
185 zalloced
[allocedNodes
].size
= size
;
190 zinsert(zmem
*zp
, int ndx
)
199 for (; i
>= ndx
; i
--, z1
--, z2
--)
201 z2
->start
= z1
->start
;
207 zdelete(zmem
*zp
, int ndx
)
215 for (i
=ndx
; i
<totalNodes
-1; i
++, z1
++, z2
++)
217 z1
->start
= z2
->start
;
226 for (i
=0; i
<availableNodes
-1; i
++)
228 if (zavailable
[i
].start
+ zavailable
[i
].size
==
229 zavailable
[i
+1].start
)
231 zavailable
[i
].size
+= zavailable
[i
+1].size
;
232 zdelete(zavailable
, i
+1); availableNodes
--;
238 /* This is the simplest way possible. Should fix this. */
239 void *realloc(void *start
, size_t newsize
)
241 void *newstart
= malloc(newsize
);
242 bcopy(start
, newstart
, newsize
);