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. --- tests/src/main.c | 46 ---------------------------------------------- tests/src/test_preload.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ tests/src/test_show.c | 25 +++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 46 deletions(-) delete mode 100644 tests/src/main.c create mode 100644 tests/src/test_preload.c create mode 100644 tests/src/test_show.c (limited to 'tests/src') 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