hplatform.h 7.8 KB

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