hdef.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #ifndef IS_NUM
  55. #define IS_NUM(c) ((c) >= '0' && (c) <= '9')
  56. #endif
  57. #ifndef IS_ALPHANUM
  58. #define IS_ALPHANUM(c) (IS_ALPHA(c) || IS_NUM(c))
  59. #endif
  60. #ifndef IS_CNTRL
  61. #define IS_CNTRL(c) ((c) >= 0 && (c) < 0x20)
  62. #endif
  63. #ifndef IS_GRAPH
  64. #define IS_GRAPH(c) ((c) >= 0x20 && (c) < 0x7F)
  65. #endif
  66. #ifndef IS_HEX
  67. #define IS_HEX(c) (IS_NUM(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
  68. #endif
  69. #ifndef IS_LOWER
  70. #define IS_LOWER(c) (((c) >= 'a' && (c) <= 'z'))
  71. #endif
  72. #ifndef IS_UPPER
  73. #define IS_UPPER(c) (((c) >= 'A' && (c) <= 'Z'))
  74. #endif
  75. #ifndef LOWER
  76. #define LOWER(c) ((c) | 0x20)
  77. #endif
  78. #ifndef UPPER
  79. #define UPPER(c) ((c) & ~0x20)
  80. #endif
  81. // LD, LU, LLD, LLU for explicit conversion of integer
  82. // #ifndef LD
  83. // #define LD(v) ((long)(v))
  84. // #endif
  85. // #ifndef LU
  86. // #define LU(v) ((unsigned long)(v))
  87. // #endif
  88. #ifndef LLD
  89. #define LLD(v) ((long long)(v))
  90. #endif
  91. #ifndef LLU
  92. #define LLU(v) ((unsigned long long)(v))
  93. #endif
  94. #ifndef _WIN32
  95. // MAKEWORD, HIBYTE, LOBYTE
  96. #ifndef MAKEWORD
  97. #define MAKEWORD(h, l) ( (((WORD)h) << 8) | (l & 0xff) )
  98. #endif
  99. #ifndef HIBYTE
  100. #define HIBYTE(w) ( (BYTE)(((WORD)w) >> 8) )
  101. #endif
  102. #ifndef LOBYTE
  103. #define LOBYTE(w) ( (BYTE)(w & 0xff) )
  104. #endif
  105. // MAKELONG, HIWORD, LOWORD
  106. #ifndef MAKELONG
  107. #define MAKELONG(h, l) ( ((int32_t)h) << 16 | (l & 0xffff) )
  108. #endif
  109. #ifndef HIWORD
  110. #define HIWORD(n) ( (WORD)(((int32_t)n) >> 16) )
  111. #endif
  112. #ifndef LOWORD
  113. #define LOWORD(n) ( (WORD)(n & 0xffff) )
  114. #endif
  115. #endif // _WIN32
  116. // MAKEINT64, HIINT, LOINT
  117. #ifndef MAKEINT64
  118. #define MAKEINT64(h, l) ( ((int64_t)h) << 32 | (l & 0xffffffff) )
  119. #endif
  120. #ifndef HIINT
  121. #define HIINT(n) ( (int32_t)(((int64_t)n) >> 32) )
  122. #endif
  123. #ifndef LOINT
  124. #define LOINT(n) ( (int32_t)(n & 0xffffffff) )
  125. #endif
  126. #ifndef MAKE_FOURCC
  127. #define MAKE_FOURCC(a, b, c, d) \
  128. ( ((uint32)d) | ( ((uint32)c) << 8 ) | ( ((uint32)b) << 16 ) | ( ((uint32)a) << 24 ) )
  129. #endif
  130. #ifndef MAX
  131. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  132. #endif
  133. #ifndef MIN
  134. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  135. #endif
  136. #ifndef LIMIT
  137. #define LIMIT(lower, v, upper) ((v) < (lower) ? (lower) : (v) > (upper) ? (upper) : (v))
  138. #endif
  139. #ifndef MAX_PATH
  140. #define MAX_PATH 260
  141. #endif
  142. #ifndef NULL
  143. #ifdef __cplusplus
  144. #define NULL 0
  145. #else
  146. #define NULL ((void*)0)
  147. #endif
  148. #endif
  149. #ifndef TRUE
  150. #define TRUE 1
  151. #endif
  152. #ifndef FALSE
  153. #define FALSE 0
  154. #endif
  155. #ifndef SAFE_ALLOC
  156. #define SAFE_ALLOC(p, size)\
  157. do {\
  158. void* ptr = malloc(size);\
  159. if (!ptr) {\
  160. fprintf(stderr, "malloc failed!\n");\
  161. exit(-1);\
  162. }\
  163. memset(ptr, 0, size);\
  164. *(void**)&(p) = ptr;\
  165. } while(0)
  166. #endif
  167. #ifndef SAFE_FREE
  168. #define SAFE_FREE(p) do {if (p) {free(p); (p) = NULL;}} while(0)
  169. #endif
  170. #ifndef SAFE_DELETE
  171. #define SAFE_DELETE(p) do {if (p) {delete (p); (p) = NULL;}} while(0)
  172. #endif
  173. #ifndef SAFE_DELETE_ARRAY
  174. #define SAFE_DELETE_ARRAY(p) do {if (p) {delete[] (p); (p) = NULL;}} while(0)
  175. #endif
  176. #ifndef SAFE_RELEASE
  177. #define SAFE_RELEASE(p) do {if (p) {(p)->release(); (p) = NULL;}} while(0)
  178. #endif
  179. #ifndef SAFE_CLOSE
  180. #define SAFE_CLOSE(fd) do {if ((fd) >= 0) {close(fd); (fd) = -1;}} while(0)
  181. #endif
  182. #define STRINGIFY(x) STRINGIFY_HELPER(x)
  183. #define STRINGIFY_HELPER(x) #x
  184. #define STRINGCAT(x, y) STRINGCAT_HELPER(x, y)
  185. #define STRINGCAT_HELPER(x, y) x##y
  186. #ifndef offsetof
  187. #define offsetof(type, member) \
  188. ((size_t)(&((type*)0)->member))
  189. #endif
  190. #ifndef offsetofend
  191. #define offsetofend(type, member) \
  192. (offsetof(type, member) + sizeof(((type*)0)->member))
  193. #endif
  194. #ifndef container_of
  195. #define container_of(ptr, type, member) \
  196. ((type*)((char*)(ptr) - offsetof(type, member)))
  197. #endif
  198. #ifdef PRINT_DEBUG
  199. #define printd(...) printf(__VA_ARGS__)
  200. #else
  201. #define printd(...)
  202. #endif
  203. #ifdef PRINT_ERROR
  204. #define printe(...) fprintf(stderr, __VA_ARGS__)
  205. #else
  206. #define printe(...)
  207. #endif
  208. #endif // HV_DEF_H_