From: Eugene Syromyatnikov Date: Fri, 22 Feb 2019 17:34:33 +0000 (+0100) Subject: xmalloc: introduce xzalloc X-Git-Tag: v5.1~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c8e4b438ebc0d769946febc491d955e29686d1f5;p=strace xmalloc: introduce xzalloc * xmalloc.h (xmalloc, xcalloc, xreallocarray): Document. (xzalloc): New function, a thin wrapper around xcalloc with xmalloc interface. Co-Authored-by: Dmitry V. Levin --- diff --git a/xmalloc.h b/xmalloc.h index a4e7792f..57412cf8 100644 --- a/xmalloc.h +++ b/xmalloc.h @@ -19,9 +19,28 @@ # define xcalloc strace_calloc # define xmalloc strace_malloc +/** Allocate memory, die if the allocation has failed. */ +void *xmalloc(size_t size) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1)); + +/** + * Allocate an array and zero it out (similar to calloc), die if the allocation + * has failed or if the product of nmemb and size is too big. + */ void *xcalloc(size_t nmemb, size_t size) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1, 2)); -void *xmalloc(size_t size) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1)); + +/** Wrapper for xcalloc(1, size) with xmalloc-like interface. */ +ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1)) +static inline void * +xzalloc(size_t size) +{ + return xcalloc(1, size); +} + +/** + * Reallocate memory for the array, die if the allocation has failed or + * if the product of nmemb and size is too big. + */ void *xreallocarray(void *ptr, size_t nmemb, size_t size) ATTRIBUTE_ALLOC_SIZE((2, 3));