-/* $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.
*
* 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;
}