-
Notifications
You must be signed in to change notification settings - Fork 174
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
Merged
+207
−69
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,10 +161,12 @@ | |
| * ``` | ||
| * #ifndef PRISM_XALLOCATOR_H | ||
| * #define PRISM_XALLOCATOR_H | ||
| * #define xmalloc my_malloc | ||
| * #define xrealloc my_realloc | ||
| * #define xcalloc my_calloc | ||
| * #define xfree my_free | ||
| * #define xmalloc my_malloc | ||
| * #define xrealloc my_realloc | ||
| * #define xcalloc my_calloc | ||
| * #define xfree my_free | ||
| * #define xrealloc_sized my_realloc_sized // (optional) | ||
| * #define xfree_sized my_free_sized // (optional) | ||
| * #endif | ||
| * ``` | ||
| */ | ||
|
|
@@ -204,6 +206,28 @@ | |
| #endif | ||
| #endif | ||
|
|
||
| #ifndef xfree_sized | ||
| /** | ||
| * The free_sized function that should be used. This can be overridden with the | ||
| * PRISM_XALLOCATOR define. | ||
| * If not defined, defaults to calling xfree. | ||
| */ | ||
| #define xfree_sized(p, s) xfree(((void)(s), (p))) | ||
| #endif | ||
|
|
||
| #ifndef xrealloc_sized | ||
| /** | ||
| * The xrealloc_sized function that should be used. This can be overridden with the | ||
| * PRISM_XALLOCATOR define. | ||
| * If not defined, defaults to calling xrealloc. | ||
| */ | ||
| #define xrealloc_sized(p, ns, os) xrealloc((p), ((void)(os), (ns))) | ||
| #endif | ||
|
|
||
| #ifdef PRISM_BUILD_DEBUG | ||
| #include "prism/debug_allocator.h" | ||
| #endif | ||
|
Comment on lines
+227
to
+229
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now |
||
|
|
||
| /** | ||
| * If PRISM_BUILD_MINIMAL is defined, then we're going to define every possible | ||
| * switch that will turn off certain features of prism. | ||
|
|
||
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
Oops, something went wrong.
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