Skip to content

andrewgregory/mGarbageCollector.c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NAME

mGarbageCollector.c - track and free allocated resources

SYNOPSIS

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.

DESCRIPTION

Types

typedef void (mgc_fn_free_t) (void*);
typedef void (mgc_fn_fail_t) (void*);
typedef struct mgc_t mgc_t;

Functions

mpp_t *mgc_new(mgc_fn_fail_t failfn, void *failctx);

Create and return a new garbage collection object. If failfn is not NULL, it will be called with failctx as its sole argument on any internal error or if mgc_add is called with a NULL 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. If mgc has an associated fail function and ptr is NULL, the fail function will be called. Otherwise, if ptr is NULL, it will be discarded and fn will not be called on cleanup.

mgc_fn_free_t *mgc_remove_item(mgc_t *mgc, void *ptr);

Search mgc for ptr and remove it without freeing it. The associated free function is returned if the ptr is found, else NULL 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 freeing mgc itself.

void mgc_free(mgc_t *mgc);

Clear and free mgc.

Helpers

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 a mallocd file descriptor to close and retries on EINTR.

void mgc_fclose(void *stream);

fclose wrapper that retries in EINTR.

EXAMPLES

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 AND LICENSE

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.

SEE ALSO

Project URL: http://github.com/andrewgregory/mGarbageCollector.c

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published