summaryrefslogtreecommitdiff
path: root/src/include/mm
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-09-14 12:56:39 +0200
committerCarlos Maiolino <[email protected]>2025-09-14 12:56:39 +0200
commite79687494bc3b01f42a79b0c355533e6e39e9a9d (patch)
treef25713cee9d69a61c26756c9784bb16ef5c8011d /src/include/mm
parentbbba609cd83cc99fe65feb6230347d164f621b30 (diff)
Add a simple heap implementation
Add a simple API implementing a heap memory area. This specifies 2 main data structures: struct heap - Defines a heap: Holds a table with all entries in the specific heap and the initial heap memory address struct heap_table - Describes the usage state of every PAGE within the specific heap. Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'src/include/mm')
-rw-r--r--src/include/mm/heap.h50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/include/mm/heap.h b/src/include/mm/heap.h
new file mode 100644
index 0000000..4c55ae4
--- /dev/null
+++ b/src/include/mm/heap.h
@@ -0,0 +1,50 @@
+#ifndef HEAP_H
+#define HEAP_H
+#include <stddef.h>
+#include <toxic/config.h>
+
+/*
+ * Heap table consists of an array of 1-byte long entries describing
+ * each PAGE_SIZE region inside the heap.
+ */
+
+/*
+ * Each entry in the heap table is 1byte long, where:
+ *
+ * bit 7: Set when next page in this heap belongs to the same allocation
+ * bit 6: Set when the current page in the first in this allocation map
+ * bit 5-1: Reserved
+ * bit 0: Used flag
+ */
+#define HEAP_ENTRY_INUSE 0x1
+#define HEAP_ENTRY_FREE 0x0
+
+#define HEAP_ENTRY_HAS_NEXT 0x80 /* 0b1000000 */
+#define HEAP_ENTRY_IS_FIRST 0x40 /* 0b01000000 */
+
+
+#define HEAP_ENTRY_USED_MASK ((unsigned char) 0x01)
+/*
+ * Define a macro for entry size so in future
+ * I can turn this into a proper structure
+ */
+#define HEAP_ENTRY_SIZE (sizeof(unsigned char))
+
+struct heap_table {
+ unsigned char *entries;
+ size_t count;
+};
+
+/* Heap descriptor */
+struct heap {
+ struct heap_table *table;
+ void *start_addr;
+};
+
+int heap_create(struct heap *heap, void *start, void *end,
+ struct heap_table *tbl);
+
+void * heap_malloc(struct heap *heap, size_t size);
+void heap_free(struct heap *heap, void *ptr);
+
+#endif /* HEAP_H */