Created
May 23, 2020 22:22
-
-
Save spacelatte/498a2c01730d4b0ae3cd4eed4f76af07 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| typedef void (*generic_function)(); | |
| typedef struct Allocation { | |
| size_t size; | |
| void *dataptr; | |
| char usertag[1024]; | |
| generic_function *source; | |
| struct Allocation *next; | |
| } allocation_t; | |
| static allocation_t *allocation_root = NULL; | |
| void myfree(void *ptr, generic_function source) { | |
| allocation_t *rem = NULL; | |
| if(allocation_root && allocation_root->dataptr == ptr) { | |
| rem = allocation_root; | |
| allocation_root = allocation_root->next; | |
| goto quit; | |
| } | |
| for(allocation_t *tmp = allocation_root; tmp; tmp = tmp->next) { | |
| if(tmp->next && tmp->next.dataptr == ptr) { | |
| rem = tmp->next; | |
| tmp->next = tmp->next->next; | |
| goto quit; | |
| } | |
| continue; | |
| } | |
| quit: | |
| assert(rem != NULL); // ptr should be as dataptr in list... | |
| free(rem); | |
| free(ptr); | |
| ptr = NULL; | |
| return; | |
| } | |
| void* mymalloc(size_t size, generic_function source, const char *tag) { | |
| allocation_t *alloc = calloc(1, sizeof(allocation_t)); | |
| alloc->dataptr = calloc(1, size); | |
| alloc->source = source; | |
| alloc->size = size; | |
| alloc->next = allocation_root; // ! not thread safe ! | |
| allocation_root = alloc; | |
| strncpy(alloc->usertag, tag, sizeof(alloc->usertag)); | |
| return alloc->dataptr; | |
| } | |
| void listfuckups() { | |
| size_t count = 0; | |
| for(allocation_t *tmp = allocation_root; tmp; tmp = tmp->next) { | |
| fprintf(stderr, | |
| "# ALLOC id:%03ld size:%03ld src:%08p ptr:%08p tag:'%s'\n", | |
| ++count, | |
| tmp->size, | |
| tmp->source, | |
| tmp->dataptr, | |
| tmp->usertag, | |
| NULL | |
| ); | |
| continue; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment