From ce8b634aebd092ad46edcd6f168e7799d42084fc Mon Sep 17 00:00:00 2001 From: Gregory Date: Thu, 9 May 2019 23:53:14 +0300 Subject: [PATCH] add show_alloc_mem --- src/show_alloc_mem.c | 37 +++++++++++++++++++++++++++++++------ t/fuzzy_fast.c | 3 ++- t/malloc_tests.c | 7 ++++--- t/meson.build | 2 +- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/show_alloc_mem.c b/src/show_alloc_mem.c index 580e524..aa66ab9 100644 --- a/src/show_alloc_mem.c +++ b/src/show_alloc_mem.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* show_alloc_mem.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: gtertysh +#+ +:+ +#+ */ +/* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/05/08 21:45:17 by gtertysh #+# #+# */ -/* Updated: 2019/05/08 21:45:41 by gtertysh ### ########.fr */ +/* Updated: 2019/05/09 23:46:34 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,10 +15,32 @@ #include "libft.h" #include "stdio.h" +static void print_addr(void *addr) +{ + size_t i; + size_t bit_4; + size_t count; + + count = sizeof(addr) * 2; + i = 0; + while (i < count) + { + bit_4 = ((size_t)addr >> ((count - i - 1) * 4)) & 0xf; + if (bit_4 < 10) + ft_putnbr(bit_4); + else + ft_putchar('A' + bit_4 - 10); + i++; + } +} + static void print_size(void *addr, size_t size) { - printf("%p - %p : size %zu", addr, addr + size, size); - fflush(stdout); + print_addr(addr + size); + ft_putstr(" - "); + print_addr(addr); + ft_putstr(" : size "); + ft_putnbr(size); } static void print_arena(t_arena *arena) @@ -30,14 +52,17 @@ static void print_arena(t_arena *arena) else ft_putstr("LARGE : "); print_size(arena, arena->size); - ft_putstr("\n"); + ft_putstr(" of ARENA\n"); } static void print_chunk(t_chunk *chunk) { ft_putstr(" "); print_size(chunk, chunk->size); - ft_putstr("\n"); + if (chunk->is_free) + ft_putstr(" of free block\n"); + else + ft_putstr(" of not free block\n"); } void show_alloc_mem(void) diff --git a/t/fuzzy_fast.c b/t/fuzzy_fast.c index c683522..8858cec 100644 --- a/t/fuzzy_fast.c +++ b/t/fuzzy_fast.c @@ -10,6 +10,7 @@ #include #include +#include "ft_malloc.h" struct item { void *ptr; @@ -105,7 +106,7 @@ int main(int argc, const char *const argv[]) { } } - /* show_alloc_mem(); */ + show_alloc_mem(); } close(dev_urandom_fd); diff --git a/t/malloc_tests.c b/t/malloc_tests.c index 909d315..f672133 100644 --- a/t/malloc_tests.c +++ b/t/malloc_tests.c @@ -6,7 +6,7 @@ /* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/05/06 22:35:37 by foton #+# #+# */ -/* Updated: 2019/05/08 19:47:45 by gtertysh ### ########.fr */ +/* Updated: 2019/05/09 23:49:52 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -98,9 +98,10 @@ int realloc_return_same_pointer(void) t_chunk *new_ptr; g_base.next = &g_base; - ptr = malloc(20); - new_ptr = realloc(ptr, 10); + ptr = malloc(20000); + new_ptr = realloc(ptr, 100); _IS(ptr == new_ptr); + // show_alloc_mem(); _END("realloc_return_same_pointer"); } diff --git a/t/meson.build b/t/meson.build index b940a5d..f9239be 100644 --- a/t/meson.build +++ b/t/meson.build @@ -27,4 +27,4 @@ malloc_fuzzy_fast = executable( test('basic', malloc_tests) test('fuzzy', malloc_fuzzy, args: ['10', '1000', '0', '10000']) -test('fuzzy_fast', malloc_fuzzy_fast, args: ['100', '1000', '0', '10000']) \ No newline at end of file +test('fuzzy_fast', malloc_fuzzy_fast, args: ['10', '1000', '0', '10000']) \ No newline at end of file