hbase.h 1.9 KB

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