This commit is contained in:
2025-02-13 13:37:18 +01:00
parent 9824c9d230
commit 561211f709
10 changed files with 94 additions and 68 deletions

View File

@ -21,7 +21,8 @@ SRCS_NAMES = malloc.c \
free.c\
realloc.c\
utils.c\
env_debug.c
env_debug.c\
program_end.c
SRCS_DIR = srcs
@ -39,8 +40,9 @@ LFT_DIR = libft/
all: libft_malloc.so
exec: $(OBJS_DIR) $(OBJS) $(LFT)
$(CC) -o a.out $(FLAGS) main.c $(OBJS) $(LFT)
exec: $(OBJS_DIR) $(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)
ln -sf $(NAME) libft_malloc.so

View File

@ -6,7 +6,7 @@
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
# Updated: 2024/12/09 17:25:04 by tomoron ### ########.fr #
# Updated: 2024/12/10 18:31:35 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
FLAGS = -Wall -Wextra -Werror -g -fPIC
all: $(NAME)

View File

@ -6,22 +6,22 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/06 19:16:40 by tomoron #+# #+# */
/* Updated: 2024/12/09 17:45:45 by tomoron ### ########.fr */
/* Updated: 2024/12/10 18:18:03 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/malloc.h"
#include <stdio.h>
t_settings *get_settings()
t_settings *get_settings(void)
{
static t_settings settings;
char *str;
static t_settings settings;
char *str;
if(!settings.initialized)
if (!settings.initialized)
{
if (getenv("MALLOC_SHOW_LEAKS"))
settings.show_leaks = 0;
settings.show_leaks = 1;
str = getenv("MALLOC_DEBUG_LEVEL");
if (!str || !ft_strcmp(str, "NONE"))
settings.debug_level = 0;
@ -33,5 +33,5 @@ t_settings *get_settings()
settings.debug_level = 3;
settings.initialized = 1;
}
return(&settings);
return (&settings);
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/30 18:46:07 by tomoron #+# #+# */
/* Updated: 2024/12/09 19:16:14 by tomoron ### ########.fr */
/* Updated: 2024/12/10 19:13:55 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -43,23 +43,20 @@ int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur, char *fnc)
while (cur)
{
if (cur->next == *alloc)
{
*res = cur;
return (1);
}
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);
log_str("invalid pointer but address is known", 2, 1, 1);
*alloc = cur;
*res = prev;
return (1);
}
if(*res)
return(1);
prev = cur;
cur = cur->next;
}
log_str("invalid pointer inside of a chunk", 2, 1, 1);
log_str("invalid pointer inside of a chunk", 3, 1, 1);
invalid_pointer(fnc);
return (0);
}
@ -74,7 +71,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);
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);

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/26 16:38:01 by tomoron #+# #+# */
/* Updated: 2024/12/09 17:53:13 by tomoron ### ########.fr */
/* Updated: 2024/12/10 18:19:55 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -55,9 +55,9 @@ typedef struct s_allocations
typedef struct t_settings
{
uint8_t debug_level:2;
uint8_t show_leaks:1;
uint8_t initialized:1;
uint8_t debug_level:2;
uint8_t show_leaks:1;
uint8_t initialized:1;
} t_settings; //size 1
typedef unsigned long t_ul;
@ -65,10 +65,10 @@ typedef unsigned long t_ul;
extern t_allocations g_allocs;
extern pthread_mutex_t g_mallock;
size_t align(size_t nb, size_t align_nb);
size_t align(size_t nb, size_t align_nb);
void *malloc(size_t size);
t_settings *get_settings();
t_settings *get_settings(void);
void show_alloc_mem(void);
void free(void *ptr);
void *realloc(void *ptr, size_t size);

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/23 17:19:59 by tomoron #+# #+# */
/* Updated: 2024/12/09 18:48:53 by tomoron ### ########.fr */
/* Updated: 2024/12/10 18:12:18 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,14 +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_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);
log_str("mmap call successful", 3, 1, 1);
if (no_write)
return (chunk);
chunk->space_left = size - sizeof(t_mem_chunk);
@ -92,7 +92,7 @@ void *malloc(size_t size)
t_alloc *res;
pthread_mutex_lock(&g_mallock);
log_str("malloc called with size ",3, 1, 0);
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);
@ -108,7 +108,7 @@ void *malloc(size_t size)
res++;
}
}
if(res)
if (res)
log_str("malloc call successful", 3, 1, 1);
else
log_str("malloc call failed", 1, 1, 1);

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/03 18:44:32 by tomoron #+# #+# */
/* Updated: 2024/12/09 18:21:47 by tomoron ### ########.fr */
/* Updated: 2024/12/10 18:18:45 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,7 +23,7 @@ t_mem_chunk *create_new_chunk(int is_small, t_mem_chunk **chunk, \
size_t mmap_size;
log_str("no suitable address found in previously allocated chunks, \
creating new chunk", 3 , 1 ,1);
creating new chunk", 3, 1, 1);
if (is_small)
mmap_size = SMALL_CHUNK_SIZE;
else

25
srcs/program_end.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* program_end.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/10 17:11:27 by tomoron #+# #+# */
/* Updated: 2024/12/10 18:13:37 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/malloc.h"
#include <stdio.h>
void __attribute__((destructor)) malloc_end(void)
{
if (get_settings()->show_leaks)
{
ft_putstr_fd("\n\033[31m\033[1mMALLOC_SHOW_LEAKS is defined, here is \
the leak report\n", 1);
show_alloc_mem();
ft_putstr_fd("\033[0m", 1);
}
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/02 14:41:22 by tomoron #+# #+# */
/* Updated: 2024/12/09 20:07:24 by tomoron ### ########.fr */
/* Updated: 2024/12/10 18:14:56 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -53,10 +53,12 @@ static void *realloc_prealloc(t_alloc *alloc, t_mem_chunk *chunk, \
|| (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);
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);
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);
@ -68,7 +70,7 @@ static void *realloc_large(t_alloc *alloc, size_t size)
if (!get_prev_alloc(&alloc, &prev, g_allocs.large, "realloc"))
{
log_str("unknown pointer given to realloc", 1, 1 ,1);
log_str("unknown pointer given to realloc", 1, 1, 1);
return (0);
}
return (realloc_recreate(alloc, size));
@ -84,14 +86,14 @@ void *realloc(void *ptr, size_t size)
alloc = (t_alloc *)ptr - 1;
pthread_mutex_lock(&g_mallock);
ptr = realloc_prealloc(alloc, g_allocs.tiny, 0, size);
if(!ptr)
if (!ptr)
ptr = realloc_prealloc(alloc, g_allocs.small, 1, size);
if(!ptr)
if (!ptr)
ptr = realloc_large(alloc, size);
if(ptr)
if (ptr)
log_str("realloc sucessful", 3, 1, 1);
else
log_str("realloc failed", 1, 1, 1);
pthread_mutex_unlock(&g_mallock);
return(ptr);
return (ptr);
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/03 13:20:54 by tomoron #+# #+# */
/* Updated: 2024/12/09 18:11:39 by tomoron ### ########.fr */
/* Updated: 2024/12/10 18:16:13 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,64 +27,64 @@ void invalid_pointer(char *fnc)
write(2, "(): invalid pointer\n", 21);
}
int log_fd(int closefd)
int log_fd(int closefd)
{
static int fd;
static int fd;
if(closefd)
if (closefd)
{
if(fd > 0)
if (fd > 0)
close(fd);
return(0);
return (0);
}
if(fd == 0)
if (fd == 0)
{
fd = open("malloc.log", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if(fd == -1)
if (fd == -1)
{
ft_putnbr_fd(errno, 1);
write(2, "malloc(): can't open log file\n", 37);
}
}
return(fd);
return (fd);
}
void log_str(char *str, int level, int print_level, int nl)
void log_str(char *str, int level, int print_level, int nl)
{
int fd;
int fd;
if(level > get_settings()->debug_level)
if (level > get_settings()->debug_level)
return ;
fd = log_fd(0);
if(fd == -1)
if (fd == -1)
return ;
if(print_level && level == 1)
if (print_level && level == 1)
ft_putstr_fd("[ERR] ", fd);
else if(print_level && level == 2)
else if (print_level && level == 2)
ft_putstr_fd("[WARN] ", fd);
else if(print_level && level == 3)
else if (print_level && level == 3)
ft_putstr_fd("[INFO] ", fd);
ft_putstr_fd(str, fd);
if(nl)
if (nl)
write(fd, "\n", 1);
}
void log_ul(unsigned long nb, int level, int print_level, int nl)
void log_ul(unsigned long nb, int level, int print_level, int nl)
{
int fd;
int fd;
if(level > get_settings()->debug_level)
if (level > get_settings()->debug_level)
return ;
fd = log_fd(0);
if(fd == -1)
if (fd == -1)
return ;
if(print_level && level == 1)
if (print_level && level == 1)
ft_putstr_fd("[ERR] ", fd);
else if(print_level && level == 2)
else if (print_level && level == 2)
ft_putstr_fd("[WARN] ", fd);
else if(print_level && level == 3)
else if (print_level && level == 3)
ft_putstr_fd("[INFO] ", fd);
ft_putulnbr_fd(nb, fd);
if(nl)
if (nl)
write(fd, "\n", 1);
}