tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DmdStdContainers.h (2330B)


      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 dom_canvas_DmdStdContainers_h
      6 #define dom_canvas_DmdStdContainers_h
      7 
      8 #include <unordered_map>
      9 #include <unordered_set>
     10 
     11 #include "mozilla/MemoryReporting.h"
     12 #include "mozilla/layers/BuildConstants.h"
     13 
     14 namespace mozilla::webgl {
     15 
     16 // -
     17 
     18 template <class T>
     19 class dmd_allocator {
     20 public:
     21  using value_type = T;
     22 
     23 private:
     24  size_t mMallocSize = 0;
     25 
     26 public:
     27  dmd_allocator() = default;
     28 
     29  // -
     30 
     31  template <class U>
     32  friend class dmd_allocator;
     33 
     34  template <class U>
     35  explicit dmd_allocator(const dmd_allocator<U>& rhs) {
     36    if constexpr (kIsDmd) {
     37      mMallocSize = rhs.mMallocSize;
     38    }
     39  }
     40 
     41  // -
     42 
     43  value_type* allocate(const size_t n) {
     44    const auto p = std::allocator<value_type>{}.allocate(n);
     45    if constexpr (kIsDmd) {
     46      mMallocSize += moz_malloc_size_of(p);
     47    }
     48    return p;
     49  }
     50 
     51  void deallocate(value_type* const p, const size_t n) {
     52    if constexpr (kIsDmd) {
     53      mMallocSize -= moz_malloc_size_of(p);
     54    }
     55    std::allocator<value_type>{}.deallocate(p, n);
     56  }
     57 
     58  // -
     59 
     60  size_t SizeOfExcludingThis(mozilla::MallocSizeOf) const {
     61    return mMallocSize;
     62  }
     63 };
     64 
     65 // -
     66 
     67 template <class Key, class T, class Hash = std::hash<Key>,
     68          class KeyEqual = std::equal_to<Key>,
     69          class Allocator = dmd_allocator<std::pair<const Key, T>>,
     70          class _StdT = std::unordered_map<Key, T, Hash, KeyEqual, Allocator>>
     71 class dmd_unordered_map : public _StdT {
     72 public:
     73  using StdT = _StdT;
     74 
     75  size_t SizeOfExcludingThis(mozilla::MallocSizeOf mso) const {
     76    const auto& a = StdT::get_allocator();
     77    return a.SizeOfExcludingThis(mso);
     78  }
     79 };
     80 
     81 // -
     82 
     83 template <class Key, class Hash = std::hash<Key>,
     84          class KeyEqual = std::equal_to<Key>,
     85          class Allocator = dmd_allocator<Key>,
     86          class _StdT = std::unordered_set<Key, Hash, KeyEqual, Allocator>>
     87 class dmd_unordered_set : public _StdT {
     88 public:
     89  using StdT = _StdT;
     90 
     91  size_t SizeOfExcludingThis(mozilla::MallocSizeOf mso) const {
     92    const auto& a = StdT::get_allocator();
     93    return a.SizeOfExcludingThis(mso);
     94  }
     95 };
     96 
     97 // -
     98 
     99 }  // namespace mozilla::webgl
    100 
    101 #endif  // dom_canvas_DmdStdContainers_h