LinuxCommon.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #pragma once
  2. #ifdef __LP64__
  3. #define BF64
  4. #else
  5. #define BF32
  6. #endif
  7. #define BOOST_DETAIL_NO_CONTAINER_FWD
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <pthread.h>
  12. #include <time.h>
  13. #include <assert.h>
  14. #include <sys/time.h>
  15. //#include <libkern/OSAtomic.h>
  16. #include <cstdlib>
  17. #include <unistd.h>
  18. #include <wchar.h>
  19. #include <math.h>
  20. #include <stdarg.h>
  21. #include <string.h>
  22. #include <pthread.h>
  23. #include <wctype.h>
  24. #include <stddef.h>
  25. //#define offsetof(type, member) __builtin_offsetof (type, member)
  26. extern "C"
  27. {
  28. //#define FFI_BUILDING
  29. //#include "third_party/libffi/x86_64-apple-darwin12.5.0/include/ffi.h"
  30. }
  31. #define BF_ENDIAN_LITTLE
  32. #define _NOEXCEPT noexcept
  33. #define NTAPI
  34. #define FFI_STDCALL FFI_DEFAULT_ABI
  35. #define FFI_THISCALL FFI_DEFAULT_ABI
  36. #define FFI_FASTCALL FFI_DEFAULT_ABI
  37. #define INVALID_SOCKET -1
  38. typedef uint64_t uint64;
  39. typedef uint32_t uint32;
  40. typedef uint16_t uint16;
  41. typedef uint8_t uint8;
  42. typedef int64_t int64;
  43. typedef int32_t int32;
  44. typedef int16_t int16;
  45. typedef int8_t int8;
  46. typedef unsigned int uint;
  47. //#define BF_PLATFORM_SDL
  48. #define NOP
  49. //#define BF_NOTHROW throw ()
  50. //#define BF_NOTHROW noexcept
  51. #define BF_NOTHROW
  52. #ifdef BF64
  53. typedef int64 intptr;
  54. typedef uint64 uintptr;
  55. #else
  56. typedef int32 intptr;
  57. typedef uint32 uintptr;
  58. #endif
  59. typedef wchar_t* BSTR;
  60. typedef int HRESULT;
  61. typedef uint8 BYTE;
  62. typedef uint16 WORD;
  63. typedef uint32 DWORD;
  64. typedef int32 LONG;
  65. typedef pthread_key_t BFTlsKey;
  66. typedef pthread_t BF_THREADID;
  67. typedef pthread_t BF_THREADHANDLE;
  68. #define BF_HAS_TLS_DECLSPEC
  69. #define BF_TLS_DECLSPEC thread_local
  70. //:int64 abs(int64 val);
  71. #define _stricmp stricmp
  72. #define strnicmp strncasecmp
  73. struct IID
  74. {
  75. unsigned long Data1;
  76. unsigned short Data2;
  77. unsigned short Data3;
  78. unsigned char Data4[ 8 ];
  79. };
  80. typedef void* HANDLE;
  81. typedef void* HMODULE;
  82. // We only need the stdcall attribute for x32?
  83. //#define BFSTDCALL __attribute__((stdcall))
  84. //#include "../notwin/NotWin.h"
  85. #ifdef DEBUG
  86. #define _DEBUG
  87. #endif
  88. #define NOT_IMPL throw "Unimplemented";
  89. //ARM
  90. #if defined(__x86_64__) || defined(__i386__)
  91. #define BF_FULL_MEMORY_FENCE() __asm__ __volatile__("mfence": : :"memory")
  92. #define BF_SPINWAIT_NOP() __asm__ volatile ("pause\n" : : : "memory" );
  93. #else
  94. #define BF_FULL_MEMORY_FENCE() __sync_synchronize()
  95. #define BF_SPINWAIT_NOP() ((void) 0)
  96. #endif
  97. #define BF_COMPILER_FENCE() __asm__ __volatile__("": : :"memory")
  98. #define BF_THREAD_YIELD() sched_yield()
  99. #if defined _DEBUG || defined BF_DEBUG_ASSERTS
  100. #define BF_ASSERT(_Expression) (void)( (!!(_Expression)) || (Beefy::BFFatalError(#_Expression, __FILE__, __LINE__), 0) )
  101. #else
  102. #define BF_ASSERT(_Expression) (void)(0)
  103. #endif
  104. #define BF_ASSERT_REL(_Expression) (void)( (!!(_Expression)) || (Beefy::BFFatalError(#_Expression, __FILE__, __LINE__), 0) )
  105. #define BF_FATAL(msg) (void) ((Beefy::BFFatalError(msg, __FILE__, __LINE__), 0) )
  106. #if defined _DEBUG || defined BF_DEBUG_ASSERTS
  107. #define BF_DBG_FATAL(msg) (void) ((Beefy::BFFatalError(msg, __FILE__, __LINE__), 0) )
  108. #else
  109. #define BF_DBG_FATAL(msg)
  110. #endif
  111. #define BF_NOINLINE __attribute__ ((noinline))
  112. #define BF_NAKED
  113. #define stricmp strcasecmp
  114. #define _alloca alloca
  115. #define DIR_SEP_CHAR '/'
  116. #define DIR_SEP_CHAR_ALT '\\'
  117. static char* itoa(int value, char* str, int base)
  118. {
  119. if (base == 16)
  120. sprintf(str, "%X", value);
  121. else
  122. sprintf(str, "%d", value);
  123. return str;
  124. }
  125. inline uint32 InterlockedCompareExchange(volatile uint32* dest, uint32 exch, uint32 comp)
  126. {
  127. return __sync_val_compare_and_swap(dest, comp, exch);
  128. }
  129. inline uint64 InterlockedCompareExchange64(volatile int64* dest, int64 exch, int64 comp)
  130. {
  131. return __sync_val_compare_and_swap(dest, comp, exch);
  132. }
  133. inline void* InterlockedCompareExchangePointer(void* volatile* dest, void* exch, void* comp)
  134. {
  135. return __sync_val_compare_and_swap(dest, comp, exch);
  136. }
  137. inline uint32 InterlockedExchange(volatile uint32* dest, uint32 val)
  138. {
  139. return __sync_lock_test_and_set(dest, val);
  140. }
  141. inline uint64 InterlockedExchange64(volatile int64* dest, int64 val)
  142. {
  143. return __sync_lock_test_and_set(dest, val);
  144. }
  145. inline uint32 InterlockedExchangeAdd(volatile uint32* dest, uint32 val)
  146. {
  147. return __sync_add_and_fetch(dest, val);
  148. }
  149. inline int32 InterlockedIncrement(volatile uint32* val)
  150. {
  151. return __sync_add_and_fetch(val, 1);
  152. }
  153. inline int64 InterlockedIncrement64(volatile int64* val)
  154. {
  155. return __sync_add_and_fetch(val, 1);
  156. }
  157. inline int32 InterlockedDecrement(volatile uint32* val)
  158. {
  159. return __sync_add_and_fetch(val, -1);
  160. }
  161. inline int64 InterlockedDecrement64(volatile int64* val)
  162. {
  163. return __sync_add_and_fetch(val, -1);
  164. }