hbase.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. //--------------------alloc/free---------------------------
  8. HV_EXPORT void* hv_malloc(size_t size);
  9. HV_EXPORT void* hv_realloc(void* oldptr, size_t newsize, size_t oldsize);
  10. HV_EXPORT void* hv_calloc(size_t nmemb, size_t size);
  11. HV_EXPORT void* hv_zalloc(size_t size);
  12. HV_EXPORT void hv_free(void* ptr);
  13. #define HV_ALLOC(ptr, size)\
  14. do {\
  15. *(void**)&(ptr) = hv_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. hv_free(ptr);\
  23. printd("free( %p )\tat [%s:%d:%s]\n", ptr, __FILE__, __LINE__, __FUNCTION__);\
  24. ptr = NULL;\
  25. }\
  26. } while(0)
  27. #define STACK_OR_HEAP_ALLOC(ptr, size, stack_size)\
  28. unsigned char _stackbuf_[stack_size] = { 0 };\
  29. if ((size) > (stack_size)) {\
  30. HV_ALLOC(ptr, size);\
  31. } else {\
  32. *(unsigned char**)&(ptr) = _stackbuf_;\
  33. }
  34. #define STACK_OR_HEAP_FREE(ptr)\
  35. if ((unsigned char*)(ptr) != _stackbuf_) {\
  36. HV_FREE(ptr);\
  37. }
  38. #define HV_DEFAULT_STACKBUF_SIZE 1024
  39. #define HV_STACK_ALLOC(ptr, size) STACK_OR_HEAP_ALLOC(ptr, size, HV_DEFAULT_STACKBUF_SIZE)
  40. #define HV_STACK_FREE(ptr) STACK_OR_HEAP_FREE(ptr)
  41. HV_EXPORT long hv_alloc_cnt();
  42. HV_EXPORT long hv_free_cnt();
  43. HV_INLINE void hv_memcheck() {
  44. printf("Memcheck => alloc:%ld free:%ld\n", hv_alloc_cnt(), hv_free_cnt());
  45. }
  46. #define HV_MEMCHECK atexit(hv_memcheck);
  47. //--------------------string-------------------------------
  48. HV_EXPORT char* hv_strupper(char* str);
  49. HV_EXPORT char* hv_strlower(char* str);
  50. HV_EXPORT char* hv_strreverse(char* str);
  51. HV_EXPORT bool hv_strstartswith(const char* str, const char* start);
  52. HV_EXPORT bool hv_strendswith(const char* str, const char* end);
  53. HV_EXPORT bool hv_strcontains(const char* str, const char* sub);
  54. // strncpy n = sizeof(dest_buf)-1
  55. // hv_strncpy n = sizeof(dest_buf)
  56. HV_EXPORT char* hv_strncpy(char* dest, const char* src, size_t n);
  57. // strncat n = sizeof(dest_buf)-1-strlen(dest)
  58. // hv_strncpy n = sizeof(dest_buf)
  59. HV_EXPORT char* hv_strncat(char* dest, const char* src, size_t n);
  60. #if !HAVE_STRLCPY
  61. #define strlcpy hv_strncpy
  62. #endif
  63. #if !HAVE_STRLCAT
  64. #define strlcat hv_strncat
  65. #endif
  66. #define hv_strrchr_dot(str) strrchr(str, '.')
  67. HV_EXPORT char* hv_strrchr_dir(const char* filepath);
  68. // basename
  69. HV_EXPORT const char* hv_basename(const char* filepath);
  70. HV_EXPORT const char* hv_suffixname(const char* filename);
  71. // mkdir -p
  72. HV_EXPORT int hv_mkdir_p(const char* dir);
  73. // rmdir -p
  74. HV_EXPORT int hv_rmdir_p(const char* dir);
  75. // path
  76. HV_EXPORT bool hv_exists(const char* path);
  77. HV_EXPORT bool hv_isdir(const char* path);
  78. HV_EXPORT bool hv_isfile(const char* path);
  79. HV_EXPORT bool hv_islink(const char* path);
  80. HV_EXPORT size_t hv_filesize(const char* filepath);
  81. HV_EXPORT char* get_executable_path(char* buf, int size);
  82. HV_EXPORT char* get_executable_dir(char* buf, int size);
  83. HV_EXPORT char* get_executable_file(char* buf, int size);
  84. HV_EXPORT char* get_run_dir(char* buf, int size);
  85. // random
  86. HV_EXPORT int hv_rand(int min, int max);
  87. HV_EXPORT void hv_random_string(char *buf, int len);
  88. // 1 y on yes true enable
  89. HV_EXPORT bool hv_getboolean(const char* str);
  90. END_EXTERN_C
  91. #endif // HV_BASE_H_