create get_chunk func to simplify

This commit is contained in:
starnakin 2024-07-31 15:04:58 +02:00
parent c7e205c8e5
commit 5ebe1346ac
6 changed files with 44 additions and 27 deletions

View File

@ -61,3 +61,23 @@ int get_append_unused_chunk(void *root, size_t size, chunk_t *result, size_t new
}
return 0;
}
int get_chunk(void *data_start, chunk_t *result, int *category)
{
int i;
void *chunk_pos;
for (i = TINY; i <= LARGE; i++)
{
chunk_pos = raw_get_chunk_by_root(allocs_tree[i], data_start);
if (chunk_pos == NULL)
continue;
break;
}
if (chunk_pos == NULL)
return 1;
chunk_read(chunk_pos, result);
if (category)
*category = i;
return 0;
}

View File

@ -6,3 +6,4 @@ void chunk_iter(chunk_t *root, void (*f)(chunk_t*));
void chunk_merge(chunk_t *first, chunk_t *second);
int get_append_unused_chunk(void *root, size_t size, chunk_t *result, size_t new_block_size);
int chunk_split(chunk_t *chunk, size_t new_size);
int get_chunk(void *data_start, chunk_t *result, int *category);

View File

@ -1,6 +1,10 @@
#include "chunk.h"
#include "malloc.h"
#include "chunk_manager.h"
#include "block.h"
#include "raw_chunk_manager.h"
#include <unistd.h>
#include <stdbool.h>
void free_tiny(chunk_t *chunk)
@ -47,28 +51,20 @@ static void (*free_funcs[3])(chunk_t*) = {free_tiny, free_small, free_large};
void ft_free(void *ptr)
{
void *raw_chunk;
chunk_t chunk;
size_t i;
int category;
if (ptr == NULL)
return;
for (i = TINY; i <= LARGE; i++)
if (get_chunk(ptr, &chunk, &category))
{
raw_chunk = raw_get_chunk(allocs_tree[i], ptr);
if (raw_chunk == NULL)
continue;
break;
}
#ifdef DEBUG
if (raw_chunk == NULL)
write(2, "chunk not found\n", 16);
#endif
chunk_read(raw_chunk, &chunk);
return;
}
#ifdef DEBUG
if (chunk.is_used == false)
write(2, "double free\n", 12);
#endif
// CALL THE RIGHT FREE FUNCTION DEPEND ON SIZE
free_funcs[i](&chunk);
free_funcs[category](&chunk);
}

View File

@ -15,7 +15,7 @@ void *raw_get_last_chunk(void **root)
return current;
}
void *raw_get_chunk(void **root, void *data_start)
void *raw_get_chunk_by_root(void **root, void *data_start)
{
void**current = root;

View File

@ -1,6 +1,6 @@
#pragma once
void *raw_get_last_chunk(void **root);
void *raw_get_chunk(void **root, void *data_start);
void *raw_get_chunk_by_root(void **root, void *data_start);
int get_unused_chunk_raw(void *root, size_t size, chunk_t *result);
int is_alone_on_block(const chunk_t *chunk);

View File

@ -37,13 +37,13 @@ int main(int ac, char **av)
ptr1 = ft_malloc(10000000);
ptr2 = ft_malloc(10000000);
ptr3 = ft_malloc(10000000);
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[LARGE], ptr1) == NULL), "alloc first", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[LARGE], ptr2) == NULL), "alloc second", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[LARGE], ptr3) == NULL), "alloc third", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[LARGE], ptr1) == NULL), "alloc first", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[LARGE], ptr2) == NULL), "alloc second", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[LARGE], ptr3) == NULL), "alloc third", "");
ft_free(ptr1);
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[LARGE], ptr2) == NULL), "free disorder1", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[LARGE], ptr2) == NULL), "free disorder1", "");
ft_free(ptr3);
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[LARGE], ptr2) == NULL), "free disorder2", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[LARGE], ptr2) == NULL), "free disorder2", "");
ft_free(ptr2);
test(NULL, allocs_tree[LARGE], "free disorder3", "");
@ -57,13 +57,13 @@ int main(int ac, char **av)
ptr1 = ft_malloc(4);
ptr2 = ft_malloc(4);
ptr3 = ft_malloc(4);
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[TINY], ptr1) == NULL), "alloc first", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[TINY], ptr2) == NULL), "alloc second", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[TINY], ptr3) == NULL), "alloc third", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[TINY], ptr1) == NULL), "alloc first", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[TINY], ptr2) == NULL), "alloc second", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[TINY], ptr3) == NULL), "alloc third", "");
ft_free(ptr1);
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[TINY], ptr2) == NULL), "free disorder1", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[TINY], ptr2) == NULL), "free disorder1", "");
ft_free(ptr3);
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[TINY], ptr2) == NULL), "free disorder2", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[TINY], ptr2) == NULL), "free disorder2", "");
ft_free(ptr2);
test(NULL, allocs_tree[TINY], "free disorder3", "");
show_alloc_mem();