1 /* Pixman uses some non-standard compiler features. This file ensures
6 * FUNC must be defined to expand to the current function
7 * PIXMAN_EXPORT should be defined to whatever is required to
8 * export functions from a shared library
9 * limits limits for various types must be defined
10 * inline must be defined
11 * force_inline must be defined
13 #if defined (__GNUC__)
14 # define FUNC ((const char*) (__PRETTY_FUNCTION__))
15 #elif defined (__sun) || (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
16 # define FUNC ((const char*) (__func__))
18 # define FUNC ((const char*) ("???"))
21 #if defined (__GNUC__)
22 # define unlikely(expr) __builtin_expect ((expr), 0)
24 # define unlikely(expr) (expr)
27 #if defined (__GNUC__)
28 # define MAYBE_UNUSED __attribute__((unused))
34 # define INT16_MIN (-32767-1)
38 # define INT16_MAX (32767)
42 # define INT32_MIN (-2147483647-1)
46 # define INT32_MAX (2147483647)
50 # define UINT32_MIN (0)
54 # define UINT32_MAX (4294967295U)
58 # define INT64_MIN (-9223372036854775807-1)
62 # define INT64_MAX (9223372036854775807)
66 # define SIZE_MAX ((size_t)-1)
71 # define M_PI 3.14159265358979323846
75 /* 'inline' is available only in C++ in MSVC */
76 # define inline __inline
77 # define force_inline __forceinline
78 # define noinline __declspec(noinline)
79 #elif defined __GNUC__ || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
80 # define inline __inline__
81 # define force_inline __inline__ __attribute__ ((__always_inline__))
82 # define noinline __attribute__((noinline))
85 # define force_inline inline
93 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(_WIN32)
94 # define PIXMAN_EXPORT __attribute__ ((visibility("default")))
95 /* Sun Studio 8 visibility */
96 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
97 # define PIXMAN_EXPORT __global
99 # define PIXMAN_EXPORT
103 #define CONTAINER_OF(type, member, data) \
104 ((type *)(((uint8_t *)data) - offsetof (type, member)))
107 #if defined(PIXMAN_NO_TLS)
109 # define PIXMAN_DEFINE_THREAD_LOCAL(type, name) \
111 # define PIXMAN_GET_THREAD_LOCAL(name) \
116 # define PIXMAN_DEFINE_THREAD_LOCAL(type, name) \
118 # define PIXMAN_GET_THREAD_LOCAL(name) \
121 #elif defined(__MINGW32__)
123 # define _NO_W32_PSEUDO_MODIFIERS
124 # include <windows.h>
126 # define PIXMAN_DEFINE_THREAD_LOCAL(type, name) \
127 static volatile int tls_ ## name ## _initialized = 0; \
128 static void *tls_ ## name ## _mutex = NULL; \
129 static unsigned tls_ ## name ## _index; \
132 tls_ ## name ## _alloc (void) \
134 type *value = calloc (1, sizeof (type)); \
136 TlsSetValue (tls_ ## name ## _index, value); \
140 static force_inline type * \
141 tls_ ## name ## _get (void) \
144 if (!tls_ ## name ## _initialized) \
146 if (!tls_ ## name ## _mutex) \
148 void *mutex = CreateMutexA (NULL, 0, NULL); \
149 if (InterlockedCompareExchangePointer ( \
150 &tls_ ## name ## _mutex, mutex, NULL) != NULL) \
152 CloseHandle (mutex); \
155 WaitForSingleObject (tls_ ## name ## _mutex, 0xFFFFFFFF); \
156 if (!tls_ ## name ## _initialized) \
158 tls_ ## name ## _index = TlsAlloc (); \
159 tls_ ## name ## _initialized = 1; \
161 ReleaseMutex (tls_ ## name ## _mutex); \
163 if (tls_ ## name ## _index == 0xFFFFFFFF) \
165 value = TlsGetValue (tls_ ## name ## _index); \
167 value = tls_ ## name ## _alloc (); \
171 # define PIXMAN_GET_THREAD_LOCAL(name) \
172 tls_ ## name ## _get ()
174 #elif defined(_MSC_VER)
176 # define PIXMAN_DEFINE_THREAD_LOCAL(type, name) \
177 static __declspec(thread) type name
178 # define PIXMAN_GET_THREAD_LOCAL(name) \
181 #elif defined(HAVE_PTHREADS)
185 # define PIXMAN_DEFINE_THREAD_LOCAL(type, name) \
186 static pthread_once_t tls_ ## name ## _once_control = PTHREAD_ONCE_INIT; \
187 static pthread_key_t tls_ ## name ## _key; \
190 tls_ ## name ## _destroy_value (void *value) \
196 tls_ ## name ## _make_key (void) \
198 pthread_key_create (&tls_ ## name ## _key, \
199 tls_ ## name ## _destroy_value); \
203 tls_ ## name ## _alloc (void) \
205 type *value = calloc (1, sizeof (type)); \
207 pthread_setspecific (tls_ ## name ## _key, value); \
211 static force_inline type * \
212 tls_ ## name ## _get (void) \
214 type *value = NULL; \
215 if (pthread_once (&tls_ ## name ## _once_control, \
216 tls_ ## name ## _make_key) == 0) \
218 value = pthread_getspecific (tls_ ## name ## _key); \
220 value = tls_ ## name ## _alloc (); \
225 # define PIXMAN_GET_THREAD_LOCAL(name) \
226 tls_ ## name ## _get ()
230 # error "Unknown thread local support for this system. Pixman will not work with multiple threads. Define PIXMAN_NO_TLS to acknowledge and accept this limitation and compile pixman without thread-safety support."