hplatform.h 7.9 KB

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