Add page alignment

This commit is contained in:
Maxime Bloch 2020-01-29 05:32:50 +01:00
parent 8ae1816819
commit 5b8f5cac54
No known key found for this signature in database
GPG Key ID: CE32A7D95B7D6418
2 changed files with 54 additions and 14 deletions

View File

@ -60,6 +60,15 @@ void kernel_main(void) {
free(memory_str);
free(management_str);
// Some dummy allocations to demonstrate the states of memory and to showcase the memory dump printout.
void* ptr = alloc(30);
alloc(30);
alloc(31);
free(ptr);
void* ptr2 = alloc(64);
alloc(61);
free(ptr2);
print((are_interrupts_enabled()) ? "Interrupts!\n" : "No interrupts :(\n");

View File

@ -10,10 +10,13 @@
#define MEMORY_START 0x200000
//#define MEMORY_END 0x300000
#define PAGE_ALIGNMENT 4
//size_t total_memory = MEMORY_END - MEMORY_START;
typedef struct page_tag {
size_t size;
size_t realsize;
struct page_tag *prev;
struct page_tag *next;
@ -22,14 +25,21 @@ typedef struct page_tag {
page_tag *start = (void *) MEMORY_START;;
size_t calculate_realsize(size_t size) {
if (size % PAGE_ALIGNMENT == 0) return size + 0;
return size + PAGE_ALIGNMENT - (size % PAGE_ALIGNMENT);
}
void *alloc(size_t size) {
page_tag *curr_page = start;
while (curr_page->next != NULL) {
page_tag *new_page = ((void *) curr_page) + sizeof(page_tag) + curr_page->size;
if ((void *) new_page + sizeof(page_tag) + size <= (void *) curr_page->next) {
page_tag *new_page = ((void *) curr_page) + sizeof(page_tag) + curr_page->realsize;
size_t realsize = calculate_realsize(size);
if ((void *) new_page + sizeof(page_tag) + realsize <= (void *) curr_page->next) {
new_page->size = size;
new_page->realsize = realsize;
curr_page->next->prev = new_page;
new_page->next = curr_page->next;
@ -42,8 +52,9 @@ void *alloc(size_t size) {
curr_page = curr_page->next;
}
page_tag *new_page = ((void *) curr_page) + sizeof(page_tag) + curr_page->size;
page_tag *new_page = ((void *) curr_page) + sizeof(page_tag) + curr_page->realsize;
new_page->size = size;
new_page->realsize = calculate_realsize(size);
curr_page->next = new_page;
new_page->prev = curr_page;
@ -68,19 +79,27 @@ int amount_of_digits(int x) {
return n;
}
#define MEM_SIZE_WIDTH 3
#define SPACED_ARRAY {' ', ' ', ' '};
void print_memory() {
print("____________________\n");
print(" === MEM DUMP === \n");
print("--------------------\n");
print("Page tag size: %d bytes\n", sizeof(page_tag));
print("Memory alignment: %d bytes\n", PAGE_ALIGNMENT);
print("=> idx: [page_tag_address] [data_address (size|aligment_padding_size)]\n\n");
page_tag *curr_page = start;
int i = 0;
bool left = true;
while (curr_page != NULL) {
char padding[4] = {' ', ' ', ' ', ' '};
padding[4 - amount_of_digits(curr_page->size)] = '\0';
print("%d: [%x (%d)] [%x (%s%d)]",
char padding[MEM_SIZE_WIDTH] = SPACED_ARRAY;
padding[MEM_SIZE_WIDTH - amount_of_digits(curr_page->size)] = '\0';
print("%d: [%x] [%x (%s%d|%d)]",
i,
curr_page, sizeof(page_tag),
(curr_page + 1), padding, curr_page->size);
curr_page,
(curr_page + 1), padding, curr_page->size,
curr_page->realsize - curr_page->size);
if (left) {
print(" | ");
@ -90,16 +109,28 @@ void print_memory() {
left = !left;
bool empty_print = false;
void *empty_start = (void *) curr_page + sizeof(page_tag) + curr_page->size;
void *empty_start = (void *) curr_page + sizeof(page_tag) + curr_page->realsize;
if (empty_start + sizeof(page_tag) < (void *) curr_page->next) {
print("_: empty page (%d, %d)",
size_t memory_left = (void *) curr_page->next -
(empty_start + sizeof(page_tag));
char empty_padding[MEM_SIZE_WIDTH] = SPACED_ARRAY;
empty_padding[MEM_SIZE_WIDTH - amount_of_digits(memory_left)] = '\0';
print("_: free space (%d + %s%d| ) ",
sizeof(page_tag),
(void *) curr_page->next -
(empty_start + sizeof(page_tag)));
empty_padding,
memory_left
);
empty_print = true;
} else if (empty_start < (void *) curr_page->next) {
print("_: not enough room (%d)",
(void *) curr_page->next - empty_start);
size_t memory_left = (void *) curr_page->next - empty_start;
char empty_padding[MEM_SIZE_WIDTH] = SPACED_ARRAY;
empty_padding[MEM_SIZE_WIDTH - amount_of_digits(memory_left)] = '\0';
print("_: not enough room (%s%d| ) ",
empty_padding,
memory_left);
empty_print = true;
}
if (empty_print) {