Add printing on terminal. Add some usage on boot.

This commit is contained in:
Maxime Bloch 2020-01-28 18:54:42 +01:00
parent fafe06ed2b
commit 4f214b2d5a
No known key found for this signature in database
GPG key ID: CE32A7D95B7D6418
3 changed files with 187 additions and 159 deletions

View file

@ -54,9 +54,33 @@ void kernel_main(void)
}
management_str[13] = 0;
terminal_writestring(memory_str);
terminal_writestring(management_str);
terminal_writestring("Mem after freeing!\n");
free(memory_str);
print_memory();
free(management_str);
print_memory();
char* memory_after_free = alloc(sizeof(char) * 12);
for (int i = 0; i < 13; i++) {
memory_after_free[i] = " Some text\n"[i];
}
memory_after_free[13] = 0;
terminal_writestring(memory_after_free);
terminal_writestring("Memory after new allocation!\n");
alloc(1000);
print_memory();
terminal_writestring("Free again\n");
free(memory_after_free);
print_memory();
terminal_writestring((are_interrupts_enabled())? "Interrupts!\n": "No interrupts :(\n");
interrupt_init();

View file

@ -1,148 +0,0 @@
#ifndef LINKED_LIST_C
#define LINKED_LIST_C
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct page_tag {
size_t size;
struct page_tag *prev;
struct page_tag *next;
} page_tag;
page_tag *start;
page_tag *__searchPage(void *data) {
page_tag *curr_page = start;
while (curr_page + (sizeof(page_tag)) != data) {
curr_page = curr_page->next;
}
return curr_page;
}
///**
// * @param alignment
// * @param size
// * @return A block aligned with the alignment parameter.
// */
//void* aligned_alloc(int alignment, int size){
// // TODO
// return NULL;
//}
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 <= curr_page->next) {
new_page->size = size;
curr_page->next->prev = new_page;
new_page->next = curr_page->next;
curr_page->next = new_page;
new_page->prev = curr_page;
return new_page + 1;
}
curr_page = curr_page->next;
}
page_tag *new_page = ((void *) curr_page) + sizeof(page_tag) + curr_page->size;
new_page->size = size;
curr_page->next = new_page;
new_page->prev = curr_page;
return new_page + 1;
}
void __free(void *data) {
page_tag *data_tag = data - sizeof(page_tag);
printf("found tag at %p\n", data_tag);
data_tag->prev->next = data_tag->next;
data_tag->next->prev = data_tag->prev;
}
void print() {
printf("==== MEM DUMP ====\n");
page_tag *curr_page = start;
int i = 0;
while (curr_page != NULL) {
printf("%d: [%p (%zu)] [%p (%zu)]\n",
i,
curr_page, sizeof(page_tag),
curr_page + 1, curr_page->size);
void *empty_start = (void *) curr_page + sizeof(page_tag) + curr_page->size;
if (empty_start + sizeof(page_tag) < (void *) curr_page->next) {
printf("empty_page (%ld, %ld)\n",
sizeof(page_tag),
(void *) curr_page->next -
(empty_start + sizeof(page_tag)));
} else if (empty_start < (void *) curr_page->next) {
printf("not enough room (%ld)\n",
(void *) curr_page->next - empty_start);
}
curr_page = curr_page->next;
i++;
}
printf("-------------------\n\n");
}
void test_allocs() {
void *ptr0 = __alloc(64);
void *ptr1 = __alloc(64);
void *ptr4 = __alloc(64);
print();
printf("Free nr 2 -> %p\n", ptr1);
__free(ptr1);
print();
printf("Alloc 32 bytes\n");
void *ptr2 = __alloc(32);
print();
printf("Alloc 32 bytes\n");
__alloc(32);
print();
printf("Alloc 8 bytes\n");
void *ptr3 = __alloc(8);
print();
__free(ptr2);
print();
__free(ptr4);
print();
__free(ptr3);
print();
__free(ptr0);
print();
printf("#########\nTry almost fill\n");
__alloc(160);
print();
__alloc(50);
print();
}
int main() {
start = malloc(sizeof(size_t) * 1000);
start->next = NULL;
start->prev = NULL;
test_allocs();
}
#endif // LINKED_LIST_C

View file

