hbase.h 1.9 KB

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