diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2023-08-31 10:42:42 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2023-08-31 10:42:42 -0400 |
commit | 1bb2b6ccfc11d4aec61287441379cb6584b8fb5f (patch) | |
tree | e0b3be0162d850b76c59284745bc40696e1a7e84 /src | |
parent | 345047be5b8766a9763d4a717ff034e4e4083ade (diff) | |
download | bfs-1bb2b6ccfc11d4aec61287441379cb6584b8fb5f.tar.xz |
alloc: New varena_grow() function
Diffstat (limited to 'src')
-rw-r--r-- | src/alloc.c | 15 | ||||
-rw-r--r-- | src/alloc.h | 14 |
2 files changed, 29 insertions, 0 deletions
diff --git a/src/alloc.c b/src/alloc.c index 8a4dc3a..0b978ba 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -283,6 +283,21 @@ void *varena_realloc(struct varena *varena, void *ptr, size_t old_count, size_t return ret; } +void *varena_grow(struct varena *varena, void *ptr, size_t *count) { + size_t old_count = *count; + + // Round up to the limit of the current size class. If we're already at + // the limit, go to the next size class. + size_t new_shift = varena_size_class(varena, old_count + 1) + varena->shift; + size_t new_count = (size_t)1 << new_shift; + + ptr = varena_realloc(varena, ptr, old_count, new_count); + if (ptr) { + *count = new_count; + } + return ptr; +} + void varena_free(struct varena *varena, void *ptr, size_t count) { struct arena *arena = varena_get(varena, count); arena_free(arena, ptr); diff --git a/src/alloc.h b/src/alloc.h index b5dfa68..5f0c423 100644 --- a/src/alloc.h +++ b/src/alloc.h @@ -271,6 +271,20 @@ void *varena_alloc(struct varena *varena, size_t count); void *varena_realloc(struct varena *varena, void *ptr, size_t old_count, size_t new_count); /** + * Grow a flexible struct by an arbitrary amount. + * + * @param varena + * The varena to allocate from. + * @param ptr + * The object to resize. + * @param count + * Pointer to the flexible array length. + * @return + * The resized struct, or NULL on failure. + */ +void *varena_grow(struct varena *varena, void *ptr, size_t *count); + +/** * Free an arena-allocated flexible struct. * * @param varena |