hbase.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef HV_BASE_H_
  2. #define HV_BASE_H_
  3. #include "hdef.h"
  4. BEGIN_EXTERN_C
  5. //---------------------safe alloc/free---------------------------
  6. extern unsigned int g_alloc_cnt;
  7. extern unsigned int g_free_cnt;
  8. void* safe_malloc(size_t size);
  9. void* safe_realloc(void* oldptr, size_t newsize, size_t oldsize);
  10. void* safe_calloc(size_t nmemb, size_t size);
  11. void* safe_zalloc(size_t size);
  12. #define HV_ALLOC(ptr, size)\
  13. do {\
  14. void** pptr = (void**)&(ptr);\
  15. *pptr = safe_zalloc(size);\
  16. printd("alloc(%p, size=%llu)\tat [%s:%d:%s]\n", ptr, (unsigned long long)size, __FILE__, __LINE__, __FUNCTION__);\
  17. } while(0)
  18. #define HV_ALLOC_SIZEOF(ptr) HV_ALLOC(ptr, sizeof(*(ptr)))
  19. #define HV_FREE(ptr)\
  20. do {\
  21. if (ptr) {\
  22. printd("free( %p )\tat [%s:%d:%s]\n", ptr, __FILE__, __LINE__, __FUNCTION__);\
  23. free(ptr);\
  24. ptr = NULL;\
  25. ++g_free_cnt;\
  26. }\
  27. } while(0)
  28. static inline void hv_memcheck() {
  29. printf("Memcheck => alloc:%u free:%u\n", g_alloc_cnt, g_free_cnt);
  30. }
  31. #define HV_MEMCHECK atexit(hv_memcheck);
  32. //-----------------------------safe string-----------------------
  33. char* strupper(char* str);
  34. char* strlower(char* str);
  35. char* strreverse(char* str);
  36. bool strstartswith(const char* str, const char* start);
  37. bool strendswith(const char* str, const char* end);
  38. bool strcontains(const char* str, const char* sub);
  39. // strncpy n = sizeof(dest_buf)-1
  40. // safe_strncpy n = sizeof(dest_buf)
  41. char* safe_strncpy(char* dest, const char* src, size_t n);
  42. // strncat n = sizeof(dest_buf)-1-strlen(dest)
  43. // safe_strncpy n = sizeof(dest_buf)
  44. char* safe_strncat(char* dest, const char* src, size_t n);
  45. #if !HAVE_STRLCPY
  46. #define strlcpy safe_strncpy
  47. #endif
  48. #if !HAVE_STRLCAT
  49. #define strlcat safe_strncat
  50. #endif
  51. // 1 y on yes true enable
  52. bool getboolean(const char* str);
  53. END_EXTERN_C
  54. #endif // HV_BASE_H_