hplatform.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #ifndef HV_PLATFORM_H_
  2. #define HV_PLATFORM_H_
  3. #include "hconfig.h"
  4. // OS
  5. #if defined(WIN64) || defined(_WIN64)
  6. #define OS_WIN64
  7. #define OS_WIN32
  8. #elif defined(WIN32)|| defined(_WIN32)
  9. #define OS_WIN32
  10. #elif defined(ANDROID) || defined(__ANDROID__)
  11. #define OS_ANDROID
  12. #define OS_LINUX
  13. #elif defined(linux) || defined(__linux) || defined(__linux__)
  14. #define OS_LINUX
  15. #elif defined(__APPLE__) && (defined(__GNUC__) || defined(__xlC__) || defined(__xlc__))
  16. #include <TargetConditionals.h>
  17. #if defined(TARGET_OS_MAC) && TARGET_OS_MAC
  18. #define OS_MAC
  19. #elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
  20. #define OS_IOS
  21. #endif
  22. #define OS_DARWIN
  23. #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  24. #define OS_FREEBSD
  25. #define OS_BSD
  26. #elif defined(__NetBSD__)
  27. #define OS_NETBSD
  28. #define OS_BSD
  29. #elif defined(__OpenBSD__)
  30. #define OS_OPENBSD
  31. #define OS_BSD
  32. #elif defined(sun) || defined(__sun) || defined(__sun__)
  33. #define OS_SOLARIS
  34. #else
  35. #error "Unsupported operating system platform!"
  36. #endif
  37. #if defined(OS_WIN32) || defined(OS_WIN64)
  38. #undef OS_UNIX
  39. #define OS_WIN
  40. #else
  41. #define OS_UNIX
  42. #endif
  43. // ARCH
  44. #if defined(__i386) || defined(__i386__) || defined(_M_IX86)
  45. #define ARCH_X86
  46. #define ARCH_X86_32
  47. #elif defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64)
  48. #define ARCH_X64
  49. #define ARCH_X86_64
  50. #elif defined(__arm__)
  51. #define ARCH_ARM
  52. #elif defined(__aarch64__) || defined(__ARM64__)
  53. #define ARCH_ARM64
  54. #else
  55. #define ARCH_UNKNOWN
  56. #warning "Unknown hardware architecture!"
  57. #endif
  58. // COMPILER
  59. #if defined (_MSC_VER)
  60. #define COMPILER_MSVC
  61. #if (_MSC_VER < 1200) // Visual C++ 6.0
  62. #define MSVS_VERSION 1998
  63. #define MSVC_VERSION 60
  64. #elif (_MSC_VER >= 1200) && (_MSC_VER < 1300) // Visual Studio 2002, MSVC++ 7.0
  65. #define MSVS_VERSION 2002
  66. #define MSVC_VERSION 70
  67. #elif (_MSC_VER >= 1300) && (_MSC_VER < 1400) // Visual Studio 2003, MSVC++ 7.1
  68. #define MSVS_VERSION 2003
  69. #define MSVC_VERSION 71
  70. #elif (_MSC_VER >= 1400) && (_MSC_VER < 1500) // Visual Studio 2005, MSVC++ 8.0
  71. #define MSVS_VERSION 2005
  72. #define MSVC_VERSION 80
  73. #elif (_MSC_VER >= 1500) && (_MSC_VER < 1600) // Visual Studio 2008, MSVC++ 9.0
  74. #define MSVS_VERSION 2008
  75. #define MSVC_VERSION 90
  76. #elif (_MSC_VER >= 1600) && (_MSC_VER < 1700) // Visual Studio 2010, MSVC++ 10.0
  77. #define MSVS_VERSION 2010
  78. #define MSVC_VERSION 100
  79. #elif (_MSC_VER >= 1700) && (_MSC_VER < 1800) // Visual Studio 2012, MSVC++ 11.0
  80. #define MSVS_VERSION 2012
  81. #define MSVC_VERSION 110
  82. #elif (_MSC_VER >= 1800) && (_MSC_VER < 1900) // Visual Studio 2013, MSVC++ 12.0
  83. #define MSVS_VERSION 2013
  84. #define MSVC_VERSION 120
  85. #elif (_MSC_VER >= 1900) && (_MSC_VER < 1910) // Visual Studio 2015, MSVC++ 14.0
  86. #define MSVS_VERSION 2015
  87. #define MSVC_VERSION 140
  88. #elif (_MSC_VER >= 1910) && (_MSC_VER < 1920) // Visual Studio 2017, MSVC++ 15.0
  89. #define MSVS_VERSION 2017
  90. #define MSVC_VERSION 150
  91. #elif (_MSC_VER >= 1920) && (_MSC_VER < 2000) // Visual Studio 2019, MSVC++ 16.0
  92. #define MSVS_VERSION 2019
  93. #define MSVC_VERSION 160
  94. #endif
  95. #undef HAVE_STDATOMIC_H
  96. #define HAVE_STDATOMIC_H 0
  97. #undef HAVE_SYS_TIME_H
  98. #define HAVE_SYS_TIME_H 0
  99. #undef HAVE_PTHREAD_H
  100. #define HAVE_PTHREAD_H 0
  101. #pragma warning (disable: 4018) // signed/unsigned comparison
  102. #pragma warning (disable: 4100) // unused param
  103. #pragma warning (disable: 4244) // conversion loss of data
  104. #pragma warning (disable: 4251) // STL dll
  105. #pragma warning (disable: 4267) // size_t => int
  106. #pragma warning (disable: 4819) // Unicode
  107. #pragma warning (disable: 4996) // _CRT_SECURE_NO_WARNINGS
  108. #elif defined(__MINGW32__) || defined(__MINGW64__)
  109. #define COMPILER_MINGW
  110. #elif defined(__GNUC__)
  111. #define COMPILER_GCC
  112. #ifndef _GNU_SOURCE
  113. #define _GNU_SOURCE 1
  114. #endif
  115. #elif defined(__clang__)
  116. #define COMPILER_CLANG
  117. #else
  118. #warning "Untested compiler!"
  119. #endif
  120. // headers
  121. #ifdef OS_WIN
  122. #ifndef WIN32_LEAN_AND_MEAN
  123. #define WIN32_LEAN_AND_MEAN
  124. #endif
  125. #define _CRT_NONSTDC_NO_DEPRECATE
  126. #define _CRT_SECURE_NO_WARNINGS
  127. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  128. #include <winsock2.h>
  129. #include <ws2tcpip.h> // for inet_pton,inet_ntop
  130. #include <windows.h>
  131. #include <process.h> // for getpid,exec
  132. #include <direct.h> // for mkdir,rmdir,chdir,getcwd
  133. #include <io.h> // for open,close,read,write,lseek,tell
  134. #define hv_delay(ms) Sleep(ms)
  135. #define hv_mkdir(dir) mkdir(dir)
  136. #ifndef S_ISREG
  137. #define S_ISREG(st_mode) (((st_mode) & S_IFMT) == S_IFREG)
  138. #endif
  139. #ifndef S_ISDIR
  140. #define S_ISDIR(st_mode) (((st_mode) & S_IFMT) == S_IFDIR)
  141. #endif
  142. #else
  143. #include <unistd.h>
  144. #include <dirent.h> // for mkdir,rmdir,chdir,getcwd
  145. // socket
  146. #include <sys/socket.h>
  147. #include <sys/select.h>
  148. #include <arpa/inet.h>
  149. #include <netinet/in.h>
  150. #include <netinet/tcp.h>
  151. #include <netinet/udp.h>
  152. #include <netdb.h> // for gethostbyname
  153. #define hv_delay(ms) usleep((ms)*1000)
  154. #define hv_mkdir(dir) mkdir(dir, 0777)
  155. #endif
  156. #ifdef _MSC_VER
  157. typedef int pid_t;
  158. typedef int gid_t;
  159. typedef int uid_t;
  160. #define strcasecmp stricmp
  161. #define strncasecmp strnicmp
  162. #else
  163. typedef int BOOL;
  164. typedef unsigned char BYTE;
  165. typedef unsigned short WORD;
  166. typedef void* HANDLE;
  167. #include <strings.h>
  168. #define stricmp strcasecmp
  169. #define strnicmp strncasecmp
  170. #endif
  171. // ENDIAN
  172. #ifndef BIG_ENDIAN
  173. #define BIG_ENDIAN 4321
  174. #endif
  175. #ifndef LITTLE_ENDIAN
  176. #define LITTLE_ENDIAN 1234
  177. #endif
  178. #ifndef NET_ENDIAN
  179. #define NET_ENDIAN BIG_ENDIAN
  180. #endif
  181. // BYTE_ORDER
  182. #ifndef BYTE_ORDER
  183. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(__ARMEL__)
  184. #define BYTE_ORDER LITTLE_ENDIAN
  185. #elif defined(__ARMEB__)
  186. #define BYTE_ORDER BIG_ENDIAN
  187. #endif
  188. #endif
  189. // ANSI C
  190. #include <assert.h>
  191. #include <stddef.h>
  192. #include <stdarg.h>
  193. #include <stdio.h>
  194. #include <stdlib.h>
  195. #include <string.h>
  196. #include <ctype.h>
  197. #include <float.h>
  198. #include <limits.h>
  199. #include <errno.h>
  200. #include <time.h>
  201. #include <math.h>
  202. #include <signal.h>
  203. #ifndef __cplusplus
  204. #if HAVE_STDBOOL_H
  205. #include <stdbool.h>
  206. #else
  207. #ifndef bool
  208. #define bool char
  209. #endif
  210. #ifndef true
  211. #define ture 1
  212. #endif
  213. #ifndef false
  214. #define false 0
  215. #endif
  216. #endif
  217. #endif
  218. #if HAVE_STDINT_H
  219. #include <stdint.h>
  220. #elif defined(_MSC_VER) && _MSC_VER < 1700
  221. typedef __int8 int8_t;
  222. typedef __int16 int16_t;
  223. typedef __int32 int32_t;
  224. typedef __int64 int64_t;
  225. typedef unsigned __int8 uint8_t;
  226. typedef unsigned __int16 uint16_t;
  227. typedef unsigned __int32 uint32_t;
  228. typedef unsigned __int64 uint64_t;
  229. #endif
  230. typedef float float32_t;
  231. typedef double float64_t;
  232. typedef int (*method_t)(void* userdata);
  233. typedef void (*procedure_t)(void* userdata);
  234. #if HAVE_SYS_TYPES_H
  235. #include <sys/types.h>
  236. #endif
  237. #if HAVE_SYS_STAT_H
  238. #include <sys/stat.h>
  239. #endif
  240. #if HAVE_SYS_TIME_H
  241. #include <sys/time.h> // for gettimeofday
  242. #endif
  243. #if HAVE_FCNTL_H
  244. #include <fcntl.h>
  245. #endif
  246. #if HAVE_PTHREAD_H
  247. #include <pthread.h>
  248. #endif
  249. #endif // HV_PLATFORM_H_