wip fuck it

This commit is contained in:
Maieul BOYER 2024-05-09 19:05:18 +02:00
parent 746a1ad2ad
commit 3937b70957
No known key found for this signature in database
7 changed files with 120 additions and 177 deletions

View file

@ -39,7 +39,7 @@
#define STACK_VERSION_NONE ((t_stack_version)-1)
#define TS_DECODE_ERROR (-1)
#if 1 == 1
#if false
# undef malloc
# undef calloc
# undef realloc

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:48:17 by maiboyer #+# #+# */
/* Updated: 2024/05/08 19:15:05 by maiboyer ### ########.fr */
/* Updated: 2024/05/09 13:30:12 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,31 +17,33 @@
#include "me/types.h"
#include <stdalign.h>
#define ARENA_SIZE_DEFAULT 4096 * 2 * 2
#define PAGE_SIZE_DEFAULT 4096 * 4
#define BLOCK_PADDING "\xFE\xDC\xAB\xC0\xFE\xEE"
typedef struct s_arena_block
typedef struct s_mblock
{
t_usize size;
bool end;
bool used;
t_u8 padding[6];
} t_arena_block;
struct s_mblock *next;
struct s_mpage *page;
t_usize size;
bool used;
t_u8 padding[7];
} t_mblock;
typedef struct s_arena_page
typedef struct s_mpage
{
t_usize page_size;
struct s_arena_page *next;
} t_arena_page;
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_arena_page *get_head_arena(void);
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_arena_page **out);
t_error alloc_arena_page(t_usize min_size, t_mpage **out);
t_error get_block_for_page(t_usize size, t_arena_page *page,
t_arena_block **out);
void print_pages_info(void);
t_arena_page *get_page_from_ptr(void *ptr);
t_mblock *get_block_for_size(t_usize size);
void print_pages_info(void);
t_mpage *get_page_from_ptr(void *ptr);
#endif /* ALLOC_INTERNAL_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 10:13:06 by maiboyer #+# #+# */
/* Updated: 2024/05/08 19:37:39 by maiboyer ### ########.fr */
/* Updated: 2024/05/09 17:57:54 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,60 +22,16 @@
#include <stdio.h>
#include <stdlib.h>
t_arena_page *find_page_for(t_usize data_size, t_arena_page *page)
{
t_arena_block *block;
t_arena_page *last_page;
last_page = NULL;
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("Failed Malloc"), NULL);
last_page = page;
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->used && block->size >= data_size)
return (page);
block = (void *)((t_u8 *)block + block->size + sizeof(*block));
}
last_page = page;
page = page->next;
}
if (last_page != NULL)
{
if (alloc_arena_page(usize_round_up_to(data_size + sizeof(*block),
ARENA_SIZE_DEFAULT),
&last_page->next))
return (me_abort("Failed Malloc"), NULL);
return (last_page->next);
}
return ((me_abort("Found no pages for memory"), 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;
t_arena_block *block;
t_mblock *block;
size = usize_round_up_to(size, 16);
arena = find_page_for(size, get_head_arena());
if (get_block_for_page(size, arena, &block))
block = get_block_for_size(size);
if (block == NULL)
return (me_abort("Found no page for me_malloc"), NULL);
block->used = true;
return ((t_u8 *)block + sizeof(*block));
return ((void *)(((t_usize)block) + sizeof(t_mblock)));
}
void *me_calloc(t_usize elem_size, t_usize elem_count)
@ -87,16 +43,15 @@ void *me_calloc(t_usize elem_size, t_usize elem_count)
void *me_realloc(void *ptr, t_usize new_size)
{
t_arena_block *block;
void *ret;
t_mblock *block;
void *ret;
if (ptr == NULL)
return (me_malloc(new_size));
block = (void *)((t_u8 *)(ptr) - sizeof(t_arena_block));
block = (void *)((t_u8 *)(ptr) - sizeof(t_mblock));
if (block->size <= new_size)
return (ptr);
ret = me_malloc(new_size);
mem_copy(ret, ptr, block->size);
me_free(ptr);
return (ret);
@ -104,22 +59,14 @@ void *me_realloc(void *ptr, t_usize new_size)
void me_free(void *ptr)
{
t_arena_page *page;
t_arena_block *cur;
t_arena_block *next;
t_mblock *cur;
print_trace();
page = get_page_from_ptr(ptr);
if (page == NULL)
me_abort("Tried to me_free with me_free something that isn't allocated "
"with me_alloc");
cur = (void *)(((t_usize)ptr) - sizeof(*cur));
cur = (void *)(((t_usize)ptr) - sizeof(t_mblock));
cur->used = false;
next = (void *)(((t_usize)cur) + sizeof(*cur) + cur->size);
if (((t_usize)page) <= ((t_usize)next) &&
((t_usize)next) <= ((t_usize)page) + page->page_size + sizeof(*page))
if (cur->next != NULL && cur->page == cur->next->page && !cur->next->used)
{
if (!next->used)
cur->size += next->size + sizeof(*cur);
cur->size += sizeof(t_mblock) + cur->next->size;
mem_set_zero(cur->next->padding, 7);
cur->next = cur->next->next;
}
}

View file

@ -6,24 +6,6 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/08 19:11:20 by maiboyer #+# #+# */
/* Updated: 2024/05/08 19:14:57 by maiboyer ### ########.fr */
/* Updated: 2024/05/09 08:04:37 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/alloc/alloc.h"
#include "me/alloc/alloc_internal.h"
t_arena_page *get_page_from_ptr(void *ptr)
{
t_arena_page *page;
page = get_head_arena();
while (page)
{
if (((t_usize)page) <= ((t_usize)ptr) &&
((t_usize)ptr) <= ((t_usize)page) + page->page_size + sizeof(*page))
return (page);
page = page->next;
}
return (NULL);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:47:50 by maiboyer #+# #+# */
/* Updated: 2024/05/08 22:05:36 by maiboyer ### ########.fr */
/* Updated: 2024/05/09 18:21:33 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,8 @@
#include "me/fs/putendl_fd.h"
#include "me/fs/putnbr_fd.h"
#include "me/fs/putstr_fd.h"
#include "me/mem/mem_compare.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/num/usize.h"
#include <stdio.h>
@ -22,81 +24,90 @@
void *__libc_malloc(size_t size);
t_arena_page *get_head_arena(void)
t_mpage *alloc_page(t_usize size)
{
static t_arena_page *val = NULL;
t_mpage *val;
if (val == NULL)
{
if (alloc_arena_page(ARENA_SIZE_DEFAULT, &val))
{
me_putstr_fd("Error: malloc failed\n", 2);
exit(1);
}
}
size = usize_round_up_to(size, PAGE_SIZE_DEFAULT);
val = __libc_malloc(PAGE_SIZE_DEFAULT);
if (val == NULL || sizeof(t_mpage) >= PAGE_SIZE_DEFAULT)
return (NULL);
val->next = NULL;
val->page_size = PAGE_SIZE_DEFAULT;
val->first = (t_mblock *)(((t_usize)val) + sizeof(t_mpage));
val->first->page = val;
val->first->next = NULL;
mem_copy(val->first->padding, BLOCK_PADDING, 7);
val->first->used = false;
val->first->size = PAGE_SIZE_DEFAULT - sizeof(t_mpage);
me_putstr_fd("Allocating a page of size ", 2);
me_putnbr_fd(size, 2);
me_putendl_fd("", 2);
return (val);
}
t_error alloc_arena_page(t_usize min_size, t_arena_page **out)
t_mpage *get_head_arena(void)
{
t_arena_block *block;
static t_mpage *val = NULL;
printf("Allocating page with size %zu\n", min_size);
min_size = usize_round_up_to(min_size, ARENA_SIZE_DEFAULT);
if (out == NULL)
return (ERROR);
*out = __libc_malloc(sizeof(t_arena_page) + min_size);
if (*out == NULL)
return (ERROR);
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->used = false;
block->size = min_size - sizeof(*block);
return (NO_ERROR);
if (val == NULL)
val = alloc_page(PAGE_SIZE_DEFAULT);
if (val == NULL)
(me_putstr_fd("Failed to alloc first page", 2), exit(1));
return (val);
}
t_error get_block_for_page(t_usize size, t_arena_page *page,
t_arena_block **out)
t_mblock *split_block(t_mblock *self, t_usize min_size)
{
t_arena_block *cur;
t_arena_block *next;
t_usize leftover;
t_usize remaining;
t_mblock *old_next;
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_usize)((t_u8 *)cur - (t_u8 *)page) <
page->page_size - sizeof(*page))
min_size = usize_round_up_to(min_size, 16);
if (self->size > (min_size + sizeof(t_mpage) + 16))
{
remaining = self->size - min_size - sizeof(t_mpage);
printf("Splitting %zu into %zu and %zu\n", self->size, min_size,
remaining);
self->size = min_size;
old_next = self->next;
self->next =
(t_mblock *)(((t_usize)self) + self->size + sizeof(t_mpage));
self->next->page = self->page;
self->next->next = old_next;
self->next->used = false;
self->next->size = remaining;
mem_copy(self->next->padding, BLOCK_PADDING, 7);
}
return (self);
}
t_mblock *get_block_for_size(t_usize size)
{
t_mblock *last;
t_mblock *cur;
last = NULL;
cur = get_head_arena()->first;
while (cur)
{
if (!cur->used && cur->size >= size)
{
if (cur->size > size + sizeof(*cur) * 2)
{
leftover = cur->size - size - sizeof(*cur);
cur->size = size;
next = (void *)(((t_usize)cur) + size);
next->size = leftover;
next->end = cur->end;
next->used = false;
cur->end = false;
}
*out = cur;
return (NO_ERROR);
}
cur = (void *)((t_u8 *)cur + cur->size + sizeof(*cur));
return (split_block(cur, size));
last = cur;
cur = cur->next;
}
return (ERROR);
if (last == NULL)
return (NULL);
last->page->next = alloc_page(size);
if (last->page->next == NULL)
me_abort("Failed to alloc page!");
last->next = last->page->next->first;
return (split_block(last->page->next->first, size));
}
void print_pages_info(void)
{
t_arena_page *page;
t_i32 page_nb;
t_mpage *page;
t_i32 page_nb;
page = get_head_arena();
page_nb = 0;

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 11:08:03 by maiboyer #+# #+# */
/* Updated: 2024/05/08 23:26:22 by maiboyer ### ########.fr */
/* Updated: 2024/05/09 07:43:10 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -54,7 +54,7 @@ static void print_trace_inner(void **trace, t_str *messages, t_usize i)
fflush(stderr);
snprintf(
syscom, sizeof(syscom) / sizeof(syscom[0]),
"addr2line %#x -e %.*s -ipfa"
"addr2line %#x -e %.*s -ipf"
"| 1>&2 rg \"^(.*) at %s(.*)\"'$' --replace '$1 at $2' --color never",
(t_u32)(convert_to_vma((t_usize)trace[i])), p, messages[i], BASE_PATH);
if (system(syscom))

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 13:08:52 by maiboyer #+# #+# */
/* Updated: 2024/05/08 19:33:27 by maiboyer ### ########.fr */
/* Updated: 2024/05/09 17:55:46 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,27 +16,28 @@
#include "me/types.h"
#include <stdlib.h>
void __libc_free(void *ptr);
void me_exit(t_i32 exit_code)
{
t_arena_page *page;
t_arena_page *tmp;
t_arena_block *block;
t_usize count_block;
t_mpage *page;
t_mpage *tmp;
t_mblock *block;
t_usize count_block;
page = get_head_arena();
count_block = 0;
while (page)
{
block = (void *)(((t_usize)page) + sizeof(*page));
while (((t_usize)page) <= ((t_usize)block) &&
((t_usize)block) <=
((t_usize)page) + page->page_size + sizeof(*page))
block = page->first;
while (block)
{
count_block += block->used;
block = (void *)(((t_usize)block) + sizeof(*block) + block);
if (block->used)
count_block += 1;
block = block->next;
}
tmp = page->next;
me_free(page);
__libc_free(page);
page = tmp;
}
if (count_block != 0)