diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-08-30 19:32:55 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-08-30 19:33:40 +0200 |
commit | 65460fc99d4257fed9bc437fad6ae190935c4e68 (patch) | |
tree | 9e66ca2718d8c4b944be2184c6ecd5c227f8fbba /include/malloc.h | |
parent | m (diff) | |
download | slibc-65460fc99d4257fed9bc437fad6ae190935c4e68.tar.gz slibc-65460fc99d4257fed9bc437fad6ae190935c4e68.tar.bz2 slibc-65460fc99d4257fed9bc437fad6ae190935c4e68.tar.xz |
malloc: add overflow checks and add aligned alloc-functions
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'include/malloc.h')
-rw-r--r-- | include/malloc.h | 129 |
1 files changed, 127 insertions, 2 deletions
diff --git a/include/malloc.h b/include/malloc.h index c18f885..0e17817 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -97,7 +97,8 @@ void* realloc(void*, size_t) * beginning of a memory allocation on the heap. * However, if it is `NULL`, nothing will happen. */ -void free(void*) __slibc_warning("Use 'fast_free' or 'secure_free' instead."); +void free(void*) + __slibc_warning("Use 'fast_free' or 'secure_free' instead."); /** * This function is identical to `free`. @@ -112,7 +113,131 @@ void free(void*) __slibc_warning("Use 'fast_free' or 'secure_free' instead."); * However, if it is `NULL`, nothing will happen. */ #ifndef _PORTABLE_SOURCE -void cfree(void*, ...) __deprecated("'cfree' is deprecated and not portable, use 'free' instead."); +void cfree(void*, ...) + __deprecated("'cfree' is deprecated and not portable, use 'free' instead."); +#endif + + +#ifndef _PORTABLE_SOURCE +/** + * Variant of `malloc` that returns an address with a + * specified alignment. + * + * It is unspecified how the function works. This implemention + * will allocate a bit of extra memory and shift the returned + * pointer so that it is aligned. + * + * As a GNU-compliant slibc extension, memory allocated + * with this function can be freed with `free`. + * + * @param boundary The alignment. + * @param size The number of bytes to allocated. + * @return Pointer to the beginning of the new allocation. + * If `size` is zero, this function will either return + * `NULL` (that is what this implement does) or return + * a unique pointer that can later be freed with `free`. + * `NULL` is returned on error, and `errno` is set to + * indicate the error. + * + * @throws ENOMEM The process cannot allocate more memory. + * @throws EINVAL If `boundary` is not a power of two. + */ +void* memalign(size_t, size_t) +#ifdef __C11__ + __deprecated("'memalign' has be deprecated by 'aligned_alloc' in C11.") +#endif + __GCC_ONLY(__attribute__((malloc, warn_unused_result))); +#endif + +/** + * `posix_memalign(p, b, n)` is equivalent to + * `(*p = memalign(b, n), *p ? 0 : errno)`, except + * `boundary` must also be a multiple of `sizeof(void*)`, + * and `errno` is unspecified. + * + * As a GNU-compliant slibc extension, memory allocated + * with this function can be freed with `free`. + * + * @param ptrptr Output parameter for the allocated memory. + * @param boundary The alignment. + * @param size The number of bytes to allocated. + * @return Zero on success, a value for `errno` on error. + * + * @throws ENOMEM The process cannot allocate more memory. + * @throws EINVAL If `boundary` is not a power-of-two multiple of `sizeof(void*)`. + */ +int posix_memalign(void**, size_t, size_t) + __GCC_ONLY(__attribute__((nonnull))); + +#ifndef _PORTABLE_SOURCE +/** + * `valloc(n)` is equivalent to `memalign(sysconf(_SC_PAGESIZE), n)`. + * + * As a GNU-compliant slibc extension, memory allocated + * with this function can be freed with `free`. + * + * @param size The number of bytes to allocated. + * @return Pointer to the beginning of the new allocation. + * If `size` is zero, this function will either return + * `NULL` (that is what this implement does) or return + * a unique pointer that can later be freed with `free`. + * `NULL` is returned on error, and `errno` is set to + * indicate the error. + * + * @throws ENOMEM The process cannot allocate more memory. + */ +void* valloc(size_t) + __GCC_ONLY(__attribute__((malloc, warn_unused_result))) + __deprecated("'valloc' is deprecated, use 'memalign' or 'posix_memalign' instead."); + +#ifdef _GNU_SOURCE +/** + * This function works like `valloc`, except the allocation size, + * including auxiliary space, is rounded up to the next multiple + * of the page size. + * + * @param size The number of bytes to allocated. + * @return Pointer to the beginning of the new allocation. + * If `size` is zero, this function will either return + * `NULL` (that is what this implement does) or return + * a unique pointer that can later be freed with `free`. + * `NULL` is returned on error, and `errno` is set to + * indicate the error. + * + * @throws ENOMEM The process cannot allocate more memory. + */ +void* pvalloc(size_t) + __GCC_ONLY(__attribute__((malloc, warn_unused_result))) + __deprecated("'pvalloc' is deprecated, use 'memalign' or 'posix_memalign' instead."); +#endif +#endif + +#ifdef __C11__ +/** + * This function is identical to `memalign`, + * except it can be freed with `free`. + * + * Variant of `malloc` that returns an address with a + * specified alignment. + * + * It is unspecified how the function works. This implemention + * will allocate a bit of extra memory and shift the returned + * pointer so that it is aligned. + * + * @param boundary The alignment. + * @param size The number of bytes to allocated. + * @return Pointer to the beginning of the new allocation. + * If `size` is zero, this function will either return + * `NULL` (that is what this implement does) or return + * a unique pointer that can later be freed with `free`. + * `NULL` is returned on error, and `errno` is set to + * indicate the error. + * + * @throws ENOMEM The process cannot allocate more memory. + * @throws EINVAL If `boundary` is not a power of two. + */ +void* aligned_alloc(size_t, size_t) + __GCC_ONLY(__attribute__((malloc, warn_unused_result))); #endif |