2020-01-29 22:53:38 +01:00
|
|
|
#ifndef MEMORY_C
|
|
|
|
#define MEMORY_C
|
2020-01-28 18:54:42 +01:00
|
|
|
|
2020-01-08 22:36:49 +01:00
|
|
|
|
2020-01-07 14:53:27 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2020-01-28 18:54:42 +01:00
|
|
|
#include "terminal.c"
|
2020-01-28 22:25:37 +01:00
|
|
|
#include "util/printer.c"
|
2020-01-28 18:54:42 +01:00
|
|
|
|
|
|
|
#define MEMORY_START 0x200000
|
|
|
|
//#define MEMORY_END 0x300000
|
|
|
|
|
2020-01-29 05:32:50 +01:00
|
|
|
#define PAGE_ALIGNMENT 4
|
|
|
|
|
2020-01-28 18:54:42 +01:00
|
|
|
//size_t total_memory = MEMORY_END - MEMORY_START;
|
|
|
|
|
|
|
|
typedef struct page_tag {
|
|
|
|
size_t size;
|
2020-01-29 05:32:50 +01:00
|
|
|
size_t realsize;
|
2020-01-28 18:54:42 +01:00
|
|
|
|
|
|
|
struct page_tag *prev;
|
|
|
|
struct page_tag *next;
|
|
|
|
} page_tag;
|
|
|
|
|
|
|
|
|
|
|
|
page_tag *start = (void *) MEMORY_START;;
|
|
|
|
|
2020-01-29 05:32:50 +01:00
|
|
|
size_t calculate_realsize(size_t size) {
|
2020-01-29 22:53:38 +01:00
|
|
|
if (size % PAGE_ALIGNMENT == 0) return size;
|
2020-01-29 05:32:50 +01:00
|
|
|
return size + PAGE_ALIGNMENT - (size % PAGE_ALIGNMENT);
|
|
|
|
}
|
|
|
|
|
2020-01-28 18:54:42 +01:00
|
|
|
void *alloc(size_t size) {
|
|
|
|
page_tag *curr_page = start;
|
|
|
|
|
|
|
|
while (curr_page->next != NULL) {
|
2020-01-29 05:32:50 +01:00
|
|
|
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) {
|
2020-01-28 18:54:42 +01:00
|
|
|
|
|
|
|
new_page->size = size;
|
2020-01-29 05:32:50 +01:00
|
|
|
new_page->realsize = realsize;
|
2020-01-07 14:53:27 +01:00
|
|
|
|
2020-01-28 18:54:42 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-29 05:32:50 +01:00
|
|
|
page_tag *new_page = ((void *) curr_page) + sizeof(page_tag) + curr_page->realsize;
|
2020-01-28 18:54:42 +01:00
|
|
|
new_page->size = size;
|
2020-01-29 05:32:50 +01:00
|
|
|
new_page->realsize = calculate_realsize(size);
|
2020-01-28 18:54:42 +01:00
|
|
|
|
|
|
|
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;
|
2020-01-08 22:36:49 +01:00
|
|
|
}
|
2020-01-28 18:54:42 +01:00
|
|
|
|
2020-01-29 04:31:44 +01:00
|
|
|
int amount_of_digits(int x) {
|
|
|
|
if (x == 0) return 1;
|
|
|
|
int n = 0;
|
|
|
|
while (x != 0) {
|
|
|
|
n += 1;
|
|
|
|
x /= 10;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2020-01-29 05:32:50 +01:00
|
|
|
#define MEM_SIZE_WIDTH 3
|
|
|
|
#define SPACED_ARRAY {' ', ' ', ' '};
|
|
|
|
|
2020-01-28 18:54:42 +01:00
|
|
|
void print_memory() {
|
2020-01-28 23:10:35 +01:00
|
|
|
print("____________________\n");
|
|
|
|
print(" === MEM DUMP === \n");
|
2020-01-29 05:32:50 +01:00
|
|
|
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");
|
2020-01-28 18:54:42 +01:00
|
|
|
page_tag *curr_page = start;
|
|
|
|
int i = 0;
|
2020-01-29 04:31:44 +01:00
|
|
|
bool left = true;
|
2020-01-28 18:54:42 +01:00
|
|
|
while (curr_page != NULL) {
|
2020-01-29 05:32:50 +01:00
|
|
|
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)]",
|
2020-01-28 22:35:25 +01:00
|
|
|
i,
|
2020-01-29 05:32:50 +01:00
|
|
|
curr_page,
|
|
|
|
(curr_page + 1), padding, curr_page->size,
|
|
|
|
curr_page->realsize - curr_page->size);
|
2020-01-29 04:31:44 +01:00
|
|
|
|
|
|
|
if (left) {
|
|
|
|
print(" | ");
|
|
|
|
} else {
|
|
|
|
print("\n");
|
|
|
|
}
|
|
|
|
left = !left;
|
2020-01-28 22:35:25 +01:00
|
|
|
|
2020-01-29 04:31:44 +01:00
|
|
|
bool empty_print = false;
|
2020-01-29 05:32:50 +01:00
|
|
|
void *empty_start = (void *) curr_page + sizeof(page_tag) + curr_page->realsize;
|
2020-01-28 22:35:25 +01:00
|
|
|
if (empty_start + sizeof(page_tag) < (void *) curr_page->next) {
|
2020-01-29 05:32:50 +01:00
|
|
|
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| ) ",
|
2020-01-28 22:35:25 +01:00
|
|
|
sizeof(page_tag),
|
2020-01-29 05:32:50 +01:00
|
|
|
empty_padding,
|
|
|
|
memory_left
|
|
|
|
);
|
2020-01-29 04:31:44 +01:00
|
|
|
empty_print = true;
|
2020-01-28 22:35:25 +01:00
|
|
|
} else if (empty_start < (void *) curr_page->next) {
|
2020-01-29 05:32:50 +01:00
|
|
|
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);
|
2020-01-29 04:31:44 +01:00
|
|
|
empty_print = true;
|
2020-01-28 22:35:25 +01:00
|
|
|
}
|
2020-01-29 04:31:44 +01:00
|
|
|
if (empty_print) {
|
|
|
|
if (left) {
|
|
|
|
print(" | ");
|
|
|
|
} else {
|
|
|
|
print("\n");
|
|
|
|
}
|
|
|
|
left = !left;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-28 18:54:42 +01:00
|
|
|
curr_page = curr_page->next;
|
2020-01-28 22:25:37 +01:00
|
|
|
i += 1;
|
2020-01-28 18:54:42 +01:00
|
|
|
}
|
2020-01-29 04:31:44 +01:00
|
|
|
if (!left) {
|
|
|
|
print("\n");
|
|
|
|
}
|
2020-01-28 23:10:35 +01:00
|
|
|
print("____________________\n");
|
2020-01-28 18:54:42 +01:00
|
|
|
}
|
2020-01-28 23:10:35 +01:00
|
|
|
|
2020-01-29 04:31:44 +01:00
|
|
|
int command_mem_dump(char *string) {
|
2020-01-28 23:10:35 +01:00
|
|
|
print_memory();
|
|
|
|
}
|
|
|
|
|
2020-01-28 18:54:42 +01:00
|
|
|
//
|
|
|
|
//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();
|
|
|
|
//}
|
|
|
|
|
2020-01-29 22:53:38 +01:00
|
|
|
#endif // MEMORY_C
|