2 * cacao/mm/allocator2.c
4 * Copyright (c) 1998 A. Krall, R. Grafl, M. Gschwind, M. Probst, Ph. Tomsich
5 * See file COPYRIGHT for information on usage and disclaimer of warranties
7 * Authors: Philipp Tomsich EMAIL: cacao@complang.tuwien.ac.at
9 * $Id: allocator2.c 98 1998-11-30 22:04:43Z phil $
12 #include "allocator.h"
14 static FREE_EXACT* __freelist_exact[1 << EXACT];
15 static FREE_EXACT** freelist_exact = &__freelist_exact[-1];
16 static FREE_LARGE* freelist_large[LARGE << SUBBIT];
19 static unsigned char highest[256];
21 # if EXACT_TOP_BIT < 8
23 static unsigned char highest_0[2048];
25 static unsigned char highest_0[1024];
28 static unsigned char* highest_8 = &highest_0[256];
29 static unsigned char* highest_16 = &highest_0[512];
30 static unsigned char* highest_24 = &highest_0[768];
32 static unsigned char* highest_32 = &highest_0[1024];
33 static unsigned char* highest_40 = &highest_0[1280];
34 static unsigned char* highest_48 = &highest_0[1538];
35 static unsigned char* highest_56 = &highest_0[1792];
39 static unsigned char highest_8[1792];
41 static unsigned char highest_8[768];
44 static unsigned char* highest_16 = &highest_8[256];
45 static unsigned char* highest_24 = &highest_8[512];
47 static unsigned char* highest_32 = &highest_8[768];
48 static unsigned char* highest_40 = &highest_8[1280];
49 static unsigned char* highest_48 = &highest_8[1538];
50 static unsigned char* highest_56 = &highest_8[1792];
57 find_highest(SIZE bits){
59 /* optimize for large allocation sizes */
62 if (bits & 0xff00000000000000ULL)
63 return 56 + highest[bits >> 56 & 0xff];
64 if (bits & 0xff000000000000ULL)
65 return 48 + highest[bits >> 48 & 0xff];
66 if (bits & 0xff0000000000ULL)
67 return 40 + highest[bits >> 40 & 0xff];
68 if (bits & 0xff00000000ULL)
69 return 32 + highest[bits >> 32 & 0xff];
72 if (bits & 0xff000000)
73 return 24 + highest[bits >> 24 & 0xff];
75 return 16 + highest[bits >> 16 & 0xff];
77 return 8 + highest[bits >> 8 & 0xff];
79 # if EXACT_TOP_BIT < 8
80 return highest[bits & 0xff];
84 if (bits & 0xff00000000000000ULL)
85 return highest_56[bits >> 56 & 0xff];
86 if (bits & 0xff000000000000ULL)
87 return highest_48[bits >> 48 & 0xff];
88 if (bits & 0xff0000000000ULL)
89 return highest_40[bits >> 40 & 0xff];
90 if (bits & 0xff00000000ULL)
91 return highest_32[bits >> 32 & 0xff];
94 if (bits & 0xff000000)
95 return highest_24[bits >> 24 & 0xff];
97 return highest_16[bits >> 16 & 0xff];
99 return highest_8[bits >> 8 & 0xff];
101 # if EXACT_TOP_BIT < 8
102 return highest_0[bits & 0xff];
107 /* optimize for small allocation sizes */
110 # if EXACT_TOP_BIT < 8
111 if (!(bits & ~0xffULL))
112 return highest[bits & 0xff];
115 if (!(bits & ~0xffffULL))
116 return 8 + highest[bits >> 8 & 0xff];
118 if (!(bits & ~0xffffffULL))
119 return 16 + highest[bits >> 16 & 0xff];
122 return 24 + highest[bits >> 24 & 0xff];
124 if (!(bits & ~0xffffffffULL))
125 return 24 + highest[bits >> 24 & 0xff];
127 if (!(bits & ~0xffffffffffULL))
128 return 32 + highest[bits >> 32 & 0xff];
130 if (!(bits & ~0xffffffffffffULL))
131 return 40 + highest[bits >> 40 & 0xff];
133 if (!(bits & ~0xffffffffffffffULL))
134 return 48 + highest[bits >> 48 & 0xff];
136 return 56 + highest[bits >> 56 & 0xff];
139 # if EXACT_TOP_BIT < 8
140 if (!(bits & ~0xffULL))
141 return highest_0[bits & 0xff];
144 if (!(bits & ~0xffffULL))
145 return highest_8[bits >> 8 & 0xff];
147 if (!(bits & ~0xffffffULL))
148 return highest_16[bits >> 16 & 0xff];
151 return highest_24[bits >> 24 & 0xff];
153 if (!(bits & ~0xffffffffULL))
154 return highest_24[bits >> 24 & 0xff];
156 if (!(bits & ~0xffffffffffULL))
157 return highest_32[bits >> 32 & 0xff];
159 if (!(bits & ~0xffffffffffffULL))
160 return highest_40[bits >> 40 & 0xff];
162 if (!(bits & ~0xffffffffffffffULL))
163 return highest_48[bits >> 48 & 0xff];
165 return highest_56[bits >> 56 & 0xff];
176 static unsigned char r[10] = { 1, 1, 2, 4, 8, 16, 32, 64, 128 };
184 for (k = 0; k < 10; ++k) {
185 for (m = 0; m < r[k]; ++m) {
187 highest[i] = j - (EXACT + ALIGN) - 1;
189 # if (EXACT_TOP_BIT < 8)
190 highest_0 [i] = j - 8 - (EXACT + ALIGN) - 1;
192 highest_8 [i] = j + 8 - (EXACT + ALIGN) - 1;
193 highest_16 [i] = 8 + highest_8 [i];
194 highest_24 [i] = 16 + highest_8 [i];
196 highest_32 [i] = 24 + highest_8 [i];
197 highest_40 [i] = 32 + highest_8 [i];
198 highest_48 [i] = 40 + highest_8 [i];
199 highest_56 [i] = 48 + highest_8 [i];
210 #define PRESERVE_LOCALITY 0
213 static SIZE allocator_largest_free = 0;
216 #if PRESERVE_LOCALITY
217 static void* allocator_last_splitoff = 0;
218 static SIZE allocator_last_splitoff_size = 0;
227 allocator_largest_free = 0;
230 #if PRESERVE_LOCALITY
231 allocator_last_splitoff = 0;
232 allocator_last_splitoff_size = 0;
235 for (i = 1; i <= (1 << EXACT); ++i)
236 freelist_exact[i] = 0;
238 for (i = 0; i < (LARGE << SUBBIT); ++i)
239 freelist_large[i] = 0;
246 #if COUNT_ALLOCATIONS
247 static unsigned long long allocator_allocations = 0;
250 allocator_display_count()
252 fprintf(stderr, "total allocations: %d\n", allocator_allocations);
262 "allocator_init: $Id: allocator2.c 98 1998-11-30 22:04:43Z phil $\n\n");
264 fprintf(stderr, "\t%d bit addresses\n", ADDRESS);
265 fprintf(stderr, "\t%d bit alignment\n", ALIGN);
266 fprintf(stderr, "\t%d exact bits\n", EXACT );
267 fprintf(stderr, "\t%d large bits\n", LARGE );
268 fprintf(stderr, "\t%d subbin bits\n", SUBBIT );
269 fprintf(stderr, "\tusing %s tables\n",
270 SMALL_TABLES ? "small (256 byte)" : "large (2048 byte)");
271 fprintf(stderr, "\tusing %s highest-bit search strategy\n",
272 MSB_TO_LSB_SEARCH ? "MSB-to-LSB" : "LSB-to-MSB");
275 "\tusing %d bytes for exact bins, %d bytes for sorted bins\n\n",
276 (1 << EXACT) << ALIGN,
277 (LARGE << SUBBIT) << ALIGN);
280 init_table(); /* set up the highest-table */
281 allocator_reset(); /* clear the freelists & internal variables */
285 size_to_bin(SIZE size)
288 if (size > ((1 << EXACT) << ALIGN)) {
292 unsigned char highbit = find_highest(size);
293 unsigned char shift = highbit + (EXACT + ALIGN - SUBBIT);
294 static int mask = (1 << SUBBIT) - 1;
295 int bin = (highbit << SUBBIT) + ( (size >> shift) & mask );
297 int bin = find_highest(size);
301 printf("==> large bin: %d\n", bin);
308 printf("==> exact bin: %d\n", (int)((size >> ALIGN) - 1));
311 return (size >> ALIGN);
319 #define OLDEST_FIRST 0
322 allocator_free(void* chunk, SIZE size)
324 assert(chunk && size);
327 if (size > allocator_largest_free)
328 allocator_largest_free = size;
331 if (size > ((1 << EXACT) << ALIGN)) {
332 int bin = size_to_bin(size);
333 FREE_LARGE* curr = freelist_large[bin];
335 ((FREE_LARGE*)chunk)->size = size;
337 if (!freelist_large[bin] || (freelist_large[bin]->size > size)) {
339 ((FREE_LARGE*)chunk)->next = freelist_large[bin];
340 freelist_large[bin] = (FREE_LARGE*)chunk;
344 while ((curr->next) && (curr->next->size <= size)) {
348 /* insert or append */
349 ((FREE_LARGE*)chunk)->next = curr->next;
350 curr->next=(FREE_LARGE*)chunk;
352 int bin = size >> ALIGN;
355 if (!freelist_exact[bin]) {
358 ((FREE_EXACT*)chunk)->next = freelist_exact[bin];
359 freelist_exact[bin] = (FREE_EXACT*)chunk;
362 FREE_EXACT* curr = (FREE_EXACT*)freelist_exact[bin];
363 while (curr->next && (curr < (FREE_EXACT*)chunk))
367 ((FREE_EXACT*)chunk)->next = curr->next;
368 curr->next = (FREE_EXACT*)chunk;
377 allocator_alloc(SIZE size)
383 #if COUNT_ALLOCATIONS
384 allocator_allocations++;
388 if (size > allocator_largest_free)
392 #if PRESERVE_LOCALITY
393 allocator_last_splitoff = 0;
394 allocator_last_splitoff_size = 0;
397 if (size <= ((1 << EXACT) << ALIGN)) {
398 /* search the exact bins */
402 while (bin <= maxbin) {
403 if (freelist_exact[bin]) {
405 result = (void*)freelist_exact[bin];
406 freelist_exact[bin]=freelist_exact[bin]->next;
409 if ((bin << ALIGN) > size)
410 allocator_free((void*)((long)result + size), (bin << ALIGN) - size);
419 bin = size_to_bin(size);
422 maxbin = LARGE << SUBBIT;
424 while (bin < maxbin) {
425 if (freelist_large[bin]) {
426 FREE_LARGE* chunk = freelist_large[bin];
428 if (chunk->size >= size) {
430 freelist_large[bin] = chunk->next;
433 if (chunk->size > size)
434 allocator_free((void*)((long)chunk + size), (chunk->size - size));
439 while (chunk->next && (chunk->next->size < size))
444 FREE_LARGE* new_chunk = chunk->next;
445 chunk->next = new_chunk->next;
448 if (new_chunk->size > size)
449 allocator_free((void*)((long)new_chunk + size), (new_chunk->size - size));
451 return (void*)new_chunk;
458 return result; /* NULL */
466 for (i = 1; i <= 1 << EXACT; ++i) {
468 FREE_EXACT* chunk = freelist_exact[i];
476 printf("exact bin %d (%d byte blocks): %d\n", i, i * (1 << ALIGN), count);
478 for (i = 0; i < LARGE << SUBBIT; ++i) {
480 FREE_LARGE* chunk = freelist_large[i];
483 printf("large bin %d:\n", i);
486 printf("\t%d bytes @ 0x%lx\n", chunk->size, chunk);
492 printf("dump complete.\n");
499 * These are local overrides for various environment variables in Emacs.
500 * Please do not remove this and leave it at the end of the file, where
501 * Emacs will automagically detect them.
502 * ---------------------------------------------------------------------
505 * indent-tabs-mode: t