From 2df0cd9d2640d1c72b5a55dbd9885e0131cbd69d Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Thu, 13 Oct 2022 09:08:05 -0400 Subject: [PATCH] Begin integrating new Memory API --- src/Telodendria.c | 53 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/src/Telodendria.c b/src/Telodendria.c index 3259728..599174e 100644 --- a/src/Telodendria.c +++ b/src/Telodendria.c @@ -33,6 +33,7 @@ #include #include +#include #include #include #include @@ -40,7 +41,48 @@ #include #include -HttpServer *httpServer = NULL; +static void TelodendriaMemoryHook(MemoryAction a, MemoryInfo *i, void *args) +{ + LogConfig *lc = (LogConfig *) args; + char *action; + + switch (a) + { + case MEMORY_ALLOCATE: + action = "Allocated"; + break; + case MEMORY_REALLOCATE: + action = "Re-allocated"; + break; + case MEMORY_FREE: + action = "Freed"; + break; + default: + action = "Unknown operation on"; + break; + } + + Log(lc, LOG_DEBUG, "%s:%d %s(): %s %lu bytes of memory at %p.", + MemoryInfoGetFile(i), MemoryInfoGetLine(i), + MemoryInfoGetFunc(i), action, MemoryInfoGetSize(i), + MemoryInfoGetPointer(i)); +} + +static void +TelodendriaMemoryIterator(MemoryInfo *i, void *args) +{ + LogConfig *lc = (LogConfig *) args; + + /* We haven't freed the logger memory yet */ + if (MemoryInfoGetPointer(i) != lc) + { + Log(lc, LOG_DEBUG, "%lu bytes of memory leaked from %s() (%s:%d)", + MemoryInfoGetSize(i), MemoryInfoGetFunc(i), + MemoryInfoGetFile(i), MemoryInfoGetLine(i)); + } +} + +static HttpServer *httpServer = NULL; static void TelodendriaSignalHandler(int signalNo) @@ -114,6 +156,8 @@ main(int argc, char **argv) return EXIT_FAILURE; } + MemoryHook(TelodendriaMemoryHook, lc); + TelodendriaPrintHeader(lc); #ifdef __OpenBSD__ @@ -402,8 +446,13 @@ finish: HttpServerFree(httpServer); Log(lc, LOG_DEBUG, "Freed HTTP Server."); } - Log(lc, LOG_DEBUG, "Exiting with code '%d'.", exit); TelodendriaConfigFree(tConfig); + + Log(lc, LOG_DEBUG, ""); + MemoryIterate(TelodendriaMemoryIterator, lc); + Log(lc, LOG_DEBUG, ""); + + Log(lc, LOG_DEBUG, "Exiting with code '%d'.", exit); LogConfigFree(lc); return exit; }