summaryrefslogtreecommitdiff
path: root/src/memory.c
blob: 970ba29aee45b80a028ed52dab94706dbd009db8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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;
	}
}