Fix free list (#113)

This commit is contained in:
Alex Kotov 2022-11-27 19:31:56 +04:00 committed by GitHub
parent 2a52c22f36
commit 5ea65b6b0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 122 additions and 2 deletions

View File

@ -187,8 +187,6 @@ void *KernAux_FreeList_malloc(void *const malloc, size_t size)
if (node) {
// Can we split the block?
if (node->size - size >= MIN_SPLIT_SIZE) {
node->size = NODE_HEADER_SIZE + size;
KernAux_FreeList_Node new_node =
(KernAux_FreeList_Node)(((uintptr_t)&node->block) + size);
@ -200,6 +198,7 @@ void *KernAux_FreeList_malloc(void *const malloc, size_t size)
new_node->orig_ptr = new_node;
new_node->size = node->size - size - NODE_HEADER_SIZE;
node->size = NODE_HEADER_SIZE + size;
KernAux_FreeList_insert(free_list, new_node, node, node->next);
}

View File

@ -9,10 +9,13 @@
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void test_default();
static void test_cross_zone_defrag();
static void test_hello_and_mrb_state();
static void test_calloc();
static void test_calloc_nomem();
@ -26,6 +29,7 @@ static void test_realloc_increase();
static void test_realloc_decrease();
static size_t nodes_count(KernAux_FreeList free_list);
static void print_nodes(KernAux_FreeList free_list);
static const char *const hello = "Hello, World!";
@ -33,6 +37,7 @@ void test_main()
{
test_default();
test_cross_zone_defrag();
test_hello_and_mrb_state();
test_calloc();
test_calloc_nomem();
@ -59,6 +64,29 @@ size_t nodes_count(const KernAux_FreeList free_list)
return nodes_count;
}
void print_nodes(const KernAux_FreeList free_list)
{
printf("\n\n\n");
printf("========================================\n");
printf("free_list: %p\n", (void*)free_list);
printf("free_list->head: %p\n", (void*)free_list->head);
for (
KernAux_FreeList_Node item_node = free_list->head;
item_node;
item_node = item_node->next
) {
printf("----------------------------------------\n");
printf("item_node: %p\n", (void*)item_node);
printf("item_node->orig_ptr: %p\n", (void*)item_node->orig_ptr);
printf("item_node->next: %p\n", (void*)item_node->next);
printf("item_node->prev: %p\n", (void*)item_node->prev);
printf("&item_node->block: %p\n", (void*)&item_node->block);
printf("item_node->size: %lu\n", (unsigned long)item_node->size);
}
printf("========================================\n");
printf("\n\n\n");
}
void test_default()
{
char memory_block[1000];
@ -111,6 +139,99 @@ void test_cross_zone_defrag()
assert(nodes_count(&free_list) == 1);
}
void test_hello_and_mrb_state()
{
char *const zone = malloc(1024 * 128); // 128 KiB
assert(zone);
struct KernAux_FreeList free_list = KernAux_FreeList_create(NULL);
printf("KernAux_FreeList_create(NULL) = %p\n", (void*)&free_list);
print_nodes(&free_list);
KernAux_FreeList_add_zone(&free_list, zone, 1024 * 128);
printf(
"KernAux_FreeList_add_zone(&free_list, zone = %p, 1024 * 128 = %i)\n",
(void*)zone,
1024 * 128
);
print_nodes(&free_list);
{
char *const str = KernAux_Malloc_malloc(&free_list.malloc, 100);
printf(
"KernAux_Malloc_malloc(&free_list.malloc, 100) = %p\n",
(void*)str
);
print_nodes(&free_list);
assert(str);
KernAux_Malloc_free(&free_list.malloc, str);
printf(
"KernAux_Malloc_free(&free_list.malloc, str = %p)\n",
(void*)str
);
print_nodes(&free_list);
}
{
char *const str =
KernAux_Malloc_realloc(&free_list.malloc, NULL, 100);
printf(
"KernAux_Malloc_realloc(&free_list.malloc, NULL, 100) = %p\n",
(void*)str
);
print_nodes(&free_list);
assert(str);
KernAux_Malloc_free(&free_list.malloc, str);
printf(
"KernAux_Malloc_free(&free_list.malloc, str = %p)\n",
(void*)str
);
print_nodes(&free_list);
}
{
char *const mrb = KernAux_Malloc_malloc(&free_list.malloc, 6356);
printf(
"KernAux_Malloc_malloc(&free_list.malloc, 6356) = %p\n",
(void*)mrb
);
print_nodes(&free_list);
assert(mrb);
KernAux_Malloc_free(&free_list.malloc, mrb);
printf(
"KernAux_Malloc_free(&free_list.malloc, mrb = %p)\n",
(void*)mrb
);
print_nodes(&free_list);
}
{
char *const mrb = KernAux_Malloc_realloc(&free_list.malloc, NULL, 6356);
printf(
"KernAux_Malloc_realloc(&free_list.malloc, NULL, 6356) = %p\n",
(void*)mrb
);
print_nodes(&free_list);
assert(mrb);
KernAux_Malloc_free(&free_list.malloc, mrb);
printf(
"KernAux_Malloc_free(&free_list.malloc, mrb = %p)\n",
(void*)mrb
);
print_nodes(&free_list);
}
free(zone);
}
void test_calloc()
{
char zone[1000];