From cf711741c0b76a66baff916e3d781cdf6022023f Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 16 Oct 2015 03:28:13 +0200 Subject: add custom_realloc 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 | 25 +++++++++++++++++++++++++ src/slibc-alloc.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/include/slibc-alloc.h b/include/slibc-alloc.h index 3c92fcf..7bd2e81 100644 --- a/include/slibc-alloc.h +++ b/include/slibc-alloc.h @@ -108,6 +108,31 @@ void* fast_realloc(void*, size_t) void* secure_realloc(void*, size_t) __GCC_ONLY(__attribute__((warn_unused_result))); +/** + * This function behaves exactly like `realloc`, + * except you can freely select what memory it clears. + * + * `crealloc(p, n)` is equivalent to (but slightly fast than) + * `custom_realloc(p, n, 1, 1, 1)`. + * + * `fast_realloc(p, n)` is equivalent to (but slightly fast than) + * `custom_realloc(p, n, 0, 0, 0)`. + * + * `secure_realloc(p, n)` is equivalent to (but slightly fast than) + * `custom_realloc(p, n, 1, 0, 1)`. + * + * @param ptr The old allocation, see `realloc` for more details. + * @param size The new allocation size, see `realloc` for more details. + * @param clear_old Whether the disowned area is cleared, even if `ptr` is returned. + * @param clear_new Whether the newly claimed area is cleared. + * @param clear_free Whether the old allocation is cleared if a new pointer is returned. + * @return The new allocation, see `realloc` for more details. + * + * @throws ENOMEM The process cannot allocate more memory. + */ +void* custom_realloc(void*, size_t, int, int, int) + __GCC_ONLY(__attribute__((warn_unused_result))); + /** * This function behaves exactly like `fast_realloc`, except: * - Its behaviour is undefined if `ptr` is `NULL`. diff --git a/src/slibc-alloc.c b/src/slibc-alloc.c index 2043ce0..1b85aad 100644 --- a/src/slibc-alloc.c +++ b/src/slibc-alloc.c @@ -89,6 +89,13 @@ size_t allocsize(void* segment) /** * Common code for realloc-functions, apart from `naive_realloc`. + * + * @param ptr:void* The old allocation, see `realloc` for more details. + * @param size:size_t The new allocation size, see `realloc` for more details. + * @param CLEAR_OLD:int Whether the disowned area is cleared, even if `ptr` is returned. + * @param CLEAR_NEW:int Whether the newly claimed area is cleared. + * @param CLEAR_FREE:int Whether the old allocation is cleared if a new pointer is returned. + * @return The new allocation, see `realloc` for more details. */ #define REALLOC(ptr, size, CLEAR_OLD, CLEAR_NEW, CLEAR_FREE) \ size_t old_size; \ @@ -171,6 +178,34 @@ void* secure_realloc(void* ptr, size_t size) } +/** + * This function behaves exactly like `realloc`, + * except you can freely select what memory it clears. + * + * `crealloc(p, n)` is equivalent to (but slightly fast than) + * `custom_realloc(p, n, 1, 1, 1)`. + * + * `fast_realloc(p, n)` is equivalent to (but slightly fast than) + * `custom_realloc(p, n, 0, 0, 0)`. + * + * `secure_realloc(p, n)` is equivalent to (but slightly fast than) + * `custom_realloc(p, n, 1, 0, 1)`. + * + * @param ptr The old allocation, see `realloc` for more details. + * @param size The new allocation size, see `realloc` for more details. + * @param clear_old Whether the disowned area is cleared, even if `ptr` is returned. + * @param clear_new Whether the newly claimed area is cleared. + * @param clear_free Whether the old allocation is cleared if a new pointer is returned. + * @return The new allocation, see `realloc` for more details. + * + * @throws ENOMEM The process cannot allocate more memory. + */ +void* custom_realloc(void* ptr, size_t size, int clear_old, int clear_new, int clear_free) +{ + REALLOC(ptr, size, clear_old, clear_new, clear_free); +} + + /** * This function behaves exactly like `fast_realloc`, except: * - Its behaviour is undefined if `ptr` is `NULL`. -- cgit v1.2.3-70-g09d2