1
0

hbase.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #define strrchr_dot(str) strrchr(str, '.')
  52. char* strrchr_dir(const char* filepath);
  53. // basename
  54. const char* hv_basename(const char* filepath);
  55. const char* hv_suffixname(const char* filename);
  56. // mkdir -p
  57. int hv_mkdir_p(const char* dir);
  58. // rmdir -p
  59. int hv_rmdir_p(const char* dir);
  60. // 1 y on yes true enable
  61. bool getboolean(const char* str);
  62. char* get_executable_path(char* buf, int size);
  63. char* get_executable_dir(char* buf, int size);
  64. char* get_executable_file(char* buf, int size);
  65. char* get_run_dir(char* buf, int size);
  66. END_EXTERN_C
  67. #endif // HV_BASE_H_