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> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 13:20:25 by maiboyer #+# #+# */ /* Created: 2024/05/02 13:20:25 by maiboyer #+# #+# */
/* Updated: 2024/05/03 12:18:29 by maiboyer ### ########.fr */ /* Updated: 2024/05/08 18:20:53 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,9 +14,9 @@
#define SIGNAL_HANDLER_H #define SIGNAL_HANDLER_H
#include "me/types.h" #include "me/types.h"
#include <stdio.h>
#include <readline/readline.h> #include <readline/readline.h>
#include <signal.h> #include <signal.h>
#include <stdio.h>
typedef t_i32 t_signal; typedef t_i32 t_signal;

View file

@ -1,6 +1,7 @@
#ifndef TREE_SITTER_ARRAY_H_ #ifndef TREE_SITTER_ARRAY_H_
#define TREE_SITTER_ARRAY_H_ #define TREE_SITTER_ARRAY_H_
#include "me/alloc/alloc.h"
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <limits.h> #include <limits.h>
@ -38,6 +39,18 @@
#define STACK_VERSION_NONE ((t_stack_version)-1) #define STACK_VERSION_NONE ((t_stack_version)-1)
#define TS_DECODE_ERROR (-1) #define TS_DECODE_ERROR (-1)
#if 1 == 1
# undef malloc
# undef calloc
# undef realloc
# undef free
# define malloc(s) me_malloc((s))
# define calloc(s, l) me_calloc((s), (l))
# define realloc(p, t) me_realloc((p), (t))
# define free(p) me_free((p))
#endif
#define Array(T) \ #define Array(T) \
struct \ struct \
{ \ { \
@ -184,7 +197,6 @@
array_insert(self, _index, value); \ array_insert(self, _index, value); \
} while (0) } while (0)
// Get a subtree's children, which are allocated immediately before the // Get a subtree's children, which are allocated immediately before the
// tree's own heap data. // tree's own heap data.
#define ts_subtree_children(self) \ #define ts_subtree_children(self) \
@ -275,10 +287,10 @@ typedef struct s_parse_node
typedef struct s_tree_cursor_entry typedef struct s_tree_cursor_entry
{ {
const union u_subtree *subtree; const union u_subtree *subtree;
t_length position; t_length position;
uint32_t child_index; uint32_t child_index;
uint32_t structural_child_index; uint32_t structural_child_index;
uint32_t descendant_index; uint32_t descendant_index;
} t_tree_cursor_entry; } t_tree_cursor_entry;
typedef struct s_tree_cursor typedef struct s_tree_cursor
@ -1537,8 +1549,8 @@ void ts_stack_clear(t_stack *);
typedef void (*StackIterateCallback)(void *, t_state_id, uint32_t); typedef void (*StackIterateCallback)(void *, t_state_id, uint32_t);
void ts_external_scanner_state_init(t_external_scanner_state *, const char *, void ts_external_scanner_state_init(t_external_scanner_state *, const char *,
unsigned); unsigned);
const char *ts_external_scanner_state_data(const t_external_scanner_state *); const char *ts_external_scanner_state_data(const t_external_scanner_state *);
bool ts_external_scanner_state_eq(const t_external_scanner_state *self, bool ts_external_scanner_state_eq(const t_external_scanner_state *self,
const char *, unsigned); const char *, unsigned);
@ -1579,7 +1591,8 @@ t_subtree ts_subtree_edit(t_subtree, const t_input_edit *edit,
char *ts_subtree_string(t_subtree, t_symbol, bool, const t_language *, char *ts_subtree_string(t_subtree, t_symbol, bool, const t_language *,
bool include_all); bool include_all);
t_subtree ts_subtree_last_external_token(t_subtree); t_subtree ts_subtree_last_external_token(t_subtree);
const t_external_scanner_state *ts_subtree_external_scanner_state(t_subtree self); const t_external_scanner_state *ts_subtree_external_scanner_state(
t_subtree self);
bool ts_subtree_external_scanner_state_eq(t_subtree, t_subtree); bool ts_subtree_external_scanner_state_eq(t_subtree, t_subtree);
#define SUBTREE_GET(self, name) \ #define SUBTREE_GET(self, name) \

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/29 11:35:51 by rparodi #+# #+# */ /* Created: 2024/03/29 11:35:51 by rparodi #+# #+# */
/* Updated: 2024/05/07 13:00:21 by maiboyer ### ########.fr */ /* Updated: 2024/05/08 19:07:54 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,6 +14,8 @@
#include "app/env.h" #include "app/env.h"
#include "me/hashmap/hashmap_env.h" #include "me/hashmap/hashmap_env.h"
#include "me/alloc/alloc_internal.h"
void ts_parser_delete(t_first_parser *self); void ts_parser_delete(t_first_parser *self);
void ft_free(void *ptr) void ft_free(void *ptr)
@ -46,5 +48,6 @@ void ft_exit(t_utils *maiboyerlpb, t_u8 exit_status)
{ {
if (maiboyerlpb != NULL) if (maiboyerlpb != NULL)
ft_free_utils(maiboyerlpb); ft_free_utils(maiboyerlpb);
print_pages_info();
me_exit(exit_status); me_exit(exit_status);
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */ /* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
/* Updated: 2024/05/08 15:36:52 by maiboyer ### ########.fr */ /* Updated: 2024/05/08 19:22:47 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,10 +14,16 @@
#include "app/node.h" #include "app/node.h"
#include "app/signal_handler.h" #include "app/signal_handler.h"
#include "gmr/symbols.h" #include "gmr/symbols.h"
#include "me/alloc/alloc.h"
#include "me/string/str_len.h" #include "me/string/str_len.h"
#include "minishell.h" #include "minishell.h"
#include "parser/api.h" #include "parser/api.h"
#undef free
#undef malloc
#undef realloc
#undef calloc
t_first_parser *ts_parser_new(); t_first_parser *ts_parser_new();
void ts_tree_delete(t_first_tree *); void ts_tree_delete(t_first_tree *);
t_parse_node ts_tree_root_node(t_first_tree *); t_parse_node ts_tree_root_node(t_first_tree *);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 18:36:40 by maiboyer #+# #+# */ /* Created: 2024/04/28 18:36:40 by maiboyer #+# #+# */
/* Updated: 2024/05/07 12:51:18 by maiboyer ### ########.fr */ /* Updated: 2024/05/08 18:35:15 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,20 +15,21 @@
#include "me/mem/mem_alloc_array.h" #include "me/mem/mem_alloc_array.h"
#include "me/string/str_l_copy.h" #include "me/string/str_l_copy.h"
#include "parser/api.h" #include "parser/api.h"
#include <stdio.h>
t_node build_node(t_parse_node current, t_const_str input); t_node build_node(t_parse_node current, t_const_str input);
t_parse_node ts_node_child(t_parse_node parent, t_usize idx); t_parse_node ts_node_child(t_parse_node parent, t_usize idx);
t_symbol ts_node_symbol(t_parse_node self); t_symbol ts_node_symbol(t_parse_node self);
t_const_str ts_node_type(t_parse_node self); t_const_str ts_node_type(t_parse_node self);
t_u32 ts_node_start_byte(t_parse_node self); t_u32 ts_node_start_byte(t_parse_node self);
t_u32 ts_node_end_byte(t_parse_node self); t_u32 ts_node_end_byte(t_parse_node self);
t_u32 ts_node_child_count(t_parse_node self); t_u32 ts_node_child_count(t_parse_node self);
t_node *build_childs(t_parse_node parent, t_const_str input, t_usize count) t_node *build_childs(t_parse_node parent, t_const_str input, t_usize count)
{ {
t_node *ret; t_node *ret;
t_usize idx; t_usize idx;
t_parse_node child; t_parse_node child;
ret = mem_alloc_array(sizeof(*ret), count); ret = mem_alloc_array(sizeof(*ret), count);
if (ret == NULL) if (ret == NULL)

View file

@ -6,16 +6,16 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 13:22:14 by maiboyer #+# #+# */ /* Created: 2024/05/02 13:22:14 by maiboyer #+# #+# */
/* Updated: 2024/05/08 15:33:52 by maiboyer ### ########.fr */ /* Updated: 2024/05/08 18:20:46 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "app/signal_handler.h" #include "app/signal_handler.h"
#include "me/printf/printf.h"
#include "me/types.h" #include "me/types.h"
#include "readline/readline.h" #include "readline/readline.h"
#include "me/printf/printf.h"
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
void sigint_handle(int sig, siginfo_t *info, void *ucontext) void sigint_handle(int sig, siginfo_t *info, void *ucontext)
{ {
@ -56,7 +56,7 @@ void sigsegv_handle(int sig, siginfo_t *info, void *ucontext)
(void)(sig); (void)(sig);
(void)(info); (void)(info);
(void)(ucontext); (void)(ucontext);
me_eprintf("SEGFAULT!!!\n"); me_eprintf("Abort: SEGFAULT!!!\n");
print_trace(); print_trace();
exit(139); exit(139);
} }
@ -74,9 +74,9 @@ t_error install_signal(void)
data.sa_sigaction = sigquit_handle; data.sa_sigaction = sigquit_handle;
if (sigaction(SIGQUIT, &data, NULL)) if (sigaction(SIGQUIT, &data, NULL))
return (ERROR); return (ERROR);
data.sa_sigaction = sigsegv_handle; data.sa_sigaction = sigsegv_handle;
if (sigaction(SIGSEGV, &data, NULL)) if (sigaction(SIGSEGV, &data, NULL))
return (ERROR); return (ERROR);
return (NO_ERROR); return (NO_ERROR);
} }

View file

@ -6,7 +6,7 @@
# By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ # # By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # # 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_BOLD = \033[1m
COL_RESET = \033[0m COL_RESET = \033[0m
# TODO: REMOVE FOR RENDU !!!!!
CFLAGS += -DPRINT_BACKTRACE
.PHONY = all bonus clean re subject .PHONY = all bonus clean re subject
all: $(NAME) all: $(NAME)

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:48:17 by maiboyer #+# #+# */ /* 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 "me/types.h"
#include <stdalign.h> #include <stdalign.h>
#define ARENA_SIZE_DEFAULT 4096 #define ARENA_SIZE_DEFAULT 4096 * 2 * 2
typedef struct s_arena_block typedef struct s_arena_block
{ {
t_usize size; t_usize size;
bool end; bool end;
bool free; bool used;
t_u8 padding[6]; t_u8 padding[6];
} t_arena_block; } t_arena_block;
typedef struct s_arena_page 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 // 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_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 */ #endif /* ALLOC_INTERNAL_H */

