wip: memory is almost fixed and working fine (infinite loop on zones linked lists)
This commit is contained in:
@ -9,7 +9,7 @@ Zone *vzones[3];
|
||||
static void add_zone(Zone *zone, block_type_t type)
|
||||
{
|
||||
// We put the zone at the beginning of the list
|
||||
if (vzones[type]) {
|
||||
if (vzones[type] && vzones[type] != zone) {
|
||||
zone->next = vzones[type];
|
||||
vzones[type]->prev = zone;
|
||||
}
|
||||
@ -40,12 +40,10 @@ static void new_block(Zone *zone, uint32_t zone_size)
|
||||
zone->free->prev_free = new_block;
|
||||
new_block->next = zone->free;
|
||||
new_block->next_free = zone->free;
|
||||
PRINT_PTR(new_block);
|
||||
PRINT_PTR(new_block->next_free);
|
||||
}
|
||||
/* kprintf("before zone: %p zone->free: %p new_block: %p\n", zone, */
|
||||
/* zone->free, new_block); */
|
||||
zone->free = new_block;
|
||||
/* kprintf("after zone: %p zone->free: %p new_block: %p\n", zone, */
|
||||
/* zone->free, new_block); */
|
||||
assert(zone->free == new_block);
|
||||
}
|
||||
|
||||
|
@ -14,22 +14,6 @@ void show_valloc_mem(void)
|
||||
int count = 0;
|
||||
for (Zone *zone_it = vzones[type]; zone_it != NULL;
|
||||
zone_it = zone_it->next) {
|
||||
#if FULL_INFO
|
||||
if (zone_it->free)
|
||||
kprintf("---------- AVAILABLE %s [n°%d - %p] "
|
||||
"----------\n",
|
||||
vzones_name[type], count, zone_it);
|
||||
int i = 0;
|
||||
for (Block *block_it = zone_it->free; block_it != NULL;
|
||||
block_it = block_it->next_free) {
|
||||
kprintf("%p - %p : %u bytes\n", block_it->ptr,
|
||||
(uint32_t)block_it->ptr +
|
||||
block_it->sub_size + sizeof(Block),
|
||||
block_it->sub_size);
|
||||
}
|
||||
if (zone_it->free)
|
||||
kprintf("\n");
|
||||
#endif
|
||||
if (zone_it->used)
|
||||
kprintf("---------- IN_USE %s [n°%d - %p] "
|
||||
"----------\n",
|
||||
@ -47,6 +31,21 @@ void show_valloc_mem(void)
|
||||
if (zone_it->used)
|
||||
kprintf("\n");
|
||||
count++;
|
||||
#if FULL_INFO
|
||||
if (zone_it->free)
|
||||
kprintf("---------- AVAILABLE %s [n°%d - %p] "
|
||||
"----------\n",
|
||||
vzones_name[type], count, zone_it);
|
||||
for (Block *block_it = zone_it->free; block_it != NULL;
|
||||
block_it = block_it->next_free) {
|
||||
kprintf("%p - %p : %u bytes\n", block_it->ptr,
|
||||
(uint32_t)block_it->ptr +
|
||||
block_it->sub_size + sizeof(Block),
|
||||
block_it->sub_size);
|
||||
}
|
||||
if (zone_it->free)
|
||||
kprintf("\n");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
kprintf("Total: %u\n", total_size);
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "alloc.h"
|
||||
#include "debug.h"
|
||||
#include "kprintf.h"
|
||||
#include "terminal.h"
|
||||
#include <stdint.h>
|
||||
|
||||
int new_zone(block_type_t type, uint32_t size);
|
||||
@ -12,10 +13,13 @@ int new_zone(block_type_t type, uint32_t size);
|
||||
static Block *find_block(Zone *head, uint32_t size)
|
||||
{
|
||||
for (Zone *zone_it = head; zone_it != NULL; zone_it = zone_it->next) {
|
||||
assert(zone_it->free);
|
||||
for (Block *block_it = zone_it->free; block_it != NULL;
|
||||
block_it = block_it->next_free) {
|
||||
/* PRINT_PTR(block_it); */
|
||||
assert(block_it);
|
||||
if (block_it->in_use) {
|
||||
PRINT_PTR(block_it);
|
||||
PRINT_PTR(block_it->zone);
|
||||
}
|
||||
assert(!block_it->in_use);
|
||||
if (size <= block_it->size) {
|
||||
assert(block_it->zone == zone_it);
|
||||
@ -39,6 +43,16 @@ static Block *find_block(Zone *head, uint32_t size)
|
||||
* Let's say the metadata takes a size of 2:
|
||||
* ... -> [metadata][data][remaining size] -> [6]
|
||||
* ^ ^ ^
|
||||
PRINT_UINT(size);
|
||||
PRINT_UINT(size);
|
||||
PRINT_UINT(block_it->size);
|
||||
if (size <= block_it->size) {
|
||||
assert(block_it->zone == zone_it);
|
||||
return (block_it);
|
||||
PRINT_UINT(block_it->size);
|
||||
if (size <= block_it->size) {
|
||||
assert(block_it->zone == zone_it);
|
||||
return (block_it);
|
||||
* 2 10 20
|
||||
*
|
||||
* So now our block [new] will become:
|
||||
@ -51,11 +65,16 @@ static Block *find_block(Zone *head, uint32_t size)
|
||||
static void frag_block(Zone *zone, Block *old_block, uint32_t size)
|
||||
{
|
||||
Block *new_block = (Block *)align_mem((uint32_t)old_block + size);
|
||||
assert(!(new_block >=
|
||||
(Block *)((uint32_t)zone + get_zone_size(zone->type))));
|
||||
/* PRINT_PTR(old_block); */
|
||||
/* PRINT_PTR(new_block); */
|
||||
/* PRINT_UINT(old_block->size); */
|
||||
/* PRINT_UINT(size); */
|
||||
/* PRINT_PTR(zone); */
|
||||
assert(new_block <
|
||||
(Block *)((uint32_t)zone + get_zone_size(zone->type)));
|
||||
|
||||
// Newly created block metadata
|
||||
new_block->size = old_block->size - size;
|
||||
new_block->size = old_block->size - align_mem(size);
|
||||
new_block->sub_size = new_block->size;
|
||||
new_block->in_use = false;
|
||||
new_block->ptr = (void *)((uint32_t)new_block + sizeof(Block));
|
||||
@ -69,7 +88,9 @@ static void frag_block(Zone *zone, Block *old_block, uint32_t size)
|
||||
new_block->next_used = NULL;
|
||||
|
||||
new_block->prev_free = old_block->prev_free;
|
||||
new_block->next_free = old_block->next_free;
|
||||
new_block->next_free = NULL;
|
||||
if (new_block != old_block->next_free)
|
||||
new_block->next_free = old_block->next_free;
|
||||
|
||||
if (zone->free == old_block)
|
||||
zone->free = new_block;
|
||||
@ -147,6 +168,9 @@ void *vmalloc(uint32_t size)
|
||||
available = find_block(head, size);
|
||||
}
|
||||
assert(available != NULL);
|
||||
/* PRINT_PTR(available); */
|
||||
/* PRINT_PTR(available->zone); */
|
||||
/* PRINT_PTR(available->zone->size); */
|
||||
if (type == LARGE)
|
||||
save_block(head, available, available->zone);
|
||||
else
|
||||
|
Reference in New Issue
Block a user