diff --git a/Makefile b/Makefile index 0d12a1b..bc37084 100755 --- a/Makefile +++ b/Makefile @@ -20,7 +20,8 @@ SRCS_NAMES = malloc.c \ show_alloc_mem.c\ free.c\ realloc.c\ - utils.c + utils.c\ + env_debug.c SRCS_DIR = srcs diff --git a/libft/Makefile b/libft/Makefile index 1e51d5f..8253965 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/03 15:26:54 by tomoron ### ########.fr # +# Updated: 2024/12/09 17:25:04 by tomoron ### ########.fr # # # # **************************************************************************** # @@ -54,6 +54,7 @@ SRCS = ft_atoi.c\ ft_set_color.c\ ft_isspace.c\ ft_str_is_only_char.c\ + ft_putulnbr.c SRCS_BONUS = ft_lstnew.c\ ft_lstadd_front.c\ diff --git a/libft/ft_putulnbr.c b/libft/ft_putulnbr.c new file mode 100755 index 0000000..57cbf1c --- /dev/null +++ b/libft/ft_putulnbr.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putulnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/09 17:24:29 by tomoron #+# #+# */ +/* Updated: 2024/12/09 17:24:33 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putulnbr_fd(unsigned long nb, int fd) +{ + if (nb >= 10) + ft_putulnbr_fd(nb / 10, fd); + ft_putchar_fd((nb % 10) + 48, fd); +} diff --git a/libft/libft.h b/libft/libft.h index f5c8c7a..da469c2 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/11/27 17:48:14 by tomoron ### ########.fr */ +/* Updated: 2024/12/09 17:25:45 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,6 +45,7 @@ void ft_lstadd_front(t_list **lst, t_list *new); char **ft_split(char const *str, char charset); void ft_lstadd_back(t_list **lst, t_list *new); 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); void *ft_memset(void *s, int c, size_t n); char *ft_get_color(int r, int g, int b); diff --git a/srcs/env_debug.c b/srcs/env_debug.c new file mode 100644 index 0000000..08620cd --- /dev/null +++ b/srcs/env_debug.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env_debug.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/06 19:16:40 by tomoron #+# #+# */ +/* Updated: 2024/12/09 17:45:45 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "includes/malloc.h" +#include + +t_settings *get_settings() +{ + static t_settings settings; + char *str; + + if(!settings.initialized) + { + if (getenv("MALLOC_SHOW_LEAKS")) + settings.show_leaks = 0; + str = getenv("MALLOC_DEBUG_LEVEL"); + if (!str || !ft_strcmp(str, "NONE")) + settings.debug_level = 0; + else if (str && !ft_strcmp(str, "ERR")) + settings.debug_level = 1; + else if (str && !ft_strcmp(str, "WARN")) + settings.debug_level = 2; + else if (str && !ft_strcmp(str, "INFO")) + settings.debug_level = 3; + settings.initialized = 1; + } + return(&settings); +} diff --git a/srcs/free.c b/srcs/free.c index b429a08..2b1a14f 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/05 17:01:32 by tomoron ### ########.fr */ +/* Updated: 2024/12/09 19:16:14 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,6 +50,8 @@ int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur, char *fnc) if ((cur->next > *alloc || cur->next == 0) && cur <= *alloc && \ ((t_ul)(*alloc) - (t_ul)cur) <= cur->size) { + log_str("invalid pointer but adress is inside of an \ +allocation", 2, 1, 1); *alloc = cur; *res = prev; return (1); @@ -57,6 +59,7 @@ int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur, char *fnc) prev = cur; cur = cur->next; } + log_str("invalid pointer inside of a chunk", 2, 1, 1); invalid_pointer(fnc); return (0); } @@ -71,6 +74,7 @@ static int free_prealloc(t_alloc *alloc, t_mem_chunk **main_chunk, \ chunk = get_alloc_chunk(alloc, *main_chunk, is_small); if (!chunk) return (0); + log_str("free pointer chunk found", 3 ,1 ,1); if (!get_prev_alloc(&alloc, &prev, chunk->first, "free")) return (1); chunk->space_left -= alloc->size + sizeof(t_alloc); @@ -96,26 +100,33 @@ static void free_large(t_alloc *alloc) t_alloc *prev; if (!get_prev_alloc(&alloc, &prev, g_allocs.large, "free")) + { + log_str("unknown pointer given", 1, 1, 1); return ; + } if (alloc == g_allocs.large) g_allocs.large = alloc->next; else if (prev) prev->next = alloc->next; + log_str("munmap called with size ", 3, 1, 0); + log_ul(alloc->size + sizeof(t_alloc), 3, 0, 1); munmap(alloc, alloc->size + sizeof(t_alloc)); + log_str("free sucessfull", 3, 1, 1); } void free(void *ptr) { t_alloc *alloc; + log_str("free function called", 3, 1, 1); if (!ptr) return ; alloc = (t_alloc *)ptr - 1; pthread_mutex_lock(&g_mallock); if (free_prealloc(alloc, &g_allocs.tiny, 0)) - pthread_mutex_unlock(&g_mallock); + log_str("free successful", 3, 1, 1); else if (free_prealloc(alloc, &g_allocs.small, 1)) - pthread_mutex_unlock(&g_mallock); + log_str("free successful", 3, 1, 1); else free_large(alloc); pthread_mutex_unlock(&g_mallock); diff --git a/srcs/includes/libft.h b/srcs/includes/libft.h index 7150347..c2d70f1 100755 --- a/srcs/includes/libft.h +++ b/srcs/includes/libft.h @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/30 16:55:48 by tomoron #+# #+# */ -/* Updated: 2024/11/30 11:34:40 by tomoron ### ########.fr */ +/* Updated: 2024/12/09 17:45:01 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ # include # include # include -# include "./ft_printf.h" +# include "../../libft/ft_printf/ft_printf.h" typedef struct s_list { @@ -45,6 +45,7 @@ void ft_lstadd_front(t_list **lst, t_list *new); char **ft_split(char const *str, char charset); void ft_lstadd_back(t_list **lst, t_list *new); 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); void *ft_memset(void *s, int c, size_t n); char *ft_get_color(int r, int g, int b); diff --git a/srcs/includes/malloc.h b/srcs/includes/malloc.h index 08d0862..dd5fee6 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/05 16:39:23 by tomoron ### ########.fr */ +/* Updated: 2024/12/09 17:53:13 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ # include # include # include +# include +# include # include "libft.h" # define PAGE_SIZE sysconf(_SC_PAGESIZE) @@ -46,8 +48,18 @@ typedef struct s_allocations t_mem_chunk *tiny; t_mem_chunk *small; t_alloc *large; + + short log_level; + int fd; } t_allocations; +typedef struct t_settings +{ + uint8_t debug_level:2; + uint8_t show_leaks:1; + uint8_t initialized:1; +} t_settings; //size 1 + typedef unsigned long t_ul; extern t_allocations g_allocs; @@ -55,9 +67,12 @@ extern pthread_mutex_t g_mallock; size_t align(size_t nb, size_t align_nb); -void *malloc(size_t size); -void show_alloc_mem(void); -void free(void *ptr); -void *realloc(void *ptr, size_t size); +void *malloc(size_t size); +t_settings *get_settings(); +void show_alloc_mem(void); +void free(void *ptr); +void *realloc(void *ptr, size_t size); +void log_str(char *str, int level, int print_level, int nl); +void log_ul(unsigned long nb, int level, int print_level, int nl); #endif diff --git a/srcs/malloc.c b/srcs/malloc.c index 0f255da..ddd793c 100644 --- a/srcs/malloc.c +++ b/srcs/malloc.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/23 17:19:59 by tomoron #+# #+# */ -/* Updated: 2024/12/05 17:01:08 by tomoron ### ########.fr */ +/* Updated: 2024/12/09 18:48:53 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,8 +25,14 @@ void *get_memory(size_t size, int no_write) chunk = mmap(0, size, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + log_str("mmap called with size ",3, 1, 0); + log_ul(size, 3, 0, 1); if (chunk == MAP_FAILED) + { + log_str("mmap call failed", 1, 1, 1); return (0); + } + log_str("mmap call successful", 3, 1 ,1); if (no_write) return (chunk); chunk->space_left = size - sizeof(t_mem_chunk); @@ -63,6 +69,7 @@ static void *pre_allocated(size_t size, t_mem_chunk **chunk, int is_small) tmp = *chunk; prev = *chunk; res = 0; + log_str("malloc size corresponds to preallocated alloc size", 3, 1, 1); while (tmp) { if (tmp->space_left >= size) @@ -85,6 +92,8 @@ void *malloc(size_t size) t_alloc *res; pthread_mutex_lock(&g_mallock); + log_str("malloc called with size ",3, 1, 0); + log_ul(size, 3, 0, 1); if (size <= TINY_MALLOC) res = pre_allocated(size, &(g_allocs.tiny), 0); else if (size <= SMALL_MALLOC) @@ -94,12 +103,15 @@ void *malloc(size_t size) res = get_memory(size + sizeof(t_alloc), 1); if (res) { - res->size = size; - res->next = g_allocs.large; + *res = (t_alloc){size, g_allocs.large}; g_allocs.large = res; res++; } } + if(res) + log_str("malloc call successful", 3, 1, 1); + else + log_str("malloc call failed", 1, 1, 1); pthread_mutex_unlock(&g_mallock); return (res); } diff --git a/srcs/malloc_utils.c b/srcs/malloc_utils.c index 4eee64c..82aca4a 100644 --- a/srcs/malloc_utils.c +++ b/srcs/malloc_utils.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/03 18:44:32 by tomoron #+# #+# */ -/* Updated: 2024/12/03 19:01:11 by tomoron ### ########.fr */ +/* Updated: 2024/12/09 18:21:47 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,8 @@ t_mem_chunk *create_new_chunk(int is_small, t_mem_chunk **chunk, \ t_mem_chunk *new; size_t mmap_size; + log_str("no suitable address found in previously allocated chunks, \ +creating new chunk", 3 , 1 ,1); if (is_small) mmap_size = SMALL_CHUNK_SIZE; else @@ -47,16 +49,22 @@ static t_alloc *get_suitable_addr_in_chunk_middle_end(size_t space_left, \ free_space = ((t_ul)tmp->next - (t_ul)tmp) - \ (tmp->size + sizeof(t_alloc)); if (free_space >= size + sizeof(t_alloc)) + { + log_str("suitable address found between two alocations", 3, 1, 1); return (reserve_addr( (void *)((char *)tmp + tmp->size + sizeof(t_alloc)), size, tmp, chunk)); + } space_left -= free_space; tmp = tmp->next; } if (space_left >= size + sizeof(t_alloc)) + { + log_str("suitable address found at the end of a chunk", 3, 1, 1); return (reserve_addr( (t_alloc *)((char *)tmp + tmp->size + sizeof(t_alloc)), size, tmp, chunk)); + } return (0); } @@ -72,6 +80,8 @@ t_alloc *get_suitable_addr_in_chunk(t_mem_chunk *chunk, size_t size) tmp = chunk->first; chunk->first = reserve_addr((void *)(chunk + 1), size, 0, chunk) - 1; chunk->first->next = tmp; + log_str("suitable address found in the start of an already allocated \ +chunk", 3, 1, 1); return (chunk->first + 1); } return (get_suitable_addr_in_chunk_middle_end(space_lft, tmp, size, chunk)); diff --git a/srcs/realloc.c b/srcs/realloc.c index e83f02b..854bed8 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/05 17:32:25 by tomoron ### ########.fr */ +/* Updated: 2024/12/09 20:07:24 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,6 +44,7 @@ static void *realloc_prealloc(t_alloc *alloc, t_mem_chunk *chunk, \ return (0); if (alloc->size >= size) { + log_str("new realloc size is smaller, reducing", 3, 1, 1); chunk->space_left += alloc->size - size; alloc->size = size; return (alloc + 1); @@ -51,7 +52,11 @@ static void *realloc_prealloc(t_alloc *alloc, t_mem_chunk *chunk, \ if ((size > (size_t)TINY_MALLOC && is_small == 0) || (size > SMALL_MALLOC && is_small == 1) || (t_ul)(alloc->next) - (t_ul)(alloc + 1) < size) + { + log_str("new realloc size doesn't fit in the current position, reallocating", 3, 1, 1); return (realloc_recreate(alloc, size)); + } + log_str("new realloc size fits in the current position, making it bigger",3, 1, 1); chunk->space_left -= size - alloc->size; alloc->size = size; return (alloc + 1); @@ -62,7 +67,10 @@ static void *realloc_large(t_alloc *alloc, size_t size) t_alloc *prev; if (!get_prev_alloc(&alloc, &prev, g_allocs.large, "realloc")) + { + log_str("unknown pointer given to realloc", 1, 1 ,1); return (0); + } return (realloc_recreate(alloc, size)); } @@ -70,6 +78,7 @@ void *realloc(void *ptr, size_t size) { t_alloc *alloc; + log_str("realloc function called", 3, 1, 1); if (!ptr) return (malloc(size)); alloc = (t_alloc *)ptr - 1; @@ -79,6 +88,10 @@ void *realloc(void *ptr, size_t size) ptr = realloc_prealloc(alloc, g_allocs.small, 1, size); if(!ptr) ptr = realloc_large(alloc, size); + if(ptr) + log_str("realloc sucessful", 3, 1, 1); + else + log_str("realloc failed", 1, 1, 1); pthread_mutex_unlock(&g_mallock); return(ptr); } diff --git a/srcs/utils.c b/srcs/utils.c index 8e85871..1925e9c 100644 --- a/srcs/utils.c +++ b/srcs/utils.c @@ -6,12 +6,14 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/03 13:20:54 by tomoron #+# #+# */ -/* Updated: 2024/12/04 18:52:46 by tomoron ### ########.fr */ +/* Updated: 2024/12/09 18:11:39 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "includes/malloc.h" +#include + size_t align(size_t nb, size_t align_nb) { if (nb % align_nb) @@ -24,3 +26,65 @@ void invalid_pointer(char *fnc) write(2, fnc, ft_strlen(fnc)); write(2, "(): invalid pointer\n", 21); } + +int log_fd(int closefd) +{ + static int fd; + + if(closefd) + { + if(fd > 0) + close(fd); + return(0); + } + if(fd == 0) + { + fd = open("malloc.log", O_WRONLY | O_CREAT | O_TRUNC, 0644); + if(fd == -1) + { + ft_putnbr_fd(errno, 1); + write(2, "malloc(): can't open log file\n", 37); + } + } + return(fd); +} + +void log_str(char *str, int level, int print_level, int nl) +{ + int fd; + + if(level > get_settings()->debug_level) + return ; + fd = log_fd(0); + if(fd == -1) + return ; + if(print_level && level == 1) + ft_putstr_fd("[ERR] ", fd); + else if(print_level && level == 2) + ft_putstr_fd("[WARN] ", fd); + else if(print_level && level == 3) + ft_putstr_fd("[INFO] ", fd); + ft_putstr_fd(str, fd); + if(nl) + write(fd, "\n", 1); +} + +void log_ul(unsigned long nb, int level, int print_level, int nl) +{ + int fd; + + if(level > get_settings()->debug_level) + return ; + fd = log_fd(0); + if(fd == -1) + return ; + if(print_level && level == 1) + ft_putstr_fd("[ERR] ", fd); + else if(print_level && level == 2) + ft_putstr_fd("[WARN] ", fd); + else if(print_level && level == 3) + ft_putstr_fd("[INFO] ", fd); + ft_putulnbr_fd(nb, fd); + if(nl) + write(fd, "\n", 1); +}