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/. */
4 
5 #ifndef mozilla_cxxalloc_h
6 #define mozilla_cxxalloc_h
7 
8 /*
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
12  * pointer.
13  *
14  * Each operator new immediately below returns a pointer to memory
15  * that can be delete'd by any of
16  *
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)|
20  *
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.
24  *
25  * NB: mozilla::fallible can be used instead of std::nothrow.
26  */
27 
28 #ifndef MOZALLOC_EXPORT_NEW
29 #  define MOZALLOC_EXPORT_NEW MFBT_API
30 #endif
31 
new(size_t size)32 MOZALLOC_EXPORT_NEW void* operator new(size_t size) noexcept(false) {
33   return moz_xmalloc(size);
34 }
35 
new(size_t size,const std::nothrow_t &)36 MOZALLOC_EXPORT_NEW void* operator new(size_t size,
37                                        const std::nothrow_t&) noexcept(true) {
38   return malloc_impl(size);
39 }
40 
noexcept(false)41 MOZALLOC_EXPORT_NEW void* operator new[](size_t size) noexcept(false) {
42   return moz_xmalloc(size);
43 }
44 
45 MOZALLOC_EXPORT_NEW void* operator new[](size_t size,
noexcept(true)46                                          const std::nothrow_t&) noexcept(true) {
47   return malloc_impl(size);
48 }
49 
delete(void * ptr)50 MOZALLOC_EXPORT_NEW void operator delete(void* ptr) noexcept(true) {
51   return free_impl(ptr);
52 }
53 
delete(void * ptr,const std::nothrow_t &)54 MOZALLOC_EXPORT_NEW void operator delete(void* ptr,
55                                          const std::nothrow_t&) noexcept(true) {
56   return free_impl(ptr);
57 }
58 
noexcept(true)59 MOZALLOC_EXPORT_NEW void operator delete[](void* ptr) noexcept(true) {
60   return free_impl(ptr);
61 }
62 
63 MOZALLOC_EXPORT_NEW void operator delete[](
noexcept(true)64     void* ptr, const std::nothrow_t&) noexcept(true) {
65   return free_impl(ptr);
66 }
67 
68 #if defined(XP_WIN)
69 // We provide the global sized delete overloads unconditionally because the
70 // MSVC runtime headers do, despite compiling with /Zc:sizedDealloc-
delete(void * ptr,size_t)71 MOZALLOC_EXPORT_NEW void operator delete(void* ptr,
72                                          size_t /*size*/) noexcept(true) {
73   return free_impl(ptr);
74 }
75 
76 MOZALLOC_EXPORT_NEW void operator delete[](void* ptr,
noexcept(true)77                                            size_t /*size*/) noexcept(true) {
78   return free_impl(ptr);
79 }
80 #endif
81 
82 #endif /* mozilla_cxxalloc_h */
83