diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-11-14 23:10:00 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-11-14 23:10:00 +0100 |
commit | f912f24e28558a6cc078240d5b97e2cc76b67868 (patch) | |
tree | e6552c74f9422fc38c61abd0b5af6afca4cc26c7 | |
parent | escape: 192 128 → \0 (diff) | |
download | slibc-f912f24e28558a6cc078240d5b97e2cc76b67868.tar.gz slibc-f912f24e28558a6cc078240d5b97e2cc76b67868.tar.bz2 slibc-f912f24e28558a6cc078240d5b97e2cc76b67868.tar.xz |
add mallocz
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r-- | doc/info/chap/introduction.texinfo | 6 | ||||
-rw-r--r-- | include/malloc.h | 21 | ||||
-rw-r--r-- | include/slibc/portability.h | 6 | ||||
-rw-r--r-- | src/malloc.c | 25 |
4 files changed, 58 insertions, 0 deletions
diff --git a/doc/info/chap/introduction.texinfo b/doc/info/chap/introduction.texinfo index 92f6273..ba08dbe 100644 --- a/doc/info/chap/introduction.texinfo +++ b/doc/info/chap/introduction.texinfo @@ -398,6 +398,12 @@ or @sc{SVID}@. To override this, define @code{_GNU_SOURCE} does not automatically define @code{_BSD_SOURCE} or @code{_SVID_SOURCE}@. +@item _PLAN9_SOURCE +@cpindex Plan 9 from Bell Labs +@lvindex _PLAN9_SOURCE +Enables extensions from Plan 9 from Bell Labs +C standard library. + @end table @c TODO: _LARGEFILE_SUPPORT @c TODO: _LARGEFILE64_SUPPORT diff --git a/include/malloc.h b/include/malloc.h index 66adcbd..f4a910a 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -73,6 +73,27 @@ void* malloc(size_t) void* calloc(size_t, size_t) __GCC_ONLY(__attribute__((__malloc__, __warn_unused_result__))); +#if defined(__PLAN9_SOURCE) +/** + * Variant of `malloc` that conditionally clears the allocation with zeroes. + * + * This is a Plan 9 from Bell Labs extension. + * + * @param size The size of the allocation. + * @param clear Clear the allocation unless this value is zero. + * @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* mallocz(size_t, int) + __GCC_ONLY(__attribute__((__malloc__, __warn_unused_result__))); +#endif + #if !defined(__PORTABLE) /** * Variant of `malloc` that clears the allocation with zeroes. diff --git a/include/slibc/portability.h b/include/slibc/portability.h index 5eae353..5ccb9fb 100644 --- a/include/slibc/portability.h +++ b/include/slibc/portability.h @@ -56,6 +56,9 @@ #ifdef __SUS_SOURCE # undef __SUS_SOURCE #endif +#ifdef __PLAN9_SOURCE +# undef __PLAN9_SOURCE +#endif #ifdef __GNU_SOURCE # undef __GNU_SOURCE #endif @@ -94,6 +97,9 @@ * is not defined. */ #if !defined(__PORTABLE) +# if defined(_PLAN9_SOURCE) +# define __PLAN9_SOURCE _PLAN9_SOURCE +# endif # if defined(_GNU_SOURCE) # define __GNU_SOURCE _GNU_SOURCE # endif diff --git a/src/malloc.c b/src/malloc.c index cd22ccd..864a8f5 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -122,6 +122,31 @@ void* calloc(size_t elem_count, size_t elem_size) /** + * Variant of `malloc` that conditionally clears the allocation with zeroes. + * + * This is a Plan 9 from Bell Labs extension. + * + * @param size The size of the allocation. + * @param clear Clear the allocation unless this value is zero. + * @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* mallocz(size_t size, int clear) +{ + void* ptr = memalign(sizeof(max_align_t), size); + if ((ptr != NULL) && clear) + explicit_bzero(ptr, size); + return ptr; +} + + +/** * Variant of `malloc` that clears the allocation with zeroes. * * `zalloc(n)` is equivalent to `calloc(1, n)`, or equivalently, |