mem.inline.h 1014 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #ifndef AFUN_MEM_INLINE_H
  2. #define AFUN_MEM_INLINE_H
  3. #include <cstdlib>
  4. #include "log.h"
  5. #include "exit_.h"
  6. #include "tool-logger.h"
  7. /* 取代calloc函数 */
  8. namespace aFuntool {
  9. template <typename T>
  10. static void *safeFree(T *ptr) {if (ptr != nullptr) free((void *)ptr); return nullptr;}
  11. static void *safeCalloc(size_t n, size_t size){
  12. void *re = calloc(n, size);
  13. if (re == nullptr) {
  14. if (SysLogger)
  15. fatalErrorLog(SysLogger, EXIT_FAILURE, "The memory error");
  16. else
  17. aFunExit(EXIT_FAILURE);
  18. }
  19. return re;
  20. }
  21. template <typename T>
  22. static void *safeCalloc(size_t n, T &t){
  23. void *re = calloc(n, sizeof(decltype(*t))); // 自动推断类型
  24. if (re == nullptr) {
  25. if (SysLogger)
  26. fatalErrorLog(SysLogger, EXIT_FAILURE, "The memory error");
  27. else
  28. aFunExit(EXIT_FAILURE);
  29. }
  30. return re;
  31. }
  32. }
  33. #endif //AFUN_MEM_INLINE_H