diff options
author | Sami Kerola | 2018-10-06 13:31:09 +0200 |
---|---|---|
committer | Sami Kerola | 2018-11-21 21:54:18 +0100 |
commit | f1b327f8d5c8de7bf7fae99e85765d0954a25bac (patch) | |
tree | 775dc313067395bf7efea88b1119d754211b8e6f /include | |
parent | libblkid: add check for DRBD9 (diff) | |
download | kernel-qcow2-util-linux-f1b327f8d5c8de7bf7fae99e85765d0954a25bac.tar.gz kernel-qcow2-util-linux-f1b327f8d5c8de7bf7fae99e85765d0954a25bac.tar.xz kernel-qcow2-util-linux-f1b327f8d5c8de7bf7fae99e85765d0954a25bac.zip |
include/c: use returns_nonnull function attribute in xalloc.h
Let the compiler optimize based on the knowledge that the return value will
never be null.
Reference: https://patchwork.ozlabs.org/patch/281112/#631159
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'include')
-rw-r--r-- | include/c.h | 6 | ||||
-rw-r--r-- | include/xalloc.h | 12 |
2 files changed, 13 insertions, 5 deletions
diff --git a/include/c.h b/include/c.h index b769b5843..8b2a2d19a 100644 --- a/include/c.h +++ b/include/c.h @@ -80,6 +80,12 @@ # endif #endif +#if (__GNUC__ >= 5) || ((__GNUC__ >= 4) && (__GNUC_MINOR__ >= 9)) +# define __ul_returns_nonnull __attribute__((returns_nonnull)) +#else +# define __ul_returns_nonnull +#endif + /* * Force a compilation error if condition is true, but also produce a * result (of value 0 and type size_t), so the expression can be used diff --git a/include/xalloc.h b/include/xalloc.h index 8870ac0d9..1d225d4a0 100644 --- a/include/xalloc.h +++ b/include/xalloc.h @@ -26,7 +26,7 @@ static inline void __err_oom(const char *file, unsigned int line) #define err_oom() __err_oom(__FILE__, __LINE__) -static inline __ul_alloc_size(1) +static inline __ul_alloc_size(1) __ul_returns_nonnull void *xmalloc(const size_t size) { void *ret = malloc(size); @@ -36,7 +36,7 @@ void *xmalloc(const size_t size) return ret; } -static inline __ul_alloc_size(2) +static inline __ul_alloc_size(2) __ul_returns_nonnull void *xrealloc(void *ptr, const size_t size) { void *ret = realloc(ptr, size); @@ -46,7 +46,7 @@ void *xrealloc(void *ptr, const size_t size) return ret; } -static inline __ul_calloc_size(1, 2) +static inline __ul_calloc_size(1, 2) __ul_returns_nonnull void *xcalloc(const size_t nelems, const size_t size) { void *ret = calloc(nelems, size); @@ -56,7 +56,8 @@ void *xcalloc(const size_t nelems, const size_t size) return ret; } -static inline char __attribute__((warn_unused_result)) *xstrdup(const char *str) +static inline char __attribute__((warn_unused_result)) __ul_returns_nonnull +*xstrdup(const char *str) { char *ret; @@ -70,7 +71,8 @@ static inline char __attribute__((warn_unused_result)) *xstrdup(const char *str) return ret; } -static inline char * __attribute__((warn_unused_result)) xstrndup(const char *str, size_t size) +static inline char * __attribute__((warn_unused_result)) __ul_returns_nonnull +xstrndup(const char *str, size_t size) { char *ret; |