xpcom/glue/nsClassHashtable.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #ifndef nsClassHashtable_h__
     7 #define nsClassHashtable_h__
     9 #include "nsBaseHashtable.h"
    10 #include "nsHashKeys.h"
    11 #include "nsAutoPtr.h"
    13 /**
    14  * templated hashtable class maps keys to C++ object pointers.
    15  * See nsBaseHashtable for complete declaration.
    16  * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
    17  *   for a complete specification.
    18  * @param Class the class-type being wrapped
    19  * @see nsInterfaceHashtable, nsClassHashtable
    20  */
    21 template<class KeyClass,class T>
    22 class nsClassHashtable :
    23   public nsBaseHashtable< KeyClass, nsAutoPtr<T>, T* >
    24 {
    25 public:
    26   typedef typename KeyClass::KeyType KeyType;
    27   typedef T* UserDataType;
    28   typedef nsBaseHashtable< KeyClass, nsAutoPtr<T>, T* > base_type;
    30   nsClassHashtable()
    31   {
    32   }
    33   explicit nsClassHashtable(uint32_t aInitSize)
    34     : nsBaseHashtable<KeyClass,nsAutoPtr<T>,T*>(aInitSize)
    35   {
    36   }
    38   /**
    39    * @copydoc nsBaseHashtable::Get
    40    * @param pData if the key doesn't exist, pData will be set to nullptr.
    41    */
    42   bool Get(KeyType aKey, UserDataType* pData) const;
    44   /**
    45    * @copydoc nsBaseHashtable::Get
    46    * @returns nullptr if the key is not present.
    47    */
    48   UserDataType Get(KeyType aKey) const;
    50   /**
    51    * Remove the entry for the given key from the hashtable and return it in
    52    * aOut.  If the key is not in the hashtable, aOut's pointer is set to
    53    * nullptr.
    54    *
    55    * Normally, an entry is deleted when it's removed from an nsClassHashtable,
    56    * but this function transfers ownership of the entry back to the caller
    57    * through aOut -- the entry will be deleted when aOut goes out of scope.
    58    *
    59    * @param aKey the key to get and remove from the hashtable
    60    */
    61   void RemoveAndForget(KeyType aKey, nsAutoPtr<T> &aOut);
    62 };
    64 //
    65 // nsClassHashtable definitions
    66 //
    68 template<class KeyClass,class T>
    69 bool
    70 nsClassHashtable<KeyClass,T>::Get(KeyType aKey, T** retVal) const
    71 {
    72   typename base_type::EntryType* ent = this->GetEntry(aKey);
    74   if (ent)
    75   {
    76     if (retVal)
    77       *retVal = ent->mData;
    79     return true;
    80   }
    82   if (retVal)
    83     *retVal = nullptr;
    85   return false;
    86 }
    88 template<class KeyClass,class T>
    89 T*
    90 nsClassHashtable<KeyClass,T>::Get(KeyType aKey) const
    91 {
    92   typename base_type::EntryType* ent = this->GetEntry(aKey);
    94   if (!ent)
    95     return nullptr;
    97   return ent->mData;
    98 }
   100 template<class KeyClass,class T>
   101 void
   102 nsClassHashtable<KeyClass,T>::RemoveAndForget(KeyType aKey, nsAutoPtr<T> &aOut)
   103 {
   104   aOut = nullptr;
   105   nsAutoPtr<T> ptr;
   107   typename base_type::EntryType *ent = this->GetEntry(aKey);
   108   if (!ent)
   109     return;
   111   // Transfer ownership from ent->mData into aOut.
   112   aOut = ent->mData;
   114   this->Remove(aKey);
   115 }
   117 #endif // nsClassHashtable_h__

mercurial