1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_cxxalloc_h
6 #define mozilla_cxxalloc_h
9 * We implement the default operators new/delete as part of
10 * libmozalloc, replacing their definitions in libstdc++. The
11 * operator new* definitions in libmozalloc will never return a NULL
14 * Each operator new immediately below returns a pointer to memory
15 * that can be delete'd by any of
17 * (1) the matching infallible operator delete immediately below
18 * (2) the matching system |operator delete(void*, std::nothrow)|
19 * (3) the matching system |operator delete(void*) noexcept(false)|
21 * NB: these are declared |noexcept(false)|, though they will never
22 * throw that exception. This declaration is consistent with the rule
23 * that |::operator new() noexcept(false)| will never return NULL.
25 * NB: mozilla::fallible can be used instead of std::nothrow.
28 #ifndef MOZALLOC_EXPORT_NEW
29 # define MOZALLOC_EXPORT_NEW MFBT_API
32 MOZALLOC_EXPORT_NEW
void* operator new(size_t size
) noexcept(false) {
33 return moz_xmalloc(size
);
36 MOZALLOC_EXPORT_NEW
void* operator new(size_t size
,
37 const std::nothrow_t
&) noexcept(true) {
38 return malloc_impl(size
);
41 MOZALLOC_EXPORT_NEW
void* operator new[](size_t size
) noexcept(false) {
42 return moz_xmalloc(size
);
45 MOZALLOC_EXPORT_NEW
void* operator new[](size_t size
,
46 const std::nothrow_t
&) noexcept(true) {
47 return malloc_impl(size
);
50 MOZALLOC_EXPORT_NEW
void operator delete(void* ptr
) noexcept(true) {
51 return free_impl(ptr
);
54 MOZALLOC_EXPORT_NEW
void operator delete(void* ptr
,
55 const std::nothrow_t
&)noexcept(true) {
56 return free_impl(ptr
);
59 MOZALLOC_EXPORT_NEW
void operator delete[](void* ptr
) noexcept(true) {
60 return free_impl(ptr
);
63 MOZALLOC_EXPORT_NEW
void operator delete[](
64 void* ptr
, const std::nothrow_t
&) noexcept(true) {
65 return free_impl(ptr
);
69 // We provide the global sized delete overloads unconditionally because the
70 // MSVC runtime headers do, despite compiling with /Zc:sizedDealloc-
71 MOZALLOC_EXPORT_NEW
void operator delete(void* ptr
,
72 size_t /*size*/) noexcept(true) {
73 return free_impl(ptr
);
76 MOZALLOC_EXPORT_NEW
void operator delete[](void* ptr
,
77 size_t /*size*/) noexcept(true) {
78 return free_impl(ptr
);
82 #endif /* mozilla_cxxalloc_h */