hdef.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #ifndef HV_DEF_H_
  2. #define HV_DEF_H_
  3. #include "hplatform.h"
  4. #ifndef ABS
  5. #define ABS(n) ((n) > 0 ? (n) : -(n))
  6. #endif
  7. #ifndef NABS
  8. #define NABS(n) ((n) < 0 ? (n) : -(n))
  9. #endif
  10. #ifndef ARRAY_SIZE
  11. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
  12. #endif
  13. #ifndef BITSET
  14. #define BITSET(p, n) (*(p) |= (1u << (n)))
  15. #endif
  16. #ifndef BITCLR
  17. #define BITCLR(p, n) (*(p) &= ~(1u << (n)))
  18. #endif
  19. #ifndef BITGET
  20. #define BITGET(i, n) ((i) & (1u << (n)))
  21. #endif
  22. /*
  23. #ifndef CR
  24. #define CR '\r'
  25. #endif
  26. #ifndef LF
  27. #define LF '\n'
  28. #endif
  29. #ifndef CRLF
  30. #define CRLF "\r\n"
  31. #endif
  32. */
  33. #define FLOAT_PRECISION 1e-6
  34. #define FLOAT_EQUAL_ZERO(f) (ABS(f) < FLOAT_PRECISION)
  35. #ifndef INFINITE
  36. #define INFINITE (uint32_t)-1
  37. #endif
  38. /*
  39. ASCII:
  40. [0, 0x20) control-charaters
  41. [0x20, 0x7F) printable-charaters
  42. 0x0A => LF
  43. 0x0D => CR
  44. 0x20 => SPACE
  45. 0x7F => DEL
  46. [0x09, 0x0D] => \t\n\v\f\r
  47. [0x30, 0x39] => 0~9
  48. [0x41, 0x5A] => A~Z
  49. [0x61, 0x7A] => a~z
  50. */
  51. #ifndef IS_ALPHA
  52. #define IS_ALPHA(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
  53. #endif
  54. // NOTE: IS_NUM conflicts with mysql.h
  55. #ifndef IS_DIGIT
  56. #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
  57. #endif
  58. #ifndef IS_ALPHANUM
  59. #define IS_ALPHANUM(c) (IS_ALPHA(c) || IS_DIGIT(c))
  60. #endif
  61. #ifndef IS_CNTRL
  62. #define IS_CNTRL(c) ((c) >= 0 && (c) < 0x20)
  63. #endif
  64. #ifndef IS_GRAPH
  65. #define IS_GRAPH(c) ((c) >= 0x20 && (c) < 0x7F)
  66. #endif
  67. #ifndef IS_HEX
  68. #define IS_HEX(c) (IS_DIGIT(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
  69. #endif
  70. #ifndef IS_LOWER
  71. #define IS_LOWER(c) (((c) >= 'a' && (c) <= 'z'))
  72. #endif
  73. #ifndef IS_UPPER
  74. #define IS_UPPER(c) (((c) >= 'A' && (c) <= 'Z'))
  75. #endif
  76. #ifndef LOWER
  77. #define LOWER(c) ((c) | 0x20)
  78. #endif
  79. #ifndef UPPER
  80. #define UPPER(c) ((c) & ~0x20)
  81. #endif
  82. // LD, LU, LLD, LLU for explicit conversion of integer
  83. // #ifndef LD
  84. // #define LD(v) ((long)(v))
  85. // #endif
  86. // #ifndef LU
  87. // #define LU(v) ((unsigned long)(v))
  88. // #endif
  89. #ifndef LLD
  90. #define LLD(v) ((long long)(v))
  91. #endif
  92. #ifndef LLU
  93. #define LLU(v) ((unsigned long long)(v))
  94. #endif
  95. #ifndef _WIN32
  96. // MAKEWORD, HIBYTE, LOBYTE
  97. #ifndef MAKEWORD
  98. #define MAKEWORD(h, l) ( (((WORD)h) << 8) | (l & 0xff) )
  99. #endif
  100. #ifndef HIBYTE
  101. #define HIBYTE(w) ( (BYTE)(((WORD)w) >> 8) )
  102. #endif
  103. #ifndef LOBYTE
  104. #define LOBYTE(w) ( (BYTE)(w & 0xff) )
  105. #endif
  106. // MAKELONG, HIWORD, LOWORD
  107. #ifndef MAKELONG
  108. #define MAKELONG(h, l) ( ((int32_t)h) << 16 | (l & 0xffff) )
  109. #endif
  110. #ifndef HIWORD
  111. #define HIWORD(n) ( (WORD)(((int32_t)n) >> 16) )
  112. #endif
  113. #ifndef LOWORD
  114. #define LOWORD(n) ( (WORD)(n & 0xffff) )
  115. #endif
  116. #endif // _WIN32
  117. // MAKEINT64, HIINT, LOINT
  118. #ifndef MAKEINT64
  119. #define MAKEINT64(h, l) ( ((int64_t)h) << 32 | (l & 0xffffffff) )
  120. #endif
  121. #ifndef HIINT
  122. #define HIINT(n) ( (int32_t)(((int64_t)n) >> 32) )
  123. #endif
  124. #ifndef LOINT
  125. #define LOINT(n) ( (int32_t)(n & 0xffffffff) )
  126. #endif
  127. #ifndef MAKE_FOURCC
  128. #define MAKE_FOURCC(a, b, c, d) \
  129. ( ((uint32)d) | ( ((uint32)c) << 8 ) | ( ((uint32)b) << 16 ) | ( ((uint32)a) << 24 ) )
  130. #endif
  131. #ifndef MAX
  132. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  133. #endif
  134. #ifndef MIN
  135. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  136. #endif
  137. #ifndef LIMIT
  138. #define LIMIT(lower, v, upper) ((v) < (lower) ? (lower) : (v) > (upper) ? (upper) : (v))
  139. #endif
  140. #ifndef MAX_PATH
  141. #define MAX_PATH 260
  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 TRUE
  151. #define TRUE 1
  152. #endif
  153. #ifndef FALSE
  154. #define FALSE 0
  155. #endif
  156. #ifndef SAFE_ALLOC
  157. #define SAFE_ALLOC(p, size)\
  158. do {\
  159. void* ptr = malloc(size);\
  160. if (!ptr) {\
  161. fprintf(stderr, "malloc failed!\n");\
  162. exit(-1);\
  163. }\
  164. memset(ptr, 0, size);\
  165. *(void**)&(p) = ptr;\
  166. } while(0)
  167. #endif
  168. #ifndef SAFE_FREE
  169. #define SAFE_FREE(p) do {if (p) {free(p); (p) = NULL;}} while(0)
  170. #endif
  171. #ifndef SAFE_DELETE
  172. #define SAFE_DELETE(p) do {if (p) {delete (p); (p) = NULL;}} while(0)
  173. #endif
  174. #ifndef SAFE_DELETE_ARRAY
  175. #define SAFE_DELETE_ARRAY(p) do {if (p) {delete[] (p); (p) = NULL;}} while(0)
  176. #endif
  177. #ifndef SAFE_RELEASE
  178. #define SAFE_RELEASE(p) do {if (p) {(p)->release(); (p) = NULL;}} while(0)
  179. #endif
  180. #ifndef SAFE_CLOSE
  181. #define SAFE_CLOSE(fd) do {if ((fd) >= 0) {close(fd); (fd) = -1;}} while(0)
  182. #endif
  183. #define STRINGIFY(x) STRINGIFY_HELPER(x)
  184. #define STRINGIFY_HELPER(x) #x
  185. #define STRINGCAT(x, y) STRINGCAT_HELPER(x, y)
  186. #define STRINGCAT_HELPER(x, y) x##y
  187. #ifndef offsetof
  188. #define offsetof(type, member) \
  189. ((size_t)(&((type*)0)->member))
  190. #endif
  191. #ifndef offsetofend
  192. #define offsetofend(type, member) \
  193. (offsetof(type, member) + sizeof(((type*)0)->member))
  194. #endif
  195. #ifndef container_of
  196. #define container_of(ptr, type, member) \
  197. ((type*)((char*)(ptr) - offsetof(type, member)))
  198. #endif
  199. #ifdef PRINT_DEBUG
  200. #define printd(...) printf(__VA_ARGS__)
  201. #else
  202. #define printd(...)
  203. #endif
  204. #ifdef PRINT_ERROR
  205. #define printe(...) fprintf(stderr, __VA_ARGS__)
  206. #else
  207. #define printe(...)
  208. #endif
  209. #endif // HV_DEF_H_