mGarbageCollector.c - track and free allocated resources
mGargageCollector.c is a lightweight tool for tracking and freeing allocated resources. Its primary intended use is in test suites, where it greatly simplifies allocating resources during setup. A failure callback function can be used to easily detect and indicate setup failures without having to manually check every call.
- typedef void (mgc_fn_free_t) (void*);
- typedef void (mgc_fn_fail_t) (void*);
- typedef struct mgc_t mgc_t;
- mpp_t *mgc_new(mgc_fn_fail_t failfn, void *failctx);
-
Create and return a new garbage collection object. If
failfn
is notNULL
, it will be called withfailctx
as its sole argument on any internal error or ifmgc_add
is called with aNULL
pointer. This is primarily intended for programs, such as tests, that need to cleanup and immediately exit on failure. - void *mgc_add(mgc_t *mgc, void *ptr, mgc_fn_free_t* fn);
-
Append an item to
mgc
with an associated free function. Ifmgc
has an associated fail function andptr
isNULL
, the fail function will be called. Otherwise, ifptr
isNULL
, it will be discarded andfn
will not be called on cleanup. - mgc_fn_free_t *mgc_remove_item(mgc_t *mgc, void *ptr);
-
Search
mgc
forptr
and remove it without freeing it. The associated free function is returned if theptr
is found, elseNULL
is returned. - void mgc_free_item(mgc_t *mgc, void *ptr);
-
Immediately free
ptr
using its associated free function. - void mgc_clear(mgc_t *mgc);
-
Free all items associated with
mgc
without freeingmgc
itself. - void mgc_free(mgc_t *mgc);
-
Clear and free
mgc
.
Each of these functions behaves the same as its standard counterpart without the mgc_
prefix or leading mgc_t
argument except that the result is automatically tracked by mgc
and may cause failfn
to be called if one was used.
- void *mgc_malloc(mgc_t *mgc, size_t size);
- void *mgc_calloc(mgc_t *mgc, size_t nelem, size_t elsize);
- void *mgc_realloc_ptr(mgc_t *mgc, void *ptr, size_t size);
- char *mgc_strdup(mgc_t *mgc, const char *s);
- char *mgc_strndup(mgc_t *mgc, const char *s, size_t size);
- char *mgc_vasprintf(mgc_t *mgc, const char *restrict fmt, va_list ap);
- char *mgc_asprintf(mgc_t *mgc, const char *restrict fmt, ...);
- int mgc_openat(mgc_t *mgc, int fd, const char *path, int oflag, ...);
- int mgc_open(mgc_t *mgc, const char *path, int oflag, ...);
- FILE *mgc_fopen(mgc_t *mgc, const char *restrict pathname, const char *restrict mode);
- FILE *mgc_fdopen(mgc_t *mgc, int fildes, const char *mode);
- FILE *mgc_fmemopen(mgc_t *mgc, void *restrict buf, size_t size, const char *mode);
- FILE *mgc_open_memstream(mgc_t *mgc, char **ptr, size_t *sizeloc);
- void mgc_close(void *fdp);
-
close
wrapper that takes a pointer to amalloc
d file descriptor to close and retries onEINTR
. - void mgc_fclose(void *stream);
-
fclose
wrapper that retries inEINTR
.
mgc_t *mgc = NULL;
void cleanup(void) {
mp_free(mgc);
}
void die(void *unused) {
perror("setup failed");
cleanup();
exit(EXIT_FAIL);
}
int main(int argc, char *argv[]) {
mgc = mgc_new(die, NULL);
char *str = mgc_strdup("foobar");
/* str is guaranteed to be valid */
assert(strcmp("foobar", str) == 0);
cleanup();
return 0;
}
Copyright 2020 Andrew Gregory <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Project URL: http://github.com/andrewgregory/mGarbageCollector.c