summaryrefslogtreecommitdiff
path: root/tools/src/heapblock.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/src/heapblock.c')
-rw-r--r--tools/src/heapblock.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/tools/src/heapblock.c b/tools/src/heapblock.c
new file mode 100644
index 0000000..5f07f44
--- /dev/null
+++ b/tools/src/heapblock.c
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: MIT */
+
+#include "heapblock.h"
+#include "assert.h"
+#include "types.h"
+#include "utils.h"
+#include "xnuboot.h"
+
+/*
+ * This is a non-freeing allocator, used as a backend for malloc and for uncompressing data.
+ *
+ * Allocating 0 bytes is allowed, and guarantees "infinite" (until the end of RAM) space is
+ * available at the returned pointer as long as no other malloc/heapblock calls occur, which is
+ * useful as a buffer for unknown-length uncompressed data. A subsequent call with a size will then
+ * actually reserve the block.
+ */
+
+static void *heap_base;
+
+void heapblock_init(void)
+{
+ void *top_of_kernel_data = (void *)cur_boot_args.top_of_kernel_data;
+
+ heap_base = top_of_kernel_data;
+ heapblock_alloc(0); // align base
+
+ printf("Heap base: %p\n", heap_base);
+}
+
+void *heapblock_alloc(size_t size)
+{
+ return heapblock_alloc_aligned(size, 64);
+}
+
+void *heapblock_alloc_aligned(size_t size, size_t align)
+{
+ assert((align & (align - 1)) == 0);
+ assert(heap_base);
+
+ uintptr_t block = (((uintptr_t)heap_base) + align - 1) & ~(align - 1);
+ heap_base = (void *)(block + size);
+
+ return (void *)block;
+}