hbase.h 1.8 KB

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