diff options
author | Jason Ekstrand <[email protected]> | 2019-05-09 18:52:44 -0500 |
---|---|---|
committer | Jason Ekstrand <[email protected]> | 2019-05-14 12:30:22 -0500 |
commit | 6c0f75c953e6640838d818b8f3603a56b0483f5d (patch) | |
tree | 2aff0d4c8ed0596eb00e4e07654751a64fe97dbb /src/util/ralloc.c | |
parent | 621232694176ea83752505643b106c8d1c719893 (diff) |
util/ralloc: Add helpers for growing zero-initialized memory
Unfortunately, we can't quite follow the standard C conventions for
these because ralloc doesn't know the sizes of pointers.
Reviewed-by: Eric Anholt <[email protected]>
Diffstat (limited to 'src/util/ralloc.c')
-rw-r--r-- | src/util/ralloc.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/util/ralloc.c b/src/util/ralloc.c index fc35661996d..0d20223d98f 100644 --- a/src/util/ralloc.c +++ b/src/util/ralloc.c @@ -198,6 +198,21 @@ reralloc_size(const void *ctx, void *ptr, size_t size) } void * +rerzalloc_size(const void *ctx, void *ptr, size_t old_size, size_t new_size) +{ + if (unlikely(ptr == NULL)) + return rzalloc_size(ctx, new_size); + + assert(ralloc_parent(ptr) == ctx); + ptr = resize(ptr, new_size); + + if (new_size > old_size) + memset((char *)ptr + old_size, 0, new_size - old_size); + + return ptr; +} + +void * ralloc_array_size(const void *ctx, size_t size, unsigned count) { if (count > SIZE_MAX/size) @@ -224,6 +239,16 @@ reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count) return reralloc_size(ctx, ptr, size * count); } +void * +rerzalloc_array_size(const void *ctx, void *ptr, size_t size, + unsigned old_count, unsigned new_count) +{ + if (new_count > SIZE_MAX/size) + return NULL; + + return rerzalloc_size(ctx, ptr, size * old_count, size * new_count); +} + void ralloc_free(void *ptr) { |