1
0

hbase.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #ifndef HV_BASE_H_
  2. #define HV_BASE_H_
  3. #include "hexport.h"
  4. #include "hplatform.h" // for bool
  5. #include "hdef.h" // for printd
  6. BEGIN_EXTERN_C
  7. //--------------------safe alloc/free---------------------------
  8. HV_EXPORT void* safe_malloc(size_t size);
  9. HV_EXPORT void* safe_realloc(void* oldptr, size_t newsize, size_t oldsize);
  10. HV_EXPORT void* safe_calloc(size_t nmemb, size_t size);
  11. HV_EXPORT void* safe_zalloc(size_t size);
  12. HV_EXPORT void safe_free(void* ptr);
  13. #define HV_ALLOC(ptr, size)\
  14. do {\
  15. *(void**)&(ptr) = 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. safe_free(ptr);\
  23. printd("free( %p )\tat [%s:%d:%s]\n", ptr, __FILE__, __LINE__, __FUNCTION__);\
  24. ptr = NULL;\
  25. }\
  26. } while(0)
  27. HV_EXPORT long hv_alloc_cnt();
  28. HV_EXPORT long hv_free_cnt();
  29. static inline void hv_memcheck() {
  30. printf("Memcheck => alloc:%ld free:%ld\n", hv_alloc_cnt(), hv_free_cnt());
  31. }
  32. #define HV_MEMCHECK atexit(hv_memcheck);
  33. //--------------------safe string-------------------------------
  34. HV_EXPORT char* strupper(char* str);
  35. HV_EXPORT char* strlower(char* str);
  36. HV_EXPORT char* strreverse(char* str);
  37. HV_EXPORT bool strstartswith(const char* str, const char* start);
  38. HV_EXPORT bool strendswith(const char* str, const char* end);
  39. HV_EXPORT bool strcontains(const char* str, const char* sub);
  40. // strncpy n = sizeof(dest_buf)-1
  41. // safe_strncpy n = sizeof(dest_buf)
  42. HV_EXPORT char* safe_strncpy(char* dest, const char* src, size_t n);
  43. // strncat n = sizeof(dest_buf)-1-strlen(dest)
  44. // safe_strncpy n = sizeof(dest_buf)
  45. HV_EXPORT char* safe_strncat(char* dest, const char* src, size_t n);
  46. #if !HAVE_STRLCPY
  47. #define strlcpy safe_strncpy
  48. #endif
  49. #if !HAVE_STRLCAT
  50. #define strlcat safe_strncat
  51. #endif
  52. #define strrchr_dot(str) strrchr(str, '.')
  53. HV_EXPORT char* strrchr_dir(const char* filepath);
  54. // basename
  55. HV_EXPORT const char* hv_basename(const char* filepath);
  56. HV_EXPORT const char* hv_suffixname(const char* filename);
  57. // mkdir -p
  58. HV_EXPORT int hv_mkdir_p(const char* dir);
  59. // rmdir -p
  60. HV_EXPORT int hv_rmdir_p(const char* dir);
  61. // 1 y on yes true enable
  62. HV_EXPORT bool getboolean(const char* str);
  63. HV_EXPORT char* get_executable_path(char* buf, int size);
  64. HV_EXPORT char* get_executable_dir(char* buf, int size);
  65. HV_EXPORT char* get_executable_file(char* buf, int size);
  66. HV_EXPORT char* get_run_dir(char* buf, int size);
  67. END_EXTERN_C
  68. #endif // HV_BASE_H_