finished show_mem_alloc_ex

This commit is contained in:
2025-02-15 16:22:08 +01:00
parent f5b2f8b8bf
commit 0a64743980
4 changed files with 76 additions and 17 deletions

View File

@ -22,7 +22,9 @@ SRCS_NAMES = malloc.c \
realloc.c\ realloc.c\
utils.c\ utils.c\
env_debug.c\ env_debug.c\
program_end.c program_end.c\
hex_dump.c\
show_alloc_mem_ex.c
SRCS_DIR = srcs SRCS_DIR = srcs

6
main.c
View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/14 17:19:23 by tomoron #+# #+# */ /* Created: 2025/02/14 17:19:23 by tomoron #+# #+# */
/* Updated: 2025/02/14 17:19:59 by tomoron ### ########.fr */ /* Updated: 2025/02/15 15:00:37 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,5 @@ int main(void)
ptr = malloc(1203); ptr = malloc(1203);
show_alloc_mem(); show_alloc_mem();
ptr = realloc(ptr, 12045); ptr = realloc(ptr, 12045);
show_alloc_mem(); show_alloc_mem_ex();
free(ptr);
show_alloc_mem();
} }

View File

@ -6,40 +6,98 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/14 18:03:26 by tomoron #+# #+# */ /* Created: 2025/02/14 18:03:26 by tomoron #+# #+# */
/* Updated: 2025/02/14 19:38:12 by tomoron ### ########.fr */ /* Updated: 2025/02/15 16:19:00 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "includes/malloc.h" #include "includes/malloc.h"
static void hex_dump_show_address(long unsigned addr)
static void put_hex_padded(t_ul addr, int padding, int prefix)
{ {
i int len;
t_ul tmp;
len = 1;
tmp = addr;
while (tmp >= 16)
{
tmp /= 16;
len++;
}
if(prefix)
write(1, "0x", 2);
while(len < padding)
{
write(1, "0", 1);
len++;
}
put_ulnbr_base(addr, "0123456789abcdef");
}
static void put_char_dot(char c)
{
if(c >= 32 && c <= 126)
write(1, &c, 1);
else
write(1, ".", 1);
} }
static void hex_dump_show_line(char *addr, int len) static void hex_dump_show_line(char *addr, int len)
{ {
hex_dump_show_address((long unsigned)addr); int i;
put_hex_padded((long unsigned)addr, 16, 1);
write(1, " ", 2);
i = 0;
while(i < 16)
{
if(i < len)
put_hex_padded(addr[i], 2, 0);
else
write(1, " ", 2);
write(1, " ", 1);
if(i == 7 || i == 15)
write(1, " ", 1);
i++;
}
i = 0;
write(1, "|", 1);
while(i < len)
{
put_char_dot(addr[i]);
i++;
}
write(1, "|", 1);
write(1, "\n", 1);
(void)len;
} }
void hex_dump(char *addr, size_t nb) void hex_dump(char *addr, size_t nb)
{ {
char buf[16]; char buf[16];
size_t cur; size_t cur;
int lst_same;
cur = 0; cur = 0;
while (cur < nb) lst_same = 0;
while (cur + 16 < nb)
{ {
if(cur && !ft_memcmp(buf, addr + cur, 16)) if(cur && !ft_memcmp(buf, addr + cur, 16))
{ {
cur += 16; cur += 16;
if(!lst_same)
write(1, "*\n", 2);
lst_same = 1;
continue; continue;
} }
lst_same = 0;
ft_memcpy(buf, addr + cur, 16); 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); hex_dump_show_line(addr + cur, 16);
cur += 16; cur += 16;
} }
if(cur < nb)
hex_dump_show_line(addr + cur, nb - cur);
put_hex_padded((long unsigned)addr, 16, 1);
write(1, "\n", 1);
} }

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: 2025/02/14 18:16:06 by tomoron ### ########.fr */ /* Updated: 2025/02/15 15:00:21 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -70,13 +70,14 @@ size_t show_pre_allocated(char *type, t_mem_chunk *chunk, int dump);
size_t show_large(int dump); size_t show_large(int dump);
void put_ulnbr_base(t_ul nbr, char *base); void put_ulnbr_base(t_ul nbr, char *base);
void hex_dump(char *addr, size_t nb); void hex_dump(char *addr, size_t nb);
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);
void *malloc(size_t size); void *malloc(size_t size);
t_settings *get_settings(void); t_settings *get_settings(void);
void show_alloc_mem(void); void show_alloc_mem(void);
void show_alloc_mem_ex(void);
void free(void *ptr); void free(void *ptr);
void *realloc(void *ptr, size_t size); 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