summaryrefslogtreecommitdiff
path: root/src/memory.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/memory.c')
-rw-r--r--src/memory.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/memory.c b/src/memory.c
new file mode 100644
index 0000000..970ba29
--- /dev/null
+++ b/src/memory.c
@@ -0,0 +1,42 @@
+
+#include <stdint.h>
+#include <limits.h>
+#include <stddef.h>
+#include "memory.h"
+#include "multiboot.h"
+
+struct memory_area {
+ uint32_t len;
+ struct memory_area *next;
+};
+
+struct memory_area *first = NULL;
+
+void init_memory(multiboot_info_t *mb) {
+ struct memory_area *prev = NULL;
+ for (
+ multiboot_memory_map_t *e = (multiboot_memory_map_t *) mb->mmap_addr;
+ (unsigned int) e < mb->mmap_addr + mb->mmap_length;
+ e += 1
+ ) {
+ if (e->type != MULTIBOOT_MEMORY_AVAILABLE) continue;
+ // memory below 1MB is not safe to use.
+ if (e->addr < 1000000) continue;
+ // memory above UINT_MAX is unusable.
+ if (e->addr > UINT_MAX) continue;
+
+ uint32_t length;
+ // if the memory reaches outside of UINT_MAX, truncate length value
+ if (e->addr + e->len > UINT_MAX)
+ length = UINT_MAX - e->addr;
+ else length = e->len;
+
+ struct memory_area *cur = (struct memory_area *) e->addr;
+ cur->len = length;
+
+ if (prev != NULL)
+ prev->next = (struct memory_area *) e->addr;
+
+ prev = cur;
+ }
+}