diff --git a/Minishell.mk b/Minishell.mk index 74e8722e..36aa45cc 100644 --- a/Minishell.mk +++ b/Minishell.mk @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/04/28 17:28:30 by maiboyer #+# #+# # -# Updated: 2024/05/04 19:20:22 by maiboyer ### ########.fr # +# Updated: 2024/05/07 22:23:08 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -15,7 +15,7 @@ OBJDIRNAME ?= # Flags -CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD -lreadline -I./includes -I./output/include -I./stdme/output/include +CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD -lreadline -I./includes -I./output/include -I./stdme/output/include -Wl,-E # Sources LIB = ./libft/ft_bzero.c \ ./libft/ft_calloc.c \ diff --git a/parser/Makefile b/parser/Makefile index fe6a30c7..44d68de3 100644 --- a/parser/Makefile +++ b/parser/Makefile @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/05/02 15:00:49 by maiboyer ### ########.fr # +# Updated: 2024/05/07 22:23:52 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -17,9 +17,8 @@ BONUS_FLAGS = NAME = libgmr.a LIB_NAME ?= TARGET = $(BUILD_DIR)/$(NAME) -CC = cc +CC ?= cc CFLAGS = -Wall -Wextra -Werror -MMD -I./includes -I../includes -I../output/include - include ./Filelist.mk SRC_FILES += ./src/combined ./src/scanner diff --git a/stdme/Makefile b/stdme/Makefile index 91144530..e64cd8e0 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/07 13:14:26 by maiboyer ### ########.fr # +# Updated: 2024/05/07 22:24:22 by maiboyer ### ########.fr # # # # **************************************************************************** # diff --git a/stdme/include/me/alloc/alloc_internal.h b/stdme/include/me/alloc/alloc_internal.h index 3aab2240..db3127e9 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 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 -#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 */ diff --git a/stdme/src/alloc/alloc.c b/stdme/src/alloc/alloc.c index df5e6497..9422c678 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/07 14:54:56 by maiboyer ### ########.fr */ +/* Updated: 2024/05/07 22:00:20 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,29 +21,48 @@ #include #include -// 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; } diff --git a/stdme/src/alloc/get_arena.c b/stdme/src/alloc/get_arena.c index 636f6257..f396af14 100644 --- a/stdme/src/alloc/get_arena.c +++ b/stdme/src/alloc/get_arena.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 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); +} diff --git a/stdme/src/os/abort.c b/stdme/src/os/abort.c index 0ac06cf7..7d8c29c9 100644 --- a/stdme/src/os/abort.c +++ b/stdme/src/os/abort.c @@ -6,15 +6,40 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 +#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 + +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); }