]>
git.saurik.com Git - apple/boot.git/blob - gen/libsa/zalloc.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 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 1.1 (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
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
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright 1993 NeXT Computer, Inc.
26 * All rights reserved.
28 * Sam's simple memory allocator.
47 static zmem
*zalloced
;
48 static zmem
*zavailable
;
49 static short availableNodes
, allocedNodes
, totalNodes
;
50 static char *zalloc_base
;
52 static void zallocate(char *start
,int size
);
53 static void zinsert(zmem
*zp
, int ndx
);
54 static void zdelete(zmem
*zp
, int ndx
);
55 static void zcoalesce(void);
58 // define the block of memory that the allocator will use
59 void malloc_init(char *start
, int size
, int nodes
)
63 zalloced
= (zmem
*)zalloc_base
;
64 start
+= sizeof(zmem
) * nodes
;
65 zavailable
= (zmem
*)start
;
66 start
+= sizeof(zmem
) * nodes
;
67 zavailable
[0].start
= start
;
68 zavailable
[0].size
= size
;
73 void * malloc(size_t size
)
78 extern char _DATA__end
;
82 // this used to follow the bss but some bios' corrupted it...
83 malloc_init((char *)ZALLOC_ADDR
, ZALLOC_LEN
, ZALLOC_NODES
);
86 size
= ((size
+ 0xf) & ~0xf);
88 for (i
=0; i
<availableNodes
; i
++)
90 // uses first possible node, doesn't try to find best fit
91 if (zavailable
[i
].size
== size
)
93 zallocate(ret
= zavailable
[i
].start
, size
);
94 zdelete(zavailable
, i
); availableNodes
--;
97 else if (zavailable
[i
].size
> size
)
99 zallocate(ret
= zavailable
[i
].start
, size
);
100 zavailable
[i
].start
+= size
;
101 zavailable
[i
].size
-= size
;
108 /* if (ret + size >= (char*)_sp()) _stop("stack clobbered"); */
109 if (ret
+ size
>= (char *)(ZALLOC_ADDR
+ ZALLOC_LEN
))
110 _stop("Out of memory");
120 int i
, tsize
, found
= 0;
121 char *start
= pointer
;
125 for (i
=0; i
<allocedNodes
; i
++)
127 if (zalloced
[i
].start
== start
)
129 tsize
= zalloced
[i
].size
;
132 printf(" zz out %d\n",zout
);
134 zdelete(zalloced
, i
); allocedNodes
--;
141 for (i
=0; i
<availableNodes
; i
++)
143 if ((start
+tsize
) == zavailable
[i
].start
) // merge it in
145 zavailable
[i
].start
= start
;
146 zavailable
[i
].size
+= tsize
;
151 if (i
>0 && (zavailable
[i
-1].start
+ zavailable
[i
-1].size
==
154 zavailable
[i
-1].size
+= tsize
;
159 if ((start
+tsize
) < zavailable
[i
].start
)
161 zinsert(zavailable
, i
); availableNodes
++;
162 zavailable
[i
].start
= start
;
163 zavailable
[i
].size
= tsize
;
168 zavailable
[i
].start
= start
;
169 zavailable
[i
].size
= tsize
;
177 zallocate(char *start
,int size
)
181 printf(" alloc %d, total 0x%x\n",size
,zout
);
183 zalloced
[allocedNodes
].start
= start
;
184 zalloced
[allocedNodes
].size
= size
;
189 zinsert(zmem
*zp
, int ndx
)
198 for (; i
>= ndx
; i
--, z1
--, z2
--)
200 z2
->start
= z1
->start
;
206 zdelete(zmem
*zp
, int ndx
)
214 for (i
=ndx
; i
<totalNodes
-1; i
++, z1
++, z2
++)
216 z1
->start
= z2
->start
;
225 for (i
=0; i
<availableNodes
-1; i
++)
227 if (zavailable
[i
].start
+ zavailable
[i
].size
==
228 zavailable
[i
+1].start
)
230 zavailable
[i
].size
+= zavailable
[i
+1].size
;
231 zdelete(zavailable
, i
+1); availableNodes
--;
237 /* This is the simplest way possible. Should fix this. */
238 void *realloc(void *start
, size_t newsize
)
240 void *newstart
= malloc(newsize
);
241 bcopy(start
, newstart
, newsize
);