welp i'll fix it tomorrow
This commit is contained in:
parent
941bac31b6
commit
27eb1f10b1
7 changed files with 143 additions and 56 deletions
|
|
@ -6,7 +6,7 @@
|
|||
# By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/11/03 13:20:01 by maiboyer #+# #+# #
|
||||
# Updated: 2024/05/07 13:14:26 by maiboyer ### ########.fr #
|
||||
# Updated: 2024/05/07 22:24:22 by maiboyer ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 09:48:17 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/07 10:14:16 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/05/07 22:03:02 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,13 +15,21 @@
|
|||
|
||||
#include "me/alloc/alloc.h"
|
||||
#include "me/types.h"
|
||||
#include <stdalign.h>
|
||||
|
||||
#define ARENA_SIZE 16384
|
||||
#define ARENA_SIZE_DEFAULT 4096
|
||||
|
||||
typedef struct s_arena_block
|
||||
{
|
||||
t_usize size;
|
||||
bool end;
|
||||
bool free;
|
||||
t_u8 padding[6];
|
||||
} t_arena_block;
|
||||
|
||||
typedef struct s_arena_page
|
||||
{
|
||||
t_u8 bytes[ARENA_SIZE];
|
||||
t_usize current_index;
|
||||
t_usize page_size;
|
||||
struct s_arena_page *next;
|
||||
} t_arena_page;
|
||||
|
||||
|
|
@ -29,6 +37,8 @@ typedef struct s_arena_page
|
|||
t_arena_page *get_head_arena(void);
|
||||
|
||||
// Will return ERROR if it couldn't malloc the page
|
||||
t_error alloc_arena(t_arena_page **out);
|
||||
t_error alloc_arena_page(t_usize min_size, t_arena_page **out);
|
||||
|
||||
t_error get_block_for_page(t_usize size, t_arena_page *page, t_arena_block **out);
|
||||
|
||||
#endif /* ALLOC_INTERNAL_H */
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 10:13:06 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/07 14:54:56 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/05/07 22:00:20 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -21,29 +21,48 @@
|
|||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// the `+ 16` is twice the size of size_t, only to stay aligned on a 16 byte
|
||||
t_arena_page *find_page_for(t_usize data_size, t_arena_page *page)
|
||||
{
|
||||
t_arena_block *block;
|
||||
|
||||
while (page != NULL)
|
||||
{
|
||||
if (page->page_size <= data_size + sizeof(t_arena_block))
|
||||
{
|
||||
if (page->next == NULL &&
|
||||
alloc_arena_page(usize_round_up_to(data_size + sizeof(*block),
|
||||
ARENA_SIZE_DEFAULT),
|
||||
&page->next))
|
||||
return (me_abort(), NULL);
|
||||
page = page->next;
|
||||
}
|
||||
block = (t_arena_block *)(&page[1]);
|
||||
while (block)
|
||||
{
|
||||
if ((t_u8 *)block >= (t_u8 *)page + sizeof(*page) + page->page_size)
|
||||
break;
|
||||
if (block->free && block->size >= data_size)
|
||||
return (page);
|
||||
block = (void *)((t_u8 *)block + block->size + sizeof(*block));
|
||||
}
|
||||
page = page->next;
|
||||
}
|
||||
return ((me_abort(), NULL));
|
||||
}
|
||||
|
||||
// the is twice the size of size_t, only to stay aligned on a 16 byte
|
||||
// alignement
|
||||
void *me_malloc(t_usize size)
|
||||
{
|
||||
t_arena_page *arena;
|
||||
void *ret;
|
||||
t_arena_page *arena;
|
||||
t_arena_block *block;
|
||||
|
||||
size = usize_round_up_to(size, 16);
|
||||
if (size + 16 > ARENA_SIZE)
|
||||
me_abort();
|
||||
arena = get_head_arena();
|
||||
while (arena->next != NULL && arena->current_index + 16 + size > ARENA_SIZE)
|
||||
arena = arena->next;
|
||||
if (arena->current_index + 16 + size > ARENA_SIZE)
|
||||
{
|
||||
if (alloc_arena(&arena->next))
|
||||
me_abort();
|
||||
arena = arena->next;
|
||||
}
|
||||
*(t_usize *)&arena->bytes[arena->current_index] = size;
|
||||
ret = (void *)&arena->bytes[arena->current_index + 16];
|
||||
arena->current_index += 16 + size;
|
||||
return (ret);
|
||||
arena = find_page_for(size, get_head_arena());
|
||||
if (get_block_for_page(size, arena, &block))
|
||||
return (me_abort(), NULL);
|
||||
block->free = false;
|
||||
return ((t_u8 *)block + sizeof(*block));
|
||||
}
|
||||
|
||||
void *me_calloc(t_usize elem_size, t_usize elem_count)
|
||||
|
|
@ -55,35 +74,27 @@ void *me_calloc(t_usize elem_size, t_usize elem_count)
|
|||
|
||||
void *me_realloc(void *ptr, t_usize new_size)
|
||||
{
|
||||
t_arena_page *arena;
|
||||
t_usize size;
|
||||
void *ret;
|
||||
t_arena_page *arena;
|
||||
t_arena_block *block;
|
||||
void *ret;
|
||||
|
||||
arena = get_head_arena();
|
||||
while (arena != NULL && !((void *)&arena->bytes <= ptr &&
|
||||
ptr <= (void *)(&arena->bytes) + ARENA_SIZE))
|
||||
if (arena == NULL)
|
||||
return (NULL);
|
||||
block = (void *)((t_u8 *)(ptr) - sizeof(t_arena_block));
|
||||
while (arena && !((t_u8 *)arena + arena->page_size + sizeof(*arena) > (t_u8 *)block && (t_u8 *)block > (t_u8 *)arena))
|
||||
arena = arena->next;
|
||||
if (arena == NULL)
|
||||
return (NULL);
|
||||
size = *(t_usize *)((t_u8 *)(ptr)-16);
|
||||
if (size <= new_size)
|
||||
if (block->size <= new_size)
|
||||
return (ptr);
|
||||
ret = me_malloc(new_size);
|
||||
mem_copy(ret, ptr, size);
|
||||
mem_copy(ret, ptr, block->size);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
void me_free(void *ptr)
|
||||
{
|
||||
t_arena_page *arena;
|
||||
|
||||
arena = get_head_arena();
|
||||
while (arena != NULL && !((void *)&arena->bytes <= ptr &&
|
||||
ptr <= (void *)(&arena->bytes) + ARENA_SIZE))
|
||||
arena = arena->next;
|
||||
if (arena == NULL)
|
||||
{
|
||||
me_putstr_fd("Tried to free with me_free !\n", 2);
|
||||
free(ptr);
|
||||
}
|
||||
(void)(ptr);
|
||||
// t_arena_page *arena;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 09:47:50 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/07 14:20:20 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/05/07 22:04:20 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,6 +14,7 @@
|
|||
#include "me/alloc/alloc_internal.h"
|
||||
#include "me/fs/putstr_fd.h"
|
||||
#include "me/mem/mem_set_zero.h"
|
||||
#include "me/num/usize.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void *__libc_malloc(size_t size);
|
||||
|
|
@ -24,7 +25,7 @@ t_arena_page *get_head_arena(void)
|
|||
|
||||
if (val == NULL)
|
||||
{
|
||||
if (alloc_arena(&val))
|
||||
if (alloc_arena_page(ARENA_SIZE_DEFAULT, &val))
|
||||
{
|
||||
me_putstr_fd("Error: malloc failed\n", 2);
|
||||
exit(1);
|
||||
|
|
@ -33,13 +34,54 @@ t_arena_page *get_head_arena(void)
|
|||
return (val);
|
||||
}
|
||||
|
||||
t_error alloc_arena(t_arena_page **out)
|
||||
t_error alloc_arena_page(t_usize min_size, t_arena_page **out)
|
||||
{
|
||||
t_arena_block *block;
|
||||
min_size = usize_round_up_to(min_size, ARENA_SIZE_DEFAULT);
|
||||
if (out == NULL)
|
||||
return (ERROR);
|
||||
*out = __libc_malloc(sizeof(**out));
|
||||
*out = __libc_malloc(sizeof(t_arena_page) + min_size);
|
||||
if (*out == NULL)
|
||||
return (ERROR);
|
||||
mem_set_zero(*out, sizeof(**out));
|
||||
mem_set_zero((t_u8 *)*out + sizeof(**out), min_size);
|
||||
(*out)->page_size = min_size;
|
||||
(*out)->next = NULL;
|
||||
block = (t_arena_block *)((t_u8 *)*out + sizeof(**out));
|
||||
block->end = true;
|
||||
block->free = true;
|
||||
block->size = min_size - sizeof(*block);
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error get_block_for_page(t_usize size, t_arena_page *page,
|
||||
t_arena_block **out)
|
||||
{
|
||||
t_arena_block *cur;
|
||||
t_arena_block *next;
|
||||
t_usize leftover;
|
||||
|
||||
if (page == NULL || out == NULL)
|
||||
return (ERROR);
|
||||
if (page->page_size - sizeof(t_arena_block) <= size)
|
||||
return (ERROR);
|
||||
cur = (void *)((t_u8 *)page + sizeof(*page));
|
||||
while ((t_u8 *)cur < (t_u8 *)page + page->page_size + sizeof(*page))
|
||||
{
|
||||
if (cur->free && cur->size >= size)
|
||||
{
|
||||
if (cur->size > size + sizeof(*cur))
|
||||
{
|
||||
leftover = cur->size - size - sizeof(*cur);
|
||||
cur->size = size;
|
||||
next = (void *)((t_u8 *)cur + cur->size + sizeof(*cur));
|
||||
next->size = leftover;
|
||||
next->end = cur->end;
|
||||
cur->end = false;
|
||||
}
|
||||
*out = cur;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
cur = (void *)((t_u8 *)cur + cur->size + sizeof(*cur));
|
||||
}
|
||||
return (ERROR);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,15 +6,40 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/07 11:08:03 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/07 13:09:59 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/05/07 22:26:15 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/alloc/alloc_internal.h"
|
||||
#include "me/types.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#define PRINT_BACKTRACE
|
||||
|
||||
#if defined(PRINT_BACKTRACE) || defined(BACKTRACE_DEEP)
|
||||
|
||||
# ifndef BACKTRACE_DEEP
|
||||
# define BACKTRACE_DEEP 256
|
||||
# endif
|
||||
# include "me/fs/putendl_fd.h"
|
||||
# include <execinfo.h>
|
||||
|
||||
static void print_trace(void)
|
||||
{
|
||||
void *array[BACKTRACE_DEEP];
|
||||
t_i32 size;
|
||||
|
||||
size = backtrace(array, BACKTRACE_DEEP);
|
||||
backtrace_symbols_fd(array, size, 2);
|
||||
}
|
||||
#else
|
||||
|
||||
static void print_trace(void)
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
void me_abort(void)
|
||||
{
|
||||
print_trace();
|
||||
me_exit(1);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue