aboutsummaryrefslogtreecommitdiffstats
path: root/menu/heap.c
diff options
context:
space:
mode:
Diffstat (limited to 'menu/heap.c')
-rw-r--r--menu/heap.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/menu/heap.c b/menu/heap.c
new file mode 100644
index 00000000..72ebab6a
--- /dev/null
+++ b/menu/heap.c
@@ -0,0 +1,71 @@
+/*
+ * Very simple heap manager.
+ * Allocation Strategy: The lower end of the heap, is the end of the BSS.
+ *
+ * During an alloc, if there is enough space below the high end of heap,
+ * we return a pointer to the allocated space and move curr.
+ * Space once allocated is never deallocated!
+ * We run out of space if we get within BUFSIZE bytes of the stack pointer.
+ */
+
+#include "heap.h"
+#include "string.h"
+#include "biosio.h"
+
+#ifndef NULL
+#define NULL ((void *)0)
+#endif
+
+extern char _end[];
+static unsigned int heap_curr = (unsigned int)_end;
+
+static inline unsigned int currsp(void)
+{
+ unsigned int stkptr;
+
+ asm("movl %%esp,%0 " : "=rm" (stkptr) );
+ return stkptr;
+}
+
+static inline void _checkheap(void)
+{
+ if (currsp() < heap_curr) // Heap corrupted
+ {
+ csprint("Heap Corrupted, bailing out\n");
+ asm volatile("int $0x21" : : "a" (0x4C7f)); /* Exit with error */
+ return;
+ }
+}
+
+void * malloc(unsigned int num) // Allocate so much space
+{
+ unsigned int ans, heap_max;
+
+ _checkheap();
+ heap_max = currsp() - STACKSIZE;
+
+ if ( heap_curr+num > heap_max )
+ return NULL;
+ ans = heap_curr;
+ heap_curr += num;
+ return (void *) ans;
+}
+
+/* We don't actually ever use these; if enabled,
+ probably _checkheap() shouldn't be inline.*/
+#if 0
+
+int checkalloc(unsigned int num)
+{
+ _checkheap();
+ return (heap_curr + num < heap_max);
+}
+
+
+void free(void * ptr)
+{
+ _checkheap();
+ return;
+}
+
+#endif