- Simple pointers tracer to free all traced pointers at once
- Now supports scoped and global tracing
- To build an example
cd examples
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 -g ../garbage*.c <example file>
global.c
: How to use global tracingscoped.c
: How to use scoped tracingmixed.c
: How to use both global and scoped tracingbinging.c
: How to use both global and scoped binginguntrace.c
: How to use both global and scoped untrace
#include <stdio.h>
#include <string.h>
#include "garbage.h"
int main(void) {
char *str = strdup("hello"); /* allocate some data */
global_trace(str); /* use the global scope to trace the str pointer */
printf("str: %s\n", str); /* it should print normally */
global_free(); /* free the global scope */
printf("str: %s\n", str); /* it should print garbage */
return 0;
}
#include <stdio.h>
#include <string.h>
#include "garbage.h"
int main(void) {
garbage scope = NULL; /* initialize the scope object to NULL */
char *str = strdup("hello"); /* allocate some data */
scoped_trace(&scope, str); /* trace the str pointer */
printf("str: %s\n", str); /* it should print normally */
scoped_free(&scope); /* free the scope */
printf("str: %s\n", str); /* it should print garbage now */
return 0;
}
- Trace a pointer to free later
Returns:
0
on fail and1
on success
int scoped_trace(garbage *scope, void *ptr);
int global_trace(void *ptr);
- Untrace already traced pointer
void scoped_untrace(garbage *scope, void *ptr);
void global_untrace(void *ptr);
- Bind or add tracers from scope to another and destroy the source scope.
- Also bind or add tracers from scope to global scope and destroy the source.
Returns:
0
on fail and1
on success
void scoped_bind(global_scope *dest, global_trace *src);
void global_bind(global_trace *scope);
- Free all traced pointers
void scoped_free(garbage *scope);
void global_free(void);
- Check if scope is tracing a pointer
Returns:
0
on fail and1
on success
int scoped_has(garbage *scope, void *ptr);
int global_has(void *ptr);