diff --git a/includes/app/signal_handler.h b/includes/app/signal_handler.h index ffdd22cd..22004527 100644 --- a/includes/app/signal_handler.h +++ b/includes/app/signal_handler.h @@ -6,7 +6,7 @@ /* 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 #include "me/types.h" -#include #include #include +#include typedef t_i32 t_signal; diff --git a/parser/src/api.h b/parser/src/api.h index 9dbcc7a1..b4d4e40f 100644 --- a/parser/src/api.h +++ b/parser/src/api.h @@ -1,6 +1,7 @@ #ifndef TREE_SITTER_ARRAY_H_ #define TREE_SITTER_ARRAY_H_ +#include "me/alloc/alloc.h" #include #include #include @@ -38,6 +39,18 @@ #define STACK_VERSION_NONE ((t_stack_version)-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) \ struct \ { \ @@ -184,7 +197,6 @@ array_insert(self, _index, value); \ } while (0) - // Get a subtree's children, which are allocated immediately before the // tree's own heap data. #define ts_subtree_children(self) \ @@ -275,10 +287,10 @@ typedef struct s_parse_node typedef struct s_tree_cursor_entry { const union u_subtree *subtree; - t_length position; - uint32_t child_index; - uint32_t structural_child_index; - uint32_t descendant_index; + t_length position; + uint32_t child_index; + uint32_t structural_child_index; + uint32_t descendant_index; } t_tree_cursor_entry; typedef struct s_tree_cursor @@ -1537,8 +1549,8 @@ void ts_stack_clear(t_stack *); typedef void (*StackIterateCallback)(void *, t_state_id, uint32_t); -void ts_external_scanner_state_init(t_external_scanner_state *, const char *, - unsigned); +void ts_external_scanner_state_init(t_external_scanner_state *, const char *, + unsigned); const char *ts_external_scanner_state_data(const t_external_scanner_state *); bool ts_external_scanner_state_eq(const t_external_scanner_state *self, 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 *, bool include_all); 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); #define SUBTREE_GET(self, name) \ diff --git a/sources/ft_exit.c b/sources/ft_exit.c index 32db3d21..99fa0e96 100644 --- a/sources/ft_exit.c +++ b/sources/ft_exit.c @@ -6,7 +6,7 @@ /* 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 "me/hashmap/hashmap_env.h" +#include "me/alloc/alloc_internal.h" + void ts_parser_delete(t_first_parser *self); void ft_free(void *ptr) @@ -46,5 +48,6 @@ void ft_exit(t_utils *maiboyerlpb, t_u8 exit_status) { if (maiboyerlpb != NULL) ft_free_utils(maiboyerlpb); + print_pages_info(); me_exit(exit_status); } diff --git a/sources/main.c b/sources/main.c index 50316b21..32b4f06c 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,7 +6,7 @@ /* 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/signal_handler.h" #include "gmr/symbols.h" +#include "me/alloc/alloc.h" #include "me/string/str_len.h" #include "minishell.h" #include "parser/api.h" +#undef free +#undef malloc +#undef realloc +#undef calloc + t_first_parser *ts_parser_new(); void ts_tree_delete(t_first_tree *); t_parse_node ts_tree_root_node(t_first_tree *); diff --git a/sources/node/node.c b/sources/node/node.c index 4c1d1607..072ade1b 100644 --- a/sources/node/node.c +++ b/sources/node/node.c @@ -6,7 +6,7 @@ /* 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/string/str_l_copy.h" #include "parser/api.h" +#include -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_symbol ts_node_symbol(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_end_byte(t_parse_node self); -t_u32 ts_node_child_count(t_parse_node self); +t_symbol ts_node_symbol(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_end_byte(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 *ret; - t_usize idx; - t_parse_node child; + t_node *ret; + t_usize idx; + t_parse_node child; ret = mem_alloc_array(sizeof(*ret), count); if (ret == NULL) diff --git a/sources/signal_handler.c b/sources/signal_handler.c index 2c782ae9..1a9a08db 100644 --- a/sources/signal_handler.c +++ b/sources/signal_handler.c @@ -6,16 +6,16 @@ /* 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 "me/printf/printf.h" #include "me/types.h" #include "readline/readline.h" -#include "me/printf/printf.h" -#include #include +#include 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)(info); (void)(ucontext); - me_eprintf("SEGFAULT!!!\n"); + me_eprintf("Abort: SEGFAULT!!!\n"); print_trace(); exit(139); } @@ -74,9 +74,9 @@ t_error install_signal(void) data.sa_sigaction = sigquit_handle; if (sigaction(SIGQUIT, &data, NULL)) return (ERROR); + data.sa_sigaction = sigsegv_handle; if (sigaction(SIGSEGV, &data, NULL)) return (ERROR); - return (NO_ERROR); } diff --git a/stdme/Makefile b/stdme/Makefile index c408f8ec..15be81f8 100644 --- a/stdme/Makefile +++ b/stdme/Makefile @@ -6,7 +6,7 @@ # 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_RESET = \033[0m +# TODO: REMOVE FOR RENDU !!!!! +CFLAGS += -DPRINT_BACKTRACE + + .PHONY = all bonus clean re subject all: $(NAME) diff --git a/stdme/include/me/alloc/alloc_internal.h b/stdme/include/me/alloc/alloc_internal.h index db3127e9..1efaa896 100644 --- a/stdme/include/me/alloc/alloc_internal.h +++ b/stdme/include/me/alloc/alloc_internal.h @@ -6,7 +6,7 @@ /* 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 -#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 */ diff --git a/stdme/include/me/blx/blx.h b/stdme/include/me/blx/blx.h index c64dc94f..9d499a3b 100644 --- a/stdme/include/me/blx/blx.h +++ b/stdme/include/me/blx/blx.h @@ -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; diff --git a/stdme/include/me/img/qoi.h b/stdme/include/me/img/qoi.h index d679be67..3c7545a3 100644 --- a/stdme/include/me/img/qoi.h +++ b/stdme/include/me/img/qoi.h @@ -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); diff --git a/stdme/include/me/types.h b/stdme/include/me/types.h index d8b0c419..210ce6b1 100644 --- a/stdme/include/me/types.h +++ b/stdme/include/me/types.h @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/stdme/src.list b/stdme/src.list index 54a82a24..f9b4794a 100644 --- a/stdme/src.list +++ b/stdme/src.list @@ -1,4 +1,5 @@ alloc/alloc +alloc/alloc_get_page_from_bloc alloc/get_arena blx/blx blx/blx_create_fontsheet diff --git a/stdme/src/alloc/alloc.c b/stdme/src/alloc/alloc.c index ddfcf08c..bcb41058 100644 --- a/stdme/src/alloc/alloc.c +++ b/stdme/src/alloc/alloc.c @@ -6,7 +6,7 @@ /* 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 #include #include +#include #include 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); + } } diff --git a/stdme/src/alloc/alloc_get_page_from_bloc.c b/stdme/src/alloc/alloc_get_page_from_bloc.c new file mode 100644 index 00000000..0e976e39 --- /dev/null +++ b/stdme/src/alloc/alloc_get_page_from_bloc.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* alloc_get_page_from_bloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/stdme/src/alloc/get_arena.c b/stdme/src/alloc/get_arena.c index f396af14..705db40f 100644 --- a/stdme/src/alloc/get_arena.c +++ b/stdme/src/alloc/get_arena.c @@ -6,15 +6,18 @@ /* 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_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 #include 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; + } +} diff --git a/stdme/src/blx/blx.c b/stdme/src/blx/blx.c index d727a175..be972204 100644 --- a/stdme/src/blx/blx.c +++ b/stdme/src/blx/blx.c @@ -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); diff --git a/stdme/src/os/abort.c b/stdme/src/os/abort.c index 470b1220..5e34afd6 100644 --- a/stdme/src/os/abort.c +++ b/stdme/src/os/abort.c @@ -6,7 +6,7 @@ /* 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 #include #include +#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(); diff --git a/stdme/src/os/exit.c b/stdme/src/os/exit.c index 44d97af3..63d6b7e2 100644 --- a/stdme/src/os/exit.c +++ b/stdme/src/os/exit.c @@ -6,25 +6,41 @@ /* 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/fs/putendl_fd.h" +#include "me/fs/putnbr_fd.h" +#include "me/types.h" #include 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); }