summaryrefslogtreecommitdiff
path: root/libc/stdlib/malloc/heap_alloc_at.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_at.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_at.c')
-rw-r--r--libc/stdlib/malloc/heap_alloc_at.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/libc/stdlib/malloc/heap_alloc_at.c b/libc/stdlib/malloc/heap_alloc_at.c
new file mode 100644
index 000000000..8ee925488
--- /dev/null
+++ b/libc/stdlib/malloc/heap_alloc_at.c
@@ -0,0 +1,51 @@
+/*
+ * libc/stdlib/malloc-zarg/heap_alloc_at.c -- allocate at a specific address
+ *
+ * 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 SIZE bytes at address MEM in HEAP. Return the actual size
+ allocated, or 0 if we failed. */
+size_t
+__heap_alloc_at (struct heap *heap, void *mem, size_t size)
+{
+ struct heap_free_area *fa;
+ size_t alloced = 0;
+
+ size = HEAP_ADJUST_SIZE (size);
+
+ mutex_lock (heap->lock);
+
+ HEAP_DEBUG (heap, "before __heap_alloc_at");
+
+ /* Look for a free area that can contain SIZE bytes. */
+ for (fa = heap->free_areas; fa; fa = fa->next)
+ {
+ void *fa_mem = HEAP_FREE_AREA_START (fa);
+ if (fa_mem <= mem)
+ {
+ if (fa_mem == mem && fa->size >= size)
+ /* FA has the right addr, and is big enough! */
+ alloced = __heap_free_area_alloc (heap, fa, size);
+ break;
+ }
+ }
+
+ HEAP_DEBUG (heap, "after __heap_alloc_at");
+
+ mutex_unlock (heap->lock);
+
+ return alloced;
+}