mimalloc-new-delete.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. void operator delete(void* p) noexcept { mi_free(p); };
  24. void operator delete[](void* p) noexcept { mi_free(p); };
  25. void* operator new(std::size_t n) noexcept(false) { return mi_new(n); }
  26. void* operator new[](std::size_t n) noexcept(false) { return mi_new(n); }
  27. void* operator new (std::size_t n, const std::nothrow_t& tag) noexcept { (void)(tag); return mi_new_nothrow(n); }
  28. void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept { (void)(tag); return mi_new_nothrow(n); }
  29. #if (__cplusplus >= 201402L || _MSC_VER >= 1916)
  30. void operator delete (void* p, std::size_t n) noexcept { mi_free_size(p,n); };
  31. void operator delete[](void* p, std::size_t n) noexcept { mi_free_size(p,n); };
  32. #endif
  33. #if (__cplusplus > 201402L || defined(__cpp_aligned_new))
  34. void operator delete (void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
  35. void operator delete[](void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
  36. 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)); };
  37. 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)); };
  38. void* operator new( std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
  39. void* operator new[]( std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
  40. 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)); }
  41. 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)); }
  42. #endif
  43. #endif
  44. #endif // MIMALLOC_NEW_DELETE_H