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

View File

@ -6,7 +6,7 @@
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ # # By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# # # 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 = $(SRCS:.c=.o)
OBJS_BONUS = $(SRCS_BONUS:.c=.o) OBJS_BONUS = $(SRCS_BONUS:.c=.o)
FLAGS = -Wall -Wextra -Werror -g FLAGS = -Wall -Wextra -Werror -g -fPIC
all: $(NAME) all: $(NAME)

View File

@ -6,14 +6,14 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/06 19:16:40 by tomoron #+# #+# */ /* 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 "includes/malloc.h"
#include <stdio.h> #include <stdio.h>
t_settings *get_settings() t_settings *get_settings(void)
{ {
static t_settings settings; static t_settings settings;
char *str; char *str;
@ -21,7 +21,7 @@ t_settings *get_settings()
if (!settings.initialized) if (!settings.initialized)
{ {
if (getenv("MALLOC_SHOW_LEAKS")) if (getenv("MALLOC_SHOW_LEAKS"))
settings.show_leaks = 0; settings.show_leaks = 1;
str = getenv("MALLOC_DEBUG_LEVEL"); str = getenv("MALLOC_DEBUG_LEVEL");
if (!str || !ft_strcmp(str, "NONE")) if (!str || !ft_strcmp(str, "NONE"))
settings.debug_level = 0; settings.debug_level = 0;

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/30 18:46:07 by tomoron #+# #+# */ /* 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) while (cur)
{ {
if (cur->next == *alloc) if (cur->next == *alloc)
{
*res = cur; *res = cur;
return (1);
}
if ((cur->next > *alloc || cur->next == 0) && cur <= *alloc && \ if ((cur->next > *alloc || cur->next == 0) && cur <= *alloc && \
((t_ul)(*alloc) - (t_ul)cur) <= cur->size) ((t_ul)(*alloc) - (t_ul)cur) <= cur->size)
{ {
log_str("invalid pointer but adress is inside of an \ log_str("invalid pointer but address is known", 2, 1, 1);
allocation", 2, 1, 1);
*alloc = cur; *alloc = cur;
*res = prev; *res = prev;
return (1);
} }
if(*res)
return(1);
prev = cur; prev = cur;
cur = cur->next; 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); invalid_pointer(fnc);
return (0); return (0);
} }

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/26 16:38:01 by tomoron #+# #+# */ /* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -68,7 +68,7 @@ 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); void *malloc(size_t size);
t_settings *get_settings(); t_settings *get_settings(void);
void show_alloc_mem(void); void show_alloc_mem(void);
void free(void *ptr); void free(void *ptr);
void *realloc(void *ptr, size_t size); void *realloc(void *ptr, size_t size);

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/23 17:19:59 by tomoron #+# #+# */ /* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/03 18:44:32 by tomoron #+# #+# */ /* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

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> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/02 14:41:22 by tomoron #+# #+# */ /* 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) || (size > SMALL_MALLOC && is_small == 1)
|| (t_ul)(alloc->next) - (t_ul)(alloc + 1) < size) || (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)); 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; chunk->space_left -= size - alloc->size;
alloc->size = size; alloc->size = size;
return (alloc + 1); return (alloc + 1);

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/03 13:20:54 by tomoron #+# #+# */ /* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */