From 22d37d84a7ab0bb4208cdc8828541a30ef0821e2 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 30 Aug 2015 17:36:09 +0200 Subject: add memory allocation functions 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 | 54 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) (limited to 'include/slibc-alloc.h') diff --git a/include/slibc-alloc.h b/include/slibc-alloc.h index 0308482..b230f16 100644 --- a/include/slibc-alloc.h +++ b/include/slibc-alloc.h @@ -54,9 +54,12 @@ void secure_free(void*); * @throws EINVAL If `segment` is `NULL`. * @throws EFAULT If `segment` is not a pointer to an allocation * on the heap, or was not allocated with a function - * implemented in slibc. + * implemented in slibc. It is however not guaranteed + * that this will happen, undefined behaviour may be + * invoked instead. */ -size_t allocsize(void*); /* TODO not implemented */ +size_t allocsize(void*) + __GCC_ONLY(__attribute__((warn_unused_result))); /** * Variant of `realloc` that overrides newly allocated space @@ -67,8 +70,53 @@ size_t allocsize(void*); /* TODO not implemented */ * @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. + * + * @throws ENOMEM The process cannot allocate more memory. + */ +void* crealloc(void*, size_t) + __GCC_ONLY(__attribute__((warn_unused_result))); + +/** + * This function behaves exactly like `realloc`, except it is + * guaranteed to never initialise or errors data. + * + * @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. + * + * @throws ENOMEM The process cannot allocate more memory. + */ +void* fast_realloc(void*, size_t) + __GCC_ONLY(__attribute__((warn_unused_result))); + +/** + * This function behaves exactly like `crealloc`, except it + * does not initialise newly allocated size. + * + * @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. + * + * @throws ENOMEM The process cannot allocate more memory. + */ +void* secure_realloc(void*, size_t) + __GCC_ONLY(__attribute__((warn_unused_result))); + +/** + * This function behaves exactly like `fast_realloc`, except: + * - Its haviour is undefined if `ptr` is `NULL`. + * - Its haviour is undefined `size` equals the old allocation size. + * - Its haviour is undefined if `size` is zero. + * - It will never free `ptr`. + * + * @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. + * + * @throws ENOMEM The process cannot allocate more memory. */ -void* crealloc(void*, size_t); /* TODO not implemented */ +void* naive_realloc(void*, size_t) /* sic! we limit ourself to ASCII */ + __GCC_ONLY(__attribute__((nonnull, warn_unused_result))); /** -- cgit v1.2.3-70-g09d2