From 5eb009e831808779b92c2a5605a77f2b10dfb5da Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 18 Oct 2015 05:15:00 +0200 Subject: add rememalign: realloc with configurable alignment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- include/slibc-alloc.h | 56 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/slibc-alloc.h b/include/slibc-alloc.h index a36c024..f19982a 100644 --- a/include/slibc-alloc.h +++ b/include/slibc-alloc.h @@ -44,6 +44,34 @@ enum extalloc_mode * Create new allocation with `malloc` if necessary. */ EXTALLOC_MALLOC = 2, + + }; + + +/** + * Configurations for `rememalign`. + * There are independent of each other, and + * multiple can be selected by using bitwise or + * between them. + */ +enum rememalign_mode + { + /** + * Clear disowned memory. + */ + REMEMALIGN_CLEAR = 1, + + /** + * Initialise new memory. + */ + REMEMALIGN_INIT = 2, + + /** + * If a new allocation is created, copy the data + * from the old allocation over to the new allocation. + */ + REMEMALIGN_MEMCPY = 4, + }; @@ -179,20 +207,40 @@ void* custom_realloc(void*, size_t, int, int, int) void* extalloc(void*, size_t, enum extalloc_mode) __GCC_ONLY(__attribute__((__nonnull__, __warn_unused_result__))); +/** + * This function is similar to `realloc`, however its + * behaviour and pointer alignment can be tuned. + * + * @param ptr The old allocation, see `realloc` for more details. + * @param boundary The alignment. + * @param size The new allocation size, see `realloc` for more details. + * @param mode `REMEMALIGN_CLEAR`, `REMEMALIGN_INIT` or + * `REMEMALIGN_MEMCPY`, or both or neither. + * @return The new allocation, see `realloc` for more details. + * + * @throws 0 `errno` is set to zero success if `NULL` is returned. + * @throws EINVAL `mode` is invalid, or `boundary` is not a power of two. + * @throws ENOMEM The process cannot allocate more memory. + */ +void* rememalign(void*, size_t, size_t, enum rememalign_mode) + __GCC_ONLY(__attribute__((__warn_unused_result__))); + /** * This function behaves exactly like `fast_realloc`, except: * - Its behaviour is undefined if `ptr` is `NULL`. * - Its behaviour is undefined if `size` equals the old allocation size. * - Its behaviour is undefined if `size` is zero. * - It will never free `ptr`. + * - The alignment of new pointers can be specified. * - * @param ptr The old allocation, see `realloc` for more details. - * @param size The new allocation size, see `realloc` for more details. - * @return The new allocation, see `realloc` for more details. + * @param ptr The old allocation, see `realloc` for more details. + * @param boundary The alignment. + * @param size The new allocation size, see `realloc` for more details. + * @return The new allocation, see `realloc` for more details. * * @throws ENOMEM The process cannot allocate more memory. */ -void* naive_realloc(void*, size_t) /* sic! we limit ourself to ASCII */ +void* naive_realloc(void*, size_t, size_t) /* sic! we limit ourself to ASCII */ __GCC_ONLY(__attribute__((__nonnull__, __warn_unused_result__))); /** -- cgit v1.2.3-70-g09d2