Path: blob/main/system/lib/mimalloc/include/mimalloc-new-delete.h
6178 views
/* ----------------------------------------------------------------------------1Copyright (c) 2018-2020 Microsoft Research, Daan Leijen2This is free software; you can redistribute it and/or modify it under the3terms of the MIT license. A copy of the license can be found in the file4"LICENSE" at the root of this distribution.5-----------------------------------------------------------------------------*/6#pragma once7#ifndef MIMALLOC_NEW_DELETE_H8#define MIMALLOC_NEW_DELETE_H910// ----------------------------------------------------------------------------11// This header provides convenient overrides for the new and12// 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, these17// 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>2324#if defined(_MSC_VER) && defined(_Ret_notnull_) && defined(_Post_writable_byte_size_)25// stay consistent with VCRT definitions26#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#else29#define mi_decl_new(n) mi_decl_nodiscard mi_decl_restrict30#define mi_decl_new_nothrow(n) mi_decl_nodiscard mi_decl_restrict31#endif3233void operator delete(void* p) noexcept { mi_free(p); };34void operator delete[](void* p) noexcept { mi_free(p); };3536void operator delete (void* p, const std::nothrow_t&) noexcept { mi_free(p); }37void operator delete[](void* p, const std::nothrow_t&) noexcept { mi_free(p); }3839mi_decl_new(n) void* operator new(std::size_t n) noexcept(false) { return mi_new(n); }40mi_decl_new(n) void* operator new[](std::size_t n) noexcept(false) { return mi_new(n); }4142mi_decl_new_nothrow(n) void* operator new (std::size_t n, const std::nothrow_t& tag) noexcept { (void)(tag); return mi_new_nothrow(n); }43mi_decl_new_nothrow(n) void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept { (void)(tag); return mi_new_nothrow(n); }4445#if (__cplusplus >= 201402L || _MSC_VER >= 1916)46void operator delete (void* p, std::size_t n) noexcept { mi_free_size(p,n); };47void operator delete[](void* p, std::size_t n) noexcept { mi_free_size(p,n); };48#endif4950#if (__cplusplus > 201402L || defined(__cpp_aligned_new))51void operator delete (void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }52void operator delete[](void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }53void 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)); };54void 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)); };55void operator delete (void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }56void operator delete[](void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }5758void* operator new (std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }59void* operator new[](std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }60void* 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)); }61void* 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#endif63#endif6465#endif // MIMALLOC_NEW_DELETE_H666768