blob: e5e215c5c7cca81c968e1ed0b5354d3b1ce9a62a (
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#include <stdint.h>
#include <limits.h>
#include <stddef.h>
#include "memory.h"
#include "multiboot.h"
#include "bees.h"
#include "misc.h"
#include "printf.h"
struct memory_area {
uint32_t len;
struct memory_area *next;
struct memory_area *prev;
};
uint32_t memory_available = 0;
uint32_t total_memory = 0;
struct memory_area *first_area = NULL;
struct cell *first_free = NULL;
extern mem_t __kernel_start;
extern mem_t __kernel_end;
void init_memory(multiboot_info_t *mb) {
struct memory_area *prev = NULL;
struct cell *prev_cell = 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 < __kernel_start) 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;
// free memory comes after the kernel
uint32_t addr = e->addr;
if (addr < (uint32_t) __kernel_end) {
addr = (uint32_t) __kernel_end;
length -= addr - e->addr;
}
memory_available += length - sizeof(struct memory_area);
// join memory area to linked list
struct memory_area *cur = (struct memory_area *) addr;
cur->len = length;
cur->next = NULL;
cur->prev = prev;
if (prev != NULL)
prev->next = (struct memory_area *) e->addr;
prev = cur;
if (first_area == NULL) first_area = cur;
unsigned char *after = (unsigned char*) (cur + 1);
// link memory into free list
for (
// cells start after memory area struct; they must be aligned.
struct cell *c = (struct cell *)
(after + ((uint32_t) after % sizeof(struct cell)));
// no part of the cell can be outside of the memory area
(uint32_t) (c + 1) < (uint32_t) cur + cur->len;
c++
) {
if (first_free == NULL) first_free = c;
c->type = FREE_CELL;
set_cdr(c, 0);
if (prev_cell != NULL) set_cdr(prev_cell, (uint32_t) c);
prev_cell = c;
}
}
total_memory = memory_available;
}
struct cell *alloc_cell(enum cell_type t) {
if (first_free == NULL) return NULL; // GC should be invoked here somehow
if (first_free->type != FREE_CELL) bees("heap corruption has occurred");
first_free = cdr_cell(first_free);
first_free->type = t;
memory_available -= sizeof(struct cell);
return first_free;
}
|