-
Notifications
You must be signed in to change notification settings - Fork 175
Add xfree_sized and xrealloc_sized #3894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
byroot
wants to merge
2
commits into
main
Choose a base branch
from
free_sized
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+154
−10
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /** | ||
| * @file debug_allocator.h | ||
| * | ||
| * Decorate allocation function to ensure sizes are correct. | ||
| */ | ||
| #ifndef PRISM_DEBUG_ALLOCATOR_H | ||
| #define PRISM_DEBUG_ALLOCATOR_H | ||
|
|
||
| #include <string.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| static inline void * | ||
| pm_debug_malloc(size_t size) | ||
| { | ||
| size_t *memory = xmalloc(size + sizeof(size_t)); | ||
| memory[0] = size; | ||
| return memory + 1; | ||
| } | ||
|
|
||
| static inline void * | ||
| pm_debug_calloc(size_t nmemb, size_t size) | ||
| { | ||
| size_t total_size = nmemb * size; | ||
| void *ptr = pm_debug_malloc(total_size); | ||
| memset(ptr, 0, total_size); | ||
| return ptr; | ||
| } | ||
|
|
||
| static inline void * | ||
| pm_debug_realloc(void *ptr, size_t size) | ||
| { | ||
| if (ptr == NULL) { | ||
| return pm_debug_malloc(size); | ||
| } | ||
|
|
||
| size_t *memory = (size_t *)ptr; | ||
| void *raw_memory = memory - 1; | ||
| memory = (size_t *)xrealloc(raw_memory, size + sizeof(size_t)); | ||
| memory[0] = size; | ||
| return memory + 1; | ||
| } | ||
|
|
||
| static inline void | ||
| pm_debug_free(void *ptr) | ||
| { | ||
| if (ptr != NULL) { | ||
| size_t *memory = (size_t *)ptr; | ||
| xfree(memory - 1); | ||
| } | ||
| } | ||
|
|
||
| static inline void | ||
| pm_debug_free_sized(void *ptr, size_t old_size) | ||
| { | ||
| if (ptr != NULL) { | ||
| size_t *memory = (size_t *)ptr; | ||
| if (old_size != memory[-1]) { | ||
| fprintf(stderr, "[BUG] buffer %p was allocated with size %lu but freed with size %lu\n", ptr, memory[-1], old_size); | ||
| abort(); | ||
| } | ||
| xfree_sized(memory - 1, old_size + sizeof(size_t)); | ||
| } | ||
| } | ||
|
|
||
| static inline void * | ||
| pm_debug_realloc_sized(void *ptr, size_t size, size_t old_size) | ||
| { | ||
| if (ptr == NULL) { | ||
| if (old_size != 0) { | ||
| fprintf(stderr, "[BUG] realloc_sized called with NULL pointer and old size %lu\n", old_size); | ||
| abort(); | ||
| } | ||
| return pm_debug_malloc(size); | ||
| } | ||
|
|
||
| size_t *memory = (size_t *)ptr; | ||
| if (old_size != memory[-1]) { | ||
| fprintf(stderr, "[BUG] buffer %p was allocated with size %lu but realloced with size %lu\n", ptr, memory[-1], old_size); | ||
| abort(); | ||
| } | ||
| return pm_debug_realloc(ptr, size); | ||
| } | ||
|
|
||
| #undef xmalloc | ||
| #undef xrealloc | ||
| #undef xcalloc | ||
| #undef xfree | ||
| #undef xrealloc_sized | ||
| #undef xfree_sized | ||
|
|
||
| #define xmalloc pm_debug_malloc | ||
| #define xrealloc pm_debug_realloc | ||
| #define xcalloc pm_debug_calloc | ||
| #define xfree pm_debug_free | ||
| #define xrealloc_sized pm_debug_realloc_sized | ||
| #define xfree_sized pm_debug_free_sized | ||
|
|
||
| #endif |
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these should be in the else for PRISM_XALLOCATOR, in case folks want to define that, but maybe I'm not following it correctly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If they are defined in
PRISM_XALLOCATORthey won't be overritten here.The idea is that the
sizedvariants are shimed perfectly by just calling the normal function without the size