diff --git a/Makefile b/Makefile index c356002..74010a5 100755 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ SRCS = $(addprefix $(SRCS_DIR)/, $(SRCS_NAMES)) OBJS = $(addprefix $(OBJS_DIR)/, $(SRCS_NAMES:.c=.o)) -FLAGS = -Wall -Wextra -Werror -g +FLAGS = -Wall -Wextra -Werror -Wno-unused-result -Wno-free-nonheap-object -g LFT = libft/libft.a @@ -41,7 +41,7 @@ LFT_DIR = libft/ all: libft_malloc.so exec: $(OBJS_DIR) $(NAME) $(LFT) - $(CC) -o a.out $(FLAGS) main.c $(NAME) $(LFT) +# $(CC) -o a.out $(FLAGS) main.c $(NAME) $(LFT) $(CC) -o a.out $(FLAGS) main.c -L. -lft_malloc $(LFT) -Wl,-rpath=. libft_malloc.so: $(NAME) diff --git a/libft/Makefile b/libft/Makefile index 7b1735a..71a7a6e 100755 --- a/libft/Makefile +++ b/libft/Makefile @@ -6,7 +6,7 @@ # By: tomoron +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/07/28 00:35:01 by tomoron #+# #+# # -# Updated: 2024/12/10 18:31:35 by tomoron ### ########.fr # +# Updated: 2025/02/14 17:47:44 by tomoron ### ########.fr # # # # **************************************************************************** # @@ -69,7 +69,7 @@ SRCS_BONUS = ft_lstnew.c\ OBJS = $(SRCS:.c=.o) OBJS_BONUS = $(SRCS_BONUS:.c=.o) -FLAGS = -Wall -Wextra -Werror -g -fPIC +FLAGS = -Wall -Wextra -Werror -g -Wno-unused-result -fPIC all: $(NAME) diff --git a/libft/ft_printf/Makefile b/libft/ft_printf/Makefile index 7eac308..47ab34f 100755 --- a/libft/ft_printf/Makefile +++ b/libft/ft_printf/Makefile @@ -6,7 +6,7 @@ # By: marde-vr +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/04 08:03:00 by tomoron #+# #+# # -# Updated: 2024/11/30 13:39:39 by tomoron ### ########.fr # +# Updated: 2025/02/14 17:48:08 by tomoron ### ########.fr # # # # **************************************************************************** # @@ -31,7 +31,7 @@ SRCS = ft_protected_atoi.c\ OBJS = $(SRCS:.c=.o) -FLAGS = -Wall -Wextra -Werror +FLAGS = -Wall -Wextra -Werror -Wno-unused-result $(NAME): $(OBJS) ar rcs $(NAME) $(OBJS) diff --git a/libft/libft.h b/libft/libft.h index da469c2..9406154 100755 --- a/libft/libft.h +++ b/libft/libft.h @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/30 16:55:48 by tomoron #+# #+# */ -/* Updated: 2024/12/09 17:25:45 by tomoron ### ########.fr */ +/* Updated: 2025/02/13 13:41:49 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,9 +41,9 @@ char *ft_strtrim(char const *s1, char const *set); char *ft_strjoin(char const *s1, char const *s2); void ft_lstiter(t_list *lst, void (*f)(void *)); void *ft_memchr(const void *s, int c, size_t n); -void ft_lstadd_front(t_list **lst, t_list *new); +void ft_lstadd_front(t_list **lst, t_list *new_l); char **ft_split(char const *str, char charset); -void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstadd_back(t_list **lst, t_list *new_l); char **ft_split_set(char *str, char *charset); void ft_putulnbr_fd(unsigned long n, int fd); int ft_str_is_only_char(char *str, char c); diff --git a/main.c b/main.c index a6fca3b..bf697f0 100644 --- a/main.c +++ b/main.c @@ -1,9 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/14 17:19:23 by tomoron #+# #+# */ +/* Updated: 2025/02/14 17:19:59 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "srcs/includes/malloc.h" #include -int main(void) +int main(void) { - void *ptr; + void *ptr; printf("%zu\n", SMALL_CHUNK_SIZE); ptr = malloc(1203); diff --git a/srcs/env_debug.c b/srcs/env_debug.c index 7ab5e1a..a870ad5 100644 --- a/srcs/env_debug.c +++ b/srcs/env_debug.c @@ -6,12 +6,11 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/06 19:16:40 by tomoron #+# #+# */ -/* Updated: 2024/12/10 18:18:03 by tomoron ### ########.fr */ +/* Updated: 2025/02/14 17:24:10 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "includes/malloc.h" -#include t_settings *get_settings(void) { diff --git a/srcs/free.c b/srcs/free.c index c122b6d..508b9ef 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: 2024/12/10 19:13:55 by tomoron ### ########.fr */ +/* Updated: 2025/02/14 17:20:17 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,8 +51,8 @@ int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur, char *fnc) *alloc = cur; *res = prev; } - if(*res) - return(1); + if (*res) + return (1); prev = cur; cur = cur->next; } diff --git a/srcs/hex_dump.c b/srcs/hex_dump.c new file mode 100644 index 0000000..a400215 --- /dev/null +++ b/srcs/hex_dump.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hex_dump.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/14 18:03:26 by tomoron #+# #+# */ +/* Updated: 2025/02/14 19:38:12 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "includes/malloc.h" + +static void hex_dump_show_address(long unsigned addr) +{ + i +} + +static void hex_dump_show_line(char *addr, int len) +{ + hex_dump_show_address((long unsigned)addr); +} + +void hex_dump(char *addr, size_t nb) +{ + char buf[16]; + size_t cur; + + cur = 0; + while (cur < nb) + { + if(cur && !ft_memcmp(buf, addr + cur, 16)) + { + cur += 16; + continue; + } + ft_memcpy(buf, addr + cur, 16); + if(nb - cur < 16) + hex_dump_show_line(addr + cur, nb - cur); + else + hex_dump_show_line(addr + cur, 16); + cur += 16; + } +} diff --git a/srcs/includes/malloc.h b/srcs/includes/malloc.h index 59d4627..2747bc0 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: 2024/12/10 18:19:55 by tomoron ### ########.fr */ +/* Updated: 2025/02/14 18:16:06 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,6 +66,10 @@ extern t_allocations g_allocs; extern pthread_mutex_t g_mallock; size_t align(size_t nb, size_t align_nb); +size_t show_pre_allocated(char *type, t_mem_chunk *chunk, int dump); +size_t show_large(int dump); +void put_ulnbr_base(t_ul nbr, char *base); +void hex_dump(char *addr, size_t nb); void *malloc(size_t size); t_settings *get_settings(void); diff --git a/srcs/program_end.c b/srcs/program_end.c index 6becd04..db4d70c 100644 --- a/srcs/program_end.c +++ b/srcs/program_end.c @@ -6,12 +6,11 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/10 17:11:27 by tomoron #+# #+# */ -/* Updated: 2024/12/10 18:13:37 by tomoron ### ########.fr */ +/* Updated: 2025/02/14 17:24:02 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "includes/malloc.h" -#include void __attribute__((destructor)) malloc_end(void) { diff --git a/srcs/realloc.c b/srcs/realloc.c index e481397..b2ddde8 100644 --- a/srcs/realloc.c +++ b/srcs/realloc.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/02 14:41:22 by tomoron #+# #+# */ -/* Updated: 2024/12/10 18:14:56 by tomoron ### ########.fr */ +/* Updated: 2025/02/14 17:43:40 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,7 @@ static void *realloc_recreate(t_alloc *alloc, size_t size) size = alloc->size; pthread_mutex_unlock(&g_mallock); ft_memcpy(new, alloc + 1, size); - free(alloc + 1); + free((void *)(alloc + 1)); pthread_mutex_lock(&g_mallock); return (new); } diff --git a/srcs/show_alloc_mem.c b/srcs/show_alloc_mem.c index 5d24d33..25f15a5 100644 --- a/srcs/show_alloc_mem.c +++ b/srcs/show_alloc_mem.c @@ -6,13 +6,13 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/30 12:19:34 by tomoron #+# #+# */ -/* Updated: 2024/12/05 17:05:48 by tomoron ### ########.fr */ +/* Updated: 2025/02/14 18:18:48 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "includes/malloc.h" -static void put_ulnbr_base(t_ul nbr, char *base) +void put_ulnbr_base(t_ul nbr, char *base) { t_ul base_len; @@ -26,7 +26,7 @@ static void put_ulnbr_base(t_ul nbr, char *base) write(1, base + nbr, 1); } -static size_t show_allocs(t_alloc *alloc) +static size_t show_allocs(t_alloc *alloc, int dump) { size_t nb_bytes; @@ -42,12 +42,14 @@ static size_t show_allocs(t_alloc *alloc) write(1, " : ", 3); put_ulnbr_base(alloc->size, "0123456789"); write(1, " bytes\n", 7); + if(dump) + hex_dump((void *)(alloc + 1), alloc->size); alloc = alloc->next; } return (nb_bytes); } -static size_t show_pre_allocated(char *type, t_mem_chunk *chunk) +size_t show_pre_allocated(char *type, t_mem_chunk *chunk, int dump) { size_t nb_bytes; @@ -58,13 +60,13 @@ static size_t show_pre_allocated(char *type, t_mem_chunk *chunk) write(1, " : 0x", 5); put_ulnbr_base((t_ul)chunk, "0123456789ABCDEF"); write(1, "\n", 1); - nb_bytes += show_allocs(chunk->first); + nb_bytes += show_allocs(chunk->first, dump); chunk = chunk->next; } return (nb_bytes); } -static size_t show_large(void) +size_t show_large(int dump) { t_alloc *alloc; size_t total_size; @@ -84,6 +86,8 @@ static size_t show_large(void) put_ulnbr_base(alloc->size, "0123456789"); write(1, " bytes\n", 7); total_size += alloc->size; + if(dump) + hex_dump((void *)(alloc + 1), alloc->size); alloc = alloc->next; } return (total_size); @@ -95,9 +99,9 @@ void show_alloc_mem(void) total = 0; pthread_mutex_lock(&g_mallock); - total += show_pre_allocated("TINY", g_allocs.tiny); - total += show_pre_allocated("SMALL", g_allocs.small); - total += show_large(); + total += show_pre_allocated("TINY", g_allocs.tiny, 0); + total += show_pre_allocated("SMALL", g_allocs.small, 0); + total += show_large(0); pthread_mutex_unlock(&g_mallock); write(1, "Total : ", 8); put_ulnbr_base(total, "0123456789"); diff --git a/srcs/show_alloc_mem_ex.c b/srcs/show_alloc_mem_ex.c new file mode 100644 index 0000000..8c5d2e9 --- /dev/null +++ b/srcs/show_alloc_mem_ex.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* show_alloc_mem_ex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/14 17:26:41 by tomoron #+# #+# */ +/* Updated: 2025/02/14 17:48:53 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "includes/malloc.h" + +void show_alloc_mem_ex(void) +{ + size_t total; + + total = 0; + pthread_mutex_lock(&g_mallock); + total += show_pre_allocated("TINY", g_allocs.tiny, 1); + total += show_pre_allocated("SMALL", g_allocs.small, 1); + total += show_large(1); + pthread_mutex_unlock(&g_mallock); + write(1, "Total : ", 8); + put_ulnbr_base(total, "0123456789"); + write(1, " bytes\n", 7); +} diff --git a/srcs/utils.c b/srcs/utils.c index 5096999..6233165 100644 --- a/srcs/utils.c +++ b/srcs/utils.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/03 13:20:54 by tomoron #+# #+# */ -/* Updated: 2024/12/10 18:16:13 by tomoron ### ########.fr */ +/* Updated: 2025/02/14 17:45:17 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,7 @@ int log_fd(int closefd) if (fd == -1) { ft_putnbr_fd(errno, 1); - write(2, "malloc(): can't open log file\n", 37); + write(2, "malloc(): can't open log file\n", 30); } } return (fd);