feat: initial version, incomplete but functional

This commit is contained in:
2026-01-11 11:24:16 +02:00
parent 6e49342638
commit 8dd24e5a5f
6 changed files with 124 additions and 1 deletions

22
.clang-format Normal file
View File

@@ -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

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
bin/

View File

@@ -1,3 +1,3 @@
# ni-leakdetector
The Leak Detector of the NI-C system
Leak detector of the NI-C system

18
makefile Normal file
View File

@@ -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

56
nileakdetector.h Normal file
View File

@@ -0,0 +1,56 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#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)

View File

@@ -0,0 +1,26 @@
#define NILEAKDETECTOR_VERBOSE true
#include "../nileakdetector.h"
#include <stdio.h>
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;
}