hbase.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifndef HV_BASE_H_
  2. #define HV_BASE_H_
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "hdef.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. //---------------------safe alloc/free---------------------------
  11. extern unsigned int g_alloc_cnt;
  12. extern unsigned int g_free_cnt;
  13. void* safe_malloc(size_t size);
  14. void* safe_realloc(void* oldptr, size_t newsize, size_t oldsize);
  15. void* safe_calloc(size_t nmemb, size_t size);
  16. void* safe_zalloc(size_t size);
  17. #undef SAFE_ALLOC
  18. #define SAFE_ALLOC(ptr, size)\
  19. do {\
  20. void** pptr = (void**)&(ptr);\
  21. *pptr = safe_zalloc(size);\
  22. printd("alloc(%p, size=%lu)\tat [%s:%d:%s]\n", ptr, size, __FILE__, __LINE__, __FUNCTION__);\
  23. } while(0)
  24. #define SAFE_ALLOC_SIZEOF(ptr) SAFE_ALLOC(ptr, sizeof(*(ptr)))
  25. #undef SAFE_FREE
  26. #define SAFE_FREE(ptr)\
  27. do {\
  28. if (ptr) {\
  29. printd("free( %p )\tat [%s:%d:%s]\n", ptr, __FILE__, __LINE__, __FUNCTION__);\
  30. free(ptr);\
  31. ptr = NULL;\
  32. ++g_free_cnt;\
  33. }\
  34. } while(0)
  35. static inline void memcheck() {
  36. printf("Memcheck => alloc:%u free:%u\n", g_alloc_cnt, g_free_cnt);
  37. }
  38. #define MEMCHECK atexit(memcheck);
  39. //-----------------------------safe string-----------------------
  40. char* strupper(char* str);
  41. char* strlower(char* str);
  42. char* strreverse(char* str);
  43. bool strstartswith(const char* str, const char* start);
  44. bool strendswith(const char* str, const char* end);
  45. bool strcontains(const char* str, const char* sub);
  46. // strncpy n = sizeof(dest_buf)-1
  47. // safe_strncpy n = sizeof(dest_buf)
  48. char* safe_strncpy(char* dest, const char* src, size_t n);
  49. // strncat n = sizeof(dest_buf)-1-strlen(dest)
  50. // safe_strncpy n = sizeof(dest_buf)
  51. char* safe_strncat(char* dest, const char* src, size_t n);
  52. #if !HAVE_STRLCPY
  53. #define strlcpy safe_strncpy
  54. #endif
  55. #if !HAVE_STRLCAT
  56. #define strlcat safe_strncat
  57. #endif
  58. // 1 y on yes true enable
  59. bool getboolean(const char* str);
  60. #ifdef __cplusplus
  61. } // extern "C"
  62. #endif
  63. #endif // HV_BASE_H_