]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/macro.h
Merge branch 'master' of git://0pointer.de/pulseaudio into dbus-work
[pulseaudio] / src / pulsecore / macro.h
index fd33b7bb08c7eb8f6f0ad6bec4a4d1da0cfdb408..cf662510897b0c375336e89aeebf117e1d44852a 100644 (file)
@@ -8,7 +8,7 @@
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
-  by the Free Software Foundation; either version 2 of the License,
+  by the Free Software Foundation; either version 2.1 of the License,
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
@@ -29,8 +29,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
-#include <pulsecore/log.h>
 #include <pulse/gccmacro.h>
 
 #ifndef PACKAGE
@@ -40,7 +40,7 @@
 #ifndef PA_LIKELY
 #ifdef __GNUC__
 #define PA_LIKELY(x) (__builtin_expect(!!(x),1))
-#define PA_UNLIKELY(x) (__builtin_expect((x),0))
+#define PA_UNLIKELY(x) (__builtin_expect(!!(x),0))
 #else
 #define PA_LIKELY(x) (x)
 #define PA_UNLIKELY(x) (x)
 #define PA_PAGE_SIZE ((size_t) 4096)
 #endif
 
+/* Rounds down */
+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*));
 }
 #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));
 }
 #define PA_PAGE_ALIGN_PTR(x) (pa_page_align_ptr(x))
 
+/* Rounds up */
 static inline size_t pa_page_align(size_t l) {
-    return l & ~(PA_PAGE_SIZE-1);
+    return ((l + PA_PAGE_SIZE - 1) / PA_PAGE_SIZE) * PA_PAGE_SIZE;
 }
 #define PA_PAGE_ALIGN(x) (pa_page_align(x))
 
@@ -165,8 +174,8 @@ typedef int pa_bool_t;
 
 #define pa_return_null_if_fail(expr) pa_return_val_if_fail(expr, NULL)
 
-/* An assert which guarantees side effects of x, i.e. is never
- * optimized away */
+/* pa_assert_se() is an assert which guarantees side effects of x,
+ * i.e. is never optimized away, regardless of NDEBUG or FASTPATH. */
 #define pa_assert_se(expr)                                              \
     do {                                                                \
         if (PA_UNLIKELY(!(expr))) {                                     \
@@ -175,30 +184,56 @@ typedef int pa_bool_t;
         }                                                               \
     } while (FALSE)
 
-/* An assert that may be optimized away by defining NDEBUG */
+/* Does exactly nothing */
+#define pa_nop() do {} while (FALSE)
+
+/* pa_assert() is an assert that may be optimized away by defining
+ * NDEBUG. pa_assert_fp() is an assert that may be optimized away by
+ * defining FASTPATH. It is supposed to be used in inner loops. It's
+ * there for extra paranoia checking and should probably be removed in
+ * production builds. */
 #ifdef NDEBUG
-#define pa_assert(expr) do {} while (FALSE)
+#define pa_assert(expr) pa_nop()
+#define pa_assert_fp(expr) pa_nop()
+#elif defined (FASTPATH)
+#define pa_assert(expr) pa_assert_se(expr)
+#define pa_assert_fp(expr) pa_nop()
 #else
 #define pa_assert(expr) pa_assert_se(expr)
+#define pa_assert_fp(expr) pa_assert_se(expr)
 #endif
 
+#ifdef NDEBUG
+#define pa_assert_not_reached() pa_nop()
+#else
 #define pa_assert_not_reached()                                         \
     do {                                                                \
         pa_log_error("Code should not be reached at %s:%u, function %s(). Aborting.", __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
         abort();                                                        \
     } while (FALSE)
+#endif
 
-#define PA_PTR_TO_UINT(p) ((unsigned int) (unsigned long) (p))
-#define PA_UINT_TO_PTR(u) ((void*) (unsigned long) (u))
+/* A compile time assertion */
+#define pa_assert_cc(expr)                         \
+    do {                                           \
+        switch (0) {                               \
+            case 0:                                \
+            case !!(expr):                         \
+                ;                                  \
+        }                                          \
+    } while (FALSE)
 
-#define PA_PTR_TO_UINT32(p) ((uint32_t) PA_PTR_TO_UINT(p))
-#define PA_UINT32_TO_PTR(u) PA_UINT_TO_PTR((uint32_t) u)
+#define PA_PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
+#define PA_UINT_TO_PTR(u) ((void*) ((uintptr_t) (u)))
 
-#define PA_PTR_TO_INT(p) ((int) PA_PTR_TO_UINT(p))
-#define PA_INT_TO_PTR(u) PA_UINT_TO_PTR((int) u)
+#define PA_PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
+#define PA_UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
 
-#define PA_PTR_TO_INT32(p) ((int32_t) PA_PTR_TO_UINT(p))
-#define PA_INT32_TO_PTR(u) PA_UINT_TO_PTR((int32_t) u)
+#define PA_PTR_TO_INT(p) ((int) ((intptr_t) (p)))
+#define PA_INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
+
+#define PA_PTR_TO_INT32(p) ((int32_t) ((intptr_t) (p)))
+#define PA_INT32_TO_PTR(u) ((void*) ((intptr_t) (u)))
 
 #ifdef OS_IS_WIN32
 #define PA_PATH_SEP "\\"
@@ -208,7 +243,7 @@ typedef int pa_bool_t;
 #define PA_PATH_SEP_CHAR '/'
 #endif
 
-#ifdef __GNUC__
+#if defined(__GNUC__) && defined(__ELF__)
 
 #define PA_WARN_REFERENCE(sym, msg)                  \
     __asm__(".section .gnu.warning." #sym);          \
@@ -221,4 +256,16 @@ typedef int pa_bool_t;
 
 #endif
 
+#if defined(__i386__) || defined(__x86_64__)
+#define PA_DEBUG_TRAP __asm__("int $3")
+#else
+#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)))
+
+/* We include this at the very last place */
+#include <pulsecore/log.h>
+
 #endif