From 8849d801b9d3767390e3e1ed6b562db738ac1bcb Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Fri, 27 Feb 2026 11:04:07 +0100 Subject: Add show_alloc_mem and test_show, rename test to test_preload Implement show_alloc_mem() to print all zones and allocations by ascending address. Add test_show binary that links directly against libft_malloc.so to exercise it. --- Makefile | 2 +- src/show_alloc_mem.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/Makefile | 25 ++++++++++---- tests/src/main.c | 46 ------------------------- tests/src/test_preload.c | 46 +++++++++++++++++++++++++ tests/src/test_show.c | 25 ++++++++++++++ 6 files changed, 177 insertions(+), 54 deletions(-) create mode 100644 src/show_alloc_mem.c delete mode 100644 tests/src/main.c create mode 100644 tests/src/test_preload.c create mode 100644 tests/src/test_show.c diff --git a/Makefile b/Makefile index 2935da6..79d9f2d 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ LIBFT = $(LIBFT_DIR)/lib/libft.a NAME = $(LIBDIR)/libft_malloc_$(HOSTTYPE).so LINK = $(LIBDIR)/libft_malloc.so -SRCS = malloc.c free.c realloc.c zone.c +SRCS = malloc.c free.c realloc.c zone.c show_alloc_mem.c OBJS = $(SRCS:%.c=$(OBJDIR)/%.o) diff --git a/src/show_alloc_mem.c b/src/show_alloc_mem.c new file mode 100644 index 0000000..6fd5e37 --- /dev/null +++ b/src/show_alloc_mem.c @@ -0,0 +1,87 @@ +/** + * @file show_alloc_mem.c + * @brief Print every allocated zone and block to stdout. + */ + +#include "libft.h" +#include "malloc.h" +#include "malloc_internal.h" + +static void +_s_put_ptr (void *ptr) +{ + const char hex[] = "0123456789abcdef"; + char buf[20]; + unsigned long value; + int i; + + value = (unsigned long)ptr; + i = (int)sizeof (buf); + while (value) + { + buf[--i] = hex[value % 16]; + value /= 16; + } + buf[--i] = 'x'; + buf[--i] = '0'; + ft_putstr_fd (buf + i, 1); +} + +static void +_s_put_size (size_t n) +{ + if (n >= 10) + _s_put_size (n / 10); + ft_putchar_fd ('0' + (n % 10), 1); +} + +static size_t +_s_print_zone (char *label, t_zone *zone) +{ + t_chunk *chunk; + void *start; + void *end; + size_t total; + + total = 0; + while (zone) + { + ft_putstr_fd (label, 1); + ft_putstr_fd (" : ", 1); + _s_put_ptr (zone); + ft_putchar_fd ('\n', 1); + chunk = (t_chunk *)((char *)zone + ALIGN (sizeof (t_zone))); + while (chunk) + { + if (!chunk->is_free) + { + start = (char *)chunk + ALIGN (sizeof (t_chunk)); + end = (char *)start + chunk->size; + _s_put_ptr (start); + ft_putstr_fd (" - ", 1); + _s_put_ptr (end); + ft_putstr_fd (" : ", 1); + _s_put_size (chunk->size); + ft_putendl_fd (" bytes", 1); + total += chunk->size; + } + chunk = chunk->next; + } + zone = zone->next; + } + return (total); +} + +void +show_alloc_mem (void) +{ + size_t total; + + total = 0; + total += _s_print_zone ("TINY", g_heap.tiny); + total += _s_print_zone ("SMALL", g_heap.small); + total += _s_print_zone ("LARGE", g_heap.large); + ft_putstr_fd ("Total : ", 1); + _s_put_size (total); + ft_putendl_fd (" bytes", 1); +} diff --git a/tests/Makefile b/tests/Makefile index 04d7c1f..b964db5 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -5,18 +5,26 @@ SRCDIR = src OBJDIR = obj BINDIR = bin -NAME = $(BINDIR)/test +NAME_PRELOAD = $(BINDIR)/test_preload +NAME_SHOW = $(BINDIR)/test_show -SRCS = main.c +SRCS = test_preload.c OBJS = $(SRCS:%.c=$(OBJDIR)/%.o) MALLOC_LIB = ../lib/libft_malloc.so +MALLOC_INC = ../inc -$(NAME): $(OBJS) | $(BINDIR) +$(NAME_PRELOAD): $(OBJS) | $(BINDIR) $(CC) $(CFLAGS) -o $@ $(OBJS) -all: $(NAME) +$(NAME_SHOW): $(OBJDIR)/test_show.o $(MALLOC_LIB) | $(BINDIR) + $(CC) $(CFLAGS) -o $@ $(OBJDIR)/test_show.o -L ../lib -lft_malloc -Wl,-rpath,'$$ORIGIN/../../lib' + +$(OBJDIR)/test_show.o: $(SRCDIR)/test_show.c | $(OBJDIR) + $(CC) $(CFLAGS) -I $(MALLOC_INC) -c $< -o $@ + +all: $(NAME_PRELOAD) $(NAME_SHOW) $(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR) $(CC) $(CFLAGS) -c $< -o $@ @@ -27,12 +35,15 @@ $(OBJDIR): $(BINDIR): mkdir -p $(BINDIR) -test: $(NAME) +test: $(NAME_PRELOAD) $(NAME_SHOW) @echo "=== system malloc ===" - @$(NAME) + @$(NAME_PRELOAD) @echo "" @echo "=== ft_malloc ===" - @LD_PRELOAD=$(MALLOC_LIB) $(NAME) + @LD_PRELOAD=$(MALLOC_LIB) $(NAME_PRELOAD) + @echo "" + @echo "=== show_alloc_mem ===" + @$(NAME_SHOW) clean: rm -rf $(OBJDIR) $(BINDIR) diff --git a/tests/src/main.c b/tests/src/main.c deleted file mode 100644 index 0ded605..0000000 --- a/tests/src/main.c +++ /dev/null @@ -1,46 +0,0 @@ -/** - * @file main.c - * @brief Smoke test — call malloc once and print the returned pointer. - * - * Uses write(2) instead of printf to avoid stdio calling malloc - * internally, which would pollute output under LD_PRELOAD. - */ - -#include -#include - -/** @brief Write the hex representation of @p ptr to stdout. */ -static void -_s_put_ptr (void *ptr) -{ - const char hex[] = "0123456789abcdef"; - char buf[20]; - unsigned long v = (unsigned long)ptr; - int i; - - if (!ptr) - { - write (1, "(nil)", 5); - return; - } - i = (int)sizeof (buf); - while (v) - { - buf[--i] = hex[v % 16]; - v /= 16; - } - buf[--i] = 'x'; - buf[--i] = '0'; - write (1, buf + i, (size_t)(sizeof (buf) - i)); -} - -int -main (void) -{ - void *p = malloc (42); - write (1, "malloc(42) = ", 13); - _s_put_ptr (p); - write (1, "\n", 1); - free (p); - return (0); -} diff --git a/tests/src/test_preload.c b/tests/src/test_preload.c new file mode 100644 index 0000000..0ded605 --- /dev/null +++ b/tests/src/test_preload.c @@ -0,0 +1,46 @@ +/** + * @file main.c + * @brief Smoke test — call malloc once and print the returned pointer. + * + * Uses write(2) instead of printf to avoid stdio calling malloc + * internally, which would pollute output under LD_PRELOAD. + */ + +#include +#include + +/** @brief Write the hex representation of @p ptr to stdout. */ +static void +_s_put_ptr (void *ptr) +{ + const char hex[] = "0123456789abcdef"; + char buf[20]; + unsigned long v = (unsigned long)ptr; + int i; + + if (!ptr) + { + write (1, "(nil)", 5); + return; + } + i = (int)sizeof (buf); + while (v) + { + buf[--i] = hex[v % 16]; + v /= 16; + } + buf[--i] = 'x'; + buf[--i] = '0'; + write (1, buf + i, (size_t)(sizeof (buf) - i)); +} + +int +main (void) +{ + void *p = malloc (42); + write (1, "malloc(42) = ", 13); + _s_put_ptr (p); + write (1, "\n", 1); + free (p); + return (0); +} diff --git a/tests/src/test_show.c b/tests/src/test_show.c new file mode 100644 index 0000000..e5e7dca --- /dev/null +++ b/tests/src/test_show.c @@ -0,0 +1,25 @@ +/** + * @file test_show.c + * @brief Test show_alloc_mem — links directly against libft_malloc. + */ + +#include "malloc.h" + +int +main (void) +{ + void *a = malloc (42); + void *b = malloc (100); + void *c = malloc (500); + void *d = malloc (2000); + (void)a; + (void)b; + (void)c; + (void)d; + show_alloc_mem (); + free (a); + free (b); + free (c); + free (d); + return (0); +} -- cgit v1.2.3