Skip to content

Add infallable ck_rhs_reset_preallocated#273

Merged
sbahra merged 1 commit into
concurrencykit:masterfrom
michael-grunder:ck_rhs_reset_preallocatred
Apr 29, 2026
Merged

Add infallable ck_rhs_reset_preallocated#273
sbahra merged 1 commit into
concurrencykit:masterfrom
michael-grunder:ck_rhs_reset_preallocatred

Conversation

@michael-grunder
Copy link
Copy Markdown
Contributor

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:

/* 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 *);

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>
@michael-grunder
Copy link
Copy Markdown
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);
}

@sbahra sbahra merged commit 73f4f1d into concurrencykit:master Apr 29, 2026
18 checks passed
@sbahra
Copy link
Copy Markdown
Member

sbahra commented Apr 29, 2026

Thank you!

@michael-grunder michael-grunder deleted the ck_rhs_reset_preallocatred branch April 29, 2026 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants