add MALLOC_DEBUG_LEVEL environment variable support

This commit is contained in:
2024-12-09 20:08:28 +01:00
parent 039d29f0d7
commit 9824c9d230
12 changed files with 205 additions and 19 deletions

View File

@ -20,7 +20,8 @@ SRCS_NAMES = malloc.c \
show_alloc_mem.c\ show_alloc_mem.c\
free.c\ free.c\
realloc.c\ realloc.c\
utils.c utils.c\
env_debug.c
SRCS_DIR = srcs SRCS_DIR = srcs

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/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_set_color.c\
ft_isspace.c\ ft_isspace.c\
ft_str_is_only_char.c\ ft_str_is_only_char.c\
ft_putulnbr.c
SRCS_BONUS = ft_lstnew.c\ SRCS_BONUS = ft_lstnew.c\
ft_lstadd_front.c\ ft_lstadd_front.c\

20
libft/ft_putulnbr.c Executable file
View File

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

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/30 16:55:48 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); 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);
char **ft_split_set(char *str, char *charset); 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); int ft_str_is_only_char(char *str, char c);
void *ft_memset(void *s, int c, size_t n); void *ft_memset(void *s, int c, size_t n);
char *ft_get_color(int r, int g, int b); char *ft_get_color(int r, int g, int b);

