swapi.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 BOOL
  124. #define BOOL int
  125. #endif
  126. #ifndef TRUE
  127. #define TRUE 1
  128. #endif
  129. #ifndef FALSE
  130. #define FALSE 0
  131. #endif
  132. #ifndef NULL
  133. #ifdef __cplusplus
  134. #define NULL 0
  135. #else
  136. #define NULL ((void *)0)
  137. #endif
  138. #endif
  139. #ifndef null
  140. #define null NULL
  141. #endif
  142. #ifndef HANDLE
  143. #define HANDLE void *
  144. #endif
  145. #ifndef SOCKET
  146. #define SOCKET int
  147. #endif
  148. #ifndef INVALID_SOCKET
  149. #define INVALID_SOCKET (SOCKET)(~0)
  150. #endif
  151. #ifndef SOCKET_ERROR
  152. #define SOCKET_ERROR -1
  153. #endif
  154. #ifndef ERROR
  155. #define ERROR -1
  156. #endif
  157. #ifndef INFINITE
  158. #define INFINITE 0xFFFFFFFF
  159. #endif
  160. #ifndef WAIT_FOREVER
  161. #define WAIT_FOREVER INFINITE
  162. #endif
  163. //////////////////////////////////////////////////////////////////////////
  164. // 5, Show memory allocation situation detailed when you need
  165. #ifndef _MEM_DEBUG_DETAIL
  166. //#define _MEM_DEBUG_DETAIL
  167. #endif
  168. //////////////////////////////////////////////////////////////////////////
  169. // 6, Program logs
  170. #include "swlog.h"
  171. //////////////////////////////////////////////////////////////////////////
  172. // 7, Define common data types and macro
  173. #include "swthrd.h"
  174. #define delay_ms(ms) sw_thrd_delay(ms)
  175. // 等待线程状态切换完成的延时时间, 单位: ms
  176. #define THRDWAIT_DELAY 10
  177. // 用户等待线程安全退出的超时时间, 单位: ms
  178. #define WAITTHRD_SAFEEXIT_TIMEOUT 3000
  179. // IPv4字符串最大长度(包含'\0'结尾符, 例如: xxx.xxx.xxx.xxx)
  180. #define MAX_IPV4STR_CHARS (15+1)
  181. // 日期时间字符串长度, 包含'\0'结尾符
  182. #define MAX_TIMESTR_CHARS (19+1)
  183. #endif /* __SWAPI_H__ */