hbase.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. extern unsigned int g_alloc_cnt;
  9. extern unsigned int g_free_cnt;
  10. HV_EXPORT void* safe_malloc(size_t size);
  11. HV_EXPORT void* safe_realloc(void* oldptr, size_t newsize, size_t oldsize);
  12. HV_EXPORT void* safe_calloc(size_t nmemb, size_t size);
  13. HV_EXPORT void* safe_zalloc(size_t size);
  14. HV_EXPORT void safe_free(void* ptr);
  15. #define HV_ALLOC(ptr, size)\
  16. do {\
  17. *(void**)&(ptr) = safe_zalloc(size);\
  18. printd("alloc(%p, size=%llu)\tat [%s:%d:%s]\n", ptr, (unsigned long long)size, __FILE__, __LINE__, __FUNCTION__);\
  19. } while(0)
  20. #define HV_ALLOC_SIZEOF(ptr) HV_ALLOC(ptr, sizeof(*(ptr)))
  21. #define HV_FREE(ptr)\
  22. do {\
  23. safe_free(ptr);\
  24. printd("free( %p )\tat [%s:%d:%s]\n", ptr, __FILE__, __LINE__, __FUNCTION__);\
  25. } while(0)
  26. static inline void hv_memcheck() {
  27. printf("Memcheck => alloc:%u free:%u\n", g_alloc_cnt, g_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_