Bug 1936278 - Prevent search mode chiclet from being dismissed when clicking in page...
[gecko.git] / dom / canvas / DmdStdContainers.h
blob2975abc5800aa743b1ac9aae49248715608c24c9
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 {
15 // -
17 template <class T>
18 class dmd_allocator {
19 public:
20 using value_type = T;
22 private:
23 size_t mMallocSize = 0;
25 public:
26 dmd_allocator() = default;
28 // -
30 template <class U>
31 friend class dmd_allocator;
33 template <class U>
34 explicit dmd_allocator(const dmd_allocator<U>& rhs) {
35 if constexpr (kIsDmd) {
36 mMallocSize = rhs.mMallocSize;
40 // -
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);
47 return 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);
57 // -
59 size_t SizeOfExcludingThis(mozilla::MallocSizeOf) const {
60 return mMallocSize;
64 // -
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 {
71 public:
72 using StdT = _StdT;
74 size_t SizeOfExcludingThis(mozilla::MallocSizeOf mso) const {
75 const auto& a = StdT::get_allocator();
76 return a.SizeOfExcludingThis(mso);
80 // -
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 {
87 public:
88 using StdT = _StdT;
90 size_t SizeOfExcludingThis(mozilla::MallocSizeOf mso) const {
91 const auto& a = StdT::get_allocator();
92 return a.SizeOfExcludingThis(mso);
96 // -
98 } // namespace mozilla::webgl
100 #endif // dom_canvas_DmdStdContainers_h