mimalloc-new-delete.h 3.9 KB

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