hbase.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. safe_free(ptr);\
  22. printd("free( %p )\tat [%s:%d:%s]\n", ptr, __FILE__, __LINE__, __FUNCTION__);\
  23. } while(0)
  24. HV_EXPORT unsigned int hv_alloc_cnt();
  25. HV_EXPORT unsigned int hv_free_cnt();
  26. static inline void hv_memcheck() {
  27. printf("Memcheck => alloc:%u free:%u\n", hv_alloc_cnt(), hv_free_cnt());
  28. }
  29. #define HV_MEMCHECK atexit(hv_memcheck);
  30. //--------------------safe string-------------------------------
  31. HV_EXPORT char* strupper(char* str);
  32. HV_EXPORT char* strlower(char* str);
  33. HV_EXPORT char* strreverse(char* str);
  34. HV_EXPORT bool strstartswith(const char* str, const char* start);
  35. HV_EXPORT bool strendswith(const char* str, const char* end);
  36. HV_EXPORT bool strcontains(const char* str, const char* sub);
  37. // strncpy n = sizeof(dest_buf)-1
  38. // safe_strncpy n = sizeof(dest_buf)
  39. HV_EXPORT char* safe_strncpy(char* dest, const char* src, size_t n);
  40. // strncat n = sizeof(dest_buf)-1-strlen(dest)
  41. // safe_strncpy n = sizeof(dest_buf)
  42. HV_EXPORT char* safe_strncat(char* dest, const char* src, size_t n);
  43. #if !HAVE_STRLCPY
  44. #define strlcpy safe_strncpy
  45. #endif
  46. #if !HAVE_STRLCAT
  47. #define strlcat safe_strncat
  48. #endif
  49. #define strrchr_dot(str) strrchr(str, '.')
  50. HV_EXPORT char* strrchr_dir(const char* filepath);
  51. // basename
  52. HV_EXPORT const char* hv_basename(const char* filepath);
  53. HV_EXPORT const char* hv_suffixname(const char* filename);
  54. // mkdir -p
  55. HV_EXPORT int hv_mkdir_p(const char* dir);
  56. // rmdir -p
  57. HV_EXPORT int hv_rmdir_p(const char* dir);
  58. // 1 y on yes true enable
  59. HV_EXPORT bool getboolean(const char* str);
  60. HV_EXPORT char* get_executable_path(char* buf, int size);
  61. HV_EXPORT char* get_executable_dir(char* buf, int size);
  62. HV_EXPORT char* get_executable_file(char* buf, int size);
  63. HV_EXPORT char* get_run_dir(char* buf, int size);
  64. END_EXTERN_C
  65. #endif // HV_BASE_H_