From dc112a5b7e99934282828909cf66debf206255de Mon Sep 17 00:00:00 2001 From: Gregory Date: Wed, 8 May 2019 21:46:06 +0300 Subject: [PATCH] add show_alloc_mem with printf --- meson.build | 4 ++- src/show_alloc_mem.c | 60 ++++++++++++++++++++++++++++++++++++++++++++ t/malloc_tests.c | 2 ++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/show_alloc_mem.c diff --git a/meson.build b/meson.build index 3dd9c3e..5ba66f8 100644 --- a/meson.build +++ b/meson.build @@ -31,7 +31,9 @@ sources = [ 'src/malloc.c', 'src/free.c', 'src/realloc.c', - 'src/get_arena_type.c' + 'src/calloc.c', + 'src/get_arena_type.c', + 'src/show_alloc_mem.c', ] libft = subproject('libft') diff --git a/src/show_alloc_mem.c b/src/show_alloc_mem.c new file mode 100644 index 0000000..580e524 --- /dev/null +++ b/src/show_alloc_mem.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* show_alloc_mem.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/05/08 21:45:17 by gtertysh #+# #+# */ +/* Updated: 2019/05/08 21:45:41 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_malloc_internal.h" +#include "ft_malloc.h" +#include "libft.h" +#include "stdio.h" + +static void print_size(void *addr, size_t size) +{ + printf("%p - %p : size %zu", addr, addr + size, size); + fflush(stdout); +} + +static void print_arena(t_arena *arena) +{ + if (arena->type == TINY) + ft_putstr("TINY : "); + else if (arena->type == SMALL) + ft_putstr("SMALL : "); + else + ft_putstr("LARGE : "); + print_size(arena, arena->size); + ft_putstr("\n"); +} + +static void print_chunk(t_chunk *chunk) +{ + ft_putstr(" "); + print_size(chunk, chunk->size); + ft_putstr("\n"); +} + +void show_alloc_mem(void) +{ + t_arena *arena_runner; + t_chunk *heap_runner; + + arena_runner = g_base.next; + while (arena_runner != &g_base) + { + heap_runner = arena_runner->heap; + print_arena(arena_runner); + while (heap_runner) + { + print_chunk(heap_runner); + heap_runner = heap_runner->next; + } + arena_runner = arena_runner->next; + } +} diff --git a/t/malloc_tests.c b/t/malloc_tests.c index 97d4601..909d315 100644 --- a/t/malloc_tests.c +++ b/t/malloc_tests.c @@ -35,6 +35,7 @@ int returns_not_null_pointer(void) _IS(ptr->next == NULL); _IS(ptr->prev != NULL); _IS(ptr->prev->size > TINY * NALLOC && ptr->prev->size < SMALL * NALLOC); + // show_alloc_mem(); _END("returns_not_null_pointer"); } @@ -87,6 +88,7 @@ int malloc_creates_new_arena(void) _IS(g_base.next->next != &g_base); _IS(g_base.next->type == TINY); _IS(g_base.next->heap->next == second_arena_chunk - 1); + // show_alloc_mem(); _END("malloc_creates_new_arena"); }