hbase.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef HV_BASE_H_
  2. #define HV_BASE_H_
  3. /*
  4. * @功能:此头文件实现了一些常用的函数
  5. *
  6. */
  7. #include "hexport.h"
  8. #include "hplatform.h" // for bool
  9. #include "hdef.h" // for printd
  10. BEGIN_EXTERN_C
  11. // 安全的内存分配与释放函数
  12. //--------------------safe alloc/free---------------------------
  13. HV_EXPORT void* safe_malloc(size_t size);
  14. HV_EXPORT void* safe_realloc(void* oldptr, size_t newsize, size_t oldsize);
  15. HV_EXPORT void* safe_calloc(size_t nmemb, size_t size);
  16. HV_EXPORT void* safe_zalloc(size_t size);
  17. HV_EXPORT void safe_free(void* ptr);
  18. // 分配内存宏
  19. #define HV_ALLOC(ptr, size)\
  20. do {\
  21. *(void**)&(ptr) = safe_zalloc(size);\
  22. printd("alloc(%p, size=%llu)\tat [%s:%d:%s]\n", ptr, (unsigned long long)size, __FILE__, __LINE__, __FUNCTION__);\
  23. } while(0)
  24. // 通过sizeof计算内存占用大小,并分配内存,通常用于给结构体分配内存
  25. #define HV_ALLOC_SIZEOF(ptr) HV_ALLOC(ptr, sizeof(*(ptr)))
  26. // 释放内存宏
  27. #define HV_FREE(ptr)\
  28. do {\
  29. if (ptr) {\
  30. safe_free(ptr);\
  31. printd("free( %p )\tat [%s:%d:%s]\n", ptr, __FILE__, __LINE__, __FUNCTION__);\
  32. ptr = NULL;\
  33. }\
  34. } while(0)
  35. // 统计内存分配/释放的次数,以此检查是否有内存未释放
  36. HV_EXPORT long hv_alloc_cnt();
  37. HV_EXPORT long hv_free_cnt();
  38. HV_INLINE void hv_memcheck() {
  39. printf("Memcheck => alloc:%ld free:%ld\n", hv_alloc_cnt(), hv_free_cnt());
  40. }
  41. #define HV_MEMCHECK atexit(hv_memcheck);
  42. // 一些字符串操作函数
  43. //--------------------safe string-------------------------------
  44. // 字符串转大写
  45. HV_EXPORT char* strupper(char* str);
  46. // 字符串转小写
  47. HV_EXPORT char* strlower(char* str);
  48. // 字符串翻转
  49. HV_EXPORT char* strreverse(char* str);
  50. // 判断字符串是否以某个字符串开头
  51. HV_EXPORT bool strstartswith(const char* str, const char* start);
  52. // 判断字符串是否以某个字符串结尾
  53. HV_EXPORT bool strendswith(const char* str, const char* end);
  54. // 判断字符串中是否包含某个子串
  55. HV_EXPORT bool strcontains(const char* str, const char* sub);
  56. // 标准库里的strncpy、strncat通常需要传入sizeof(dest_buf)-1,
  57. // 如果不小心传成sizeof(dest_buf),可能造成内存越界访问以及不是以'\0'结尾,
  58. // 所以这里重新实现了下,传入sizeof(dest_buf)也能安全工作
  59. // strncpy n = sizeof(dest_buf)-1
  60. // safe_strncpy n = sizeof(dest_buf)
  61. HV_EXPORT char* safe_strncpy(char* dest, const char* src, size_t n);
  62. // strncat n = sizeof(dest_buf)-1-strlen(dest)
  63. // safe_strncpy n = sizeof(dest_buf)
  64. HV_EXPORT char* safe_strncat(char* dest, const char* src, size_t n);
  65. // 某些系统库里没有定义strlcpy和strlcat,这里兼容处理下
  66. #if !HAVE_STRLCPY
  67. #define strlcpy safe_strncpy
  68. #endif
  69. #if !HAVE_STRLCAT
  70. #define strlcat safe_strncat
  71. #endif
  72. // 查找最后一个点符号,通常用于分离文件名和后缀
  73. #define strrchr_dot(str) strrchr(str, '.')
  74. // 查找最后一个路径符号,通常用于分离路径和文件名
  75. HV_EXPORT char* strrchr_dir(const char* filepath);
  76. // basename
  77. // 获取文件名,使用上面的strrchr_dir实现
  78. HV_EXPORT const char* hv_basename(const char* filepath);
  79. // 获取后缀名,使用上面的strrchr_dot实现
  80. HV_EXPORT const char* hv_suffixname(const char* filename);
  81. // mkdir -p
  82. // 递归创建文件夹
  83. HV_EXPORT int hv_mkdir_p(const char* dir);
  84. // rmdir -p
  85. // 递归删除文件夹
  86. HV_EXPORT int hv_rmdir_p(const char* dir);
  87. // 1 y on yes true enable
  88. // 根据字符串返回对应表示的布尔类型值
  89. HV_EXPORT bool getboolean(const char* str);
  90. // 获取可执行文件完整路径
  91. HV_EXPORT char* get_executable_path(char* buf, int size);
  92. // 获取可执行文件所在目录
  93. HV_EXPORT char* get_executable_dir(char* buf, int size);
  94. // 获取可执行文件名
  95. HV_EXPORT char* get_executable_file(char* buf, int size);
  96. // 获取运行目录
  97. HV_EXPORT char* get_run_dir(char* buf, int size);
  98. END_EXTERN_C
  99. #endif // HV_BASE_H_