feat: initial version, incomplete but functional
This commit is contained in:
56
nileakdetector.h
Normal file
56
nileakdetector.h
Normal 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)
|
||||
Reference in New Issue
Block a user