]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/macro.h
core: Fix macro typo - PA_SINK_IS_LINKED -> PA_SINK_INPUT_IS_LINKED
[pulseaudio] / src / pulsecore / macro.h
index 2f6ee90ba7aab9b1a268fbabf44c6fb8ad6e7293..9a5a2670dc57980b0540ad7aa4c1d437ee6ef143 100644 (file)
@@ -29,6 +29,7 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include <pulse/gccmacro.h>
 
 #endif
 
 /* Rounds down */
-static inline void* pa_align_ptr(const void *p) {
-    return (void*) (((size_t) p) & ~(sizeof(void*)-1));
+static inline void* PA_ALIGN_PTR(const void *p) {
+    return (void*) (((size_t) p) & ~(sizeof(void*) - 1));
 }
-#define PA_ALIGN_PTR(x) (pa_align_ptr(x))
 
 /* Rounds up */
-static inline size_t pa_align(size_t l) {
-    return (((l + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*));
+static inline size_t PA_ALIGN(size_t l) {
+    return ((l + sizeof(void*) - 1) & ~(sizeof(void*) - 1));
 }
-#define PA_ALIGN(x) (pa_align(x))
 
 /* Rounds down */
-static inline void* pa_page_align_ptr(const void *p) {
-    return (void*) (((size_t) p) & ~(PA_PAGE_SIZE-1));
+static inline void* PA_PAGE_ALIGN_PTR(const void *p) {
+    return (void*) (((size_t) p) & ~(PA_PAGE_SIZE - 1));
 }
-#define PA_PAGE_ALIGN_PTR(x) (pa_page_align_ptr(x))
 
-static inline size_t pa_page_align(size_t l) {
-    return l & ~(PA_PAGE_SIZE-1);
+/* Rounds up */
+static inline size_t PA_PAGE_ALIGN(size_t l) {
+    return (l + PA_PAGE_SIZE - 1) & ~(PA_PAGE_SIZE - 1);
 }
-#define PA_PAGE_ALIGN(x) (pa_page_align(x))
 
 #define PA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
 
-/* The users of PA_MIN and PA_MAX should be aware that these macros on
- * non-GCC executed code with side effects twice. It is thus
- * considered misuse to use code with side effects as arguments to MIN
- * and MAX. */
+#if defined(__GNUC__)
+    #define PA_DECLARE_ALIGNED(n,t,v)      t v __attribute__ ((aligned (n)))
+#else
+    #define PA_DECLARE_ALIGNED(n,t,v)      t v
+#endif
+
+/* The users of PA_MIN and PA_MAX, PA_CLAMP, PA_ROUND_UP should be
+ * aware that these macros on non-GCC executed code with side effects
+ * twice. It is thus considered misuse to use code with side effects
+ * as arguments to MIN and MAX. */
 
 #ifdef __GNUC__
 #define PA_MAX(a,b)                             \
-    __extension__ ({ typeof(a) _a = (a);        \
+    __extension__ ({                            \
+            typeof(a) _a = (a);                 \
             typeof(b) _b = (b);                 \
             _a > _b ? _a : _b;                  \
         })
@@ -99,7 +104,8 @@ static inline size_t pa_page_align(size_t l) {
 
 #ifdef __GNUC__
 #define PA_MIN(a,b)                             \
-    __extension__ ({ typeof(a) _a = (a);        \
+    __extension__ ({                            \
+            typeof(a) _a = (a);                 \
             typeof(b) _b = (b);                 \
             _a < _b ? _a : _b;                  \
         })
@@ -109,7 +115,8 @@ static inline size_t pa_page_align(size_t l) {
 
 #ifdef __GNUC__
 #define PA_CLAMP(x, low, high)                                          \
-    __extension__ ({ typeof(x) _x = (x);                                \
+    __extension__ ({                                                    \
+            typeof(x) _x = (x);                                         \
             typeof(low) _low = (low);                                   \
             typeof(high) _high = (high);                                \
             ((_x > _high) ? _high : ((_x < _low) ? _low : _x));         \
@@ -120,7 +127,8 @@ static inline size_t pa_page_align(size_t l) {
 
 #ifdef __GNUC__
 #define PA_CLAMP_UNLIKELY(x, low, high)                                 \
-    __extension__ ({ typeof(x) _x = (x);                                \
+    __extension__ ({                                                    \
+            typeof(x) _x = (x);                                         \
             typeof(low) _low = (low);                                   \
             typeof(high) _high = (high);                                \
             (PA_UNLIKELY(_x > _high) ? _high : (PA_UNLIKELY(_x < _low) ? _low : _x)); \
@@ -133,6 +141,39 @@ static inline size_t pa_page_align(size_t l) {
  * make sense: we cannot know if it is more likely that the values is
  * lower or greater than the boundaries.*/
 
+#ifdef __GNUC__
+#define PA_ROUND_UP(a, b)                       \
+    __extension__ ({                            \
+            typeof(a) _a = (a);                 \
+            typeof(b) _b = (b);                 \
+            ((_a + _b - 1) / _b) * _b;          \
+        })
+#else
+#define PA_ROUND_UP(a, b) ((((a) + (b) - 1) / (b)) * (b))
+#endif
+
+#ifdef __GNUC__
+#define PA_ROUND_DOWN(a, b)                     \
+    __extension__ ({                            \
+            typeof(a) _a = (a);                 \
+            typeof(b) _b = (b);                 \
+            (_a / _b) * _b;                     \
+        })
+#else
+#define PA_ROUND_DOWN(a, b) (((a) / (b)) * (b))
+#endif
+
+#ifdef __GNUC__
+#define PA_CLIP_SUB(a, b)                       \
+    __extension__ ({                            \
+            typeof(a) _a = (a);                 \
+            typeof(b) _b = (b);                 \
+            _a > _b ? _a - _b : 0;              \
+        })
+#else
+#define PA_CLIP_SUB(a, b) ((a) > (b) ? (a) - (b) : 0)
+#endif
+
 /* This type is not intended to be used in exported APIs! Use classic "int" there! */
 #ifdef HAVE_STD_BOOL
 typedef _Bool pa_bool_t;
@@ -260,6 +301,21 @@ typedef int pa_bool_t;
 #define PA_DEBUG_TRAP raise(SIGTRAP)
 #endif
 
+#define pa_memzero(x,l) (memset((x), 0, (l)))
+#define pa_zero(x) (pa_memzero(&(x), sizeof(x)))
+
+#define PA_INT_TYPE_SIGNED(type) (!!((type) 0 > (type) -1))
+
+#define PA_INT_TYPE_MAX(type)                                          \
+    ((type) (PA_INT_TYPE_SIGNED(type)                                  \
+             ? ~(~(type) 0 << (8*sizeof(type)-1))                      \
+             : (type) -1))
+
+#define PA_INT_TYPE_MIN(type)                                          \
+    ((type) (PA_INT_TYPE_SIGNED(type)                                  \
+             ? (~(type) 0 << (8*sizeof(type)-1))                       \
+             : (type) 0))
+
 /* We include this at the very last place */
 #include <pulsecore/log.h>