|
| 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 | +// ---------------------------------------------------------------------------- |
| 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 | + |
| 25 | + #if defined(_MSC_VER) && defined(_Ret_notnull_) && defined(_Post_writable_byte_size_) |
| 26 | + // stay consistent with VCRT definitions |
| 27 | + #define mi_decl_new(n) mi_decl_nodiscard mi_decl_restrict _Ret_notnull_ _Post_writable_byte_size_(n) |
| 28 | + #define mi_decl_new_nothrow(n) mi_decl_nodiscard mi_decl_restrict _Ret_maybenull_ _Success_(return != NULL) _Post_writable_byte_size_(n) |
| 29 | + #else |
| 30 | + #define mi_decl_new(n) mi_decl_nodiscard mi_decl_restrict |
| 31 | + #define mi_decl_new_nothrow(n) mi_decl_nodiscard mi_decl_restrict |
| 32 | + #endif |
| 33 | + |
| 34 | + void operator delete(void* p) noexcept { mi_free(p); }; |
| 35 | + void operator delete[](void* p) noexcept { mi_free(p); }; |
| 36 | + |
| 37 | + void operator delete (void* p, const std::nothrow_t&) noexcept { mi_free(p); } |
| 38 | + void operator delete[](void* p, const std::nothrow_t&) noexcept { mi_free(p); } |
| 39 | + |
| 40 | + mi_decl_new(n) void* operator new(std::size_t n) noexcept(false) { return mi_new(n); } |
| 41 | + mi_decl_new(n) void* operator new[](std::size_t n) noexcept(false) { return mi_new(n); } |
| 42 | + |
| 43 | + 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); } |
| 44 | + 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); } |
| 45 | + |
| 46 | + #if (__cplusplus >= 201402L || _MSC_VER >= 1916) |
| 47 | + void operator delete (void* p, std::size_t n) noexcept { mi_free_size(p,n); }; |
| 48 | + void operator delete[](void* p, std::size_t n) noexcept { mi_free_size(p,n); }; |
| 49 | + #endif |
| 50 | + |
| 51 | + #if (__cplusplus > 201402L || defined(__cpp_aligned_new)) |
| 52 | + void operator delete (void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); } |
| 53 | + void operator delete[](void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); } |
| 54 | + 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)); }; |
| 55 | + 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)); }; |
| 56 | + void operator delete (void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); } |
| 57 | + void operator delete[](void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); } |
| 58 | + |
| 59 | + void* operator new (std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); } |
| 60 | + void* operator new[](std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); } |
| 61 | + 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)); } |
| 62 | + 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)); } |
| 63 | + #endif |
| 64 | +#endif |
| 65 | + |
| 66 | +#endif // MIMALLOC_NEW_DELETE_H |
0 commit comments