37
srcs/env_debug.c Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env_debug.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "includes/malloc.h"
#include <stdio.h>
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);
}

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/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 && \ 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 \
allocation", 2, 1, 1);
*alloc = cur; *alloc = cur;
*res = prev; *res = prev;
return (1); return (1);
@ -57,6 +59,7 @@ int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur, char *fnc)
prev = cur; prev = cur;
cur = cur->next; cur = cur->next;
} }
log_str("invalid pointer inside of a chunk", 2, 1, 1);
invalid_pointer(fnc); invalid_pointer(fnc);
return (0); 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); chunk = get_alloc_chunk(alloc, *main_chunk, is_small);
if (!chunk) if (!chunk)
return (0); return (0);
log_str("free pointer chunk found", 3 ,1 ,1);
if (!get_prev_alloc(&alloc, &prev, chunk->first, "free")) if (!get_prev_alloc(&alloc, &prev, chunk->first, "free"))
return (1); return (1);
chunk->space_left -= alloc->size + sizeof(t_alloc); chunk->space_left -= alloc->size + sizeof(t_alloc);
@ -96,26 +100,33 @@ static void free_large(t_alloc *alloc)
t_alloc *prev; t_alloc *prev;
if (!get_prev_alloc(&alloc, &prev, g_allocs.large, "free")) if (!get_prev_alloc(&alloc, &prev, g_allocs.large, "free"))
{
log_str("unknown pointer given", 1, 1, 1);
return ; return ;
}
if (alloc == g_allocs.large) if (alloc == g_allocs.large)
g_allocs.large = alloc->next; g_allocs.large = alloc->next;
else if (prev) else if (prev)
prev->next = alloc->next; 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)); munmap(alloc, alloc->size + sizeof(t_alloc));
log_str("free sucessfull", 3, 1, 1);
} }
void free(void *ptr) void free(void *ptr)
{ {
t_alloc *alloc; t_alloc *alloc;
log_str("free function called", 3, 1, 1);
if (!ptr) if (!ptr)
return ; return ;
alloc = (t_alloc *)ptr - 1; alloc = (t_alloc *)ptr - 1;
pthread_mutex_lock(&g_mallock); pthread_mutex_lock(&g_mallock);
if (free_prealloc(alloc, &g_allocs.tiny, 0)) 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)) else if (free_prealloc(alloc, &g_allocs.small, 1))
pthread_mutex_unlock(&g_mallock); log_str("free successful", 3, 1, 1);
else else
free_large(alloc); free_large(alloc);
pthread_mutex_unlock(&g_mallock); pthread_mutex_unlock(&g_mallock);

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/30 16:55:48 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 <stddef.h> # include <stddef.h>
# include <stdlib.h> # include <stdlib.h>
# include <unistd.h> # include <unistd.h>
# include "./ft_printf.h" # include "../../libft/ft_printf/ft_printf.h"
typedef struct s_list 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); 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);
char **ft_split_set(char *str, char *charset); 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); int ft_str_is_only_char(char *str, char c);
void *ft_memset(void *s, int c, size_t n); void *ft_memset(void *s, int c, size_t n);
char *ft_get_color(int r, int g, int b); char *ft_get_color(int r, int g, int b);

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/05 16:39:23 by tomoron ### ########.fr */ /* Updated: 2024/12/09 17:53:13 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,8 @@
# include <sys/mman.h> # include <sys/mman.h>
# include <unistd.h> # include <unistd.h>
# include <pthread.h> # include <pthread.h>
# include <stdint.h>
# include <fcntl.h>
# include "libft.h" # include "libft.h"
# define PAGE_SIZE sysconf(_SC_PAGESIZE) # define PAGE_SIZE sysconf(_SC_PAGESIZE)
@ -46,8 +48,18 @@ typedef struct s_allocations
t_mem_chunk *tiny; t_mem_chunk *tiny;
t_mem_chunk *small; t_mem_chunk *small;
t_alloc *large; t_alloc *large;
short log_level;
int fd;
} t_allocations; } 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; typedef unsigned long t_ul;
extern t_allocations g_allocs; 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); size_t align(size_t nb, size_t align_nb);
void *malloc(size_t size); void *malloc(size_t size);
void show_alloc_mem(void); t_settings *get_settings();
void free(void *ptr); void show_alloc_mem(void);
void *realloc(void *ptr, size_t size); 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 #endif

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/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, chunk = mmap(0, size, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE,
-1, 0); -1, 0);
log_str("mmap called with size ",3, 1, 0);
log_ul(size, 3, 0, 1);
if (chunk == MAP_FAILED) if (chunk == MAP_FAILED)
{
log_str("mmap call failed", 1, 1, 1);
return (0); return (0);
}
log_str("mmap call successful", 3, 1 ,1);
if (no_write) if (no_write)
return (chunk); return (chunk);
chunk->space_left = size - sizeof(t_mem_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; tmp = *chunk;
prev = *chunk; prev = *chunk;
res = 0; res = 0;
log_str("malloc size corresponds to preallocated alloc size", 3, 1, 1);
while (tmp) while (tmp)
{ {
if (tmp->space_left >= size) if (tmp->space_left >= size)
@ -85,6 +92,8 @@ void *malloc(size_t size)
t_alloc *res; t_alloc *res;
pthread_mutex_lock(&g_mallock); pthread_mutex_lock(&g_mallock);
log_str("malloc called with size ",3, 1, 0);
log_ul(size, 3, 0, 1);
if (size <= TINY_MALLOC) if (size <= TINY_MALLOC)
res = pre_allocated(size, &(g_allocs.tiny), 0); res = pre_allocated(size, &(g_allocs.tiny), 0);
else if (size <= SMALL_MALLOC) else if (size <= SMALL_MALLOC)
@ -94,12 +103,15 @@ void *malloc(size_t size)
res = get_memory(size + sizeof(t_alloc), 1); res = get_memory(size + sizeof(t_alloc), 1);
if (res) if (res)
{ {
res->size = size; *res = (t_alloc){size, g_allocs.large};
res->next = g_allocs.large;
g_allocs.large = res; g_allocs.large = res;
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); pthread_mutex_unlock(&g_mallock);
return (res); return (res);
} }

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/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; t_mem_chunk *new;
size_t mmap_size; size_t mmap_size;
log_str("no suitable address found in previously allocated chunks, \
creating new chunk", 3 , 1 ,1);
if (is_small) if (is_small)
mmap_size = SMALL_CHUNK_SIZE; mmap_size = SMALL_CHUNK_SIZE;
else 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) - \ free_space = ((t_ul)tmp->next - (t_ul)tmp) - \
(tmp->size + sizeof(t_alloc)); (tmp->size + sizeof(t_alloc));
if (free_space >= 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( return (reserve_addr(
(void *)((char *)tmp + tmp->size + sizeof(t_alloc)), (void *)((char *)tmp + tmp->size + sizeof(t_alloc)),
size, tmp, chunk)); size, tmp, chunk));
}
space_left -= free_space; space_left -= free_space;
tmp = tmp->next; tmp = tmp->next;
} }
if (space_left >= size + sizeof(t_alloc)) if (space_left >= size + sizeof(t_alloc))
{
log_str("suitable address found at the end of a chunk", 3, 1, 1);
return (reserve_addr( return (reserve_addr(
(t_alloc *)((char *)tmp + tmp->size + sizeof(t_alloc)), (t_alloc *)((char *)tmp + tmp->size + sizeof(t_alloc)),
size, tmp, chunk)); size, tmp, chunk));
}
return (0); return (0);
} }
@ -72,6 +80,8 @@ t_alloc *get_suitable_addr_in_chunk(t_mem_chunk *chunk, size_t size)
tmp = chunk->first; tmp = chunk->first;
chunk->first = reserve_addr((void *)(chunk + 1), size, 0, chunk) - 1; chunk->first = reserve_addr((void *)(chunk + 1), size, 0, chunk) - 1;
chunk->first->next = tmp; 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 (chunk->first + 1);
} }
return (get_suitable_addr_in_chunk_middle_end(space_lft, tmp, size, chunk)); return (get_suitable_addr_in_chunk_middle_end(space_lft, tmp, size, chunk));

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/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); return (0);
if (alloc->size >= size) if (alloc->size >= size)
{ {
log_str("new realloc size is smaller, reducing", 3, 1, 1);
chunk->space_left += alloc->size - size; chunk->space_left += alloc->size - size;
alloc->size = size; alloc->size = size;
return (alloc + 1); 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) if ((size > (size_t)TINY_MALLOC && is_small == 0)
|| (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);
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);
chunk->space_left -= size - alloc->size; chunk->space_left -= size - alloc->size;
alloc->size = size; alloc->size = size;
return (alloc + 1); return (alloc + 1);
@ -62,7 +67,10 @@ static void *realloc_large(t_alloc *alloc, size_t size)
t_alloc *prev; t_alloc *prev;
if (!get_prev_alloc(&alloc, &prev, g_allocs.large, "realloc")) if (!get_prev_alloc(&alloc, &prev, g_allocs.large, "realloc"))
{
log_str("unknown pointer given to realloc", 1, 1 ,1);
return (0); return (0);
}
return (realloc_recreate(alloc, size)); return (realloc_recreate(alloc, size));
} }
@ -70,6 +78,7 @@ void *realloc(void *ptr, size_t size)
{ {
t_alloc *alloc; t_alloc *alloc;
log_str("realloc function called", 3, 1, 1);
if (!ptr) if (!ptr)
return (malloc(size)); return (malloc(size));
alloc = (t_alloc *)ptr - 1; 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); ptr = realloc_prealloc(alloc, g_allocs.small, 1, size);
if(!ptr) if(!ptr)
ptr = realloc_large(alloc, size); 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); pthread_mutex_unlock(&g_mallock);
return(ptr); return(ptr);
} }

View File

@ -6,12 +6,14 @@
/* 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/04 18:52:46 by tomoron ### ########.fr */ /* Updated: 2024/12/09 18:11:39 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "includes/malloc.h" #include "includes/malloc.h"
#include <errno.h>
size_t align(size_t nb, size_t align_nb) size_t align(size_t nb, size_t align_nb)
{ {
if (nb % align_nb) if (nb % align_nb)
@ -24,3 +26,65 @@ void invalid_pointer(char *fnc)
write(2, fnc, ft_strlen(fnc)); write(2, fnc, ft_strlen(fnc));
write(2, "(): invalid pointer\n", 21); 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);
}