|
Packit |
3ae693 |
/*-*- Mode: C; c-basic-offset: 8 -*-*/
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#ifndef foocanberramacrohfoo
|
|
Packit |
3ae693 |
#define foocanberramacrohfoo
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
/***
|
|
Packit |
3ae693 |
This file is part of libcanberra.
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
Copyright 2008 Lennart Poettering
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
libcanberra is free software; you can redistribute it and/or modify
|
|
Packit |
3ae693 |
it under the terms of the GNU Lesser General Public License as
|
|
Packit |
3ae693 |
published by the Free Software Foundation, either version 2.1 of the
|
|
Packit |
3ae693 |
License, or (at your option) any later version.
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
libcanberra is distributed in the hope that it will be useful, but
|
|
Packit |
3ae693 |
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
3ae693 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
3ae693 |
Lesser General Public License for more details.
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
3ae693 |
License along with libcanberra. If not, see
|
|
Packit |
3ae693 |
<http://www.gnu.org/licenses/>.
|
|
Packit |
3ae693 |
***/
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#include <stdio.h>
|
|
Packit |
3ae693 |
#include <stdlib.h>
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#ifndef PACKAGE
|
|
Packit |
3ae693 |
#error "Please include config.h before including this file!"
|
|
Packit |
3ae693 |
#endif
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#if defined (__GNUC__) && __GNUC__ >= 3
|
|
Packit |
3ae693 |
#define CA_LIKELY(x) (__builtin_expect(!!(x),1))
|
|
Packit |
3ae693 |
#define CA_UNLIKELY(x) (__builtin_expect((x),0))
|
|
Packit |
3ae693 |
#else
|
|
Packit |
3ae693 |
#define CA_LIKELY(x) (x)
|
|
Packit |
3ae693 |
#define CA_UNLIKELY(x) (x)
|
|
Packit |
3ae693 |
#endif
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#ifdef __GNUC__
|
|
Packit |
3ae693 |
#define CA_PRETTY_FUNCTION __PRETTY_FUNCTION__
|
|
Packit |
3ae693 |
#else
|
|
Packit |
3ae693 |
#define CA_PRETTY_FUNCTION ""
|
|
Packit |
3ae693 |
#endif
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define ca_return_if_fail(expr) \
|
|
Packit |
3ae693 |
do { \
|
|
Packit |
3ae693 |
if (CA_UNLIKELY(!(expr))) { \
|
|
Packit |
3ae693 |
if (ca_debug()) \
|
|
Packit |
3ae693 |
fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s().\n", #expr , __FILE__, __LINE__, CA_PRETTY_FUNCTION); \
|
|
Packit |
3ae693 |
return; \
|
|
Packit |
3ae693 |
} \
|
|
Packit |
3ae693 |
} while(FALSE)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define ca_return_val_if_fail(expr, val) \
|
|
Packit |
3ae693 |
do { \
|
|
Packit |
3ae693 |
if (CA_UNLIKELY(!(expr))) { \
|
|
Packit |
3ae693 |
if (ca_debug()) \
|
|
Packit |
3ae693 |
fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s().\n", #expr , __FILE__, __LINE__, CA_PRETTY_FUNCTION); \
|
|
Packit |
3ae693 |
return (val); \
|
|
Packit |
3ae693 |
} \
|
|
Packit |
3ae693 |
} while(FALSE)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define ca_return_null_if_fail(expr) ca_return_val_if_fail(expr, NULL)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define ca_return_if_fail_unlock(expr, mutex) \
|
|
Packit |
3ae693 |
do { \
|
|
Packit |
3ae693 |
if (CA_UNLIKELY(!(expr))) { \
|
|
Packit |
3ae693 |
if (ca_debug()) \
|
|
Packit |
3ae693 |
fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s().\n", #expr , __FILE__, __LINE__, CA_PRETTY_FUNCTION); \
|
|
Packit |
3ae693 |
ca_mutex_unlock(mutex); \
|
|
Packit |
3ae693 |
return; \
|
|
Packit |
3ae693 |
} \
|
|
Packit |
3ae693 |
} while(FALSE)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define ca_return_val_if_fail_unlock(expr, val, mutex) \
|
|
Packit |
3ae693 |
do { \
|
|
Packit |
3ae693 |
if (CA_UNLIKELY(!(expr))) { \
|
|
Packit |
3ae693 |
if (ca_debug()) \
|
|
Packit |
3ae693 |
fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s().\n", #expr , __FILE__, __LINE__, CA_PRETTY_FUNCTION); \
|
|
Packit |
3ae693 |
ca_mutex_unlock(mutex); \
|
|
Packit |
3ae693 |
return (val); \
|
|
Packit |
3ae693 |
} \
|
|
Packit |
3ae693 |
} while(FALSE)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define ca_return_null_if_fail_unlock(expr, mutex) ca_return_val_if_fail_unlock(expr, NULL, mutex)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
/* An assert which guarantees side effects of x, i.e. is never
|
|
Packit |
3ae693 |
* optimized away */
|
|
Packit |
3ae693 |
#define ca_assert_se(expr) \
|
|
Packit |
3ae693 |
do { \
|
|
Packit |
3ae693 |
if (CA_UNLIKELY(!(expr))) { \
|
|
Packit |
3ae693 |
fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s(). Aborting.\n", #expr , __FILE__, __LINE__, CA_PRETTY_FUNCTION); \
|
|
Packit |
3ae693 |
abort(); \
|
|
Packit |
3ae693 |
} \
|
|
Packit |
3ae693 |
} while (FALSE)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
/* An assert that may be optimized away by defining NDEBUG */
|
|
Packit |
3ae693 |
#ifdef NDEBUG
|
|
Packit |
3ae693 |
#define ca_assert(expr) do {} while (FALSE)
|
|
Packit |
3ae693 |
#else
|
|
Packit |
3ae693 |
#define ca_assert(expr) ca_assert_se(expr)
|
|
Packit |
3ae693 |
#endif
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define ca_assert_not_reached() \
|
|
Packit |
3ae693 |
do { \
|
|
Packit |
3ae693 |
fprintf(stderr, "Code should not be reached at %s:%u, function %s(). Aborting.\n", __FILE__, __LINE__, CA_PRETTY_FUNCTION); \
|
|
Packit |
3ae693 |
abort(); \
|
|
Packit |
3ae693 |
} while (FALSE)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#ifdef __GNUC__
|
|
Packit |
3ae693 |
#define CA_MAX(a,b) \
|
|
Packit |
3ae693 |
__extension__ ({ typeof(a) _a = (a); \
|
|
Packit |
3ae693 |
typeof(b) _b = (b); \
|
|
Packit |
3ae693 |
_a > _b ? _a : _b; \
|
|
Packit |
3ae693 |
})
|
|
Packit |
3ae693 |
#else
|
|
Packit |
3ae693 |
#define CA_MAX(a, b) ((a) > (b) ? (a) : (b))
|
|
Packit |
3ae693 |
#endif
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#ifdef __GNUC__
|
|
Packit |
3ae693 |
#define CA_MIN(a,b) \
|
|
Packit |
3ae693 |
__extension__ ({ typeof(a) _a = (a); \
|
|
Packit |
3ae693 |
typeof(b) _b = (b); \
|
|
Packit |
3ae693 |
_a < _b ? _a : _b; \
|
|
Packit |
3ae693 |
})
|
|
Packit |
3ae693 |
#else
|
|
Packit |
3ae693 |
#define CA_MIN(a, b) ((a) < (b) ? (a) : (b))
|
|
Packit |
3ae693 |
#endif
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#ifdef __GNUC__
|
|
Packit |
3ae693 |
#define CA_CLAMP(x, low, high) \
|
|
Packit |
3ae693 |
__extension__ ({ typeof(x) _x = (x); \
|
|
Packit |
3ae693 |
typeof(low) _low = (low); \
|
|
Packit |
3ae693 |
typeof(high) _high = (high); \
|
|
Packit |
3ae693 |
((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \
|
|
Packit |
3ae693 |
})
|
|
Packit |
3ae693 |
#else
|
|
Packit |
3ae693 |
#define CA_CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
|
|
Packit |
3ae693 |
#endif
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#ifndef FALSE
|
|
Packit |
3ae693 |
#define FALSE (0)
|
|
Packit |
3ae693 |
#endif
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#ifndef TRUE
|
|
Packit |
3ae693 |
#define TRUE (!FALSE)
|
|
Packit |
3ae693 |
#endif
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_PTR_TO_UINT(p) ((unsigned int) (unsigned long) (p))
|
|
Packit |
3ae693 |
#define CA_UINT_TO_PTR(u) ((void*) (unsigned long) (u))
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_PTR_TO_UINT32(p) ((uint32_t) CA_PTR_TO_UINT(p))
|
|
Packit |
3ae693 |
#define CA_UINT32_TO_PTR(u) CA_UINT_TO_PTR((uint32_t) u)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_PTR_TO_INT(p) ((int) CA_PTR_TO_UINT(p))
|
|
Packit |
3ae693 |
#define CA_INT_TO_PTR(u) CA_UINT_TO_PTR((int) u)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_PTR_TO_INT32(p) ((int32_t) CA_PTR_TO_UINT(p))
|
|
Packit |
3ae693 |
#define CA_INT32_TO_PTR(u) CA_UINT_TO_PTR((int32_t) u)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
typedef int ca_bool_t;
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
ca_bool_t ca_debug(void);
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
static inline size_t ca_align(size_t l) {
|
|
Packit |
3ae693 |
return (((l + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*));
|
|
Packit |
3ae693 |
}
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_ALIGN(x) (ca_align(x))
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
typedef void (*ca_free_cb_t)(void *);
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#ifdef HAVE_BYTESWAP_H
|
|
Packit |
3ae693 |
#include <byteswap.h>
|
|
Packit |
3ae693 |
#endif
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#ifdef HAVE_BYTESWAP_H
|
|
Packit |
3ae693 |
#define CA_INT16_SWAP(x) ((int16_t) bswap_16((uint16_t) x))
|
|
Packit |
3ae693 |
#define CA_UINT16_SWAP(x) ((uint16_t) bswap_16((uint16_t) x))
|
|
Packit |
3ae693 |
#define CA_INT32_SWAP(x) ((int32_t) bswap_32((uint32_t) x))
|
|
Packit |
3ae693 |
#define CA_UINT32_SWAP(x) ((uint32_t) bswap_32((uint32_t) x))
|
|
Packit |
3ae693 |
#else
|
|
Packit |
3ae693 |
#define CA_INT16_SWAP(x) ( (int16_t) ( ((uint16_t) x >> 8) | ((uint16_t) x << 8) ) )
|
|
Packit |
3ae693 |
#define CA_UINT16_SWAP(x) ( (uint16_t) ( ((uint16_t) x >> 8) | ((uint16_t) x << 8) ) )
|
|
Packit |
3ae693 |
#define CA_INT32_SWAP(x) ( (int32_t) ( ((uint32_t) x >> 24) | ((uint32_t) x << 24) | (((uint32_t) x & 0xFF00) << 8) | ((((uint32_t) x) >> 8) & 0xFF00) ) )
|
|
Packit |
3ae693 |
#define CA_UINT32_SWAP(x) ( (uint32_t) ( ((uint32_t) x >> 24) | ((uint32_t) x << 24) | (((uint32_t) x & 0xFF00) << 8) | ((((uint32_t) x) >> 8) & 0xFF00) ) )
|
|
Packit |
3ae693 |
#endif
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#ifdef WORDS_BIGENDIAN
|
|
Packit |
3ae693 |
#define CA_INT16_FROM_LE(x) CA_INT16_SWAP(x)
|
|
Packit |
3ae693 |
#define CA_INT16_FROM_BE(x) ((int16_t)(x))
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_INT16_TO_LE(x) CA_INT16_SWAP(x)
|
|
Packit |
3ae693 |
#define CA_INT16_TO_BE(x) ((int16_t)(x))
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_UINT16_FROM_LE(x) CA_UINT16_SWAP(x)
|
|
Packit |
3ae693 |
#define CA_UINT16_FROM_BE(x) ((uint16_t)(x))
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_UINT16_TO_LE(x) CA_UINT16_SWAP(x)
|
|
Packit |
3ae693 |
#define CA_UINT16_TO_BE(x) ((uint16_t)(x))
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_INT32_FROM_LE(x) CA_INT32_SWAP(x)
|
|
Packit |
3ae693 |
#define CA_INT32_FROM_BE(x) ((int32_t)(x))
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_INT32_TO_LE(x) CA_INT32_SWAP(x)
|
|
Packit |
3ae693 |
#define CA_INT32_TO_BE(x) ((int32_t)(x))
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_UINT32_FROM_LE(x) CA_UINT32_SWAP(x)
|
|
Packit |
3ae693 |
#define CA_UINT32_FROM_BE(x) ((uint32_t)(x))
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_UINT32_TO_LE(x) CA_UINT32_SWAP(x)
|
|
Packit |
3ae693 |
#define CA_UINT32_TO_BE(x) ((uint32_t)(x))
|
|
Packit |
3ae693 |
#else
|
|
Packit |
3ae693 |
#define CA_INT16_FROM_LE(x) ((int16_t)(x))
|
|
Packit |
3ae693 |
#define CA_INT16_FROM_BE(x) CA_INT16_SWAP(x)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_INT16_TO_LE(x) ((int16_t)(x))
|
|
Packit |
3ae693 |
#define CA_INT16_TO_BE(x) CA_INT16_SWAP(x)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_UINT16_FROM_LE(x) ((uint16_t)(x))
|
|
Packit |
3ae693 |
#define CA_UINT16_FROM_BE(x) CA_UINT16_SWAP(x)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_UINT16_TO_LE(x) ((uint16_t)(x))
|
|
Packit |
3ae693 |
#define CA_UINT16_TO_BE(x) CA_UINT16_SWAP(x)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_INT32_FROM_LE(x) ((int32_t)(x))
|
|
Packit |
3ae693 |
#define CA_INT32_FROM_BE(x) CA_INT32_SWAP(x)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_INT32_TO_LE(x) ((int32_t)(x))
|
|
Packit |
3ae693 |
#define CA_INT32_TO_BE(x) CA_INT32_SWAP(x)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_UINT32_FROM_LE(x) ((uint32_t)(x))
|
|
Packit |
3ae693 |
#define CA_UINT32_FROM_BE(x) CA_UINT32_SWAP(x)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define CA_UINT32_TO_LE(x) ((uint32_t)(x))
|
|
Packit |
3ae693 |
#define CA_UINT32_TO_BE(x) CA_UINT32_SWAP(x)
|
|
Packit |
3ae693 |
#endif
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#define ca_streq(a, b) (strcmp((a),(b)) == 0)
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#ifdef __GNUC__
|
|
Packit |
3ae693 |
#define CA_GCC_DESTRUCTOR __attribute__ ((destructor))
|
|
Packit |
3ae693 |
#else
|
|
Packit |
3ae693 |
#undef CA_GCC_DESTRUCTOR
|
|
Packit |
3ae693 |
#endif
|
|
Packit |
3ae693 |
|
|
Packit |
3ae693 |
#endif
|