hplatform.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #warning "Untested 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. #elif defined(__mips__)
  55. #define ARCH_MIPS
  56. #elif defined(__mips64__)
  57. #define ARCH_MIPS64
  58. #else
  59. #warning "Untested hardware architecture!"
  60. #endif
  61. // COMPILER
  62. #if defined (_MSC_VER)
  63. #define COMPILER_MSVC
  64. #if (_MSC_VER < 1200) // Visual C++ 6.0
  65. #define MSVS_VERSION 1998
  66. #define MSVC_VERSION 60
  67. #elif (_MSC_VER >= 1200) && (_MSC_VER < 1300) // Visual Studio 2002, MSVC++ 7.0
  68. #define MSVS_VERSION 2002
  69. #define MSVC_VERSION 70
  70. #elif (_MSC_VER >= 1300) && (_MSC_VER < 1400) // Visual Studio 2003, MSVC++ 7.1
  71. #define MSVS_VERSION 2003
  72. #define MSVC_VERSION 71
  73. #elif (_MSC_VER >= 1400) && (_MSC_VER < 1500) // Visual Studio 2005, MSVC++ 8.0
  74. #define MSVS_VERSION 2005
  75. #define MSVC_VERSION 80
  76. #elif (_MSC_VER >= 1500) && (_MSC_VER < 1600) // Visual Studio 2008, MSVC++ 9.0
  77. #define MSVS_VERSION 2008
  78. #define MSVC_VERSION 90
  79. #elif (_MSC_VER >= 1600) && (_MSC_VER < 1700) // Visual Studio 2010, MSVC++ 10.0
  80. #define MSVS_VERSION 2010
  81. #define MSVC_VERSION 100
  82. #elif (_MSC_VER >= 1700) && (_MSC_VER < 1800) // Visual Studio 2012, MSVC++ 11.0
  83. #define MSVS_VERSION 2012
  84. #define MSVC_VERSION 110
  85. #elif (_MSC_VER >= 1800) && (_MSC_VER < 1900) // Visual Studio 2013, MSVC++ 12.0
  86. #define MSVS_VERSION 2013
  87. #define MSVC_VERSION 120
  88. #elif (_MSC_VER >= 1900) && (_MSC_VER < 1910) // Visual Studio 2015, MSVC++ 14.0
  89. #define MSVS_VERSION 2015
  90. #define MSVC_VERSION 140
  91. #elif (_MSC_VER >= 1910) && (_MSC_VER < 1920) // Visual Studio 2017, MSVC++ 15.0
  92. #define MSVS_VERSION 2017
  93. #define MSVC_VERSION 150
  94. #elif (_MSC_VER >= 1920) && (_MSC_VER < 2000) // Visual Studio 2019, MSVC++ 16.0
  95. #define MSVS_VERSION 2019
  96. #define MSVC_VERSION 160
  97. #endif
  98. #undef HAVE_STDATOMIC_H
  99. #define HAVE_STDATOMIC_H 0
  100. #undef HAVE_SYS_TIME_H
  101. #define HAVE_SYS_TIME_H 0
  102. #undef HAVE_PTHREAD_H
  103. #define HAVE_PTHREAD_H 0
  104. #pragma warning (disable: 4018) // signed/unsigned comparison
  105. #pragma warning (disable: 4100) // unused param
  106. #pragma warning (disable: 4102) // unreferenced label
  107. #pragma warning (disable: 4244) // conversion loss of data
  108. #pragma warning (disable: 4267) // size_t => int
  109. #pragma warning (disable: 4819) // Unicode
  110. #pragma warning (disable: 4996) // _CRT_SECURE_NO_WARNINGS
  111. #elif defined(__MINGW32__) || defined(__MINGW64__)
  112. #define COMPILER_MINGW
  113. #elif defined(__GNUC__)
  114. #define COMPILER_GCC
  115. #ifndef _GNU_SOURCE
  116. #define _GNU_SOURCE 1
  117. #endif
  118. #elif defined(__clang__)
  119. #define COMPILER_CLANG
  120. #else
  121. #warning "Untested compiler!"
  122. #endif
  123. // headers
  124. #ifdef OS_WIN
  125. #ifndef WIN32_LEAN_AND_MEAN
  126. #define WIN32_LEAN_AND_MEAN
  127. #endif
  128. #ifndef _CRT_NONSTDC_NO_DEPRECATE
  129. #define _CRT_NONSTDC_NO_DEPRECATE
  130. #endif
  131. #ifndef _CRT_SECURE_NO_WARNINGS
  132. #define _CRT_SECURE_NO_WARNINGS
  133. #endif
  134. #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
  135. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  136. #endif
  137. #include <winsock2.h>
  138. #include <ws2tcpip.h> // for inet_pton,inet_ntop
  139. #include <windows.h>
  140. #include <process.h> // for getpid,exec
  141. #include <direct.h> // for mkdir,rmdir,chdir,getcwd
  142. #include <io.h> // for open,close,read,write,lseek,tell
  143. #define hv_sleep(s) Sleep((s) * 1000)
  144. #define hv_msleep(ms) Sleep(ms)
  145. #define hv_usleep(us) Sleep((us) / 1000)
  146. #define hv_delay(ms) hv_msleep(ms)
  147. #define hv_mkdir(dir) mkdir(dir)
  148. // access
  149. #ifndef F_OK
  150. #define F_OK 0 /* test for existence of file */
  151. #endif
  152. #ifndef X_OK
  153. #define X_OK (1<<0) /* test for execute or search permission */
  154. #endif
  155. #ifndef W_OK
  156. #define W_OK (1<<1) /* test for write permission */
  157. #endif
  158. #ifndef R_OK
  159. #define R_OK (1<<2) /* test for read permission */
  160. #endif
  161. // stat
  162. #ifndef S_ISREG
  163. #define S_ISREG(st_mode) (((st_mode) & S_IFMT) == S_IFREG)
  164. #endif
  165. #ifndef S_ISDIR
  166. #define S_ISDIR(st_mode) (((st_mode) & S_IFMT) == S_IFDIR)
  167. #endif
  168. #else
  169. #include <unistd.h>
  170. #include <dirent.h> // for mkdir,rmdir,chdir,getcwd
  171. // socket
  172. #include <sys/socket.h>
  173. #include <sys/select.h>
  174. #include <arpa/inet.h>
  175. #include <netinet/in.h>
  176. #include <netinet/tcp.h>
  177. #include <netinet/udp.h>
  178. #include <netdb.h> // for gethostbyname
  179. #define hv_sleep(s) sleep(s)
  180. #define hv_msleep(ms) usleep((ms) * 1000)
  181. #define hv_usleep(us) usleep(us)
  182. #define hv_delay(ms) hv_msleep(ms)
  183. #define hv_mkdir(dir) mkdir(dir, 0777)
  184. #endif
  185. #ifdef _MSC_VER
  186. typedef int pid_t;
  187. typedef int gid_t;
  188. typedef int uid_t;
  189. #define strcasecmp stricmp
  190. #define strncasecmp strnicmp
  191. #else
  192. typedef int BOOL;
  193. typedef unsigned char BYTE;
  194. typedef unsigned short WORD;
  195. typedef void* HANDLE;
  196. #include <strings.h>
  197. #define stricmp strcasecmp
  198. #define strnicmp strncasecmp
  199. #endif
  200. // ENDIAN
  201. #ifndef BIG_ENDIAN
  202. #define BIG_ENDIAN 4321
  203. #endif
  204. #ifndef LITTLE_ENDIAN
  205. #define LITTLE_ENDIAN 1234
  206. #endif
  207. #ifndef NET_ENDIAN
  208. #define NET_ENDIAN BIG_ENDIAN
  209. #endif
  210. // BYTE_ORDER
  211. #ifndef BYTE_ORDER
  212. #if defined(ARCH_X86) || defined(ARCH_X86_64) || \
  213. defined(__ARMEL__) || defined(__AARCH64EL__)
  214. #define BYTE_ORDER LITTLE_ENDIAN
  215. #elif defined(__ARMEB__) || defined(__AARCH64EB__)
  216. #define BYTE_ORDER BIG_ENDIAN
  217. #endif
  218. #endif
  219. // ANSI C
  220. #include <assert.h>
  221. #include <stddef.h>
  222. #include <stdarg.h>
  223. #include <stdio.h>
  224. #include <stdlib.h>
  225. #include <string.h>
  226. #include <ctype.h>
  227. #include <float.h>
  228. #include <limits.h>
  229. #include <errno.h>
  230. #include <time.h>
  231. #include <math.h>
  232. #include <signal.h>
  233. #ifndef __cplusplus
  234. #if HAVE_STDBOOL_H
  235. #include <stdbool.h>
  236. #else
  237. #ifndef bool
  238. #define bool char
  239. #endif
  240. #ifndef true
  241. #define true 1
  242. #endif
  243. #ifndef false
  244. #define false 0
  245. #endif
  246. #endif
  247. #endif
  248. #if HAVE_STDINT_H
  249. #include <stdint.h>
  250. #elif defined(_MSC_VER) && _MSC_VER < 1700
  251. typedef __int8 int8_t;
  252. typedef __int16 int16_t;
  253. typedef __int32 int32_t;
  254. typedef __int64 int64_t;
  255. typedef unsigned __int8 uint8_t;
  256. typedef unsigned __int16 uint16_t;
  257. typedef unsigned __int32 uint32_t;
  258. typedef unsigned __int64 uint64_t;
  259. #endif
  260. typedef float float32_t;
  261. typedef double float64_t;
  262. typedef int (*method_t)(void* userdata);
  263. typedef void (*procedure_t)(void* userdata);
  264. #if HAVE_SYS_TYPES_H
  265. #include <sys/types.h>
  266. #endif
  267. #if HAVE_SYS_STAT_H
  268. #include <sys/stat.h>
  269. #endif
  270. #if HAVE_SYS_TIME_H
  271. #include <sys/time.h> // for gettimeofday
  272. #endif
  273. #if HAVE_FCNTL_H
  274. #include <fcntl.h>
  275. #endif
  276. #if HAVE_PTHREAD_H
  277. #include <pthread.h>
  278. #endif
  279. #endif // HV_PLATFORM_H_