From ae18c20d5a585ae4bc1e9ee6859230fee7f73ed8 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 23 Nov 2023 13:08:04 -0500 Subject: alloc: New helpers for aligned reallocation There is no aligned_realloc(), so the new xrealloc() function emulates it by manually reallocating and copying for over-aligned types. The new REALLOC_ARRAY() and REALLOC_FLEX() macros wrap xrealloc(). --- src/alloc.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/alloc.h') diff --git a/src/alloc.h b/src/alloc.h index 15e4983..a6dee99 100644 --- a/src/alloc.h +++ b/src/alloc.h @@ -155,6 +155,30 @@ void *zalloc(size_t align, size_t size); #define ZALLOC_FLEX(type, member, count) \ (type *)zalloc(alignof(type), sizeof_flex(type, member, count)) +/** + * Alignment-aware realloc(). + * + * @param ptr + * The pointer to reallocate. + * @param align + * The required alignment. + * @param old_size + * The previous allocation size. + * @param new_size + * The new allocation size. + * @return + * The reallocated memory, or NULL on failure. + */ +void *xrealloc(void *ptr, size_t align, size_t old_size, size_t new_size); + +/** Reallocate memory for an array. */ +#define REALLOC_ARRAY(type, ptr, old_count, new_count) \ + (type *)xrealloc((ptr), alignof(type), sizeof_array(type, old_count), sizeof_array(type, new_count)) + +/** Reallocate memory for a flexible struct. */ +#define REALLOC_FLEX(type, member, ptr, old_count, new_count) \ + (type *)xrealloc((ptr), alignof(type), sizeof_flex(type, member, old_count), sizeof_flex(type, member, new_count)) + /** * An arena allocator for fixed-size types. * -- cgit v1.2.3