From 4423565dab72921a84f5f39ebda0f383edda9971 Mon Sep 17 00:00:00 2001 From: mirsella Date: Thu, 21 Sep 2023 10:19:27 +0200 Subject: [PATCH] add ft_dprintf for logging --- README.md | 2 +- libft/ft_printf/ft_printf.c | 88 +++++++++------- libft/ft_printf/ft_printf.h | 51 +++++----- libft/libft.h | 193 ++++++++++++++++++------------------ src/calloc.c | 7 +- src/free.c | 8 +- src/global_init.c | 53 +++++----- src/malloc.c | 4 +- src/realloc.c | 8 +- src/reallocarray.c | 9 +- 10 files changed, 222 insertions(+), 201 deletions(-) diff --git a/README.md b/README.md index 7d65b54..4e42423 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # bonus : - Manage the use of your malloc in a multi-threaded program (so to be “thread safe” using the pthread lib). -- log to /tmp/malloc (defined in header). dprintf and strerror used as part of bonuses, but don't work on school computers, so a fallback is used. +- LOGGING and LOGFILE define in malloc.h - show_alloc_mem_asciidump() and show_alloc_mem_hexdump() functions - calloc, reallocarray, functions + simple tester - “Defragment” the freed memory. diff --git a/libft/ft_printf/ft_printf.c b/libft/ft_printf/ft_printf.c index fef399a..646d63f 100644 --- a/libft/ft_printf/ft_printf.c +++ b/libft/ft_printf/ft_printf.c @@ -12,41 +12,61 @@ #include "ft_printf.h" -void count_bytes(int *counter, int byteswrotes) -{ - if (!counter) - return ; - if (byteswrotes < 0 || *counter < 0) - *counter = -1; - else - *counter += byteswrotes; +void count_bytes(int *counter, int byteswrotes) { + if (!counter) + return; + if (byteswrotes < 0 || *counter < 0) + *counter = -1; + else + *counter += byteswrotes; } -int ft_printf(const char *format, ...) -{ - int i; - int byteswrotes; - va_list args; - t_formatoptions fo; +int ft_printf(const char *format, ...) { + int i; + int byteswrotes; + va_list args; + t_formatoptions fo; + fo.fd = STDOUT_FILENO; - i = 0; - byteswrotes = 0; - va_start(args, format); - while (format[i]) - { - if (format[i] == '%' && format[++i] != '%') - { - ft_bzero(&fo, sizeof(t_formatoptions)); - fo.precision = -1; - i += parse_callprinters(&fo, format + i, args); - count_bytes(&byteswrotes, fo.byteswrotes); - } - else - { - count_bytes(&byteswrotes, ft_putchar(format[i])); - i++; - } - } - va_end(args); - return (byteswrotes); + i = 0; + byteswrotes = 0; + va_start(args, format); + while (format[i]) { + if (format[i] == '%' && format[++i] != '%') { + ft_bzero(&fo, sizeof(t_formatoptions)); + fo.precision = -1; + i += parse_callprinters(&fo, format + i, args); + count_bytes(&byteswrotes, fo.byteswrotes); + } else { + count_bytes(&byteswrotes, ft_putchar(format[i])); + i++; + } + } + va_end(args); + return (byteswrotes); +} + +int ft_dprintf(int fd, const char *format, ...) { + int i; + int byteswrotes; + va_list args; + t_formatoptions fo; + fo.fd = fd; + + i = 0; + byteswrotes = 0; + va_start(args, format); + while (format[i]) { + if (format[i] == '%' && format[++i] != '%') { + ft_bzero(&fo, sizeof(t_formatoptions)); + fo.precision = -1; + i += parse_callprinters(&fo, format + i, args); + count_bytes(&byteswrotes, fo.byteswrotes); + } else { + count_bytes(&byteswrotes, ft_putchar(format[i])); + i++; + } + } + va_end(args); + return (byteswrotes); } diff --git a/libft/ft_printf/ft_printf.h b/libft/ft_printf/ft_printf.h index 2883d33..5545eff 100644 --- a/libft/ft_printf/ft_printf.h +++ b/libft/ft_printf/ft_printf.h @@ -11,33 +11,34 @@ /* ************************************************************************** */ #ifndef FT_PRINTF_H -# define FT_PRINTF_H +#define FT_PRINTF_H -# include "../libft.h" -# include "stdarg.h" -# include "stdio.h" +#include "../libft.h" +#include "stdarg.h" +#include "stdio.h" +#include -typedef struct s_formatoptions -{ - int byteswrotes; - int width; - int precision; - int dash; - int zero; - int hash; - int space; - int plus; -} t_formatoptions; +typedef struct s_formatoptions { + int byteswrotes; + int width; + int precision; + int dash; + int zero; + int hash; + int space; + int plus; + int fd; +} t_formatoptions; -void count_bytes(int *counter, int bytesrwotes); -void ft_print_string(t_formatoptions *fo, char *s); -void ft_print_char(t_formatoptions *fo, unsigned char c); -void ft_print_int(t_formatoptions *fo, int n); -void ft_print_unsigned_int(t_formatoptions *fo, unsigned int n); -void ft_print_hex(t_formatoptions *fo, unsigned int n, char conversion); -void ft_print_pointer(t_formatoptions *fo, unsigned long long p); -int parse_callprinters(t_formatoptions *fo, - const char *format, va_list args); -int ft_printf(const char *format, ...); +void count_bytes(int *counter, int bytesrwotes); +void ft_print_string(t_formatoptions *fo, char *s); +void ft_print_char(t_formatoptions *fo, unsigned char c); +void ft_print_int(t_formatoptions *fo, int n); +void ft_print_unsigned_int(t_formatoptions *fo, unsigned int n); +void ft_print_hex(t_formatoptions *fo, unsigned int n, char conversion); +void ft_print_pointer(t_formatoptions *fo, unsigned long long p); +int parse_callprinters(t_formatoptions *fo, const char *format, va_list args); +int ft_printf(const char *format, ...); +int ft_dprintf(int fd, const char *format, ...); #endif diff --git a/libft/libft.h b/libft/libft.h index 6a8b674..fd709a1 100644 --- a/libft/libft.h +++ b/libft/libft.h @@ -11,107 +11,106 @@ /* ************************************************************************** */ #ifndef LIBFT_H -# define LIBFT_H +#define LIBFT_H -# include +#include -# ifndef BUFFER_SIZE -# define BUFFER_SIZE 4096 -# endif +#ifndef BUFFER_SIZE +#define BUFFER_SIZE 4096 +#endif typedef struct s_list { - void *content; - struct s_list *next; -} t_list; + void *content; + struct s_list *next; +} t_list; -int ft_floor(double x); -int ft_ceil(double x); -int ft_round(double x); -int ft_isascending(int *tab, int size); -int ft_isdescending(int *tab, int size); -int ft_atoi(const char *str); -long long ft_atoll(const char *str); -int ft_isalnum(int c); -int ft_isspace(char c); -int ft_isalpha(int c); -int ft_isascii(int c); -int ft_isdigit(int c); -int ft_isprint(int c); -int ft_lstsize(t_list *lst); -int ft_memcmp(const void *s1, const void *s2, size_t n); -int ft_strncmp(const char *s1, const char *s2, size_t n); -int ft_strcmp(const char *s1, const char *s2); -int ft_tolower(int c); -int ft_toupper(int c); -int ft_putchar(char c); -int ft_putstr(char *s); -int ft_putnbr(long long n); -int ft_putnbr_base(long long n, char *base); -long long ft_llmax(long long a, long long b); -unsigned long long ft_ullmax(unsigned long long a, unsigned long long b); -long long ft_llmin(long long a, long long b); -unsigned long long ft_ullmin(unsigned long long a, unsigned long long b); -int ft_nbrlen(long long n); -int ft_nbrlen_base(long long n, int base); -int ft_ullnbrlen(unsigned long long n); -int ft_ullnbrlen_base(unsigned long long n, int base); -int ft_abs(int n); -long long ft_llabs(long long n); -int ft_printf(const char *format, ...); -char **ft_split(const char *s, const char *charset); -char **ft_splitword(const char *s, const char *word); -char *ft_itoa(int n); -char *ft_strchr(const char *s, int c); -char *ft_strdup(const char *s1); -char *ft_strndup(const char *s1, size_t n); -char **ft_tabdup(char **tab); -char **ft_lst_to_tab(t_list *lst); -char *ft_strjoin(const char *s1, const char *s2); -char *ft_strjoin_free(char *s1, char *s2); -int ft_skip_spaces(char *str); -char *ft_strnstr(const char *haystack, const char *needle, - size_t len); -char *ft_strrchr(const char *s, int c); -char *ft_strtrim(const char *s1, const char *set); -char *ft_strmapi(const char *s, char (*f)(unsigned int, char)); -char *ft_substr(const char *s, unsigned int start, size_t len); -char *ft_get_next_line(int fd, int freee); -void ft_free_tab(char **tab); -size_t ft_strlcat(char *dst, const char *src, size_t dstsize); -size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); -size_t ft_strlen(const char *s); -size_t ft_tablen(char **tab); -t_list *ft_lstlast(t_list *lst); -t_list *ft_lstmap(t_list *lst, void *(*f)(void *), - void (*del)(void *)); -t_list *ft_lstfind(t_list *begin_list, void *data_ref, - int (*cmp)(void *, void *)); -t_list *ft_lstnew(void *content); -t_list *ft_lstnew_strs(int size, char **strs); -char *ft_lst_to_str(t_list *lst); -t_list *ft_lstat(t_list *begin_list, unsigned int nbr); -void ft_lst_remove_if(t_list **begin_list, void *data_ref, - int (*cmp)(void *, void *), void (*free_fct)(void *)); -void ft_intsort(int *tab, int size); -void *ft_calloc(size_t count, size_t size); -void *ft_memccpy(void *dest, const void *src, int c, size_t n); -void *ft_memchr(const void *s, int c, size_t n); -void *ft_memcpy(void *dest, const void *src, size_t n); -void *ft_memmove(void *dest, const void *src, size_t n); -void *ft_memset(void *b, int c, size_t len); -void ft_bzero(void *s, size_t n); -void ft_lstreverse(t_list **lst); -void ft_lstadd_back(t_list **alst, t_list *newel); -void ft_lstsort(t_list **lst, int (*cmp)()); -void ft_lstadd_front(t_list **alst, t_list *newel); -void ft_lstclear(t_list **lst, void (*del)(void *)); -void ft_lstdelone(t_list *lst, void (*del)(void *)); -void ft_lstiter(t_list *lst, void (*f)(void *)); -int ft_putchar_fd(char c, int fd); -int ft_putendl_fd(char *s, int fd); -int ft_putnbr_fd(int n, int fd); -int ft_putstr_fd(char *s, int fd); -void ft_striteri(char *s, void (*f)(unsigned int, char *)); -void *ft_realloc(void *ptr, size_t ptrsize, size_t newsize); +int ft_floor(double x); +int ft_ceil(double x); +int ft_round(double x); +int ft_isascending(int *tab, int size); +int ft_isdescending(int *tab, int size); +int ft_atoi(const char *str); +long long ft_atoll(const char *str); +int ft_isalnum(int c); +int ft_isspace(char c); +int ft_isalpha(int c); +int ft_isascii(int c); +int ft_isdigit(int c); +int ft_isprint(int c); +int ft_lstsize(t_list *lst); +int ft_memcmp(const void *s1, const void *s2, size_t n); +int ft_strncmp(const char *s1, const char *s2, size_t n); +int ft_strcmp(const char *s1, const char *s2); +int ft_tolower(int c); +int ft_toupper(int c); +int ft_putchar(char c); +int ft_putstr(char *s); +int ft_putnbr(long long n); +int ft_putnbr_base(long long n, char *base); +long long ft_llmax(long long a, long long b); +unsigned long long ft_ullmax(unsigned long long a, unsigned long long b); +long long ft_llmin(long long a, long long b); +unsigned long long ft_ullmin(unsigned long long a, unsigned long long b); +int ft_nbrlen(long long n); +int ft_nbrlen_base(long long n, int base); +int ft_ullnbrlen(unsigned long long n); +int ft_ullnbrlen_base(unsigned long long n, int base); +int ft_abs(int n); +long long ft_llabs(long long n); +int ft_printf(const char *format, ...); +int ft_dprintf(int fd, const char *format, ...); +char **ft_split(const char *s, const char *charset); +char **ft_splitword(const char *s, const char *word); +char *ft_itoa(int n); +char *ft_strchr(const char *s, int c); +char *ft_strdup(const char *s1); +char *ft_strndup(const char *s1, size_t n); +char **ft_tabdup(char **tab); +char **ft_lst_to_tab(t_list *lst); +char *ft_strjoin(const char *s1, const char *s2); +char *ft_strjoin_free(char *s1, char *s2); +int ft_skip_spaces(char *str); +char *ft_strnstr(const char *haystack, const char *needle, size_t len); +char *ft_strrchr(const char *s, int c); +char *ft_strtrim(const char *s1, const char *set); +char *ft_strmapi(const char *s, char (*f)(unsigned int, char)); +char *ft_substr(const char *s, unsigned int start, size_t len); +char *ft_get_next_line(int fd, int freee); +void ft_free_tab(char **tab); +size_t ft_strlcat(char *dst, const char *src, size_t dstsize); +size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); +size_t ft_strlen(const char *s); +size_t ft_tablen(char **tab); +t_list *ft_lstlast(t_list *lst); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); +t_list *ft_lstfind(t_list *begin_list, void *data_ref, + int (*cmp)(void *, void *)); +t_list *ft_lstnew(void *content); +t_list *ft_lstnew_strs(int size, char **strs); +char *ft_lst_to_str(t_list *lst); +t_list *ft_lstat(t_list *begin_list, unsigned int nbr); +void ft_lst_remove_if(t_list **begin_list, void *data_ref, + int (*cmp)(void *, void *), void (*free_fct)(void *)); +void ft_intsort(int *tab, int size); +void *ft_calloc(size_t count, size_t size); +void *ft_memccpy(void *dest, const void *src, int c, size_t n); +void *ft_memchr(const void *s, int c, size_t n); +void *ft_memcpy(void *dest, const void *src, size_t n); +void *ft_memmove(void *dest, const void *src, size_t n); +void *ft_memset(void *b, int c, size_t len); +void ft_bzero(void *s, size_t n); +void ft_lstreverse(t_list **lst); +void ft_lstadd_back(t_list **alst, t_list *newel); +void ft_lstsort(t_list **lst, int (*cmp)()); +void ft_lstadd_front(t_list **alst, t_list *newel); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +int ft_putchar_fd(char c, int fd); +int ft_putendl_fd(char *s, int fd); +int ft_putnbr_fd(int n, int fd); +int ft_putstr_fd(char *s, int fd); +void ft_striteri(char *s, void (*f)(unsigned int, char *)); +void *ft_realloc(void *ptr, size_t ptrsize, size_t newsize); #endif diff --git a/src/calloc.c b/src/calloc.c index ef248f0..04a7d99 100644 --- a/src/calloc.c +++ b/src/calloc.c @@ -4,7 +4,8 @@ void *calloc(size_t nmemb, size_t size) { size_t overflow = nmemb * size; if (size && overflow / size != nmemb) { if (LOGGING) { - flog("calloc: integer overflow", nmemb * size); + ft_dprintf(tmpfd(), "calloc(%d, %d): overflow\n", nmemb, size); + /* flog("calloc: integer overflow", nmemb * size); */ } return NULL; } @@ -14,8 +15,8 @@ void *calloc(size_t nmemb, size_t size) { while (size % ALIGNMENT != 0) size++; if (LOGGING) { - /* dprintf(tmpfd(), "calloc(%zu, %zu)\n", nmemb, size); */ - flog("calloc(): ", size * nmemb); + ft_dprintf(tmpfd(), "calloc(%d, %d)\n", nmemb, size); + /* flog("calloc(): ", size * nmemb); */ } lock_mutex(); void *res = _malloc(nmemb * size); diff --git a/src/free.c b/src/free.c index 78c00f3..2eee793 100644 --- a/src/free.c +++ b/src/free.c @@ -10,8 +10,8 @@ void _free(void *ptr) { t_alloc *alloc = find_alloc_ptr(ptr); if (!alloc) { if (LOGGING) { - /* dprintf(tmpfd(), "free(): invalid pointer\n"); */ - flog("free: invalid pointer", (size_t)ptr); + ft_dprintf(tmpfd(), "free(%p): invalid pointer\n", ptr); + /* flog("free: invalid pointer", (size_t)ptr); */ } return; } @@ -36,8 +36,8 @@ void _free(void *ptr) { void free(void *ptr) { if (LOGGING) { - /* dprintf(tmpfd(), "free()\n"); */ - flog("free(): ", (size_t)ptr); + ft_dprintf(tmpfd(), "free(%p)\n", ptr); + /* flog("free(): ", (size_t)ptr); */ } if (!ptr) return; diff --git a/src/global_init.c b/src/global_init.c index 3819839..03039a9 100644 --- a/src/global_init.c +++ b/src/global_init.c @@ -23,30 +23,29 @@ int tmpfd() { return fd; } -void ft_strcat_nbr(char *dest, size_t nbr) { - int i; - - i = 0; - if (nbr < 0) { - dest[ft_strlen(dest)] = '-'; - nbr = -nbr; - } - if (nbr > 9) { - ft_strcat_nbr(dest, nbr / 10); - ft_strcat_nbr(dest, nbr % 10); - } else { - dest[ft_strlen(dest)] = nbr + '0'; - } -} - -void flog(char *title, size_t size) { - if (ft_strlen(title) + ft_nbrlen(size) >= 128) - return; - char buffer[128]; - ft_bzero(&buffer, 128); - ft_strlcpy(buffer, title, ft_strlen(title) + 1); - /* buffer[ft_strlen(buffer)] = ' '; */ - ft_strcat_nbr(buffer, size); - buffer[ft_strlen(buffer)] = '\n'; - ft_putstr_fd(buffer, tmpfd()); -} +/* void ft_strcat_nbr(char *dest, size_t nbr) { */ +/* int i; */ +/**/ +/* i = 0; */ +/* if (nbr < 0) { */ +/* dest[ft_strlen(dest)] = '-'; */ +/* nbr = -nbr; */ +/* } */ +/* if (nbr > 9) { */ +/* ft_strcat_nbr(dest, nbr / 10); */ +/* ft_strcat_nbr(dest, nbr % 10); */ +/* } else { */ +/* dest[ft_strlen(dest)] = nbr + '0'; */ +/* } */ +/* } */ +/**/ +/* void flog(char *title, size_t size) { */ +/* if (ft_strlen(title) + ft_nbrlen(size) >= 128) */ +/* return; */ +/* char buffer[128]; */ +/* ft_bzero(&buffer, 128); */ +/* ft_strlcpy(buffer, title, ft_strlen(title) + 1); */ +/* ft_strcat_nbr(buffer, size); */ +/* buffer[ft_strlen(buffer)] = '\n'; */ +/* ft_putstr_fd(buffer, tmpfd()); */ +/* } */ diff --git a/src/malloc.c b/src/malloc.c index 62f1be3..1015052 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -11,8 +11,8 @@ void *_malloc(size_t size) { void *malloc(size_t size) { if (LOGGING) { - /* dprintf(tmpfd(), "malloc(%zu)\n", size); */ - flog("malloc(): ", size); + ft_dprintf(tmpfd(), "malloc(%d)\n", size); + /* flog("malloc(): ", size); */ } if (!size) size = 1; diff --git a/src/realloc.c b/src/realloc.c index f78200d..760163b 100644 --- a/src/realloc.c +++ b/src/realloc.c @@ -7,8 +7,8 @@ void *_realloc(void *ptr, size_t size) { t_alloc *alloc = find_alloc_ptr(ptr); if (!alloc) { if (LOGGING) { - /* dprintf(tmpfd(), "realloc(%zu): invalid pointer\n", size); */ - flog("realloc: invalid pointer", (size_t)ptr); + ft_dprintf(tmpfd(), "realloc(%p, %d): invalid pointer\n", ptr, size); + /* flog("realloc: invalid pointer", (size_t)ptr); */ } return NULL; } @@ -39,8 +39,8 @@ void *_realloc(void *ptr, size_t size) { void *realloc(void *ptr, size_t size) { if (LOGGING) { - /* dprintf(tmpfd(), "realloc(%zu)\n", size); */ - flog("realloc(): ", size); + ft_dprintf(tmpfd(), "realloc(%p, %d)\n", ptr, size); + /* flog("realloc(): ", size); */ } while (size % ALIGNMENT != 0) size++; diff --git a/src/reallocarray.c b/src/reallocarray.c index d66e3fa..bcd4b1f 100644 --- a/src/reallocarray.c +++ b/src/reallocarray.c @@ -5,8 +5,9 @@ void *reallocarray(void *ptr, size_t nmemb, size_t size) { size_t overflow = nmemb * size; if (size && overflow / size != nmemb) { if (LOGGING) { - /* dprintf(tmpfd(), "reallocarray(%zu, %zu): overflow\n", nmemb, size); */ - flog("reallocarray(): integer overflow", (size_t)ptr); + ft_dprintf(tmpfd(), "reallocarray(%p, %d, %d): overflow\n", ptr, nmemb, + size); + /* flog("reallocarray(): integer overflow", (size_t)ptr); */ } errno = ENOMEM; return NULL; @@ -16,8 +17,8 @@ void *reallocarray(void *ptr, size_t nmemb, size_t size) { while (size % ALIGNMENT != 0) size++; if (LOGGING) { - /* dprintf(tmpfd(), "reallocarray(%zu, %zu)\n", nmemb, size); */ - flog("reallocarray(): ", (size_t)ptr); + ft_dprintf(tmpfd(), "reallocarray(%p, %d, %d)\n", ptr, nmemb, size); + /* flog("reallocarray(): ", (size_t)ptr); */ } void *res = NULL; lock_mutex();