diff options
| author | cgd <cgd@NetBSD.org> | 1997-01-22 01:18:23 +0000 |
|---|---|---|
| committer | cgd <cgd@NetBSD.org> | 1997-01-22 01:18:23 +0000 |
| commit | ea27b7aa31d10d53141223a9a4f77f406112c8ea (patch) | |
| tree | 582a8e34d19efa487eaab3edb0e06b4d1862bacc /sys/lib/libsa/alloc.c | |
| parent | 862c1d585cc107b8191c7d5ee49573011090e3ed (diff) | |
Add a whole bunch of features, from Matthias Drochner:
best-fit, rather than first-fit, algorithm.
ability to handle free()s with zero size (needed for gzip read
support).
ability to start the heap someplace else (defined by HEAP_START).
ability to limit growth of the heap (via HEAP_LIMIT).
debugging sanity checks (ifdef DEBUG).
allocation tracing support, to help debugging (ifdef ALLOC_TRACE).
and from me:
ability to pick a (smaller) first-fit algorithm (via ALLOC_FIRST_FIT).
lots of comments.
If heap limits and all of the debugging features are disabled (the default),
and ALLOC_FIRST_FIT is defined (not the default), compiled with -O on the
alpha the new version is the same (object) size as the old version.
Diffstat (limited to 'sys/lib/libsa/alloc.c')
| -rw-r--r-- | sys/lib/libsa/alloc.c | 142 |
1 files changed, 123 insertions, 19 deletions
diff --git a/sys/lib/libsa/alloc.c b/sys/lib/libsa/alloc.c index 37e98efe75d..5ebc1089326 100644 --- a/sys/lib/libsa/alloc.c +++ b/sys/lib/libsa/alloc.c @@ -1,6 +1,9 @@ -/* $NetBSD: alloc.c,v 1.4 1996/09/26 23:15:00 cgd Exp $ */ +/* $NetBSD: alloc.c,v 1.5 1997/01/22 01:18:23 cgd Exp $ */ -/*- +/* + * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved. + * Copyright (c) 1996 + * Matthias Drochner. All rights reserved. * Copyright (c) 1993 * The Regents of the University of California. All rights reserved. * @@ -64,46 +67,147 @@ * rights to redistribute these changes. */ +/* + * Dynamic memory allocator. + * + * Compile options: + * + * ALLOC_TRACE enable tracing of allocations/deallocations + + * ALLOC_FIRST_FIT use a first-fit allocation algorithm, rather than + * the default best-fit algorithm. + * + * HEAP_LIMIT heap limit address (defaults to "no limit"). + * + * HEAP_START start address of heap (defaults to '&end'). + * + * DEBUG enable debugging sanity checks. + */ + #include <sys/param.h> /* - * Dynamic memory allocator + * Each block actually has ALIGN(unsigned) + ALIGN(size) bytes allocated + * to it, as follows: + * + * 0 ... (sizeof(unsigned) - 1) + * allocated or unallocated: holds size of user-data part of block. + * + * sizeof(unsigned) ... (ALIGN(sizeof(unsigned)) - 1) + * allocated: unused + * unallocated: depends on packing of struct fl + * + * ALIGN(sizeof(unsigned)) ... (ALIGN(sizeof(unsigned)) + ALIGN(data size) - 1) + * allocated: user data + * unallocated: depends on packing of struct fl + * + * 'next' is only used when the block is unallocated (i.e. on the free list). + * However, note that ALIGN(sizeof(unsigned)) + ALIGN(data size) must + * be at least 'sizeof(struct fl)', so that blocks can be used as structures + * when on the free list. */ struct fl { - struct fl *next; unsigned size; + struct fl *next; } *freelist = (struct fl *)0; +#ifdef HEAP_START +static char *top = (char*)HEAP_START; +#else extern char end[]; static char *top = end; +#endif void * alloc(size) unsigned size; { - register struct fl *f = freelist, **prev; + register struct fl **f = &freelist, **bestf; + unsigned bestsize = 0xffffffff; /* greater than any real size */ + char *help; + int failed; + +#ifdef ALLOC_TRACE + printf("alloc(%u)", size); +#endif - prev = &freelist; - while (f && f->size < size) { - prev = &f->next; - f = f->next; +#ifdef ALLOC_FIRST_FIT + while (*f != (struct fl *)0 && (*f)->size < size) + f = &((*f)->next); + bestf = f; + failed = (*bestf == (struct fl *)0); +#else + /* scan freelist */ + while (*f) { + if ((*f)->size >= size) { + if ((*f)->size == size) /* exact match */ + goto found; + + if ((*f)->size < bestsize) { + /* keep best fit */ + bestf = f; + bestsize = (*f)->size; + } + } + f = &((*f)->next); } - if (f == (struct fl *)0) { - f = (struct fl *)ALIGN(top); - top = (char *)f + ALIGN(size); - } else - *prev = f->next; - return ((void *)f); + + /* no match in freelist if bestsize unchanged */ + failed = (bestsize == 0xffffffff); +#endif + + if (failed) { /* nothing found */ + /* + * allocate from heap, keep chunk len in + * first word + */ + help = top; + + /* make _sure_ the region can hold a struct fl. */ + if (size < ALIGN(sizeof (struct fl *))) + size = ALIGN(sizeof (struct fl *)); + top += ALIGN(sizeof(unsigned)) + ALIGN(size); +#ifdef HEAP_LIMIT + if (top > (char*)HEAP_LIMIT) + panic("heap full (0x%lx+%u)", help, size); +#endif + *(unsigned *)help = ALIGN(size); +#ifdef ALLOC_TRACE + printf("=%x\n", help + ALIGN(sizeof(unsigned))); +#endif + return(help + ALIGN(sizeof(unsigned))); + } + + /* we take the best fit */ + f = bestf; + +found: + /* remove from freelist */ + help = (char*)*f; + *f = (*f)->next; +#ifdef ALLOC_TRACE + printf("=%lx (origsize %u)\n", help + ALIGN(sizeof(unsigned)), + *(unsigned *)help); +#endif + return(help + ALIGN(sizeof(unsigned))); } void free(ptr, size) void *ptr; - unsigned size; + unsigned size; /* only for consistence check */ { - register struct fl *f = (struct fl *)ptr; - - f->size = ALIGN(size); + register struct fl *f = + (struct fl *)((char*)ptr - ALIGN(sizeof(unsigned))); +#ifdef ALLOC_TRACE + printf("free(%lx, %u) (origsize %u)\n", ptr, size, f->size); +#endif +#ifdef DEBUG + if (size > f->size) + printf("free %u bytes @%lx, should be <=%u\n", + size, ptr, f->size); +#endif + /* put into freelist */ f->next = freelist; freelist = f; } |
