hplatform.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_sleep(s) Sleep((s) * 1000)
  135. #define hv_msleep(ms) Sleep(ms)
  136. #define hv_usleep(us) Sleep((us) / 1000)
  137. #define hv_delay(ms) hv_msleep(ms)
  138. #define hv_mkdir(dir) mkdir(dir)
  139. #ifndef S_ISREG
  140. #define S_ISREG(st_mode) (((st_mode) & S_IFMT) == S_IFREG)
  141. #endif
  142. #ifndef S_ISDIR
  143. #define S_ISDIR(st_mode) (((st_mode) & S_IFMT) == S_IFDIR)
  144. #endif
  145. #else
  146. #include <unistd.h>
  147. #include <dirent.h> // for mkdir,rmdir,chdir,getcwd
  148. // socket
  149. #include <sys/socket.h>
  150. #include <sys/select.h>
  151. #include <arpa/inet.h>
  152. #include <netinet/in.h>
  153. #include <netinet/tcp.h>
  154. #include <netinet/udp.h>
  155. #include <netdb.h> // for gethostbyname
  156. #define hv_sleep(s) sleep(s)
  157. #define hv_msleep(ms) usleep((ms) * 1000)
  158. #define hv_usleep(us) usleep(us)
  159. #define hv_delay(ms) hv_msleep(ms)
  160. #define hv_mkdir(dir) mkdir(dir, 0777)
  161. #endif
  162. #ifdef _MSC_VER
  163. typedef int pid_t;
  164. typedef int gid_t;
  165. typedef int uid_t;
  166. #define strcasecmp stricmp
  167. #define strncasecmp strnicmp
  168. #else
  169. typedef int BOOL;
  170. typedef unsigned char BYTE;
  171. typedef unsigned short WORD;
  172. typedef void* HANDLE;
  173. #include <strings.h>
  174. #define stricmp strcasecmp
  175. #define strnicmp strncasecmp
  176. #endif
  177. // ENDIAN
  178. #ifndef BIG_ENDIAN
  179. #define BIG_ENDIAN 4321
  180. #endif
  181. #ifndef LITTLE_ENDIAN
  182. #define LITTLE_ENDIAN 1234
  183. #endif
  184. #ifndef NET_ENDIAN
  185. #define NET_ENDIAN BIG_ENDIAN
  186. #endif
  187. // BYTE_ORDER
  188. #ifndef BYTE_ORDER
  189. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(__ARMEL__)
  190. #define BYTE_ORDER LITTLE_ENDIAN
  191. #elif defined(__ARMEB__)
  192. #define BYTE_ORDER BIG_ENDIAN
  193. #endif
  194. #endif
  195. // ANSI C
  196. #include <assert.h>
  197. #include <stddef.h>
  198. #include <stdarg.h>
  199. #include <stdio.h>
  200. #include <stdlib.h>
  201. #include <string.h>
  202. #include <ctype.h>
  203. #include <float.h>
  204. #include <limits.h>
  205. #include <errno.h>
  206. #include <time.h>
  207. #include <math.h>
  208. #include <signal.h>
  209. #ifndef __cplusplus
  210. #if HAVE_STDBOOL_H
  211. #include <stdbool.h>
  212. #else
  213. #ifndef bool
  214. #define bool char
  215. #endif
  216. #ifndef true
  217. #define true 1
  218. #endif
  219. #ifndef false
  220. #define false 0
  221. #endif
  222. #endif
  223. #endif
  224. #if HAVE_STDINT_H
  225. #include <stdint.h>
  226. #elif defined(_MSC_VER) && _MSC_VER < 1700
  227. typedef __int8 int8_t;
  228. typedef __int16 int16_t;
  229. typedef __int32 int32_t;
  230. typedef __int64 int64_t;
  231. typedef unsigned __int8 uint8_t;
  232. typedef unsigned __int16 uint16_t;
  233. typedef unsigned __int32 uint32_t;
  234. typedef unsigned __int64 uint64_t;
  235. #endif
  236. typedef float float32_t;
  237. typedef double float64_t;
  238. typedef int (*method_t)(void* userdata);
  239. typedef void (*procedure_t)(void* userdata);
  240. #if HAVE_SYS_TYPES_H
  241. #include <sys/types.h>
  242. #endif
  243. #if HAVE_SYS_STAT_H
  244. #include <sys/stat.h>
  245. #endif
  246. #if HAVE_SYS_TIME_H
  247. #include <sys/time.h> // for gettimeofday
  248. #endif
  249. #if HAVE_FCNTL_H
  250. #include <fcntl.h>
  251. #endif
  252. #if HAVE_PTHREAD_H
  253. #include <pthread.h>
  254. #endif
  255. #endif // HV_PLATFORM_H_