@ -1,13 +1,165 @@
#ifndef MEMORY_C
#define MEMORY_C
#ifndef LINKED_LIST_C
#define LINKED_LIST_C
#include <stddef.h>
void* _curr_end = (void*) 0x200000;
#include "terminal.c"
#define MEMORY_START 0x200000
//#define MEMORY_END 0x300000
//size_t total_memory = MEMORY_END - MEMORY_START;
typedef struct page_tag {
size_t size;
struct page_tag *prev;
struct page_tag *next;
} page_tag;
page_tag *start = (void *) MEMORY_START;;
page_tag *__searchPage(void *data) {
page_tag *curr_page = start;
while (curr_page + (sizeof(page_tag)) != data) {
curr_page = curr_page->next;
}
return curr_page;
}
void *alloc(size_t size) {
void* thanks_jp = _curr_end;
_curr_end += size;
return thanks_jp;
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) {
new_page->size = size;
curr_page->next->prev = new_page;
new_page->next = curr_page->next;
curr_page->next = new_page;
new_page->prev = curr_page;
return new_page + 1;
}
#endif // MEMORY_C
curr_page = curr_page->next;
}
page_tag *new_page = ((void *) curr_page) + sizeof(page_tag) + curr_page->size;
new_page->size = size;
curr_page->next = new_page;
new_page->prev = curr_page;
new_page->next = NULL;
return new_page + 1;
}
void free(void *data) {
page_tag *data_tag = data - sizeof(page_tag);
data_tag->prev->next = data_tag->next;
data_tag->next->prev = data_tag->prev;
}
void print_memory() {
terminal_write("==== MEM DUMP ====\n", 19);
page_tag *curr_page = start;
int i = 0;
while (curr_page != NULL) {
terminal_writeint(i, 10);
terminal_write(": [", 3);
terminal_writeint((int) curr_page, 16);
terminal_write(" ", 1);
terminal_writeint(sizeof(page_tag), 10);
terminal_write("] [", 3);
terminal_writeint((int) (curr_page + 1), 16);
terminal_write(" ", 1);
terminal_writeint(curr_page->size, 10);
terminal_writestring("]\n");
// printf("%d: [%p (%zu)] [%p (%zu)]\n",
// i,
// curr_page, sizeof(page_tag),
// curr_page + 1, curr_page->size);
void *empty_start = (void *) curr_page + sizeof(page_tag) + curr_page->size;
if (empty_start + sizeof(page_tag) < (void *) curr_page->next) {
terminal_writestring("empty_page (");
terminal_writeint(sizeof(page_tag), 10);
terminal_writestring(", ");
terminal_writeint(
(void *) curr_page->next -
(empty_start + sizeof(page_tag)), 10);
terminal_writestring(")\n");
// printf("empty_page (%ld, %ld)\n",
// sizeof(page_tag),
// (void *) curr_page->next -
// (empty_start + sizeof(page_tag)));
} else if (empty_start < (void *) curr_page->next) {
terminal_writestring("not enough room (");
terminal_writeint((void *) curr_page->next - empty_start, 10);
terminal_writestring(")\n");
// printf("not enough room (%ld)\n",
// (void *) curr_page->next - empty_start);
}
curr_page = curr_page->next;
i++;
}
terminal_write("------------------\n\n", 20);
}
//
//void test_allocs() {
// void *ptr0 = __alloc(64);
// void *ptr1 = __alloc(64);
// void *ptr4 = __alloc(64);
// print();
//
// printf("Free nr 2 -> %p\n", ptr1);
// __free(ptr1);
// print();
//
// printf("Alloc 32 bytes\n");
// void *ptr2 = __alloc(32);
// print();
//
// printf("Alloc 32 bytes\n");
// __alloc(32);
// print();
//
// printf("Alloc 8 bytes\n");
// void *ptr3 = __alloc(8);
// print();
//
// __free(ptr2);
// print();
// __free(ptr4);
// print();
// __free(ptr3);
// print();
//
// __free(ptr0);
// print();
//
// printf("#########\nTry almost fill\n");
// __alloc(160);
// print();
// __alloc(50);
// print();
//
//
//}
//
//int main() {
// start = malloc(sizeof(size_t) * 1000);
// start->next = NULL;
// start->prev = NULL;
// test_allocs();
//}
#endif // LINKED_LIST_C