still working on memory stuff

This commit is contained in:
Maieul BOYER 2024-05-08 19:35:13 +02:00
parent 8e90a858ad
commit 326c9318e4
No known key found for this signature in database
18 changed files with 206 additions and 77 deletions

View file

@ -6,7 +6,7 @@
# By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/03 13:20:01 by maiboyer #+# #+# #
# Updated: 2024/05/08 16:06:05 by maiboyer ### ########.fr #
# Updated: 2024/05/08 16:18:00 by maiboyer ### ########.fr #
# #
# **************************************************************************** #
@ -47,6 +47,10 @@ COL_GREEN = \033[32m
COL_BOLD = \033[1m
COL_RESET = \033[0m
# TODO: REMOVE FOR RENDU !!!!!
CFLAGS += -DPRINT_BACKTRACE
.PHONY = all bonus clean re subject
all: $(NAME)

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:48:17 by maiboyer #+# #+# */
/* Updated: 2024/05/07 22:03:02 by maiboyer ### ########.fr */
/* Updated: 2024/05/08 19:15:05 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,14 +17,14 @@
#include "me/types.h"
#include <stdalign.h>
#define ARENA_SIZE_DEFAULT 4096
#define ARENA_SIZE_DEFAULT 4096 * 2 * 2
typedef struct s_arena_block
{
t_usize size;
bool end;
bool free;
t_u8 padding[6];
t_usize size;
bool end;
bool used;
t_u8 padding[6];
} t_arena_block;
typedef struct s_arena_page
@ -39,6 +39,9 @@ t_arena_page *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 get_block_for_page(t_usize size, t_arena_page *page, t_arena_block **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);
#endif /* ALLOC_INTERNAL_H */

View file

@ -56,7 +56,7 @@ typedef struct s_blx
void *win;
t_blx_input inputs;
t_run_function func;
t_free_function free;
t_free_function me_free;
t_blx_app app;
t_blx_data _data;
} t_blx;

View file

@ -64,7 +64,7 @@ See the function declaration below for the signature and more information.
If you don't want/need the qoi_read and qoi_write functions, you can define
QOI_NO_STDIO before including this library.
This library uses malloc() and free(). To supply your own malloc implementation
This library uses malloc() and me_free(). To supply your own malloc implementation
you can define QOI_MALLOC and QOI_FREE before including this library.
This library uses memset() to zero-initialize the index. To supply your own
@ -273,7 +273,7 @@ The function either returns NULL on failure (invalid data, or malloc or fopen
failed) or a pot_i32er to the decoded pixels. On success, the t_qoi_desc struct
will be filled with the description from the file header.
The returned pixel data should be free()d after use. */
The returned pixel data should be me_free()d after use. */
void *qoi_read(t_const_str filename, t_qoi_desc *desc,
t_i32 channels);
@ -286,7 +286,7 @@ The function either returns NULL on failure (invalid parameters or malloc
failed) or a pot_i32er to the encoded data on success. On success the out_len
is set to the size in bytes of the encoded data.
The returned qoi data should be free()d after use. */
The returned qoi data should be me_free()d after use. */
void *qoi_encode(const void *data, const t_qoi_desc *desc,
t_i32 *out_len);
@ -297,7 +297,7 @@ The function either returns NULL on failure (invalid parameters or malloc
failed) or a pot_i32er to the decoded pixels. On success, the t_qoi_desc struct
is filled with the description from the file header.
The returned pixel data should be free()d after use. */
The returned pixel data should be me_free()d after use. */
void *qoi_decode(const void *data, t_i32 size, t_qoi_desc *desc,
t_i32 channels);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 14:31:12 by maiboyer #+# #+# */
/* Updated: 2024/05/08 16:11:35 by maiboyer ### ########.fr */
/* Updated: 2024/05/08 19:27:44 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -1,4 +1,5 @@
alloc/alloc
alloc/alloc_get_page_from_bloc
alloc/get_arena
blx/blx
blx/blx_create_fontsheet

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 16:12:19 by maiboyer ### ########.fr */
/* Updated: 2024/05/08 19:20:59 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,12 +19,15 @@
#include <stdalign.h>
#include <stddef.h>
#include <stdint.h>
#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))
@ -34,6 +37,7 @@ t_arena_page *find_page_for(t_usize data_size, t_arena_page *page)
ARENA_SIZE_DEFAULT),
&page->next))
return (me_abort("Failed Malloc"), NULL);
last_page = page;
page = page->next;
}
block = (t_arena_block *)(&page[1]);
@ -41,12 +45,21 @@ t_arena_page *find_page_for(t_usize data_size, t_arena_page *page)
{
if ((t_u8 *)block >= (t_u8 *)page + sizeof(*page) + page->page_size)
break;
if (block->free && block->size >= data_size)
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));
}
@ -61,7 +74,7 @@ void *me_malloc(t_usize size)
arena = find_page_for(size, get_head_arena());
if (get_block_for_page(size, arena, &block))
return (me_abort("Found no page for me_malloc"), NULL);
block->free = false;
block->used = true;
return ((t_u8 *)block + sizeof(*block));
}
@ -74,27 +87,38 @@ 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_arena_block *block;
void *ret;
arena = get_head_arena();
if (arena == NULL)
return (NULL);
if (ptr == NULL)
return (me_malloc(new_size));
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);
if (block->size <= new_size)
return (ptr);
ret = me_malloc(new_size);
mem_copy(ret, ptr, block->size);
return (ret);
}
void me_free(void *ptr)
{
t_arena_page *page;
t_arena_block *cur;
t_arena_block *next;
(void)(ptr);
// t_arena_page *arena;
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->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 (!next->used)
cur->size += next->size + sizeof(*cur);
}
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alloc_get_page_from_bloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#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,15 +6,18 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:47:50 by maiboyer #+# #+# */
/* Updated: 2024/05/07 22:04:20 by maiboyer ### ########.fr */
/* Updated: 2024/05/08 19:24:47 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/alloc/alloc.h"
#include "me/alloc/alloc_internal.h"
#include "me/fs/putendl_fd.h"
#include "me/fs/putnbr_fd.h"
#include "me/fs/putstr_fd.h"
#include "me/mem/mem_set_zero.h"
#include "me/num/usize.h"
#include <stdio.h>
#include <stdlib.h>
void *__libc_malloc(size_t size);
@ -37,6 +40,8 @@ t_arena_page *get_head_arena(void)
t_error alloc_arena_page(t_usize min_size, t_arena_page **out)
{
t_arena_block *block;
printf("Allocating page with size %zu", min_size);
min_size = usize_round_up_to(min_size, ARENA_SIZE_DEFAULT);
if (out == NULL)
return (ERROR);
@ -48,7 +53,7 @@ t_error alloc_arena_page(t_usize min_size, t_arena_page **out)
(*out)->next = NULL;
block = (t_arena_block *)((t_u8 *)*out + sizeof(**out));
block->end = true;
block->free = true;
block->used = false;
block->size = min_size - sizeof(*block);
return (NO_ERROR);
}
@ -65,17 +70,19 @@ t_error get_block_for_page(t_usize size, t_arena_page *page,
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))
while ((t_usize)((t_u8 *)cur - (t_u8 *)page) <
page->page_size - sizeof(*page))
{
if (cur->free && cur->size >= size)
if (!cur->used && cur->size >= size)
{
if (cur->size > size + sizeof(*cur))
if (cur->size > size + sizeof(*cur) * 2)
{
leftover = cur->size - size - sizeof(*cur);
cur->size = size;
next = (void *)((t_u8 *)cur + cur->size + sizeof(*cur));
next = (void *)(((t_usize)cur) + size);
next->size = leftover;
next->end = cur->end;
next->used = false;
cur->end = false;
}
*out = cur;
@ -85,3 +92,21 @@ t_error get_block_for_page(t_usize size, t_arena_page *page,
}
return (ERROR);
}
void print_pages_info(void)
{
t_arena_page *page;
t_i32 page_nb;
page = get_head_arena();
page_nb = 0;
while (page != NULL)
{
me_putstr_fd("Page ", 2);
me_putnbr_fd(page_nb++, 2);
me_putstr_fd(" with size ", 2);
me_putnbr_fd((t_i32)page->page_size, 2);
me_putstr_fd("\n", 2);
page = page->next;
}
}

View file

@ -29,7 +29,7 @@ t_blx blx_initialize(t_run_function func, t_free_function free_fn,
{
t_blx ctx;
ctx = (t_blx){.func = func, .app = data, .free = free_fn};
ctx = (t_blx){.func = func, .app = data, .me_free = free_fn};
ctx.mlx = mlx_init();
if (ctx.mlx == NULL)
(me_eprintf("Error:\nfailed to inialize blx (mlx) !\n"), exit(1));
@ -59,14 +59,14 @@ void blx_free(t_blx app)
blx_sprite_free(app._data.screen);
blx_sprite_free(app._data.font);
mlx_do_key_autorepeaton(app.mlx);
if (app.free)
app.free(app.app);
if (app.me_free)
app.me_free(app.app);
if (app.win)
mlx_destroy_window(app.mlx, app.win);
if (app.mlx)
{
mlx_destroy_display(app.mlx);
free(app.mlx);
me_free(app.mlx);
}
vec_u8_free(app.inputs.keysyms_held);
vec_u8_free(app.inputs.keysyms_pressed);

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 16:11:05 by maiboyer ### ########.fr */
/* Updated: 2024/05/08 19:07:07 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,8 +16,8 @@
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include "me/alloc/alloc_internal.h"
#define PRINT_BACKTRACE
#ifndef BASE_PATH
# define BASE_PATH "/no_base_path_defined/"
#endif
@ -34,7 +34,7 @@ static void print_trace_inner(void **trace, t_str *messages, t_usize i)
t_i32 p;
p = 0;
fprintf(stderr, "[bt] #%zu\t", i);
fprintf(stderr, "[bt] #%-4zu\t", i);
while (messages[i][p] != '(' && messages[i][p] != ' '
&& messages[i][p] != 0)
++p;
@ -47,7 +47,8 @@ static void print_trace_inner(void **trace, t_str *messages, t_usize i)
p, \
messages[i], \
BASE_PATH);
(void)system(syscom);
if (system(syscom))
fprintf(stderr, "%s\n", messages[i]);
}
void print_trace(void)
@ -65,10 +66,11 @@ void print_trace(void)
size -= 3;
while (i < size)
print_trace_inner(trace, messages, i++);
me_free(messages);
}
#else
static void print_trace(void)
void print_trace(void)
{
}
@ -78,6 +80,8 @@ void me_abort(t_str msg)
{
if (msg == NULL)
msg = "No message (msg was NULL)";
me_putendl_fd("Memory information:", 2);
print_pages_info();
me_putstr_fd("Abort: ", 2);
me_putendl_fd(msg, 2);
print_trace();

View file

@ -6,25 +6,41 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 13:08:52 by maiboyer #+# #+# */
/* Updated: 2024/05/07 15:01:38 by maiboyer ### ########.fr */
/* Updated: 2024/05/08 19:33:27 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/types.h"
#include "me/alloc/alloc_internal.h"
#include "me/fs/putendl_fd.h"
#include "me/fs/putnbr_fd.h"
#include "me/types.h"
#include <stdlib.h>
void me_exit(t_i32 exit_code)
{
t_arena_page *arena;
t_arena_page *tmp;
t_arena_page *page;
t_arena_page *tmp;
t_arena_block *block;
t_usize count_block;
arena = get_head_arena();
while (arena)
page = get_head_arena();
count_block = 0;
while (page)
{
tmp = arena->next;
free(arena);
arena = tmp;
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))
{
count_block += block->used;
block = (void *)(((t_usize)block) + sizeof(*block) + block);
}
tmp = page->next;
me_free(page);
page = tmp;
}
if (count_block != 0)
(me_putnbr_fd(count_block, 2),
me_putendl_fd(" Blocks weren't freed when exiting !", 2));
exit(exit_code);
}