Allocator now works without the crust that was useless
This commit is contained in:
parent
ac5458d42c
commit
2061aa5f7f
4 changed files with 1 additions and 250 deletions
|
|
@ -1,50 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* alloc_internal.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 09:48:17 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/14 16:12:54 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ALLOC_INTERNAL_H
|
||||
# define ALLOC_INTERNAL_H
|
||||
|
||||
# include "aq/alloc.h"
|
||||
# include "me/types.h"
|
||||
# include <stdalign.h>
|
||||
|
||||
# define PAGE_SIZE_DEFAULT 4096
|
||||
# define BLOCK_PADDING "\xFE\xDC\xAB\xC0\xFE\xEE\x66"
|
||||
# define POOL_ADDR (void *)0xDeadBeef
|
||||
|
||||
typedef struct s_mblock
|
||||
{
|
||||
struct s_mblock *next;
|
||||
struct s_mpage *page;
|
||||
t_usize size;
|
||||
bool used;
|
||||
t_u8 padding[7];
|
||||
} t_mblock;
|
||||
|
||||
typedef struct s_mpage
|
||||
{
|
||||
t_usize page_size;
|
||||
t_mblock *first;
|
||||
struct s_mpage *next;
|
||||
} t_mpage;
|
||||
|
||||
// Will never be null, as it will allocate a new arena if it needs to do so
|
||||
t_mpage *get_head_arena(void);
|
||||
|
||||
// Will return ERROR if it couldn't malloc the page
|
||||
t_error alloc_arena_page(t_usize min_size, t_mpage **out);
|
||||
|
||||
t_mblock *get_block_for_size(t_usize size);
|
||||
void print_pages_info(void);
|
||||
bool merge_block(t_mblock *self, t_usize min_size);
|
||||
|
||||
#endif /* ALLOC_INTERNAL_H */
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
alloc
|
||||
lc_alloc/functions1
|
||||
lc_alloc/functions2
|
||||
me_alloc/find_block
|
||||
|
|
|
|||
|
|
@ -1,196 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* alloc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 10:13:06 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/14 18:26:01 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "aq/allocator.h"
|
||||
#include "aq/libc_wrapper.h"
|
||||
|
||||
typedef struct s_allocator_page
|
||||
{
|
||||
void *data;
|
||||
t_usize size;
|
||||
} t_allocator_page;
|
||||
|
||||
typedef struct s_page_list
|
||||
{
|
||||
t_usize allocated;
|
||||
t_allocator_page a[10];
|
||||
struct s_page_list *next;
|
||||
} t_page_list;
|
||||
|
||||
/*
|
||||
void *me_malloc(t_usize size)
|
||||
{
|
||||
t_mblock *block;
|
||||
|
||||
size = usize_round_up_to(size, 16);
|
||||
block = get_block_for_size(size);
|
||||
if (block == NULL)
|
||||
return (me_abort("Found no page for me_malloc"), NULL);
|
||||
vg_mem_defined(block, sizeof(*block));
|
||||
vg_mempool_alloc(POOL_ADDR, (void *)(((t_usize)block) + sizeof(*block)),
|
||||
block->size); block->used = true; mem_set_zero((t_u8 *)block + sizeof(*block),
|
||||
block->size); vg_mem_no_access(block, sizeof(*block)); return ((void
|
||||
*)(((t_usize)block) + sizeof(*block)));
|
||||
}
|
||||
|
||||
void *me_calloc(t_usize elem_size, t_usize elem_count)
|
||||
{
|
||||
if (elem_size != 0 && elem_count > SIZE_MAX / elem_size)
|
||||
me_abort("calloc overflow !");
|
||||
return (me_malloc(elem_size * elem_count));
|
||||
}
|
||||
|
||||
void *me_realloc(void *ptr, t_usize new_size)
|
||||
{
|
||||
t_mblock *block;
|
||||
void *ret;
|
||||
t_usize old_size;
|
||||
|
||||
if (ptr == NULL)
|
||||
return (me_malloc(new_size));
|
||||
block = (void *)((t_usize)(ptr) - sizeof(*block));
|
||||
vg_mem_defined(block, sizeof(*block));
|
||||
VALGRIND_CHECK_MEM_IS_ADDRESSABLE(ptr, block->size);
|
||||
block->used = true;
|
||||
if (block->size <= new_size)
|
||||
return (vg_mem_no_access(block, sizeof(*block)), ptr);
|
||||
old_size = block->size;
|
||||
vg_mem_no_access(block, sizeof(*block));
|
||||
if (false && merge_block(block, new_size))
|
||||
{
|
||||
vg_mem_defined(block, sizeof(*block));
|
||||
vg_mempool_resize(POOL_ADDR, ptr, block->size);
|
||||
VALGRIND_CHECK_MEM_IS_ADDRESSABLE(ptr, block->size);
|
||||
mem_set_zero((t_u8 *)ptr + old_size, block->size - old_size);
|
||||
vg_mem_no_access(block, sizeof(*block));
|
||||
return (ptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = me_malloc(new_size);
|
||||
mem_copy(ret, ptr, block->size);
|
||||
mem_free(ptr);
|
||||
return (ret);
|
||||
}
|
||||
}
|
||||
|
||||
void *me_realloc_array(void *ptr, t_usize elem_size, t_usize elem_count)
|
||||
{
|
||||
if (elem_size != 0 && elem_count > SIZE_MAX / elem_size)
|
||||
me_abort("realloc_array overflow !");
|
||||
return (me_realloc(ptr, elem_size * elem_count));
|
||||
}
|
||||
|
||||
void mem_free(void *ptr)
|
||||
{
|
||||
t_mblock *cur;
|
||||
|
||||
if (ptr == NULL)
|
||||
return ;
|
||||
cur = (void *)(((t_usize)ptr) - sizeof(t_mblock));
|
||||
vg_mempool_free(POOL_ADDR, ptr);
|
||||
vg_mem_defined(cur, sizeof(*cur));
|
||||
cur->used = false;
|
||||
merge_block(cur, ~(t_usize)0);
|
||||
vg_mem_no_access(cur, sizeof(*cur));
|
||||
}
|
||||
*/
|
||||
// void uninit_allocator(void)
|
||||
// {
|
||||
// t_ptr_table *table;
|
||||
// t_ptr_table *table_next;
|
||||
// t_usize i;
|
||||
// t_usize unfree_count;
|
||||
//
|
||||
// unfree_count = 0;
|
||||
// table = get_table();
|
||||
//
|
||||
// while (table)
|
||||
// {
|
||||
// i = 0;
|
||||
// while (i < PTR_LENS)
|
||||
// {
|
||||
// if (table->table[i].ptr != NULL)
|
||||
// {
|
||||
// __libc_free(table->table[i].ptr);
|
||||
// unfree_count++;
|
||||
// }
|
||||
// i++;
|
||||
// }
|
||||
// table_next = table->next;
|
||||
// __libc_free(table);
|
||||
// table = table_next;
|
||||
// }
|
||||
// if (unfree_count != 0)
|
||||
// {
|
||||
// me_putstr_fd("A total of ", 2);
|
||||
// me_putnbr_fd(unfree_count, 2);
|
||||
// me_putendl_fd(" blocks weren't freed !", 2);
|
||||
// }
|
||||
// }
|
||||
// void *me_malloc(t_usize size)
|
||||
// {
|
||||
// t_mblock *block;
|
||||
// void *ret;
|
||||
//
|
||||
// size = usize_round_up_to(size, 16);
|
||||
// printf("Allocating %zu.\n", size);
|
||||
// block = get_block_for_size(size);
|
||||
// if (block
|
||||
// == NULL)
|
||||
// return (me_abort("Found no page for me_malloc"), NULL);
|
||||
// block->used = true;
|
||||
// ret = ((t_u8 *)block->page->data) + block->offset;
|
||||
// mem_set_zero(ret, block->size);
|
||||
// return (ret);
|
||||
// }
|
||||
//
|
||||
// void *me_calloc(t_usize elem_size, t_usize elem_count)
|
||||
// {
|
||||
// if (elem_size != 0 && elem_count > SIZE_MAX / elem_size)
|
||||
// return (NULL);
|
||||
// return (me_malloc(elem_size * elem_count));
|
||||
// }
|
||||
//
|
||||
// void *me_realloc(void *ptr, t_usize new_size)
|
||||
// {
|
||||
// t_mblock *block;
|
||||
// void *ret;
|
||||
//
|
||||
// if (ptr == NULL)
|
||||
// return (me_malloc(new_size));
|
||||
// block = get_block_from_ptr(ptr);
|
||||
// if (block == NULL || block->size <= new_size)
|
||||
// return (ptr);
|
||||
// if (!merge_next_block(block, new_size))
|
||||
// return (ptr);
|
||||
// else
|
||||
// {
|
||||
// ret = me_malloc(new_size);
|
||||
// mem_copy(ret, ptr, block->size);
|
||||
// mem_free(ptr);
|
||||
// return (ret);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// void mem_free(void *ptr)
|
||||
// {
|
||||
// t_mblock *cur;
|
||||
//
|
||||
// if (ptr == NULL)
|
||||
// return ;
|
||||
// cur = get_block_from_ptr(ptr);
|
||||
// if (cur == NULL)
|
||||
// return (me_abort("Invalid free (not allocated with me_*alloc)!"));
|
||||
// cur->used = false;
|
||||
// merge_next_block(cur, ~(t_usize)0);
|
||||
// }
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/10 16:49:31 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/10 17:39:53 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/10 20:54:31 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -48,7 +48,6 @@ t_chunk *get_next_block(t_chunk *chunk, bool find_zero)
|
|||
t_chunk *split_block(t_chunk *chunk, t_usize size)
|
||||
{
|
||||
t_usize remaining;
|
||||
t_chunk *next;
|
||||
t_chunk *ac_next;
|
||||
|
||||
if (chunk == NULL)
|
||||
|
|
@ -57,7 +56,6 @@ t_chunk *split_block(t_chunk *chunk, t_usize size)
|
|||
if (chunk->size > size + sizeof(*chunk) + (2 << PAGE_ALIGN))
|
||||
{
|
||||
remaining = chunk->size - size - sizeof(*chunk);
|
||||
next = get_next_block(chunk, true);
|
||||
vg_mem_defined(chunk, sizeof(*chunk));
|
||||
chunk->size = size;
|
||||
ac_next = get_next_block(chunk, true);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue