10#ifndef ARCH_UA_ARCHITECTURE_DEFINITIONS_H_
11#define ARCH_UA_ARCHITECTURE_DEFINITIONS_H_
23#if !defined(_MSC_VER) || _MSC_VER >= 1800
27# include "ms_stdint.h"
28# if !defined(__bool_true_false_are_defined)
29# define bool unsigned char
32# define __bool_true_false_are_defined
37#if !defined(_MSC_VER) || _MSC_VER >= 1800
40# define __PRI_8_LENGTH_MODIFIER__ "hh"
41# define __PRI_64_LENGTH_MODIFIER__ "ll"
43# define PRId8 __PRI_8_LENGTH_MODIFIER__ "d"
44# define PRIi8 __PRI_8_LENGTH_MODIFIER__ "i"
45# define PRIo8 __PRI_8_LENGTH_MODIFIER__ "o"
46# define PRIu8 __PRI_8_LENGTH_MODIFIER__ "u"
47# define PRIx8 __PRI_8_LENGTH_MODIFIER__ "x"
48# define PRIX8 __PRI_8_LENGTH_MODIFIER__ "X"
64# define PRId64 __PRI_64_LENGTH_MODIFIER__ "d"
65# define PRIi64 __PRI_64_LENGTH_MODIFIER__ "i"
66# define PRIo64 __PRI_64_LENGTH_MODIFIER__ "o"
67# define PRIu64 __PRI_64_LENGTH_MODIFIER__ "u"
68# define PRIx64 __PRI_64_LENGTH_MODIFIER__ "x"
69# define PRIX64 __PRI_64_LENGTH_MODIFIER__ "X"
73#if UA_MULTITHREADING >= 100
75# define UA_THREAD_LOCAL __thread
76# elif defined(_MSC_VER)
77# define UA_THREAD_LOCAL __declspec(thread)
80#ifndef UA_THREAD_LOCAL
81# define UA_THREAD_LOCAL
86#ifdef UA_ENABLE_MALLOC_SINGLETON
89extern UA_THREAD_LOCAL void * (*UA_callocSingleton)(
size_t nelem,
size_t elsize);
90extern UA_THREAD_LOCAL void * (*UA_reallocSingleton)(
void *ptr,
size_t size);
91# define UA_malloc(size) UA_mallocSingleton(size)
92# define UA_free(ptr) UA_freeSingleton(ptr)
93# define UA_calloc(num, size) UA_callocSingleton(num, size)
94# define UA_realloc(ptr, size) UA_reallocSingleton(ptr, size)
101# if defined(__GNUC__) || defined(__clang__)
102# define UA_STACKARRAY(TYPE, NAME, SIZE) TYPE NAME[SIZE]
104# if defined(__GNUC__) || defined(__clang__)
105# define UA_alloca(size) __builtin_alloca (size)
106# elif defined(_WIN32)
107# define UA_alloca(SIZE) _alloca(SIZE)
110# define UA_alloca(SIZE) alloca(SIZE)
112# define UA_STACKARRAY(TYPE, NAME, SIZE) \
114 TYPE *(NAME) = (TYPE*)UA_alloca(sizeof(TYPE) * (SIZE))
121# define UA_assert(ignore) assert(ignore)
123# define UA_assert(ignore) do {} while(0)
130#if defined(__cplusplus) && __cplusplus >= 201103L
131# define UA_STATIC_ASSERT(cond,msg) static_assert(cond, #msg)
132#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
133# define UA_STATIC_ASSERT(cond,msg) _Static_assert(cond, #msg)
134#elif defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
135# define UA_CTASTR2(pre,post) pre ## post
136# define UA_CTASTR(pre,post) UA_CTASTR2(pre,post)
138# define __COUNTER__ __LINE__
140# define UA_STATIC_ASSERT(cond,msg) \
142 int UA_CTASTR(static_assertion_failed_,msg) : !!(cond); \
143 } UA_CTASTR(static_assertion_failed_,__COUNTER__)
145# define UA_STATIC_ASSERT(cond,msg) typedef char static_assertion_##msg[(cond)?1:-1]
149#if defined(_WIN32) && defined(UA_DYNAMIC_LINKING)
150# ifdef UA_DYNAMIC_LINKING_EXPORT
152# define __attribute__ ((dllexport))
154# define __declspec(dllexport)
158# define __attribute__ ((dllimport))
160# define __declspec(dllimport)
164# if __GNUC__ || __clang__
165# define __attribute__ ((visibility ("default")))
185#elif defined(__GNUC__)
187#elif defined(__CODEGEARC__)
194#if defined(__GNUC__) || defined(__clang__)
195# define UA_LIKELY(x) __builtin_expect((x), 1)
196# define UA_UNLIKELY(x) __builtin_expect((x), 0)
198# define UA_LIKELY(x) x
199# define UA_UNLIKELY(x) x
203#if defined(__GNUC__) || defined(__clang__)
204# define __attribute__((malloc))
205# define UA_FUNC_ATTR_PURE __attribute__ ((pure))
206# define UA_FUNC_ATTR_CONST __attribute__((const))
207# define __attribute__((warn_unused_result))
208# define UA_FORMAT(X,Y) __attribute__ ((format (printf, X, Y)))
209#elif defined(_MSC_VER) && _MSC_VER >= 1800
212# define UA_FUNC_ATTR_PURE
213# define UA_FUNC_ATTR_CONST
214# define _Check_return_
215# define UA_FORMAT(X,Y)
218# define UA_FUNC_ATTR_PURE
219# define UA_FUNC_ATTR_CONST
221# define UA_FORMAT(X,Y)
224#if defined(__GNUC__) || defined(__clang__)
225# define UA_DEPRECATED __attribute__((deprecated))
226#elif defined(_MSC_VER)
227# define UA_DEPRECATED __declspec(deprecated)
229# define UA_DEPRECATED
234#if defined(UA_INTERNAL) && (defined(__GNUC__) || defined(__clang__))
235# define UA_INTERNAL_DEPRECATED \
236 _Pragma ("GCC warning \"Macro is deprecated for internal use\"")
238# define UA_INTERNAL_DEPRECATED
241#if defined(UA_INTERNAL) && (defined(__GNUC__) || defined(__clang__))
242# define UA_INTERNAL_FUNC_ATTR_WARN_UNUSED_RESULT \
243 __attribute__((warn_unused_result))
245# define UA_INTERNAL_FUNC_ATTR_WARN_UNUSED_RESULT
250# define UA_LITTLE_ENDIAN 1
251#elif defined(__i386__) || defined(__x86_64__) || defined(__amd64__)
252# define UA_LITTLE_ENDIAN 1
253#elif (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
254 (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
255# define UA_LITTLE_ENDIAN 1
256#elif defined(__linux__)
258# if __BYTE_ORDER == __LITTLE_ENDIAN
259# define UA_LITTLE_ENDIAN 1
261#elif defined(__OpenBSD__)
262# include <sys/endian.h>
263# if BYTE_ORDER == LITTLE_ENDIAN
264# define UA_LITTLE_ENDIAN 1
266#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
267# include <sys/endian.h>
268# if _BYTE_ORDER == _LITTLE_ENDIAN
269# define UA_LITTLE_ENDIAN 1
271#elif defined(__APPLE__)
272# include <libkern/OSByteOrder.h>
273# if defined(__LITTLE_ENDIAN__)
274# define UA_LITTLE_ENDIAN 1
276#elif defined(__QNX__) || defined(__QNXNTO__)
277# include <gulliver.h>
278# if defined(__LITTLEENDIAN__)
279# define UA_LITTLE_ENDIAN 1
281#elif defined(_OS9000)
282# if defined(_LIL_END)
283# define UA_LITTLE_ENDIAN 1
286#ifndef UA_LITTLE_ENDIAN
287# define UA_LITTLE_ENDIAN 0
293#if (UA_LITTLE_ENDIAN == 1)
294UA_STATIC_ASSERT(
sizeof(
bool) == 1, cannot_overlay_integers_with_large_bool);
295# define UA_BINARY_OVERLAYABLE_INTEGER 1
297# define UA_BINARY_OVERLAYABLE_INTEGER 0
301#ifndef UA_FLOAT_IEEE754
303# define UA_FLOAT_IEEE754 1
304#elif defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \
305 defined(__ia64__) || defined(__powerpc__) || defined(__sparc__) || \
307# define UA_FLOAT_IEEE754 1
308#elif defined(__STDC_IEC_559__)
309# define UA_FLOAT_IEEE754 1
310#elif defined(ESP_PLATFORM)
311# define UA_FLOAT_IEEE754 1
313# define UA_FLOAT_IEEE754 0
323# define UA_FLOAT_LITTLE_ENDIAN 1
324#elif defined(__i386__) || defined(__x86_64__) || defined(__amd64__)
325# define UA_FLOAT_LITTLE_ENDIAN 1
326#elif defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
327 (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__)
328# define UA_FLOAT_LITTLE_ENDIAN 1
329#elif defined(__FLOAT_WORD_ORDER) && defined(__LITTLE_ENDIAN) && \
330 (__FLOAT_WORD_ORDER == __LITTLE_ENDIAN)
331# define UA_FLOAT_LITTLE_ENDIAN 1
333#ifndef UA_FLOAT_LITTLE_ENDIAN
334# define UA_FLOAT_LITTLE_ENDIAN 0
339#if (UA_FLOAT_IEEE754 == 1) && (UA_FLOAT_LITTLE_ENDIAN == 1)
340# define UA_BINARY_OVERLAYABLE_FLOAT 1
342# define UA_BINARY_OVERLAYABLE_FLOAT 0
350#if UA_MULTITHREADING >= 100
352 #define UA_atomic_sync() _ReadWriteBarrier()
354 #define UA_atomic_sync() __sync_synchronize()
357 #define UA_atomic_sync()
362#if UA_MULTITHREADING >= 100 && defined(_WIN32)
364#define _NO_WINSOCKAPI_
368#ifdef _NO_WINSOCKAPI_
374UA_atomic_xchg(
void *
volatile * addr,
void *newptr) {
375#if UA_MULTITHREADING >= 100
377 return InterlockedExchangePointer(addr, newptr);
379 return __sync_lock_test_and_set(addr, newptr);
389UA_atomic_cmpxchg(
void *
volatile * addr,
void *expected,
void *newptr) {
390#if UA_MULTITHREADING >= 100
392 return InterlockedCompareExchangePointer(addr, newptr, expected);
394 return __sync_val_compare_and_swap(addr, expected, newptr);
398 if(old == expected) {
406UA_atomic_addUInt32(
volatile uint32_t *addr, uint32_t increase) {
407#if UA_MULTITHREADING >= 100
409 return InterlockedExchangeAdd(addr, increase) + increase;
411 return __sync_add_and_fetch(addr, increase);
414 uint32_t accu = *addr;
422UA_atomic_addSize(
volatile size_t *addr,
size_t increase) {
423#if UA_MULTITHREADING >= 100
425 return InterlockedExchangeAdd((
volatile LONG *)addr, (LONG)increase) + increase;
427 return __sync_add_and_fetch(addr, increase);
438UA_atomic_subUInt32(
volatile uint32_t *addr, uint32_t decrease) {
439#if UA_MULTITHREADING >= 100
441 return InterlockedExchangeAdd(addr, - (LONG) decrease) - decrease;
443 return __sync_sub_and_fetch(addr, decrease);
446 uint32_t accu = *addr;
454UA_atomic_subSize(
volatile size_t *addr,
size_t decrease) {
455#if UA_MULTITHREADING >= 100
457 return InterlockedExchangeAdd((
volatile LONG *)addr, -(LONG)decrease) - decrease;
459 return __sync_sub_and_fetch(addr, decrease);
#define UA_STATIC_ASSERT(cond, msg)
Outputs an error message at compile time if the assert fails.
#define UA_THREAD_LOCAL
This Source Code Form is subject to the terms of the Mozilla Public License, v.