summaryrefslogtreecommitdiff
path: root/libc/stdlib/malloc/heap_alloc.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2002-07-18 15:00:07 +0000
committerEric Andersen <andersen@codepoet.org>2002-07-18 15:00:07 +0000
commit35d29fcb08fadaf006561a058746b0fce76a6a74 (patch)
treeb42a59394f8ee7dc7c11f71ae2d45b1e1beb834b /libc/stdlib/malloc/heap_alloc.c
parent3b1e82407a02aed6319c6686c5b06c2051a20cca (diff)
Miles Bader implemented a new mmap based malloc which is much
smarter than the old "malloc-simple", and actually works, unlike the old "malloc". So kill the old "malloc-simple" and the old "malloc" and replace them with Miles' new malloc implementation. Update Config files to match. Thanks Miles!
Diffstat (limited to 'libc/stdlib/malloc/heap_alloc.c')
-rw-r--r--libc/stdlib/malloc/heap_alloc.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/libc/stdlib/malloc/heap_alloc.c b/libc/stdlib/malloc/heap_alloc.c
new file mode 100644
index 000000000..22591d613
--- /dev/null
+++ b/libc/stdlib/malloc/heap_alloc.c
@@ -0,0 +1,55 @@
+/*
+ * libc/stdlib/malloc-zarg/heap_alloc.c -- allocate from a heap
+ *
+ * Copyright (C) 2002 NEC Corporation
+ * Copyright (C) 2002 Miles Bader <miles@gnu.org>
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser
+ * General Public License. See the file COPYING.LIB in the main
+ * directory of this archive for more details.
+ *
+ * Written by Miles Bader <miles@gnu.org>
+ */
+
+#include <stdlib.h>
+
+#include "heap.h"
+
+
+/* Allocate and return a block at least *SIZE bytes long from HEAP.
+ *SIZE is adjusted to reflect the actual amount allocated (which may be
+ greater than requested). */
+void *
+__heap_alloc (struct heap *heap, size_t *size)
+{
+ struct heap_free_area *fa;
+ size_t _size = *size;
+ void *mem = 0;
+
+ _size = HEAP_ADJUST_SIZE (_size);
+
+ if (_size < sizeof (struct heap_free_area))
+ /* Because we sometimes must use a freed block to hold a free-area node,
+ we must make sure that every allocated block can hold one. */
+ _size = HEAP_ADJUST_SIZE (sizeof (struct heap_free_area));
+
+ mutex_lock (heap->lock);
+
+ HEAP_DEBUG (heap, "before __heap_alloc");
+
+ /* Look for a free area that can contain _SIZE bytes. */
+ for (fa = heap->free_areas; fa; fa = fa->next)
+ if (fa->size >= _size)
+ {
+ /* Found one! */
+ mem = HEAP_FREE_AREA_START (fa);
+ *size = __heap_free_area_alloc (heap, fa, _size);
+ break;
+ }
+
+ HEAP_DEBUG (heap, "after __heap_alloc");
+
+ mutex_unlock (heap->lock);
+
+ return mem;
+}