1
0
Fork 0
mirror of https://github.com/albfan/miraclecast.git synced 2025-02-13 10:11:54 +00:00

shl: update shl-macro.h

Fix a bug in SHL_ALIGN_POWER2 and add new shl_closep helpers.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
This commit is contained in:
David Herrmann 2014-05-12 14:32:41 +02:00
parent 5c88e3df66
commit 3dcb89e98b

View file

@ -1,7 +1,7 @@
/* /*
* SHL - Macros * SHL - Macros
* *
* Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com> * Copyright (c) 2011-2014 David Herrmann <dh.herrmann@gmail.com>
* Dedicated to the Public Domain * Dedicated to the Public Domain
*/ */
@ -18,6 +18,7 @@
#include <limits.h> #include <limits.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
/* sanity checks required for some macros */ /* sanity checks required for some macros */
#if __SIZEOF_POINTER__ != 4 && __SIZEOF_POINTER__ != 8 #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) #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) static inline void shl_set_errno(int *r)
{ {
errno = *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) */ /* align to next higher power-of-2 (except for: 0 => 0, overflow => 0) */
static inline size_t SHL_ALIGN_POWER2(size_t u) 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 */ /* zero memory or type */