fix compilation error on newer clang version, fix some norm errors and start doing show_alloc_mem_ex function

This commit is contained in:
2025-02-14 19:38:42 +01:00
parent 561211f709
commit f5b2f8b8bf
14 changed files with 123 additions and 32 deletions

View File

@ -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)

View File

@ -6,7 +6,7 @@
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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)

View File

@ -6,7 +6,7 @@
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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)

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

12
main.c
View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdio.h>

View File

@ -6,12 +6,11 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdio.h>
t_settings *get_settings(void)
{

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

45
srcs/hex_dump.c Normal file
View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hex_dump.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -6,12 +6,11 @@
/* 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 */
/* Updated: 2025/02/14 17:24:02 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/malloc.h"
#include <stdio.h>
void __attribute__((destructor)) malloc_end(void)
{

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -6,13 +6,13 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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");

28
srcs/show_alloc_mem_ex.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* show_alloc_mem_ex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);