From 941bac31b6063c04869a87736b4e0fc442a0c04f Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Tue, 7 May 2024 15:21:41 +0200 Subject: [PATCH] Made a memory allocator (crude) --- Makefile | 2 +- output/src/hashmap/env/env.c | 8 +- output/src/hashmap/env/env_utils.c | 2 +- .../src/vec/parser_heredoc/parser_heredoc.c | 6 +- output/src/vec/parser_range/parser_range.c | 6 +- output/src/vec/reduce_action/reduce_action.c | 6 +- sources/exec/handle_concat.c | 4 +- sources/exec/handle_expension.c | 4 +- sources/ft_exit.c | 6 +- sources/main.c | 4 +- sources/node/node.c | 19 ++-- stdme/Makefile | 18 ++-- .../src/hashmap/C__PREFIX__.c__TEMPLATE__ | 8 +- .../hashmap/C__PREFIX___utils.c__TEMPLATE__ | 2 +- .../src/vec/C__PREFIX__.c__TEMPLATE__ | 6 +- stdme/include/me/alloc/alloc.h | 22 +++++ stdme/include/me/alloc/alloc_internal.h | 34 +++++++ stdme/include/me/buffered_str/buf_str.h | 4 +- stdme/include/me/mem/mem_realloc.h | 20 +++++ stdme/include/me/num/usize.h | 3 +- stdme/include/me/quit.h | 20 +++++ stdme/include/me/types.h | 59 ++++++------ stdme/output/src/vec/buf_str/buf_str.c | 6 +- stdme/output/src/vec/str/str.c | 6 +- stdme/output/src/vec/u8/u8.c | 6 +- stdme/output/src/vec/vec_buf_str.c | 6 +- stdme/output/src/vec/vec_str.c | 6 +- stdme/output/src/vec/vec_u8.c | 6 +- stdme/src.list | 6 ++ stdme/src/alloc/alloc.c | 89 +++++++++++++++++++ stdme/src/alloc/get_arena.c | 45 ++++++++++ stdme/src/buffered_str/mod.c | 12 +-- stdme/src/gnl/get_next_line.c | 6 +- stdme/src/hash/sip/sip_utils.c | 2 +- stdme/src/img/qoi/qoi_fs.c | 2 +- stdme/src/list/list_free_all.c | 2 +- stdme/src/list/list_free_one.c | 2 +- stdme/src/mem/mem_alloc.c | 13 +-- stdme/src/mem/mem_alloc_array.c | 11 +-- stdme/src/mem/mem_realloc.c | 19 ++++ stdme/src/num/usize/round_up.c | 25 ++++++ stdme/src/os/abort.c | 20 +++++ stdme/src/os/exit.c | 30 +++++++ stdme/src/os/process.c | 8 +- stdme/src/printf/formatter/char.c | 2 +- stdme/src/printf/formatter/decimal.c | 2 +- stdme/src/printf/formatter/unsigned_decimal.c | 2 +- stdme/src/printf/formatter/utils.c | 2 +- stdme/src/printf/formatter/utils_numbers.c | 2 +- stdme/src/printf/printf.c | 4 +- stdme/src/string/str_l_cat.c | 4 +- stdme/src/string/str_l_copy.c | 4 +- stdme/src/string/str_split.c | 2 +- 53 files changed, 469 insertions(+), 146 deletions(-) create mode 100644 stdme/include/me/alloc/alloc.h create mode 100644 stdme/include/me/alloc/alloc_internal.h create mode 100644 stdme/include/me/mem/mem_realloc.h create mode 100644 stdme/include/me/quit.h create mode 100644 stdme/src/alloc/alloc.c create mode 100644 stdme/src/alloc/get_arena.c create mode 100644 stdme/src/mem/mem_realloc.c create mode 100644 stdme/src/num/usize/round_up.c create mode 100644 stdme/src/os/abort.c create mode 100644 stdme/src/os/exit.c diff --git a/Makefile b/Makefile index f6b6c6da..0e7bed9e 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/04/29 13:58:38 by maiboyer ### ########.fr # +# Updated: 2024/05/07 13:14:07 by maiboyer ### ########.fr # # # # **************************************************************************** # diff --git a/output/src/hashmap/env/env.c b/output/src/hashmap/env/env.c index ead07456..ff35c494 100644 --- a/output/src/hashmap/env/env.c +++ b/output/src/hashmap/env/env.c @@ -43,7 +43,7 @@ t_hashmap_env *new_hashmap_with_buckets_env( hmap->cfunc = cfunc; hmap->drop = drop; if (hmap->buckets == NULL) - return ((void)free(hmap), NULL); + return ((void)me_free(hmap), NULL); return (hmap); } @@ -57,13 +57,13 @@ void drop_hashmap_env(t_hashmap_env *hmap) if (hmap->buckets[index]) { hmap->drop(hmap->buckets[index]->kv); - free(hmap->buckets[index]); + me_free(hmap->buckets[index]); } index++; } hasher_finish(&hmap->hasher); - free(hmap->buckets); - free(hmap); + me_free(hmap->buckets); + me_free(hmap); } t_entry_env *hashmap_get_entry_env(t_hashmap_env *hmap, diff --git a/output/src/hashmap/env/env_utils.c b/output/src/hashmap/env/env_utils.c index 2cde33a1..4fdd75f1 100644 --- a/output/src/hashmap/env/env_utils.c +++ b/output/src/hashmap/env/env_utils.c @@ -51,6 +51,6 @@ void remove_hashmap_env(t_hashmap_env *hmap, t_str *key) else prev->next = entry->next; hmap->drop(entry->kv); - free(entry); + me_free(entry); hmap->buckets[hashed_key % hmap->num_buckets] = NULL; } diff --git a/output/src/vec/parser_heredoc/parser_heredoc.c b/output/src/vec/parser_heredoc/parser_heredoc.c index 34db7c57..30910d31 100644 --- a/output/src/vec/parser_heredoc/parser_heredoc.c +++ b/output/src/vec/parser_heredoc/parser_heredoc.c @@ -47,7 +47,7 @@ t_error vec_parser_heredoc_push(t_vec_parser_heredoc *vec, t_heredoc element) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_heredoc)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -73,7 +73,7 @@ t_error vec_parser_heredoc_reserve(t_vec_parser_heredoc *vec, t_usize wanted_cap if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_heredoc)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -111,5 +111,5 @@ void vec_parser_heredoc_free(t_vec_parser_heredoc vec) vec.len--; } } - free(vec.buffer); + me_free(vec.buffer); } diff --git a/output/src/vec/parser_range/parser_range.c b/output/src/vec/parser_range/parser_range.c index b3c18e5b..23c66967 100644 --- a/output/src/vec/parser_range/parser_range.c +++ b/output/src/vec/parser_range/parser_range.c @@ -47,7 +47,7 @@ t_error vec_parser_range_push(t_vec_parser_range *vec, t_parser_range element) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_parser_range)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -73,7 +73,7 @@ t_error vec_parser_range_reserve(t_vec_parser_range *vec, t_usize wanted_capacit if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_parser_range)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -111,5 +111,5 @@ void vec_parser_range_free(t_vec_parser_range vec) vec.len--; } } - free(vec.buffer); + me_free(vec.buffer); } diff --git a/output/src/vec/reduce_action/reduce_action.c b/output/src/vec/reduce_action/reduce_action.c index cfbe90d4..625fdd14 100644 --- a/output/src/vec/reduce_action/reduce_action.c +++ b/output/src/vec/reduce_action/reduce_action.c @@ -47,7 +47,7 @@ t_error vec_reduce_action_push(t_vec_reduce_action *vec, t_reduce_action element if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_reduce_action)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -73,7 +73,7 @@ t_error vec_reduce_action_reserve(t_vec_reduce_action *vec, t_usize wanted_capac if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_reduce_action)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -111,5 +111,5 @@ void vec_reduce_action_free(t_vec_reduce_action vec) vec.len--; } } - free(vec.buffer); + me_free(vec.buffer); } diff --git a/sources/exec/handle_concat.c b/sources/exec/handle_concat.c index ce4d5f1b..21beb460 100644 --- a/sources/exec/handle_concat.c +++ b/sources/exec/handle_concat.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/02 15:05:06 by maiboyer #+# #+# */ -/* Updated: 2024/05/02 16:01:52 by maiboyer ### ########.fr */ +/* Updated: 2024/05/07 14:59:27 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,7 @@ t_error handle_concat(t_node *self, t_utils *shcat, t_str *ret) if (node_get_string(&self->childs[i], shcat, &tmp)) return (str_free(out), ERROR); push_str_buffer(&out, tmp); - free(tmp); + me_free(tmp); i++; } *ret = out.buf; diff --git a/sources/exec/handle_expension.c b/sources/exec/handle_expension.c index 89b40872..b1ef8592 100644 --- a/sources/exec/handle_expension.c +++ b/sources/exec/handle_expension.c @@ -6,12 +6,13 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/03 15:24:25 by maiboyer #+# #+# */ -/* Updated: 2024/05/04 18:28:39 by maiboyer ### ########.fr */ +/* Updated: 2024/05/07 14:55:25 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "app/node/handle_expension.h" #include "gmr/symbols.h" +#include "me/hashmap/hashmap_env.h" #include "me/types.h" t_error handle_expension_complex(t_node *self, t_utils *shcat, t_str *ret) @@ -26,6 +27,7 @@ t_error handle_expension_simple(t_node *self, t_utils *shcat, t_str *ret) (void)(self); (void)(shcat); (void)(ret); + //get_hashmap_env(shcat->env, (t_str *)&"fjdksf"); return (ERROR); } diff --git a/sources/ft_exit.c b/sources/ft_exit.c index 51b574cc..32db3d21 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/04 19:24:44 by maiboyer ### ########.fr */ +/* Updated: 2024/05/07 13:00:21 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,8 +39,6 @@ void ft_free_utils(t_utils *s) free(s->str_input); if (s->path) ft_free_strs(s->path); - if (s->env) - drop_hashmap_env(s->env); ts_parser_delete(s->parser.parser); } @@ -48,5 +46,5 @@ void ft_exit(t_utils *maiboyerlpb, t_u8 exit_status) { if (maiboyerlpb != NULL) ft_free_utils(maiboyerlpb); - exit(exit_status); + me_exit(exit_status); } diff --git a/sources/main.c b/sources/main.c index 85005b36..dd0a0cac 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/04 19:21:16 by maiboyer ### ########.fr */ +/* Updated: 2024/05/07 13:08:24 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ void print_node_data(t_node *t, t_usize depth) while (idx++ < depth) printf("\t"); idx = 0; - printf("%s(%llu) = %s\n", t->kind_str, t->kind, node_getstr(t)); + printf("%s(%lu) = %s\n", t->kind_str, t->kind, node_getstr(t)); while (idx < t->childs_count) print_node_data(&t->childs[idx++], depth + 1); } diff --git a/sources/node/node.c b/sources/node/node.c index bf6817fc..4c1d1607 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/04/30 16:43:35 by maiboyer ### ########.fr */ +/* Updated: 2024/05/07 12:51:18 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -82,12 +82,13 @@ t_str node_getstr(t_node *node) void free_node(t_node t) { - t_usize idx; - - idx = 0; - while (idx < t.childs_count) - free_node(t.childs[idx++]); - free(t.childs); - if (t.single_str != NULL) - free(t.single_str); + // t_usize idx; + // + // idx = 0; + // while (idx < t.childs_count) + // free_node(t.childs[idx++]); + // free(t.childs); + // if (t.single_str != NULL) + // free(t.single_str); + (void)(t); } diff --git a/stdme/Makefile b/stdme/Makefile index 983d0811..91144530 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/01 20:31:58 by maiboyer ### ########.fr # +# Updated: 2024/05/07 13:14:26 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -39,11 +39,11 @@ DEPS = $(addsuffix .d,$(addprefix $(BUILD_DIR)/,$(SRC_FILES))) \ $(addsuffix .d,$(addprefix $(BUILD_DIR)/,$(GENERIC_FILES))) LIBS = $(addprefix $(LIBS_DIR)/,$(LIBS_NAME)) INCLUDES = $(addprefix -I,$(foreach P,$(INCLUDE_DIR) $(GENERIC_INCLUDE) $(LIBS) $(addsuffix /include,$(LIBS)) vendor $(addsuffix /vendor,$(LIBS)),$(realpath $(P)))) -COL_GRAY = \\e[90m -COL_WHITE = \\e[37m -COL_GREEN = \\e[32m -COL_BOLD = \\e[1m -COL_RESET = \\e[0m +COL_GRAY = \033[90m +COL_WHITE = \033[37m +COL_GREEN = \033[32m +COL_BOLD = \033[1m +COL_RESET = \033[0m .PHONY = all bonus clean re subject @@ -52,18 +52,18 @@ all: $(NAME) $(NAME): $(TARGET) $(TARGET): $(OBJ) - @echo -e "$(COL_GRAY) Linking\t$(COL_GREEN)$(TARGET)$(COL_RESET)" + @echo -e '$(COL_GRAY) Linking\t$(COL_GREEN)$(TARGET)$(COL_RESET)' @#$(CC) $(INCLUDES) $(OBJ) $(CFLAGS) -o $(NAME) @ar rcs $(BUILD_DIR)/$(NAME) $(OBJ) $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c @mkdir -p $(dir $@) - @echo -e "$(COL_GRAY) Building\t$(COL_GREEN)$<$(COL_RESET)" + @echo -e '$(COL_GRAY) Building\t$(COL_GREEN)$<$(COL_RESET)' @$(CC) $(CFLAGS) $(WERROR) $(INCLUDES) -c $< -o $@ $(BUILD_DIR)/%.o: $(GENERIC_DIR)/%.c @mkdir -p $(dir $@) - @echo -e "$(COL_GRAY) Building\t$(COL_GREEN)$<$(COL_RESET)" + @echo -e '$(COL_GRAY) Building\t$(COL_GREEN)$<$(COL_RESET)' @$(CC) $(CFLAGS) $(WERROR) $(INCLUDES) -c $< -o $@ clean: diff --git a/stdme/generic_sources/src/hashmap/C__PREFIX__.c__TEMPLATE__ b/stdme/generic_sources/src/hashmap/C__PREFIX__.c__TEMPLATE__ index d1beb3f6..748467d4 100644 --- a/stdme/generic_sources/src/hashmap/C__PREFIX__.c__TEMPLATE__ +++ b/stdme/generic_sources/src/hashmap/C__PREFIX__.c__TEMPLATE__ @@ -43,7 +43,7 @@ t_hashmap_C__PREFIX__ *new_hashmap_with_buckets_C__PREFIX__( hmap->cfunc = cfunc; hmap->drop = drop; if (hmap->buckets == NULL) - return ((void)free(hmap), NULL); + return ((void)me_free(hmap), NULL); return (hmap); } @@ -57,13 +57,13 @@ void drop_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap) if (hmap->buckets[index]) { hmap->drop(hmap->buckets[index]->kv); - free(hmap->buckets[index]); + me_free(hmap->buckets[index]); } index++; } hasher_finish(&hmap->hasher); - free(hmap->buckets); - free(hmap); + me_free(hmap->buckets); + me_free(hmap); } t_entry_C__PREFIX__ *hashmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, diff --git a/stdme/generic_sources/src/hashmap/C__PREFIX___utils.c__TEMPLATE__ b/stdme/generic_sources/src/hashmap/C__PREFIX___utils.c__TEMPLATE__ index 00e4e2de..ff239746 100644 --- a/stdme/generic_sources/src/hashmap/C__PREFIX___utils.c__TEMPLATE__ +++ b/stdme/generic_sources/src/hashmap/C__PREFIX___utils.c__TEMPLATE__ @@ -51,6 +51,6 @@ void remove_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key) else prev->next = entry->next; hmap->drop(entry->kv); - free(entry); + me_free(entry); hmap->buckets[hashed_key % hmap->num_buckets] = NULL; } diff --git a/stdme/generic_sources/src/vec/C__PREFIX__.c__TEMPLATE__ b/stdme/generic_sources/src/vec/C__PREFIX__.c__TEMPLATE__ index 662a9659..25aa4341 100644 --- a/stdme/generic_sources/src/vec/C__PREFIX__.c__TEMPLATE__ +++ b/stdme/generic_sources/src/vec/C__PREFIX__.c__TEMPLATE__ @@ -47,7 +47,7 @@ t_error vec_C__PREFIX___push(t_vec_C__PREFIX__ *vec, C__TYPENAME__ element) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(C__TYPENAME__)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -73,7 +73,7 @@ t_error vec_C__PREFIX___reserve(t_vec_C__PREFIX__ *vec, t_usize wanted_capacity) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(C__TYPENAME__)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -111,5 +111,5 @@ void vec_C__PREFIX___free(t_vec_C__PREFIX__ vec) vec.len--; } } - free(vec.buffer); + me_free(vec.buffer); } diff --git a/stdme/include/me/alloc/alloc.h b/stdme/include/me/alloc/alloc.h new file mode 100644 index 00000000..1a1ccec4 --- /dev/null +++ b/stdme/include/me/alloc/alloc.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* alloc.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/07 09:42:02 by maiboyer #+# #+# */ +/* Updated: 2024/05/07 09:43:31 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ALLOC_H +#define ALLOC_H + +#include "me/types.h" + +void *me_malloc(t_usize size); +void *me_calloc(t_usize elem_count, t_usize elem_size); +void *me_realloc(void *ptr, t_usize size); + +#endif /* ALLOC_H */ diff --git a/stdme/include/me/alloc/alloc_internal.h b/stdme/include/me/alloc/alloc_internal.h new file mode 100644 index 00000000..3aab2240 --- /dev/null +++ b/stdme/include/me/alloc/alloc_internal.h @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* alloc_internal.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/07 09:48:17 by maiboyer #+# #+# */ +/* Updated: 2024/05/07 10:14:16 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ALLOC_INTERNAL_H +#define ALLOC_INTERNAL_H + +#include "me/alloc/alloc.h" +#include "me/types.h" + +#define ARENA_SIZE 16384 + +typedef struct s_arena_page +{ + t_u8 bytes[ARENA_SIZE]; + t_usize current_index; + struct s_arena_page *next; +} t_arena_page; + +// Will never be null, as it will allocate a new arena if it needs to do so +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); + +#endif /* ALLOC_INTERNAL_H */ diff --git a/stdme/include/me/buffered_str/buf_str.h b/stdme/include/me/buffered_str/buf_str.h index 568b1975..65ec5735 100644 --- a/stdme/include/me/buffered_str/buf_str.h +++ b/stdme/include/me/buffered_str/buf_str.h @@ -29,9 +29,9 @@ t_error str_reserve(t_buffer_str *buf, t_usize size); static inline void str_free(t_buffer_str buf) { - void free(void *); + void me_free(void *); - free(buf.buf); + me_free(buf.buf); } static inline char str_pop(t_buffer_str *buf) diff --git a/stdme/include/me/mem/mem_realloc.h b/stdme/include/me/mem/mem_realloc.h new file mode 100644 index 00000000..22057951 --- /dev/null +++ b/stdme/include/me/mem/mem_realloc.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_realloc.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/07 12:47:23 by maiboyer #+# #+# */ +/* Updated: 2024/05/07 12:47:42 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEM_REALLOC_H +#define MEM_REALLOC_H + +#include "me/types.h" + +void *mem_realloc(void *ptr, t_usize size); + +#endif /* MEM_REALLOC_H */ diff --git a/stdme/include/me/num/usize.h b/stdme/include/me/num/usize.h index 2c9cc6b0..b40ecef3 100644 --- a/stdme/include/me/num/usize.h +++ b/stdme/include/me/num/usize.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/11 14:10:03 by maiboyer #+# #+# */ -/* Updated: 2023/12/11 14:17:32 by maiboyer ### ########.fr */ +/* Updated: 2024/05/07 10:45:15 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,5 +17,6 @@ t_usize usize_rotate_left(t_usize n, t_usize by); t_usize usize_rotate_right(t_usize n, t_usize by); +t_usize usize_round_up_to(t_usize self, t_usize mul); #endif diff --git a/stdme/include/me/quit.h b/stdme/include/me/quit.h new file mode 100644 index 00000000..93dd8df2 --- /dev/null +++ b/stdme/include/me/quit.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* quit.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/07 12:54:50 by maiboyer #+# #+# */ +/* Updated: 2024/05/07 12:55:08 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef QUIT_H +#define QUIT_H + +#include "me/types.h" + +void quit(t_i32 exit_code); + +#endif /* QUIT_H */ diff --git a/stdme/include/me/types.h b/stdme/include/me/types.h index 55a0360c..6af62e3e 100644 --- a/stdme/include/me/types.h +++ b/stdme/include/me/types.h @@ -6,44 +6,51 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/03 14:31:12 by maiboyer #+# #+# */ -/* Updated: 2024/01/05 00:08:41 by maiboyer ### ########.fr */ +/* Updated: 2024/05/07 14:52:32 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef TYPES_H -# define TYPES_H +#define TYPES_H -# include -# include -# include +#include +#include +#include +#include -typedef char *t_str; -typedef const char *t_const_str; +typedef char *t_str; +typedef const char *t_const_str; -typedef unsigned char t_u8; -typedef char t_i8; -typedef unsigned short t_u16; -typedef short t_i16; -typedef int t_i32; -typedef unsigned int t_u32; -typedef unsigned long long t_u64; -typedef long long t_i64; -typedef ssize_t t_isize; -typedef size_t t_usize; +typedef uint8_t t_u8; +typedef int8_t t_i8; +typedef uint16_t t_u16; +typedef int16_t t_i16; +typedef uint32_t t_u32; +typedef int32_t t_i32; +typedef uint64_t t_u64; +typedef int64_t t_i64; +typedef ssize_t t_isize; +typedef size_t t_usize; -typedef float t_f32; -typedef double t_f64; +typedef float t_f32; +typedef double t_f64; -typedef int t_file; +typedef int t_file; typedef struct s_list { - void *content; - struct s_list *next; -} t_list; + void *content; + struct s_list *next; +} t_list; -typedef bool t_error; +typedef bool t_error; -# define ERROR 1 -# define NO_ERROR 0 +void me_abort(void); +void me_exit(t_i32 code); +void me_free(void *ptr); + +#define ERROR 1 +#define NO_ERROR 0 + +#define __NonNullable #endif diff --git a/stdme/output/src/vec/buf_str/buf_str.c b/stdme/output/src/vec/buf_str/buf_str.c index 8dd32251..f2adf089 100644 --- a/stdme/output/src/vec/buf_str/buf_str.c +++ b/stdme/output/src/vec/buf_str/buf_str.c @@ -47,7 +47,7 @@ t_error vec_buf_str_push(t_vec_buf_str *vec, t_buffer_str element) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_buffer_str)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -73,7 +73,7 @@ t_error vec_buf_str_reserve(t_vec_buf_str *vec, t_usize wanted_capacity) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_buffer_str)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -111,5 +111,5 @@ void vec_buf_str_free(t_vec_buf_str vec) vec.len--; } } - free(vec.buffer); + me_free(vec.buffer); } diff --git a/stdme/output/src/vec/str/str.c b/stdme/output/src/vec/str/str.c index 5b377023..e060e53b 100644 --- a/stdme/output/src/vec/str/str.c +++ b/stdme/output/src/vec/str/str.c @@ -47,7 +47,7 @@ t_error vec_str_push(t_vec_str *vec, t_str element) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_str)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -73,7 +73,7 @@ t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_str)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -111,5 +111,5 @@ void vec_str_free(t_vec_str vec) vec.len--; } } - free(vec.buffer); + me_free(vec.buffer); } diff --git a/stdme/output/src/vec/u8/u8.c b/stdme/output/src/vec/u8/u8.c index 8a4bfc5b..b6cd2d2c 100644 --- a/stdme/output/src/vec/u8/u8.c +++ b/stdme/output/src/vec/u8/u8.c @@ -47,7 +47,7 @@ t_error vec_u8_push(t_vec_u8 *vec, t_u8 element) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_u8)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -73,7 +73,7 @@ t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_u8)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -111,5 +111,5 @@ void vec_u8_free(t_vec_u8 vec) vec.len--; } } - free(vec.buffer); + me_free(vec.buffer); } diff --git a/stdme/output/src/vec/vec_buf_str.c b/stdme/output/src/vec/vec_buf_str.c index 8dd32251..f2adf089 100644 --- a/stdme/output/src/vec/vec_buf_str.c +++ b/stdme/output/src/vec/vec_buf_str.c @@ -47,7 +47,7 @@ t_error vec_buf_str_push(t_vec_buf_str *vec, t_buffer_str element) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_buffer_str)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -73,7 +73,7 @@ t_error vec_buf_str_reserve(t_vec_buf_str *vec, t_usize wanted_capacity) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_buffer_str)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -111,5 +111,5 @@ void vec_buf_str_free(t_vec_buf_str vec) vec.len--; } } - free(vec.buffer); + me_free(vec.buffer); } diff --git a/stdme/output/src/vec/vec_str.c b/stdme/output/src/vec/vec_str.c index 5b377023..e060e53b 100644 --- a/stdme/output/src/vec/vec_str.c +++ b/stdme/output/src/vec/vec_str.c @@ -47,7 +47,7 @@ t_error vec_str_push(t_vec_str *vec, t_str element) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_str)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -73,7 +73,7 @@ t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_str)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -111,5 +111,5 @@ void vec_str_free(t_vec_str vec) vec.len--; } } - free(vec.buffer); + me_free(vec.buffer); } diff --git a/stdme/output/src/vec/vec_u8.c b/stdme/output/src/vec/vec_u8.c index 8a4bfc5b..b6cd2d2c 100644 --- a/stdme/output/src/vec/vec_u8.c +++ b/stdme/output/src/vec/vec_u8.c @@ -47,7 +47,7 @@ t_error vec_u8_push(t_vec_u8 *vec, t_u8 element) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_u8)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -73,7 +73,7 @@ t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity) if (temp_buffer == NULL) return (ERROR); mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_u8)); - free(vec->buffer); + me_free(vec->buffer); vec->buffer = temp_buffer; vec->capacity = new_capacity; } @@ -111,5 +111,5 @@ void vec_u8_free(t_vec_u8 vec) vec.len--; } } - free(vec.buffer); + me_free(vec.buffer); } diff --git a/stdme/src.list b/stdme/src.list index fdb253e8..54a82a24 100644 --- a/stdme/src.list +++ b/stdme/src.list @@ -1,3 +1,5 @@ +alloc/alloc +alloc/get_arena blx/blx blx/blx_create_fontsheet blx/blx_handlers @@ -66,6 +68,7 @@ mem/mem_copy mem/mem_find mem/mem_find_bytes mem/mem_move +mem/mem_realloc mem/mem_set mem/mem_set_zero num/u16/rotate @@ -74,6 +77,9 @@ num/u64/from_bytes num/u64/rotate num/u8/rotate num/usize/rotate +num/usize/round_up +os/abort +os/exit os/pipe os/process os/process_inner diff --git a/stdme/src/alloc/alloc.c b/stdme/src/alloc/alloc.c new file mode 100644 index 00000000..df5e6497 --- /dev/null +++ b/stdme/src/alloc/alloc.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* alloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/07 10:13:06 by maiboyer #+# #+# */ +/* Updated: 2024/05/07 14:54:56 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/alloc/alloc.h" +#include "me/alloc/alloc_internal.h" +#include "me/fs/putstr_fd.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/num/usize.h" +#include +#include +#include +#include + +// the `+ 16` 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; + + 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); +} + +void *me_calloc(t_usize elem_size, t_usize elem_count) +{ + if (elem_size != 0 && elem_count > SIZE_MAX / elem_size) + return (NULL); + return (me_malloc(elem_size * elem_count)); +} + +void *me_realloc(void *ptr, t_usize new_size) +{ + t_arena_page *arena; + t_usize size; + void *ret; + + arena = get_head_arena(); + while (arena != NULL && !((void *)&arena->bytes <= ptr && + ptr <= (void *)(&arena->bytes) + ARENA_SIZE)) + arena = arena->next; + if (arena == NULL) + return (NULL); + size = *(t_usize *)((t_u8 *)(ptr)-16); + if (size <= new_size) + return (ptr); + ret = me_malloc(new_size); + mem_copy(ret, ptr, 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); + } +} diff --git a/stdme/src/alloc/get_arena.c b/stdme/src/alloc/get_arena.c new file mode 100644 index 00000000..636f6257 --- /dev/null +++ b/stdme/src/alloc/get_arena.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_arena.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/07 09:47:50 by maiboyer #+# #+# */ +/* Updated: 2024/05/07 14:20:20 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/alloc/alloc.h" +#include "me/alloc/alloc_internal.h" +#include "me/fs/putstr_fd.h" +#include "me/mem/mem_set_zero.h" +#include + +void *__libc_malloc(size_t size); + +t_arena_page *get_head_arena(void) +{ + static t_arena_page *val = NULL; + + if (val == NULL) + { + if (alloc_arena(&val)) + { + me_putstr_fd("Error: malloc failed\n", 2); + exit(1); + } + } + return (val); +} + +t_error alloc_arena(t_arena_page **out) +{ + if (out == NULL) + return (ERROR); + *out = __libc_malloc(sizeof(**out)); + if (*out == NULL) + return (ERROR); + mem_set_zero(*out, sizeof(**out)); + return (NO_ERROR); +} diff --git a/stdme/src/buffered_str/mod.c b/stdme/src/buffered_str/mod.c index deb4ff8c..3d335356 100644 --- a/stdme/src/buffered_str/mod.c +++ b/stdme/src/buffered_str/mod.c @@ -6,15 +6,15 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/16 17:52:12 by maiboyer #+# #+# */ -/* Updated: 2024/04/30 14:14:03 by maiboyer ### ########.fr */ +/* Updated: 2024/05/07 15:04:07 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "me/buffered_str/buf_str.h" #include "me/mem/mem_alloc.h" +#include "me/mem/mem_realloc.h" #include "me/mem/mem_set_zero.h" #include "me/string/str_l_cat.h" -#include "me/string/str_l_copy.h" #include "me/string/str_len.h" #include "me/types.h" #include @@ -29,11 +29,9 @@ t_error str_reserve(t_buffer_str *buf, t_usize size) while (size > buf->capacity) { new_capacity = (buf->capacity * 3) / 2 + 1; - temp_buffer = mem_alloc(new_capacity); + temp_buffer = mem_realloc(buf->buf, new_capacity); if (temp_buffer == NULL) return (true); - str_l_copy(temp_buffer, buf->buf, new_capacity); - free(buf->buf); buf->buf = temp_buffer; buf->capacity = new_capacity; } @@ -52,11 +50,9 @@ bool push_str_buffer(t_buffer_str *buf, t_const_str to_push) while (buf->len + to_push_len + 2 > buf->capacity) { new_capacity = (buf->capacity * 3) / 2 + 1; - temp_buffer = mem_alloc(new_capacity); + temp_buffer = mem_realloc(buf->buf, new_capacity); if (temp_buffer == NULL) return (true); - str_l_copy(temp_buffer, buf->buf, new_capacity); - free(buf->buf); buf->buf = temp_buffer; buf->capacity = new_capacity; } diff --git a/stdme/src/gnl/get_next_line.c b/stdme/src/gnl/get_next_line.c index 74ec66fa..8c2e23bd 100644 --- a/stdme/src/gnl/get_next_line.c +++ b/stdme/src/gnl/get_next_line.c @@ -92,7 +92,7 @@ static bool handle_leftovers(t_file fd, char *temp_buffer, t_buffer_str *buf) if (copy_next_line_into_buffer(fd, buf, temp_buffer, str_len(static_buffer->buf))) { - free(temp_buffer); + me_free(temp_buffer); return (true); } } @@ -118,10 +118,10 @@ t_buffer_str get_next_line(t_file fd, bool *error) return (buf); while (!read_and_copy(fd, &buf, temp_buffer, &flags) && !flags.empty_read) ; - free(temp_buffer); + me_free(temp_buffer); if (flags.error || flags.empty_read) { - free(buf.buf); + me_free(buf.buf); return (*error = true, (t_buffer_str){0}); } return (buf); diff --git a/stdme/src/hash/sip/sip_utils.c b/stdme/src/hash/sip/sip_utils.c index aed4a334..a2ab9578 100644 --- a/stdme/src/hash/sip/sip_utils.c +++ b/stdme/src/hash/sip/sip_utils.c @@ -101,6 +101,6 @@ t_u64 sip13_finish(t_sip13 *self) state.v2 ^= 0xff; compress(&state); compress(&state); - free(self); + me_free(self); return (state.v0 ^ state.v1 ^ state.v2 ^ state.v3); } diff --git a/stdme/src/img/qoi/qoi_fs.c b/stdme/src/img/qoi/qoi_fs.c index 9be0545a..6097d040 100644 --- a/stdme/src/img/qoi/qoi_fs.c +++ b/stdme/src/img/qoi/qoi_fs.c @@ -32,7 +32,7 @@ t_i32 qoi_write(t_const_str filename, const void *data, if (me_write(f, encoded, size)) return (me_close(f, NULL), 0); me_close(f, NULL); - free(encoded); + me_free(encoded); return (size); } diff --git a/stdme/src/list/list_free_all.c b/stdme/src/list/list_free_all.c index af358b99..6d9a8ffd 100644 --- a/stdme/src/list/list_free_all.c +++ b/stdme/src/list/list_free_all.c @@ -24,7 +24,7 @@ void list_free_all(t_list **lst, void (*del)(void *)) del((*lst)->content); tmp = *lst; *lst = (*lst)->next; - free(tmp); + me_free(tmp); } *lst = NULL; } diff --git a/stdme/src/list/list_free_one.c b/stdme/src/list/list_free_one.c index df442990..b28b60ea 100644 --- a/stdme/src/list/list_free_one.c +++ b/stdme/src/list/list_free_one.c @@ -18,5 +18,5 @@ void list_free_one(t_list *lst, void (*del)(void *)) if (lst == NULL || del == NULL) return ; del(lst->content); - free(lst); + me_free(lst); } diff --git a/stdme/src/mem/mem_alloc.c b/stdme/src/mem/mem_alloc.c index a52c40a5..32f4f82b 100644 --- a/stdme/src/mem/mem_alloc.c +++ b/stdme/src/mem/mem_alloc.c @@ -6,21 +6,14 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/06 14:47:49 by maiboyer #+# #+# */ -/* Updated: 2023/12/09 18:14:11 by maiboyer ### ########.fr */ +/* Updated: 2024/05/07 12:45:31 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "me/mem/mem_alloc.h" -#include +#include "me/alloc/alloc.h" void *mem_alloc(t_usize size) { - void *out; - size_t i; - - i = 0; - out = malloc(size); - while (out && i < size) - ((t_u8 *)out)[i++] = 0; - return (out); + return (me_malloc(size)); } diff --git a/stdme/src/mem/mem_alloc_array.c b/stdme/src/mem/mem_alloc_array.c index b17ce7f7..e4d74d9d 100644 --- a/stdme/src/mem/mem_alloc_array.c +++ b/stdme/src/mem/mem_alloc_array.c @@ -6,20 +6,15 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/06 15:53:21 by maiboyer #+# #+# */ -/* Updated: 2023/12/09 18:14:47 by maiboyer ### ########.fr */ +/* Updated: 2024/05/07 12:46:04 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ -#include "me/mem/mem_alloc.h" +#include "me/alloc/alloc.h" #include "me/mem/mem_alloc_array.h" #include void *mem_alloc_array(t_usize item_count, t_usize item_size) { - t_usize multiplied; - - multiplied = item_count * item_size; - if (multiplied == 0 || multiplied / item_count != item_size) - return (NULL); - return (mem_alloc(multiplied)); + return (me_calloc(item_count, item_size)); } diff --git a/stdme/src/mem/mem_realloc.c b/stdme/src/mem/mem_realloc.c new file mode 100644 index 00000000..1a40590b --- /dev/null +++ b/stdme/src/mem/mem_realloc.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_realloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/07 12:46:18 by maiboyer #+# #+# */ +/* Updated: 2024/05/07 12:47:12 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_realloc.h" +#include "me/alloc/alloc.h" + +void *mem_realloc(void *ptr, t_usize size) +{ + return (me_realloc(ptr, size)); +} diff --git a/stdme/src/num/usize/round_up.c b/stdme/src/num/usize/round_up.c new file mode 100644 index 00000000..f112b238 --- /dev/null +++ b/stdme/src/num/usize/round_up.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* round_up.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/07 11:04:51 by maiboyer #+# #+# */ +/* Updated: 2024/05/07 11:06:22 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/num/usize.h" + +t_usize usize_round_up_to(t_usize self, t_usize mul) +{ + t_usize mod; + + if (mul == 0) + return (self); + mod = self % mul; + if (mod == 0) + return (self); + return (self + (mul - mod)); +} diff --git a/stdme/src/os/abort.c b/stdme/src/os/abort.c new file mode 100644 index 00000000..0ac06cf7 --- /dev/null +++ b/stdme/src/os/abort.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* abort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/07 11:08:03 by maiboyer #+# #+# */ +/* Updated: 2024/05/07 13:09:59 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/alloc/alloc_internal.h" +#include "me/types.h" +#include + +void me_abort(void) +{ + me_exit(1); +} diff --git a/stdme/src/os/exit.c b/stdme/src/os/exit.c new file mode 100644 index 00000000..44d97af3 --- /dev/null +++ b/stdme/src/os/exit.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/07 13:08:52 by maiboyer #+# #+# */ +/* Updated: 2024/05/07 15:01:38 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/types.h" +#include "me/alloc/alloc_internal.h" +#include + +void me_exit(t_i32 exit_code) +{ + t_arena_page *arena; + t_arena_page *tmp; + + arena = get_head_arena(); + while (arena) + { + tmp = arena->next; + free(arena); + arena = tmp; + } + exit(exit_code); +} diff --git a/stdme/src/os/process.c b/stdme/src/os/process.c index 70b6b5f4..ad430744 100644 --- a/stdme/src/os/process.c +++ b/stdme/src/os/process.c @@ -73,8 +73,8 @@ t_error in_path(t_spawn_info *info, t_process *process, t_const_str path, } sp_index = 0; while (splitted_path[sp_index]) - free(splitted_path[sp_index++]); - free(splitted_path); + me_free(splitted_path[sp_index++]); + me_free(splitted_path); return (NO_ERROR); } @@ -97,7 +97,7 @@ t_error find_binary(t_spawn_info *info, t_process *process) } if (access(s.buf, X_OK | R_OK) == 0) { - free(info->binary_path); + me_free(info->binary_path); info->binary_path = s.buf; return (NO_ERROR); } @@ -117,7 +117,7 @@ static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process) close(info.stderr.vals.fd.value); vec_str_free(info.arguments); vec_str_free(info.environement); - free(info.binary_path); + me_free(info.binary_path); } t_error spawn_process(t_spawn_info info, t_process *process) diff --git a/stdme/src/printf/formatter/char.c b/stdme/src/printf/formatter/char.c index dcbe7428..ae560401 100644 --- a/stdme/src/printf/formatter/char.c +++ b/stdme/src/printf/formatter/char.c @@ -66,5 +66,5 @@ void printf_s(t_printf_arg data, t_printf_func f) .fill_zero = 0, .fill = 0, .len = len, .pretty = "", .pretty_len = 0, \ .str = start_num, .allow_zero_fill = false, .sign = NULL, \ .sign_len = 0, }, data, f); - free(start_num); + me_free(start_num); } diff --git a/stdme/src/printf/formatter/decimal.c b/stdme/src/printf/formatter/decimal.c index b99b5e30..4b6f9fcf 100644 --- a/stdme/src/printf/formatter/decimal.c +++ b/stdme/src/printf/formatter/decimal.c @@ -85,5 +85,5 @@ void printf_d(t_printf_arg data, t_printf_func f) .fill_zero = 0, .fill = 0, .sign = sign, .pretty = NULL, .len = \ str_len(start_num), .pretty_len = 0, .str = start_num, .allow_zero_fill \ = true, .sign_len = str_len(sign), }, data, f); - free(start_num); + me_free(start_num); } diff --git a/stdme/src/printf/formatter/unsigned_decimal.c b/stdme/src/printf/formatter/unsigned_decimal.c index 5d44f4e8..5bd7f72f 100644 --- a/stdme/src/printf/formatter/unsigned_decimal.c +++ b/stdme/src/printf/formatter/unsigned_decimal.c @@ -72,5 +72,5 @@ void printf_u(t_printf_arg data, t_printf_func f) .fill_zero = 0, .fill = 0, .len = str_len(start_num), \ .pretty = NULL, .pretty_len = 0, .str = start_num, \ .allow_zero_fill = true, .sign = NULL, .sign_len = 0, }, data, f); - free(start_num); + me_free(start_num); } diff --git a/stdme/src/printf/formatter/utils.c b/stdme/src/printf/formatter/utils.c index f8dd6641..74bcdc16 100644 --- a/stdme/src/printf/formatter/utils.c +++ b/stdme/src/printf/formatter/utils.c @@ -99,7 +99,7 @@ t_printf_arg print_substr(t_usize *c_idx, t_usize *nxt, t_const_str fmt, truc = str_substring(fmt, *c_idx, *nxt - *c_idx); extra.f(truc, *nxt - *c_idx, extra.p_args); - free(truc); + me_free(truc); *c_idx = *nxt + 1; return ((t_printf_arg){ .p_args = extra.p_args, diff --git a/stdme/src/printf/formatter/utils_numbers.c b/stdme/src/printf/formatter/utils_numbers.c index 58e50953..86aa24fd 100644 --- a/stdme/src/printf/formatter/utils_numbers.c +++ b/stdme/src/printf/formatter/utils_numbers.c @@ -23,7 +23,7 @@ void handle_weird_precision_stuff(t_printf_arg *data, t_prec_strs strs, data->flags &= (~ZERO_ALIGN); data->flags |= ALIGN; if (strs.free_out) - *strs.out = (free(*strs.out), (t_str)mem_alloc_array(1, 1)); + *strs.out = (me_free(*strs.out), (t_str)mem_alloc_array(1, 1)); else *strs.out = ""; *strs.pretty = ""; diff --git a/stdme/src/printf/printf.c b/stdme/src/printf/printf.c index 935b3cd0..e7b6e52e 100644 --- a/stdme/src/printf/printf.c +++ b/stdme/src/printf/printf.c @@ -99,7 +99,7 @@ t_usize me_printf(t_const_str fmt, ...) va_end(args); len = str_len(str); write(1, str, len); - free(str); + me_free(str); return (len); } @@ -114,7 +114,7 @@ t_usize me_eprintf(t_const_str fmt, ...) va_end(args); len = str_len(str); write(2, str, len); - free(str); + me_free(str); return (len); } */ diff --git a/stdme/src/string/str_l_cat.c b/stdme/src/string/str_l_cat.c index a606722a..8cfe812d 100644 --- a/stdme/src/string/str_l_cat.c +++ b/stdme/src/string/str_l_cat.c @@ -101,8 +101,8 @@ int main(void) { printf("\n"); printf("\n"); } - free(dest_libc); - free(dest_ft); + me_free(dest_libc); + me_free(dest_ft); } } R*/ diff --git a/stdme/src/string/str_l_copy.c b/stdme/src/string/str_l_copy.c index e4bb6bcb..16078374 100644 --- a/stdme/src/string/str_l_copy.c +++ b/stdme/src/string/str_l_copy.c @@ -97,8 +97,8 @@ int main(void) { printf("\n"); printf("\n"); } - free(dest_libc); - free(dest_ft); + me_free(dest_libc); + me_free(dest_ft); } } R*/ diff --git a/stdme/src/string/str_split.c b/stdme/src/string/str_split.c index d533e602..1d2bee3c 100644 --- a/stdme/src/string/str_split.c +++ b/stdme/src/string/str_split.c @@ -43,7 +43,7 @@ static t_usize local_count_words(t_const_str str, char chr) static t_str *local_split_freeall(t_str **to_free) { while (*to_free) - free(*(to_free++)); + me_free(*(to_free++)); return (NULL); }