Universal-Garbage is a lightweight Single File C library designed to mitigate memory leak issues by providing a structured approach to memory management within scopes. It employs a garbage collection mechanism to automatically handle memory deallocation, thereby reducing the risk of memory leaks and simplifying memory management for developers.
#include "UniversalGarbageOne.c"
typedef struct Car{
char *name;
char *model;
}Car;
Car *newCar(char *name, char *model){
Car *self = (Car*)malloc(sizeof(Car));
self->name = strdup(name);
self->model = strdup(model);
return self;
}
void Car_free(Car *self){
free(self->name);
free(self->model);
free(self);
}
int main(){
UniversalGarbage *garbage = newUniversalGarbage();
Car *ferrari = newCar("ferrari","red");
UniversalGarbage_add(garbage, Car_free,ferrari);
printf("name %s\n",ferrari->name);
printf("model %s\n",ferrari->model);
UniversalGarbage_free(garbage);
}| Item | Description |
|---|---|
| UniversalGarbage.c | Definition |
| UniversalGarbage.h | Declaration |
| UniversalGarbageOne.c | One file version |
| UniversalGarbage.zip | All files |
| Item | What It Is |
|---|---|
| Build and Install | Instructions for building and installing the library |
| Basic Usage | Demonstrates the basic usage of the library with a Car object example |
| Add Simple | Shows how to add simple types to the garbage collector |
| Disconnect | Explains how to disconnect resources from the garbage collector |
| Resset | Describes resetting memory for an object |
| Reallocate | Details how to reallocate memory |
| Set Return | Explains how to handle return values and error management |