From 8dd24e5a5fdc50c802c532ed11d5822e37bce901 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 11 Jan 2026 11:24:16 +0200 Subject: [PATCH] feat: initial version, incomplete but functional --- .clang-format | 22 +++++++++++++++ .gitignore | 1 + README.md | 2 +- makefile | 18 ++++++++++++ nileakdetector.h | 56 ++++++++++++++++++++++++++++++++++++++ test/nileakdetector_test.c | 26 ++++++++++++++++++ 6 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 .clang-format create mode 100644 .gitignore create mode 100644 makefile create mode 100644 nileakdetector.h create mode 100644 test/nileakdetector_test.c diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..5475a73 --- /dev/null +++ b/.clang-format @@ -0,0 +1,22 @@ +AlignAfterOpenBracket: DontAlign +AlignEscapedNewlines: DontAlign +AlignOperands: DontAlign +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: true +AllowShortEnumsOnASingleLine: true +AllowShortIfStatementsOnASingleLine: true +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterDefinitionReturnType: TopLevel +BreakBeforeTernaryOperators: false +BinPackArguments: false +BinPackParameters: false +BreakBeforeBraces: WebKit +IndentCaseLabels: false +TabWidth: 4 +IndentWidth: 4 +ContinuationIndentWidth: 4 +UseTab: ForContinuationAndIndentation +ColumnLimit: 0 +ReflowComments: false +SortIncludes: false +SpaceBeforeParens: false \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6dd29b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin/ \ No newline at end of file diff --git a/README.md b/README.md index b17a369..c3d2ec7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # ni-leakdetector -The Leak Detector of the NI-C system \ No newline at end of file +Leak detector of the NI-C system \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..f6b8451 --- /dev/null +++ b/makefile @@ -0,0 +1,18 @@ +RELEASE_flags=-DNDEBUG -O2 -g0 +DEBUG_flags=-std=c23 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og + +clean: + rm -fr bin + +format: + clang-format -i test/nileakdetector_test.c + clang-format -i nileakdetector.h + +test: bin/test + bin/test/nileakdetector_test + +.PHONY: clean format test leaks + +bin/test: test/nileakdetector_test.c + mkdir -p bin/test + cc ${DEBUG_flags} test/nileakdetector_test.c -o bin/test/nileakdetector_test \ No newline at end of file diff --git a/nileakdetector.h b/nileakdetector.h new file mode 100644 index 0000000..6091d8c --- /dev/null +++ b/nileakdetector.h @@ -0,0 +1,56 @@ +#pragma once + +#include +#include + +#ifndef NILEAKDETECTOR_VERBOSE +#define NILEAKDETECTOR_VERBOSE false +#endif + +typedef struct _NILeakDetectorData { + size_t n_malloc; + size_t n_free; +} NILeakDetectorData; + +static NILeakDetectorData nileakdetector_data = { + .n_malloc = 0, + .n_free = 0 +}; + +// TODO: calloc, realloc +// TODO: pass __FILE__ and __LINE__ so it prints the location of the operations + +void *nileakdetector_malloc(size_t block_size) { + void *result = malloc(block_size); + if (result == NULL) { + if(NILEAKDETECTOR_VERBOSE) + printf("[NILeakDetector] Erroneous Allocation (NULL)\n"); + } else { + if(NILEAKDETECTOR_VERBOSE) + printf("[NILeakDetector] Allocated %ld Bytes (%p)\n", block_size, result); + nileakdetector_data.n_malloc++; + } + return result; +} + +void nileakdetector_free(void *block) { + free(block); + if(NILEAKDETECTOR_VERBOSE) + printf("[NILeakDetector] Freed pointer (%p)\n", block); + nileakdetector_data.n_free++; +} + +void nileakdetector_print_summary() { + printf("[NILeakDetector] == SUMMARY ==\n"); + printf("[NILeakDetector] Number of mallocs: %ld\n", nileakdetector_data.n_malloc); + printf("[NILeakDetector] Number of frees: %ld\n", nileakdetector_data.n_free); + if (nileakdetector_data.n_malloc != nileakdetector_data.n_free) { + printf("[NILeakDetector] WARNING: POSSIBLE MEMORY LEAKS\n"); + } +} + +#undef malloc +#undef free + +#define malloc(sz) nileakdetector_malloc(sz) +#define free(p) nileakdetector_free(p) diff --git a/test/nileakdetector_test.c b/test/nileakdetector_test.c new file mode 100644 index 0000000..cf18b5a --- /dev/null +++ b/test/nileakdetector_test.c @@ -0,0 +1,26 @@ + +#define NILEAKDETECTOR_VERBOSE true +#include "../nileakdetector.h" + +#include + +void +test_malloc() +{ + int *x = malloc(sizeof(int)); + *x = 10; + printf("X = %d\n", *x); // Should print the right value of X + printf("\nAt this point the summary should indicate leaks...\n"); + nileakdetector_print_summary(); + printf("\nAfter this free it should be all good...\n"); + free(x); + nileakdetector_print_summary(); +} + +int +main() +{ + test_malloc(); + printf("== TESTS SUCCESSFUL ==\n"); + return 0; +}