diff options
author | Michael Shalayeff <mickey@cvs.openbsd.org> | 1997-02-06 14:22:34 +0000 |
---|---|---|
committer | Michael Shalayeff <mickey@cvs.openbsd.org> | 1997-02-06 14:22:34 +0000 |
commit | 2e7326637e61f011d39058f6e99e26c9008c4a53 (patch) | |
tree | e883e132e6926368e126b398f76957b6839a66a3 /sys/lib/libsa/alloc.c | |
parent | f5deb2d68c8ffb793e3e05b713b05e865f88476f (diff) |
remove net/zlib.c (use -lz now)
export NO_NET and SA_ZLIB to the Makefile (from Makefile.inc)
add -p printf flag (tiny code) just for gcc -Wall
import latest alloc.c from netbsd
text data bss dec hex
400 8 0 408 198
Diffstat (limited to 'sys/lib/libsa/alloc.c')
-rw-r--r-- | sys/lib/libsa/alloc.c | 156 |
1 files changed, 137 insertions, 19 deletions
diff --git a/sys/lib/libsa/alloc.c b/sys/lib/libsa/alloc.c index 785b4a77d73..d985a1930fe 100644 --- a/sys/lib/libsa/alloc.c +++ b/sys/lib/libsa/alloc.c @@ -1,7 +1,10 @@ -/* $OpenBSD: alloc.c,v 1.3 1996/12/08 15:15:45 niklas Exp $ */ -/* $NetBSD: alloc.c,v 1.4 1996/09/26 23:15:00 cgd Exp $ */ +/* $OpenBSD: alloc.c,v 1.4 1997/02/06 14:22:33 mickey Exp $ */ +/* $NetBSD: alloc.c,v 1.6 1997/02/04 18:36:33 thorpej 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. * @@ -65,46 +68,161 @@ * 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. */ + +#include "stand.h" + 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 = NULL; + unsigned bestsize = 0xffffffff; /* greater than any real size */ + char *help; + int failed; - prev = &freelist; - while (f && f->size < size) { - prev = &f->next; - f = f->next; +#ifdef ALLOC_TRACE + printf("alloc(%u)", size); +#endif + +#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("=%p\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("=%p (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; + register struct fl *f = + (struct fl *)((char*)ptr - ALIGN(sizeof(unsigned))); +#ifdef ALLOC_TRACE + printf("free(%p, %u) (origsize %u)\n", ptr, size, f->size); +#endif +#ifdef DEBUG + if (size > f->size) + printf("free %u bytes @%p, should be <=%u\n", + size, ptr, f->size); +#ifdef HEAP_START + if (ptr < (void *)HEAP_START) +#else + if (ptr < (void *)end) +#endif + printf("free: %lx before start of heap.\n", (u_long)ptr); - f->size = ALIGN(size); +#ifdef HEAP_LIMIT + if (ptr > (void *)HEAP_LIMIT) + printf("free: %lx beyond end of heap.\n", (u_long)ptr); +#endif +#endif /* DEBUG */ + /* put into freelist */ f->next = freelist; freelist = f; } |