diff --git a/Makefile b/Makefile index 3bc229f..7a15d90 100755 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ # # # **************************************************************************** # -HOSTTYPE := $(shell uname -m)_$(shell uname -s) +HOSTTYPE ?= $(shell uname -m)_$(shell uname -s) NAME := libft_malloc_$(HOSTTYPE).so @@ -47,13 +47,13 @@ exec: $(OBJS_DIR) $(NAME) $(LFT) $(CC) -o a.out $(FLAGS) main.c -L. -lft_malloc $(LFT) -Wl,-rpath=. libft_malloc.so: $(NAME) - ln -sf $(NAME) libft_malloc.so + ln -sf "$(NAME)" libft_malloc.so $(LFT): make -j$(shell nproc) -C $(LFT_DIR) $(NAME): $(OBJS_DIR) $(OBJS) $(LFT) - $(CC) -shared -o $(NAME) $(OBJS) $(LFT) + $(CC) -shared -o "$(NAME)" $(OBJS) $(LFT) $(OBJS_DIR): mkdir -p $(OBJS_DIR) @@ -66,7 +66,7 @@ clean: make -C $(LFT_DIR) fclean fclean: clean - rm -f $(NAME) + rm -f "$(NAME)" rm -f libft_malloc.so re: fclean all diff --git a/srcs/env_debug.c b/srcs/env_debug.c index a870ad5..d01c609 100644 --- a/srcs/env_debug.c +++ b/srcs/env_debug.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/06 19:16:40 by tomoron #+# #+# */ -/* Updated: 2025/02/14 17:24:10 by tomoron ### ########.fr */ +/* Updated: 2025/03/20 16:58:18 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,8 @@ t_settings *get_settings(void) { if (getenv("MALLOC_SHOW_LEAKS")) settings.show_leaks = 1; + if (getenv("MALLOC_NO_UNMAP")) + settings.no_unmap = 1; str = getenv("MALLOC_DEBUG_LEVEL"); if (!str || !ft_strcmp(str, "NONE")) settings.debug_level = 0; diff --git a/srcs/free.c b/srcs/free.c index 508b9ef..4b8de99 100644 --- a/srcs/free.c +++ b/srcs/free.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/30 18:46:07 by tomoron #+# #+# */ -/* Updated: 2025/02/14 17:20:17 by tomoron ### ########.fr */ +/* Updated: 2025/03/20 17:00:04 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,6 +61,24 @@ int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur, char *fnc) return (0); } +static void free_chunk(size_t size, t_mem_chunk *chunk, t_mem_chunk **main_chunk) +{ + t_mem_chunk *prev; + + prev = *main_chunk; + if(*main_chunk == chunk) + prev = 0; + while(prev && prev->next != chunk) + prev = prev->next; + if (!prev && !chunk->next) + return; + if(prev) + prev->next = chunk->next; + else + *main_chunk = chunk->next; + munmap(chunk, size); +} + static int free_prealloc(t_alloc *alloc, t_mem_chunk **main_chunk, \ int is_small) { @@ -79,15 +97,13 @@ static int free_prealloc(t_alloc *alloc, t_mem_chunk **main_chunk, \ chunk->first = alloc->next; else if (prev) prev->next = alloc->next; - if (!chunk->first) + if (!chunk->first && !get_settings()->no_unmap) { - if (*main_chunk == chunk) - *main_chunk = chunk->next; if (is_small) size = SMALL_CHUNK_SIZE; else size = TINY_CHUNK_SIZE; - munmap(chunk, size); + free_chunk(size, chunk, main_chunk); } return (1); } diff --git a/srcs/includes/malloc.h b/srcs/includes/malloc.h index b3e5efb..0c8979f 100644 --- a/srcs/includes/malloc.h +++ b/srcs/includes/malloc.h @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/26 16:38:01 by tomoron #+# #+# */ -/* Updated: 2025/02/15 15:00:21 by tomoron ### ########.fr */ +/* Updated: 2025/03/20 16:58:10 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,6 +57,7 @@ typedef struct t_settings { uint8_t debug_level:2; uint8_t show_leaks:1; + uint8_t no_unmap:1; uint8_t initialized:1; } t_settings; //size 1