NeuroTIC 0.0
Loading...
Searching...
No Matches
ntmemory.c File Reference

Implementation of memory management functions. More...

#include "ntmemory.h"
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for ntmemory.c:

Go to the source code of this file.

Functions

void memfree (void)
 Frees all memory blocks registered in the internal tracking registry.
 
void * memtrack (void *mem)
 Registers a memory block for tracking and automatic cleanup.
 

Variables

static void ** mem_register = NULL
 Internal registry for tracking allocated memory blocks.
 
static unsigned int mem_count = 0
 Counter for the number of tracked memory blocks.
 

Detailed Description

Implementation of memory management functions.

Provides functions for tracking dynamically allocated memory blocks and ensuring they are freed at program termination.
Uses a dynamic registry to store pointers to allocated memory, and registers a cleanup function with atexit() to automatically free all tracked memory when the program exits.

Author
Oscar Sotomayor.
Date
2026

Definition in file ntmemory.c.

Function Documentation

◆ memfree()

void memfree ( void  )

Frees all memory blocks registered in the internal tracking registry.

Frees all memory blocks registered in the internal tracking registry.
Iterates through the mem_register array, freeing each pointer and setting it to NULL to prevent dangling references.
After all tracked memory has been freed, the registry itself is freed and set to NULL.
This function is registered with atexit() to ensure that it is called automatically when the program terminates, providing a safety net for memory management and preventing leaks.

References mem_count, and mem_register.

Referenced by memtrack().

◆ memtrack()

void * memtrack ( void *  mem)

Registers a memory block for tracking and automatic cleanup.

Registers a dynamically allocated memory block for deferred release.
Adds a pointer to the internal memory registry. The registry grows dynamically as new allocations are tracked.
This function is intended to be called immediately after successful dynamic allocation (e.g. malloc, calloc, realloc).
On the first invocation, memfree() is automatically registered to execute at program termination via atexit().

References mem_count, mem_register, and memfree().

Variable Documentation

◆ mem_count

unsigned int mem_count = 0
static

Counter for the number of tracked memory blocks.

This counter keeps track of how many pointers have been registered in the mem_register array.
It is incremented each time memtrack() is called and decremented as memory blocks are freed in memfree().
The counter is used to manage the size of the registry and to ensure that all tracked memory is properly released when memfree() is invoked.

Warning
This counter must accurately reflect the number of valid pointers in the mem_register.
Any discrepancies (e.g., due to manual freeing of tracked memory) can lead to undefined behavior when memfree() is called, as it may attempt to free invalid pointers or miss freeing valid ones.

Referenced by memfree(), and memtrack().

◆ mem_register

void** mem_register = NULL
static

Internal registry for tracking allocated memory blocks.

This dynamic array stores pointers to all memory blocks that have been registered via memtrack().
The registry allows for collective management of memory, enabling the memfree() function to iterate through all tracked pointers and free them at once.
The registry is dynamically resized as new pointers are added, and is freed itself when memfree() is called.
This approach simplifies memory management by centralizing the tracking and cleanup of dynamically allocated memory within the NeuroTIC system.

Warning
This registry assumes exclusive ownership of all tracked pointers.
Manually freeing any pointer that has been registered will lead to undefined behavior when memfree() is called, as it will attempt to free already freed memory.

Referenced by memfree(), and memtrack().