]>
git.saurik.com Git - apple/boot.git/blob - i386/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.
46 static zmem
* zalloced
;
47 static zmem
* zavailable
;
48 static short availableNodes
, allocedNodes
, totalNodes
;
49 static char * zalloc_base
;
50 static void (*zerror
)();
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);
57 #define ZALLOC_NODES 384
59 static void malloc_error()
61 // printf("Out of memory\n");
64 // define the block of memory that the allocator will use
65 void malloc_init(char * start
, int size
, int nodes
)
69 zalloced
= (zmem
*) zalloc_base
;
70 start
+= sizeof(zmem
) * nodes
;
71 zavailable
= (zmem
*) start
;
72 start
+= sizeof(zmem
) * nodes
;
73 zavailable
[0].start
= start
;
74 zavailable
[0].size
= size
;
77 zerror
= malloc_error
;
80 void * malloc(size_t size
)
87 // this used to follow the bss but some bios' corrupted it...
88 malloc_init((char *)ZALLOC_ADDR
, ZALLOC_LEN
, ZALLOC_NODES
);
91 size
= ((size
+ 0xf) & ~0xf);
93 for (i
= 0; i
< availableNodes
; i
++)
95 // uses first possible node, doesn't try to find best fit
96 if ( zavailable
[i
].size
== size
)
98 zallocate(ret
= zavailable
[i
].start
, size
);
99 zdelete(zavailable
, i
); availableNodes
--;
102 else if ( zavailable
[i
].size
> size
)
104 zallocate(ret
= zavailable
[i
].start
, size
);
105 zavailable
[i
].start
+= size
;
106 zavailable
[i
].size
-= size
;
112 if ((ret
== 0) || (ret
+ size
>= (char *)(ZALLOC_ADDR
+ ZALLOC_LEN
)))
114 if (zerror
) (*zerror
)(ret
);
123 void free(void * pointer
)
125 int i
, tsize
= 0, found
= 0;
126 char * start
= pointer
;
128 if ( !start
) return;
130 for (i
= 0; i
< allocedNodes
; i
++)
132 if ( zalloced
[i
].start
== start
)
134 tsize
= zalloced
[i
].size
;
137 printf(" zz out %d\n",zout
);
139 zdelete(zalloced
, i
); allocedNodes
--;
144 if ( !found
) return;
146 for (i
= 0; i
< availableNodes
; i
++)
148 if ((start
+ tsize
) == zavailable
[i
].start
) // merge it in
150 zavailable
[i
].start
= start
;
151 zavailable
[i
].size
+= tsize
;
157 (zavailable
[i
-1].start
+ zavailable
[i
-1].size
== start
))
159 zavailable
[i
-1].size
+= tsize
;
164 if ((start
+ tsize
) < zavailable
[i
].start
)
166 zinsert(zavailable
, i
); availableNodes
++;
167 zavailable
[i
].start
= start
;
168 zavailable
[i
].size
= tsize
;
173 zavailable
[i
].start
= start
;
174 zavailable
[i
].size
= tsize
;
181 zallocate(char * start
,int size
)
185 printf(" alloc %d, total 0x%x\n",size
,zout
);
187 zalloced
[allocedNodes
].start
= start
;
188 zalloced
[allocedNodes
].size
= size
;
193 zinsert(zmem
* zp
, int ndx
)
202 for (; i
>= ndx
; i
--, z1
--, z2
--)
204 z2
->start
= z1
->start
;
210 zdelete(zmem
* zp
, int ndx
)
218 for (i
= ndx
; i
< totalNodes
-1; i
++, z1
++, z2
++)
220 z1
->start
= z2
->start
;
230 for (i
= 0; i
< availableNodes
-1; i
++)
232 if ( zavailable
[i
].start
+ zavailable
[i
].size
==
233 zavailable
[i
+1].start
)
235 zavailable
[i
].size
+= zavailable
[i
+1].size
;
236 zdelete(zavailable
, i
+1); availableNodes
--;
242 /* This is the simplest way possible. Should fix this. */
243 void * realloc(void * start
, size_t newsize
)
245 void * newstart
= malloc(newsize
);
246 bcopy(start
, newstart
, newsize
);