A simple and complete C garbage collector, with a group-based allocation system (but also not!).
Call ez_alloc()/ez_calloc() instead of malloc()/calloc() to allocate memory. Then, forget about your pointers and just call ez_cleanup() at the end of your program to free everything. No leaks, no stress. Have a nice day!
- 42 École Norm-compliant
- Simple API: Drop-in replacement for malloc/calloc
- Automatic cleanup: One function call frees all tracked memory
- Group support: Organize allocations into named groups for selective cleanup
- Error handling: Proper error reporting with set_error and errno
#include "ezalloc.h"
int main(void)
{
// Allocate memory - it's automatically tracked
int *numbers = ez_alloc(sizeof(int) * 10);
char *text = ez_calloc(sizeof(char), 100); // Zeroed memory
// Use your memory as normal
numbers[0] = 42;
strcpy(text, "Hello, ezalloc!");
// Add external pointers to tracking
void *external = malloc(1024);
ez_add(external);
// Free specific pointer if needed
ez_release(numbers);
// At the end, free everything at once
ez_cleanup();
return 0;
}Organize allocations into named groups for more control:
#include "ezgalloc.h"
int main(void)
{
// Create groups
ezg_group_create("config");
ezg_group_create("temp");
// Allocate in specific groups
char *config = ezg_alloc("config", 1024);
int *temp_data = ezg_alloc("temp", sizeof(int) * 100);
// Release all memory in a specific group
ezg_group_release("temp");
// Or delete the group entirely
ezg_group_delete("config");
// Cleanup all groups
ezg_cleanup();
return 0;
}void *ez_alloc(size_t size)- Allocate and track memoryvoid *ez_calloc(size_t size, size_t count)- Allocate zeroed memoryvoid *ez_add(void *ptr)- Track existing pointervoid ez_release(void *ptr)- Free specific pointervoid ez_cleanup(void)- Free all tracked memorychar *ez_get_error(void)- Returns a string describing the last error encountered.
int ezg_group_create(char *group)- Create a named groupvoid *ezg_alloc(char *group, size_t size)- Allocate in groupvoid *ezg_calloc(char *group, size_t size, size_t count)- Allocate zeroed in groupvoid *ezg_add(char *group, void *ptr)- Add pointer to groupvoid ezg_release(char *group, void *ptr)- Free specific pointer in groupvoid ezg_group_release(char *group)- Free all memory in groupvoid ezg_group_delete(char *group)- Delete group and free its memoryvoid ezg_cleanup(void)- Free all groupschar *ezg_get_error(void)- Returns a string describing the last error encountered.
make # Build library
make clean # Clean build artifacts
make re # Rebuild from scratchThis creates libezalloc.a which you can link with your project:
gcc your_program.c -L. -lezalloc -o your_programRun the included test suite:
make run-test- You must include only one of the two API headers
- The library is not thread-safe
Output:


