memutil.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #ifndef __MEMUTIL_H__
  2. #define __MEMUTIL_H__
  3. #pragma once
  4. #include "config.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. #include <stdlib.h>
  9. #include <string.h>
  10. static __inline void prefetch(void *p)
  11. {
  12. _asm
  13. {
  14. prefetchnta p
  15. }
  16. }
  17. #define INT64_GET_HIGH_PART(x) (*((int*)(&(x)) + 1))
  18. #define INT64_GET_LOW_PART(x) (*(int*)&(x))
  19. static __inline int int64_get_high_part(__int64 x)
  20. {
  21. return INT64_GET_HIGH_PART(x);
  22. }
  23. static __inline int int64_get_low_part(__int64 x)
  24. {
  25. return INT64_GET_LOW_PART(x);
  26. }
  27. struct __i8_helper {
  28. union {
  29. __int64 i;
  30. struct {
  31. int l;
  32. int h;
  33. };
  34. };
  35. };
  36. static __inline __int64 int64_make(int low_part, int high_part)
  37. {
  38. struct __i8_helper t;
  39. t.l = low_part;
  40. t.h = high_part;
  41. return t.i;
  42. }
  43. #define offset_of(type, member) \
  44. (((char*)(&((type*)0)->member)) - (char*)0)
  45. #define container_of(ptr, type, member) \
  46. ((type*)((char*)(ptr) - offset_of(type, member)))
  47. #define field_size(type, member) \
  48. sizeof(((type*)0)->member)
  49. #define array_size(x) \
  50. (sizeof(x)/sizeof((x)[0]))
  51. #define FLAG_BIT(f) f##_BIT
  52. #define SAFE_INVOKE_0(lpfn) if(lpfn) lpfn()
  53. #define SAFE_INVOKE_1(lpfn, param1) if(lpfn) lpfn(param1)
  54. #define SAFE_INVOKE_2(lpfn, param1, param2) if(lpfn) lpfn(param1, param2)
  55. #define SAFE_INVOKE_3(lpfn, param1, param2, param3) if(lpfn) lpfn(param1, param2, param3)
  56. #define SAFE_INVOKE_4(lpfn, param1, param2, param3, param4) if(lpfn) lpfn(param1, param2, param3, param4)
  57. #define SAFE_INVOKE_5(lpfn, param1, param2, param3, param4, param5) if(lpfn) lpfn(param1, param2, param3, param4, param5)
  58. #define SAFE_INVOKE_6(lpfn, param1, param2, param3, param4, param5, param6) if(lpfn) lpfn(param1, param2, param3, param4, param5, param6)
  59. #define SAFE_INVOKE_7(lpfn, param1, param2, param3, param4, param5, param6, param7) if(lpfn) lpfn(param1, param2, param3, param4, param5, param6, param7)
  60. #define SAFE_INVOKE_8(lpfn, param1, param2, param3, param4, param5, param6, param7, param8) if(lpfn) lpfn(param1, param2, param3, param4, param5, param6, param7, param8)
  61. #define SAFE_INVOKE_9(lpfn, param1, param2, param3, param4, param5, param6, param7, param8, param9) if(lpfn) lpfn(param1, param2, param3, param4, param5, param6, param7, param8, param9)
  62. #define INIT_COUNTER(name, x) enum { name = __LINE__ + x }
  63. #define DEF_COUNTER(name) (__LINE__ - name)
  64. /**
  65. * resize buffer with data preserved
  66. * @param old pointer to old buffer
  67. * @param elemsize element size
  68. * @param oldcnt the old element count
  69. * @param newcnt the new element count
  70. * @return return new buffer, if failed, original buffer is returned, if success old buffer will be freed
  71. */
  72. TOOLKIT_API void *buffer_resize(void* old, int elemsize, int oldcnt, int newcnt);
  73. TOOLKIT_API void *shm_buffer_resize(void* old, int elemsize, int oldcnt, int newcnt);
  74. /**
  75. * check expand
  76. */
  77. TOOLKIT_API int array_check_expand(void **old, int elemsize, int cnt, int *capacity);
  78. TOOLKIT_API int shm_array_check_expand(void **old, int elemsize, int cnt, int *capacity);
  79. static __inline void* zalloc(size_t size)
  80. {
  81. void *buf = malloc(size);
  82. if (buf) {
  83. memset(buf, 0, size);
  84. }
  85. return buf;
  86. }
  87. static __inline void* zcalloc(size_t num, size_t size)
  88. {
  89. void *buf = calloc(num, size);
  90. if (buf) {
  91. memset(buf, 0, num * size);
  92. }
  93. return buf;
  94. }
  95. TOOLKIT_API void toolkit_free(void *p);
  96. TOOLKIT_API void *toolkit_malloc(size_t size);
  97. TOOLKIT_API void *toolkit_calloc(size_t num, size_t size);
  98. TOOLKIT_API void *toolkit_zalloc(size_t size);
  99. TOOLKIT_API void *toolkit_zcalloc(size_t num, size_t size);
  100. TOOLKIT_API char *toolkit_strdup(const char *s);
  101. TOOLKIT_API wchar_t *toolkit_wcsdup(const wchar_t *s);
  102. #define ZALLOC(size) zalloc(size)
  103. #define ZCALLOC(num, size) czalloc(num, size)
  104. #define MALLOC_T(type) (type*)malloc(sizeof(type))
  105. #define ZALLOC_T(type) (type*)zalloc(sizeof(type))
  106. #define CALLOC_T(num, type) (type*)calloc(num, sizeof(type))
  107. #if !defined(_DEBUG) || !defined(DEBUG)
  108. #define ASSERT_RETURN(cond, ret) \
  109. if (!(cond)) \
  110. return ret;
  111. #else
  112. #define ASSERT_RETURN(cond, ret) assert(cond);
  113. #endif
  114. TOOLKIT_API void debug_trace(const char *fmt, ...);
  115. #if defined(_DEBUG) || defined(DEBUG)
  116. #define DEBUG_TRACE(fmt, ...) debug_trace(fmt, __VA_ARGS__);
  117. #else
  118. #define DEBUG_TRACE(fmt, ...)
  119. #endif
  120. static __inline int timeval_cmp(struct timeval *x, struct timeval *y)
  121. {
  122. if (x->tv_sec < y->tv_sec) {
  123. return -1;
  124. } else if (x->tv_sec > y->tv_sec) {
  125. return 1;
  126. } else {
  127. if (x->tv_usec < y->tv_usec) {
  128. return -1;
  129. } else if (x->tv_usec > y->tv_usec) {
  130. return 1;
  131. } else {
  132. return 0;
  133. }
  134. }
  135. }
  136. static __inline void timeval_diff(struct timeval *x, struct timeval *y, struct timeval *out)
  137. {
  138. out->tv_sec = x->tv_sec - y->tv_sec;
  139. out->tv_usec = x->tv_usec - y->tv_usec;
  140. if (out->tv_usec < 0) {
  141. out->tv_sec --;
  142. out->tv_usec = 1000000 + out->tv_usec;
  143. }
  144. }
  145. static __inline void timeval_add_sec(struct timeval *tm, unsigned int sec)
  146. {
  147. tm->tv_sec += sec;
  148. }
  149. static __inline void timeval_add_msec(struct timeval *tm, unsigned int msec)
  150. {
  151. tm->tv_sec += msec / 1000;
  152. msec = msec % 1000;
  153. tm->tv_usec += msec*1000;
  154. if (tm->tv_usec > 1000000) {
  155. tm->tv_usec -= 1000000;
  156. tm->tv_sec++;
  157. }
  158. }
  159. // return msec of x - y
  160. static __inline int timeval_sub(struct timeval *x, struct timeval *y)
  161. {
  162. return (x->tv_sec - y->tv_sec) * 1000 + (1000000 + x->tv_usec - y->tv_usec)/1000 - 1000;
  163. }
  164. TOOLKIT_API int GetSystemErrorDesc(int error_code, char *buf, int n);
  165. #ifdef __cplusplus
  166. } // extern "C" {
  167. #endif
  168. #endif //__MEMUTIL_H__