Add infallable ck_rhs_reset_preallocated#273
Merged
sbahra merged 1 commit intoApr 29, 2026
Merged
Conversation
Currently, when you want to flush a `ck_rhs_t` you must call `ck_rhs_reset[_size]` which can fail under low memory conditions (when trying to allocate the backing map with `hs->m->malloc`). If it does fail, the only choice you have is to unwind the map in-place, which while possible is less than ideal. This commit gives us an infallable pathway, allowing the user to preallocate one or more backing maps ahead of time for later use. Two new functions are introduced: ```c /* Returns the internally rounded up size required for an underlyinig ck_rhs_map for the number of elements requested */ size_t ck_rhs_map_size(ck_rhs_t *, unsigned long); /* Similar to ck_rhs_reset_size except the caller provides the underlying map allocation, making it infallible */ void ck_rhs_reset_preallocated(ck_rhs_t *, unsigned long, void *); ``` Signed-off-by: michael-grunder <michael.grunder@gmail.com>
Contributor
Author
|
I tried to make as minimal a change to the code as possible to lower the cognitive load for merging. The only other way of making the reset infallable could work would be using globals inside of the attached allocator but that's brittle if libck ever changes. static bool inRhsReset;
void *rhs_malloc(size_t size) {
if (inRhsReset) {
return getPreallocatedMap(size);
}
return myRealMalloc(size);
} |
Member
|
Thank you! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Currently, when you want to flush a
ck_rhs_tyou must callck_rhs_reset[_size]which can fail under low memory conditions (when trying to allocate the backing map withhs->m->malloc).If it does fail, the only choice you have is to unwind the map in-place, which while possible is less than ideal.
This commit gives us an infallable pathway, allowing the user to preallocate one or more backing maps ahead of time for later use.
Two new functions are introduced: