hbase.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // strncpy n = sizeof(dest_buf)-1
  44. // safe_strncpy n = sizeof(dest_buf)
  45. char* safe_strncpy(char* dest, const char* src, size_t n);
  46. // strncat n = sizeof(dest_buf)-1-strlen(dest)
  47. // safe_strncpy n = sizeof(dest_buf)
  48. char* safe_strncat(char* dest, const char* src, size_t n);
  49. #if !HAVE_STRLCPY
  50. #define strlcpy safe_strncpy
  51. #endif
  52. #if !HAVE_STRLCAT
  53. #define strlcat safe_strncat
  54. #endif
  55. // 1 y on yes true enable
  56. bool getboolean(const char* str);
  57. #ifdef __cplusplus
  58. } // extern "C"
  59. #endif
  60. #endif // HV_BASE_H_