swapi.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /************************************************************************
  2. * AUTHOR: NiuJiuRu
  3. * FILENAME: swapi.h
  4. * DESCRIPTION:
  5. * "SWAPI" library standard header file
  6. * SUPPORT PLATFORM: Linux
  7. * HISTORY:
  8. * 1, [2010-07-01] created by NiuJiuRu
  9. * 2, [2013-04-23] modified by NiuJiuRu, add "likely(x)" and "unlikely(x)"
  10. macro definition for linux platform
  11. * 3, [2025-01-07] review for linux
  12. ************************************************************************/
  13. #ifndef __SWAPI_H__
  14. #define __SWAPI_H__
  15. //////////////////////////////////////////////////////////////////////////
  16. // 1, Select platform
  17. // linux platform
  18. #if (defined __linux || defined __linux__) && !defined LINUX
  19. #define LINUX
  20. #endif
  21. // error, invalid platform
  22. #if !defined LINUX
  23. #error "SWAPI Library" currently not support this platform
  24. #endif
  25. //////////////////////////////////////////////////////////////////////////
  26. // 2, C language common header files
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <stdint.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #include <stdbool.h>
  33. #include <inttypes.h>
  34. #include <ctype.h>
  35. #include <time.h>
  36. #include <math.h>
  37. #include <wchar.h>
  38. #include <limits.h>
  39. #include <locale.h>
  40. #include <assert.h>
  41. #include <errno.h>
  42. //////////////////////////////////////////////////////////////////////////
  43. // 3, For "Linux" platform
  44. #ifdef LINUX
  45. // include "Linux" common header files
  46. #include <pthread.h>
  47. #include <semaphore.h>
  48. #include <unistd.h>
  49. #include <signal.h>
  50. #include <fcntl.h>
  51. #include <getopt.h>
  52. #include <termios.h>
  53. #include <netdb.h>
  54. #include <dirent.h>
  55. #include <arpa/inet.h>
  56. #include <netinet/in.h>
  57. #include <netinet/ip.h>
  58. #include <netinet/udp.h>
  59. #include <netinet/tcp.h>
  60. #include <netinet/ether.h>
  61. #include <net/if.h>
  62. #include <net/if_arp.h>
  63. #include <sys/types.h>
  64. #include <sys/file.h>
  65. #include <sys/stat.h>
  66. #include <sys/timeb.h>
  67. #include <sys/time.h>
  68. #include <sys/ioctl.h>
  69. #include <sys/socket.h>
  70. #include <sys/epoll.h>
  71. #include <sys/param.h>
  72. #include <sys/mman.h>
  73. #include <sys/reboot.h>
  74. #include <libgen.h>
  75. // define "Linux" common macros
  76. #define MAKEWORD(a, b) ((uint16_t)(((uint8_t)(a)) | ((uint16_t)((uint8_t)(b))) << 8))
  77. #define MAKEDWORD(a, b) ((uint32_t)(((uint16_t)(a)) | ((uint32_t)((uint16_t)(b))) << 16))
  78. #define MAKEQWORD(a, b) ((uint64_t)(((uint32_t)(a)) | ((uint64_t)((uint32_t)(b))) << 32))
  79. #define LODWORD(q) ((uint32_t)(q))
  80. #define HIDWORD(q) ((uint32_t)(((uint64_t)(q) >> 32) & 0xFFFFFFFF))
  81. #define LOWORD(d) ((uint16_t)(d))
  82. #define HIWORD(d) ((uint16_t)(((uint32_t)(d) >> 16) & 0xFFFF))
  83. #define LOBYTE(w) ((uint8_t)(w))
  84. #define HIBYTE(w) ((uint8_t)(((uint16_t)(w) >> 8) & 0xFF))
  85. // IP address is the multicast address ?
  86. #define IS_MULTICAST_IP(ip) (0xE0 <= ((ip)&0xFF) && ((ip)&0xFF) < 0xF0)
  87. // controls the I/O mode of a socket
  88. #ifndef ioctlsocket
  89. #define ioctlsocket ioctl
  90. #endif
  91. // closes an existing socket
  92. #ifndef closesocket
  93. #define closesocket close
  94. #endif
  95. // somewhere in the middle of the GCC 2.96 development cycle, we implemented
  96. // a mechanism by which the user can annotate likely branch directions and
  97. // expect the blocks to be reordered appropriately. Define __builtin_expect
  98. // to nothing for earlier compilers
  99. #if __GNUC__ == 2 && __GNUC_MINOR__ < 96
  100. #define __builtin_expect(x, expected_value) (x)
  101. #endif
  102. #ifndef likely
  103. #define likely(x) __builtin_expect(!!(x), 1)
  104. #endif
  105. #ifndef unlikely
  106. #define unlikely(x) __builtin_expect(!!(x), 0)
  107. #endif
  108. #endif
  109. //////////////////////////////////////////////////////////////////////////
  110. // 4, Define common macros
  111. #ifndef max
  112. #define max(a, b) (((a) > (b)) ? (a) : (b))
  113. #endif
  114. #ifndef min
  115. #define min(a, b) (((a) < (b)) ? (a) : (b))
  116. #endif
  117. #ifndef MAX_PATH_CHARS
  118. #define MAX_PATH_CHARS 260
  119. #endif
  120. #ifndef MAX_LINE_CHARS
  121. #define MAX_LINE_CHARS 192
  122. #endif
  123. #ifndef __cplusplus
  124. #ifndef bool
  125. #define bool unsigned char
  126. #endif
  127. #ifndef true
  128. #define true 1
  129. #endif
  130. #ifndef false
  131. #define false 0
  132. #endif
  133. #endif
  134. #ifndef BOOL
  135. #define BOOL int
  136. #endif
  137. #ifndef TRUE
  138. #define TRUE 1
  139. #endif
  140. #ifndef FALSE
  141. #define FALSE 0
  142. #endif
  143. #ifndef NULL
  144. #ifdef __cplusplus
  145. #define NULL 0
  146. #else
  147. #define NULL ((void *)0)
  148. #endif
  149. #endif
  150. #ifndef null
  151. #define null NULL
  152. #endif
  153. #ifndef HANDLE
  154. #define HANDLE void *
  155. #endif
  156. #ifndef SOCKET
  157. #define SOCKET int
  158. #endif
  159. #ifndef INVALID_SOCKET
  160. #define INVALID_SOCKET (SOCKET)(~0)
  161. #endif
  162. #ifndef SOCKET_ERROR
  163. #define SOCKET_ERROR -1
  164. #endif
  165. #ifndef ERROR
  166. #define ERROR -1
  167. #endif
  168. #ifndef INFINITE
  169. #define INFINITE 0xFFFFFFFF
  170. #endif
  171. #ifndef WAIT_FOREVER
  172. #define WAIT_FOREVER INFINITE
  173. #endif
  174. //////////////////////////////////////////////////////////////////////////
  175. // 5, Show memory allocation situation detailed when you need
  176. #ifndef _MEM_DEBUG_DETAIL
  177. //#define _MEM_DEBUG_DETAIL
  178. #endif
  179. //////////////////////////////////////////////////////////////////////////
  180. // 6, Program logs
  181. #include "swlog.h"
  182. //////////////////////////////////////////////////////////////////////////
  183. // 7, Define common data types and macro
  184. // 等待线程状态切换完成的延时时间, 单位: 毫秒
  185. #define THRDWAIT_DELAY 10
  186. // 等待线程安全退出的超时, 单位: 毫秒
  187. #define WAITTHRD_SAFEEXIT_TIMEOUT 3000
  188. // IPv4字符串最大长度(包含'\0'结尾符, 例如: xxx.xxx.xxx.xxx)
  189. #define MAX_IPV4STR_CHARS (15+1)
  190. // 日期时间字符串长度, 包含'\0'结尾符
  191. #define MAX_TIMESTR_CHARS (19+1)
  192. #endif /* __SWAPI_H__ */