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 dom_canvas_DmdStdContainers_h
6 #define dom_canvas_DmdStdContainers_h
8 #include "mozilla/MemoryReporting.h"
9 #include "mozilla/layers/BuildConstants.h"
10 #include <unordered_map>
11 #include <unordered_set>
13 namespace mozilla::webgl
{
23 size_t mMallocSize
= 0;
26 dmd_allocator() = default;
31 friend class dmd_allocator
;
34 explicit dmd_allocator(const dmd_allocator
<U
>& rhs
) {
35 if constexpr (kIsDmd
) {
36 mMallocSize
= rhs
.mMallocSize
;
42 value_type
* allocate(const size_t n
) {
43 const auto p
= std::allocator
<value_type
>{}.allocate(n
);
44 if constexpr (kIsDmd
) {
45 mMallocSize
+= moz_malloc_size_of(p
);
50 void deallocate(value_type
* const p
, const size_t n
) {
51 if constexpr (kIsDmd
) {
52 mMallocSize
-= moz_malloc_size_of(p
);
54 std::allocator
<value_type
>{}.deallocate(p
, n
);
59 size_t SizeOfExcludingThis(mozilla::MallocSizeOf
) const {
66 template <class Key
, class T
, class Hash
= std::hash
<Key
>,
67 class KeyEqual
= std::equal_to
<Key
>,
68 class Allocator
= dmd_allocator
<std::pair
<const Key
, T
>>,
69 class _StdT
= std::unordered_map
<Key
, T
, Hash
, KeyEqual
, Allocator
>>
70 class dmd_unordered_map
: public _StdT
{
74 size_t SizeOfExcludingThis(mozilla::MallocSizeOf mso
) const {
75 const auto& a
= StdT::get_allocator();
76 return a
.SizeOfExcludingThis(mso
);
82 template <class Key
, class Hash
= std::hash
<Key
>,
83 class KeyEqual
= std::equal_to
<Key
>,
84 class Allocator
= dmd_allocator
<Key
>,
85 class _StdT
= std::unordered_set
<Key
, Hash
, KeyEqual
, Allocator
>>
86 class dmd_unordered_set
: public _StdT
{
90 size_t SizeOfExcludingThis(mozilla::MallocSizeOf mso
) const {
91 const auto& a
= StdT::get_allocator();
92 return a
.SizeOfExcludingThis(mso
);
98 } // namespace mozilla::webgl
100 #endif // dom_canvas_DmdStdContainers_h