mimalloc-new-delete.h 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. /* ----------------------------------------------------------------------------
  3. Copyright (c) 2018-2020 Microsoft Research, Daan Leijen
  4. This is free software; you can redistribute it and/or modify it under the
  5. terms of the MIT license. A copy of the license can be found in the file
  6. "LICENSE" at the root of this distribution.
  7. -----------------------------------------------------------------------------*/
  8. #pragma once
  9. #ifndef MIMALLOC_NEW_DELETE_H
  10. #define MIMALLOC_NEW_DELETE_H
  11. // ----------------------------------------------------------------------------
  12. // This header provides convenient overrides for the new and
  13. // delete operations in C++.
  14. //
  15. // This header should be included in only one source file!
  16. //
  17. // On Windows, or when linking dynamically with mimalloc, these
  18. // can be more performant than the standard new-delete operations.
  19. // See <https://en.cppreference.com/w/cpp/memory/new/operator_new>
  20. // ---------------------------------------------------------------------------
  21. #if defined(__cplusplus)
  22. #include <new>
  23. #include <mimalloc.h>
  24. #if defined(_MSC_VER) && defined(_Ret_notnull_) && defined(_Post_writable_byte_size_)
  25. // stay consistent with VCRT definitions
  26. #define mi_decl_new(n) mi_decl_nodiscard mi_decl_restrict _Ret_notnull_ _Post_writable_byte_size_(n)
  27. #define mi_decl_new_nothrow(n) mi_decl_nodiscard mi_decl_restrict _Ret_maybenull_ _Success_(return != NULL) _Post_writable_byte_size_(n)
  28. #else
  29. #define mi_decl_new(n) mi_decl_nodiscard mi_decl_restrict
  30. #define mi_decl_new_nothrow(n) mi_decl_nodiscard mi_decl_restrict
  31. #endif
  32. void operator delete(void* p) noexcept { mi_free(p); };
  33. void operator delete[](void* p) noexcept { mi_free(p); };
  34. void operator delete (void* p, const std::nothrow_t&) noexcept { mi_free(p); }
  35. void operator delete[](void* p, const std::nothrow_t&) noexcept { mi_free(p); }
  36. mi_decl_new(n) void* operator new(std::size_t n) noexcept(false) { return mi_new(n); }
  37. mi_decl_new(n) void* operator new[](std::size_t n) noexcept(false) { return mi_new(n); }
  38. mi_decl_new_nothrow(n) void* operator new (std::size_t n, const std::nothrow_t& tag) noexcept { (void)(tag); return mi_new_nothrow(n); }
  39. mi_decl_new_nothrow(n) void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept { (void)(tag); return mi_new_nothrow(n); }
  40. #if (__cplusplus >= 201402L || _MSC_VER >= 1916)
  41. void operator delete (void* p, std::size_t n) noexcept { mi_free_size(p,n); };
  42. void operator delete[](void* p, std::size_t n) noexcept { mi_free_size(p,n); };
  43. #endif
  44. #if (__cplusplus > 201402L || defined(__cpp_aligned_new))
  45. void operator delete (void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
  46. void operator delete[](void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
  47. void operator delete (void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_size_aligned(p, n, static_cast<size_t>(al)); };
  48. void operator delete[](void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_size_aligned(p, n, static_cast<size_t>(al)); };
  49. void operator delete (void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
  50. void operator delete[](void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
  51. void* operator new (std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
  52. void* operator new[](std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
  53. void* operator new (std::size_t n, std::align_val_t al, const std::nothrow_t&) noexcept { return mi_new_aligned_nothrow(n, static_cast<size_t>(al)); }
  54. void* operator new[](std::size_t n, std::align_val_t al, const std::nothrow_t&) noexcept { return mi_new_aligned_nothrow(n, static_cast<size_t>(al)); }
  55. #endif
  56. #endif
  57. #endif // MIMALLOC_NEW_DELETE_H
  58. #else
  59. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  60. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)