View file

@ -56,7 +56,7 @@ typedef struct s_blx
void *win; void *win;
t_blx_input inputs; t_blx_input inputs;
t_run_function func; t_run_function func;
t_free_function free; t_free_function me_free;
t_blx_app app; t_blx_app app;
t_blx_data _data; t_blx_data _data;
} t_blx; } 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 If you don't want/need the qoi_read and qoi_write functions, you can define
QOI_NO_STDIO before including this library. 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. 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 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 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. 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, void *qoi_read(t_const_str filename, t_qoi_desc *desc,
t_i32 channels); 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 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. 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, void *qoi_encode(const void *data, const t_qoi_desc *desc,
t_i32 *out_len); 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 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. 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, void *qoi_decode(const void *data, t_i32 size, t_qoi_desc *desc,
t_i32 channels); t_i32 channels);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 14:31:12 by maiboyer #+# #+# */ /* 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
alloc/alloc_get_page_from_bloc
alloc/get_arena alloc/get_arena
blx/blx blx/blx
blx/blx_create_fontsheet blx/blx_create_fontsheet

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 10:13:06 by maiboyer #+# #+# */ /* 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 <stdalign.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
t_arena_page *find_page_for(t_usize data_size, t_arena_page *page) t_arena_page *find_page_for(t_usize data_size, t_arena_page *page)
{ {
t_arena_block *block; t_arena_block *block;
t_arena_page *last_page;
last_page = NULL;
while (page != NULL) while (page != NULL)
{ {
if (page->page_size <= data_size + sizeof(t_arena_block)) 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), ARENA_SIZE_DEFAULT),
&page->next)) &page->next))
return (me_abort("Failed Malloc"), NULL); return (me_abort("Failed Malloc"), NULL);
last_page = page;
page = page->next; page = page->next;
} }
block = (t_arena_block *)(&page[1]); 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) if ((t_u8 *)block >= (t_u8 *)page + sizeof(*page) + page->page_size)
break; break;
if (block->free && block->size >= data_size) if (!block->used && block->size >= data_size)
return (page); return (page);
block = (void *)((t_u8 *)block + block->size + sizeof(*block)); block = (void *)((t_u8 *)block + block->size + sizeof(*block));
} }
last_page = page;
page = page->next; 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)); 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()); arena = find_page_for(size, get_head_arena());
if (get_block_for_page(size, arena, &block)) if (get_block_for_page(size, arena, &block))
return (me_abort("Found no page for me_malloc"), NULL); return (me_abort("Found no page for me_malloc"), NULL);
block->free = false; block->used = true;
return ((t_u8 *)block + sizeof(*block)); 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) void *me_realloc(void *ptr, t_usize new_size)
{ {
t_arena_page *arena;
t_arena_block *block; t_arena_block *block;
void *ret; void *ret;
arena = get_head_arena(); if (ptr == NULL)
if (arena == NULL) return (me_malloc(new_size));
return (NULL);
block = (void *)((t_u8 *)(ptr) - sizeof(t_arena_block)); 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) if (block->size <= new_size)
return (ptr); return (ptr);
ret = me_malloc(new_size); ret = me_malloc(new_size);
mem_copy(ret, ptr, block->size); mem_copy(ret, ptr, block->size);
return (ret); return (ret);
} }
void me_free(void *ptr) void me_free(void *ptr)
{ {
t_arena_page *page;
t_arena_block *cur;
t_arena_block *next;
(void)(ptr); (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> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 09:47:50 by maiboyer #+# #+# */ /* 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.h"
#include "me/alloc/alloc_internal.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/fs/putstr_fd.h"
#include "me/mem/mem_set_zero.h" #include "me/mem/mem_set_zero.h"
#include "me/num/usize.h" #include "me/num/usize.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
void *__libc_malloc(size_t size); 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_error alloc_arena_page(t_usize min_size, t_arena_page **out)
{ {
t_arena_block *block; t_arena_block *block;
printf("Allocating page with size %zu", min_size);
min_size = usize_round_up_to(min_size, ARENA_SIZE_DEFAULT); min_size = usize_round_up_to(min_size, ARENA_SIZE_DEFAULT);
if (out == NULL) if (out == NULL)
return (ERROR); return (ERROR);
@ -48,7 +53,7 @@ t_error alloc_arena_page(t_usize min_size, t_arena_page **out)
(*out)->next = NULL; (*out)->next = NULL;
block = (t_arena_block *)((t_u8 *)*out + sizeof(**out)); block = (t_arena_block *)((t_u8 *)*out + sizeof(**out));
block->end = true; block->end = true;
block->free = true; block->used = false;
block->size = min_size - sizeof(*block); block->size = min_size - sizeof(*block);
return (NO_ERROR); 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) if (page->page_size - sizeof(t_arena_block) <= size)
return (ERROR); return (ERROR);
cur = (void *)((t_u8 *)page + sizeof(*page)); 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); leftover = cur->size - size - sizeof(*cur);
cur->size = size; cur->size = size;
next = (void *)((t_u8 *)cur + cur->size + sizeof(*cur)); next = (void *)(((t_usize)cur) + size);
next->size = leftover; next->size = leftover;
next->end = cur->end; next->end = cur->end;
next->used = false;
cur->end = false; cur->end = false;
} }
*out = cur; *out = cur;
@ -85,3 +92,21 @@ t_error get_block_for_page(t_usize size, t_arena_page *page,
} }
return (ERROR); 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; 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(); ctx.mlx = mlx_init();
if (ctx.mlx == NULL) if (ctx.mlx == NULL)
(me_eprintf("Error:\nfailed to inialize blx (mlx) !\n"), exit(1)); (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.screen);
blx_sprite_free(app._data.font); blx_sprite_free(app._data.font);
mlx_do_key_autorepeaton(app.mlx); mlx_do_key_autorepeaton(app.mlx);
if (app.free) if (app.me_free)
app.free(app.app); app.me_free(app.app);
if (app.win) if (app.win)
mlx_destroy_window(app.mlx, app.win); mlx_destroy_window(app.mlx, app.win);
if (app.mlx) if (app.mlx)
{ {
mlx_destroy_display(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_held);
vec_u8_free(app.inputs.keysyms_pressed); vec_u8_free(app.inputs.keysyms_pressed);

View file

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

View file

@ -6,25 +6,41 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 13:08:52 by maiboyer #+# #+# */ /* 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/alloc/alloc_internal.h"
#include "me/fs/putendl_fd.h"
#include "me/fs/putnbr_fd.h"
#include "me/types.h"
#include <stdlib.h> #include <stdlib.h>
void me_exit(t_i32 exit_code) void me_exit(t_i32 exit_code)
{ {
t_arena_page *arena; t_arena_page *page;
t_arena_page *tmp; t_arena_page *tmp;
t_arena_block *block;
t_usize count_block;
arena = get_head_arena(); page = get_head_arena();
while (arena) count_block = 0;
while (page)
{ {
tmp = arena->next; block = (void *)(((t_usize)page) + sizeof(*page));
free(arena); while (((t_usize)page) <= ((t_usize)block) &&
arena = tmp; ((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); exit(exit_code);
} }