| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #ifndef __MEMUTIL_H__
- #define __MEMUTIL_H__
- #pragma once
- #include "config.h"
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include <stdlib.h>
- #include <string.h>
- static __inline void prefetch(void *p)
- {
- _asm
- {
- prefetchnta p
- }
- }
- #define INT64_GET_HIGH_PART(x) (*((int*)(&(x)) + 1))
- #define INT64_GET_LOW_PART(x) (*(int*)&(x))
- static __inline int int64_get_high_part(__int64 x)
- {
- return INT64_GET_HIGH_PART(x);
- }
- static __inline int int64_get_low_part(__int64 x)
- {
- return INT64_GET_LOW_PART(x);
- }
- struct __i8_helper {
- union {
- __int64 i;
- struct {
- int l;
- int h;
- };
- };
- };
- static __inline __int64 int64_make(int low_part, int high_part)
- {
- struct __i8_helper t;
- t.l = low_part;
- t.h = high_part;
- return t.i;
- }
- #define offset_of(type, member) \
- (((char*)(&((type*)0)->member)) - (char*)0)
- #define container_of(ptr, type, member) \
- ((type*)((char*)(ptr) - offset_of(type, member)))
- #define field_size(type, member) \
- sizeof(((type*)0)->member)
- #define array_size(x) \
- (sizeof(x)/sizeof((x)[0]))
- #define FLAG_BIT(f) f##_BIT
- #define SAFE_INVOKE_0(lpfn) if(lpfn) lpfn()
- #define SAFE_INVOKE_1(lpfn, param1) if(lpfn) lpfn(param1)
- #define SAFE_INVOKE_2(lpfn, param1, param2) if(lpfn) lpfn(param1, param2)
- #define SAFE_INVOKE_3(lpfn, param1, param2, param3) if(lpfn) lpfn(param1, param2, param3)
- #define SAFE_INVOKE_4(lpfn, param1, param2, param3, param4) if(lpfn) lpfn(param1, param2, param3, param4)
- #define SAFE_INVOKE_5(lpfn, param1, param2, param3, param4, param5) if(lpfn) lpfn(param1, param2, param3, param4, param5)
- #define SAFE_INVOKE_6(lpfn, param1, param2, param3, param4, param5, param6) if(lpfn) lpfn(param1, param2, param3, param4, param5, param6)
- #define SAFE_INVOKE_7(lpfn, param1, param2, param3, param4, param5, param6, param7) if(lpfn) lpfn(param1, param2, param3, param4, param5, param6, param7)
- #define SAFE_INVOKE_8(lpfn, param1, param2, param3, param4, param5, param6, param7, param8) if(lpfn) lpfn(param1, param2, param3, param4, param5, param6, param7, param8)
- #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)
- #define INIT_COUNTER(name, x) enum { name = __LINE__ + x }
- #define DEF_COUNTER(name) (__LINE__ - name)
- /**
- * resize buffer with data preserved
- * @param old pointer to old buffer
- * @param elemsize element size
- * @param oldcnt the old element count
- * @param newcnt the new element count
- * @return return new buffer, if failed, original buffer is returned, if success old buffer will be freed
- */
- TOOLKIT_API void *buffer_resize(void* old, int elemsize, int oldcnt, int newcnt);
- TOOLKIT_API void *shm_buffer_resize(void* old, int elemsize, int oldcnt, int newcnt);
- /**
- * check expand
- */
- TOOLKIT_API int array_check_expand(void **old, int elemsize, int cnt, int *capacity);
- TOOLKIT_API int shm_array_check_expand(void **old, int elemsize, int cnt, int *capacity);
- static __inline void* zalloc(size_t size)
- {
- void *buf = malloc(size);
- if (buf) {
- memset(buf, 0, size);
- }
- return buf;
- }
- static __inline void* zcalloc(size_t num, size_t size)
- {
- void *buf = calloc(num, size);
- if (buf) {
- memset(buf, 0, num * size);
- }
- return buf;
- }
- TOOLKIT_API void toolkit_free(void *p);
- TOOLKIT_API void *toolkit_malloc(size_t size);
- TOOLKIT_API void *toolkit_calloc(size_t num, size_t size);
- TOOLKIT_API void *toolkit_zalloc(size_t size);
- TOOLKIT_API void *toolkit_zcalloc(size_t num, size_t size);
- TOOLKIT_API char *toolkit_strdup(const char *s);
- TOOLKIT_API wchar_t *toolkit_wcsdup(const wchar_t *s);
- #define ZALLOC(size) zalloc(size)
- #define ZCALLOC(num, size) czalloc(num, size)
- #define MALLOC_T(type) (type*)malloc(sizeof(type))
- #define ZALLOC_T(type) (type*)zalloc(sizeof(type))
- #define CALLOC_T(num, type) (type*)calloc(num, sizeof(type))
- #if !defined(_DEBUG) || !defined(DEBUG)
- #define ASSERT_RETURN(cond, ret) \
- if (!(cond)) \
- return ret;
- #else
- #define ASSERT_RETURN(cond, ret) assert(cond);
- #endif
- TOOLKIT_API void debug_trace(const char *fmt, ...);
- #if defined(_DEBUG) || defined(DEBUG)
- #define DEBUG_TRACE(fmt, ...) debug_trace(fmt, __VA_ARGS__);
- #else
- #define DEBUG_TRACE(fmt, ...)
- #endif
- static __inline int timeval_cmp(struct timeval *x, struct timeval *y)
- {
- if (x->tv_sec < y->tv_sec) {
- return -1;
- } else if (x->tv_sec > y->tv_sec) {
- return 1;
- } else {
- if (x->tv_usec < y->tv_usec) {
- return -1;
- } else if (x->tv_usec > y->tv_usec) {
- return 1;
- } else {
- return 0;
- }
- }
- }
- static __inline void timeval_diff(struct timeval *x, struct timeval *y, struct timeval *out)
- {
- out->tv_sec = x->tv_sec - y->tv_sec;
- out->tv_usec = x->tv_usec - y->tv_usec;
- if (out->tv_usec < 0) {
- out->tv_sec --;
- out->tv_usec = 1000000 + out->tv_usec;
- }
- }
- static __inline void timeval_add_sec(struct timeval *tm, unsigned int sec)
- {
- tm->tv_sec += sec;
- }
- static __inline void timeval_add_msec(struct timeval *tm, unsigned int msec)
- {
- tm->tv_sec += msec / 1000;
- msec = msec % 1000;
- tm->tv_usec += msec*1000;
- if (tm->tv_usec > 1000000) {
- tm->tv_usec -= 1000000;
- tm->tv_sec++;
- }
- }
- // return msec of x - y
- static __inline int timeval_sub(struct timeval *x, struct timeval *y)
- {
- return (x->tv_sec - y->tv_sec) * 1000 + (1000000 + x->tv_usec - y->tv_usec)/1000 - 1000;
- }
- TOOLKIT_API int GetSystemErrorDesc(int error_code, char *buf, int n);
- #ifdef __cplusplus
- } // extern "C" {
- #endif
- #endif //__MEMUTIL_H__
|