From 3dcb89e98bfc43d89a9dc34d1d012a4e6756b83c Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Mon, 12 May 2014 14:32:41 +0200 Subject: [PATCH] shl: update shl-macro.h Fix a bug in SHL_ALIGN_POWER2 and add new shl_closep helpers. Signed-off-by: David Herrmann --- src/shared/shl_macro.h | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/shared/shl_macro.h b/src/shared/shl_macro.h index 1b7b341..da7dc76 100644 --- a/src/shared/shl_macro.h +++ b/src/shared/shl_macro.h @@ -1,7 +1,7 @@ /* * SHL - Macros * - * Copyright (c) 2011-2013 David Herrmann + * Copyright (c) 2011-2014 David Herrmann * Dedicated to the Public Domain */ @@ -18,6 +18,7 @@ #include #include #include +#include /* sanity checks required for some macros */ #if __SIZEOF_POINTER__ != 4 && __SIZEOF_POINTER__ != 8 @@ -50,6 +51,14 @@ static inline void shl_freep(void *p) #define _shl_free_ _shl_cleanup_(shl_freep) +static inline void shl_closep(int *p) +{ + if (*p >= 0) + close(*p); +} + +#define _shl_close_ _shl_cleanup_(shl_closep) + static inline void shl_set_errno(int *r) { errno = *r; @@ -127,7 +136,14 @@ static inline void shl_set_errno(int *r) /* align to next higher power-of-2 (except for: 0 => 0, overflow => 0) */ static inline size_t SHL_ALIGN_POWER2(size_t u) { - return 1ULL << ((sizeof(u) * 8ULL) - __builtin_clzll(u - 1ULL)); + unsigned int shift; + + /* clz(0) is undefined */ + if (u == 1) + return 1; + + shift = sizeof(unsigned long long) * 8ULL - __builtin_clzll(u - 1ULL); + return 1ULL << shift; } /* zero memory or type */