Bug 1475461 - part 2: Make callers of PLDHashTable::Search() const methods if possible r?ehsan draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 13 Jul 2018 19:01:53 +0900
changeset 817736 dee1945e42d44fa5d963356a94aec2a95104a208
parent 817735 b32f7a1e75bb7d39a934847ff556692d30fbbdae
push id116152
push usermasayuki@d-toybox.com
push dateFri, 13 Jul 2018 11:42:50 +0000
reviewersehsan
bugs1475461
milestone63.0a1
Bug 1475461 - part 2: Make callers of PLDHashTable::Search() const methods if possible r?ehsan Some callers of PLDHashTable::Search() use const_cast, some others are not const methods due to non-const PLDHashTable::Search(). This patch removes const_cast from the former and mark some methods of the latter const. MozReview-Commit-ID: C8ayoi7mXc1
dom/commandhandler/nsCommandParams.cpp
js/xpconnect/src/XPCMaps.h
netwerk/cache/nsCacheEntry.cpp
netwerk/cache/nsCacheEntry.h
netwerk/cache/nsDiskCacheBinding.cpp
netwerk/cache/nsDiskCacheBinding.h
uriloader/base/nsDocLoader.cpp
uriloader/base/nsDocLoader.h
xpcom/base/nsCycleCollector.cpp
xpcom/ds/nsAtomTable.cpp
xpcom/ds/nsStaticNameTable.cpp
xpcom/ds/nsStaticNameTable.h
xpcom/ds/nsTHashtable.h
--- a/dom/commandhandler/nsCommandParams.cpp
+++ b/dom/commandhandler/nsCommandParams.cpp
@@ -290,18 +290,17 @@ nsCommandParams::RemoveValue(const char*
 {
   mValuesHash.Remove((void*)aName);
   return NS_OK;
 }
 
 nsCommandParams::HashEntry*
 nsCommandParams::GetNamedEntry(const char* aName) const
 {
-  return static_cast<HashEntry*>(
-           const_cast<PLDHashTable&>(mValuesHash).Search((void*)aName));
+  return static_cast<HashEntry*>(mValuesHash.Search((void*)aName));
 }
 
 nsCommandParams::HashEntry*
 nsCommandParams::GetOrMakeEntry(const char* aName, uint8_t aEntryType)
 {
   auto foundEntry = static_cast<HashEntry*>(mValuesHash.Search((void*)aName));
   if (foundEntry) { // reuse existing entry
     foundEntry->Reset(aEntryType);
--- a/js/xpconnect/src/XPCMaps.h
+++ b/js/xpconnect/src/XPCMaps.h
@@ -107,17 +107,17 @@ public:
     struct Entry : public PLDHashEntryHdr
     {
         nsISupports*      key;
         XPCWrappedNative* value;
     };
 
     static Native2WrappedNativeMap* newMap(int length);
 
-    inline XPCWrappedNative* Find(nsISupports* Obj)
+    inline XPCWrappedNative* Find(nsISupports* Obj) const
     {
         MOZ_ASSERT(Obj,"bad param");
         auto entry = static_cast<Entry*>(mTable.Search(Obj));
         return entry ? entry->value : nullptr;
     }
 
     inline XPCWrappedNative* Add(XPCWrappedNative* wrapper)
     {
@@ -173,17 +173,17 @@ public:
         const nsIID*         key;
         nsXPCWrappedJSClass* value;
 
         static const struct PLDHashTableOps sOps;
     };
 
     static IID2WrappedJSClassMap* newMap(int length);
 
-    inline nsXPCWrappedJSClass* Find(REFNSIID iid)
+    inline nsXPCWrappedJSClass* Find(REFNSIID iid) const
     {
         auto entry = static_cast<Entry*>(mTable.Search(&iid));
         return entry ? entry->value : nullptr;
     }
 
     inline nsXPCWrappedJSClass* Add(nsXPCWrappedJSClass* clazz)
     {
         MOZ_ASSERT(clazz,"bad param");
@@ -227,17 +227,17 @@ public:
         const nsIID*        key;
         XPCNativeInterface* value;
 
         static const struct PLDHashTableOps sOps;
     };
 
     static IID2NativeInterfaceMap* newMap(int length);
 
-    inline XPCNativeInterface* Find(REFNSIID iid)
+    inline XPCNativeInterface* Find(REFNSIID iid) const
     {
         auto entry = static_cast<Entry*>(mTable.Search(&iid));
         return entry ? entry->value : nullptr;
     }
 
     inline XPCNativeInterface* Add(XPCNativeInterface* iface)
     {
         MOZ_ASSERT(iface,"bad param");
@@ -285,17 +285,17 @@ public:
 
     private:
         static bool Match(const PLDHashEntryHdr* aEntry, const void* aKey);
         static void Clear(PLDHashTable* aTable, PLDHashEntryHdr* aEntry);
     };
 
     static ClassInfo2NativeSetMap* newMap(int length);
 
-    inline XPCNativeSet* Find(nsIClassInfo* info)
+    inline XPCNativeSet* Find(nsIClassInfo* info) const
     {
         auto entry = static_cast<Entry*>(mTable.Search(info));
         return entry ? entry->value : nullptr;
     }
 
     inline XPCNativeSet* Add(nsIClassInfo* info, XPCNativeSet* set)
     {
         MOZ_ASSERT(info,"bad param");
@@ -338,17 +338,17 @@ public:
     struct Entry : public PLDHashEntryHdr
     {
         nsIClassInfo*          key;
         XPCWrappedNativeProto* value;
     };
 
     static ClassInfo2WrappedNativeProtoMap* newMap(int length);
 
-    inline XPCWrappedNativeProto* Find(nsIClassInfo* info)
+    inline XPCWrappedNativeProto* Find(nsIClassInfo* info) const
     {
         auto entry = static_cast<Entry*>(mTable.Search(info));
         return entry ? entry->value : nullptr;
     }
 
     inline XPCWrappedNativeProto* Add(nsIClassInfo* info, XPCWrappedNativeProto* proto)
     {
         MOZ_ASSERT(info,"bad param");
@@ -396,17 +396,17 @@ public:
         static bool
         Match(const PLDHashEntryHdr* entry, const void* key);
 
         static const struct PLDHashTableOps sOps;
     };
 
     static NativeSetMap* newMap(int length);
 
-    inline XPCNativeSet* Find(XPCNativeSetKey* key)
+    inline XPCNativeSet* Find(XPCNativeSetKey* key) const
     {
         auto entry = static_cast<Entry*>(mTable.Search(key));
         return entry ? entry->key_value : nullptr;
     }
 
     inline XPCNativeSet* Add(const XPCNativeSetKey* key, XPCNativeSet* set)
     {
         MOZ_ASSERT(key, "bad param");
--- a/netwerk/cache/nsCacheEntry.cpp
+++ b/netwerk/cache/nsCacheEntry.cpp
@@ -417,17 +417,17 @@ nsCacheEntryHashTable::Shutdown()
     if (initialized) {
         table.ClearAndPrepareForLength(kInitialTableLength);
         initialized = false;
     }
 }
 
 
 nsCacheEntry *
-nsCacheEntryHashTable::GetEntry( const nsCString * key)
+nsCacheEntryHashTable::GetEntry( const nsCString * key) const
 {
     NS_ASSERTION(initialized, "nsCacheEntryHashTable not initialized");
     if (!initialized)  return nullptr;
 
     PLDHashEntryHdr *hashEntry = table.Search(key);
     return hashEntry ? ((nsCacheEntryHashTableEntry *)hashEntry)->cacheEntry
                      : nullptr;
 }
--- a/netwerk/cache/nsCacheEntry.h
+++ b/netwerk/cache/nsCacheEntry.h
@@ -265,17 +265,17 @@ class nsCacheEntryHashTable
 {
 public:
     nsCacheEntryHashTable();
     ~nsCacheEntryHashTable();
 
     void          Init();
     void          Shutdown();
 
-    nsCacheEntry *GetEntry( const nsCString * key);
+    nsCacheEntry *GetEntry( const nsCString * key) const;
     nsresult      AddEntry( nsCacheEntry *entry);
     void          RemoveEntry( nsCacheEntry *entry);
 
     PLDHashTable::Iterator Iter();
 
 private:
     // PLDHashTable operation callbacks
     static PLDHashNumber  HashKey(const void *key);
--- a/netwerk/cache/nsDiskCacheBinding.cpp
+++ b/netwerk/cache/nsDiskCacheBinding.cpp
@@ -187,17 +187,17 @@ nsDiskCacheBindery::CreateBinding(nsCach
     return binding;
 }
 
 
 /**
  *  FindActiveEntry :  to find active colliding entry so we can doom it
  */
 nsDiskCacheBinding *
-nsDiskCacheBindery::FindActiveBinding(uint32_t  hashNumber)
+nsDiskCacheBindery::FindActiveBinding(uint32_t  hashNumber) const
 {
     NS_ASSERTION(initialized, "nsDiskCacheBindery not initialized");
     // find hash entry for key
     auto hashEntry = static_cast<HashTableEntry*>
         (table.Search((void*)(uintptr_t)hashNumber));
     if (!hashEntry) return nullptr;
 
     // walk list looking for active entry
--- a/netwerk/cache/nsDiskCacheBinding.h
+++ b/netwerk/cache/nsDiskCacheBinding.h
@@ -98,17 +98,17 @@ public:
     ~nsDiskCacheBindery();
 
     void                    Init();
     void                    Reset();
 
     nsDiskCacheBinding *    CreateBinding(nsCacheEntry *       entry,
                                           nsDiskCacheRecord *  record);
 
-    nsDiskCacheBinding *    FindActiveBinding(uint32_t  hashNumber);
+    nsDiskCacheBinding *    FindActiveBinding(uint32_t  hashNumber) const;
     void                    RemoveBinding(nsDiskCacheBinding * binding);
     bool                    ActiveBindings();
 
     size_t                 SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf);
 
 private:
     nsresult                AddBinding(nsDiskCacheBinding * binding);
 
--- a/uriloader/base/nsDocLoader.cpp
+++ b/uriloader/base/nsDocLoader.cpp
@@ -1391,17 +1391,18 @@ nsresult nsDocLoader::AddRequestInfo(nsI
   return NS_OK;
 }
 
 void nsDocLoader::RemoveRequestInfo(nsIRequest *aRequest)
 {
   mRequestInfoHash.Remove(aRequest);
 }
 
-nsDocLoader::nsRequestInfo* nsDocLoader::GetRequestInfo(nsIRequest* aRequest)
+nsDocLoader::nsRequestInfo*
+nsDocLoader::GetRequestInfo(nsIRequest* aRequest) const
 {
   return static_cast<nsRequestInfo*>(mRequestInfoHash.Search(aRequest));
 }
 
 void nsDocLoader::ClearRequestInfoHash(void)
 {
   mRequestInfoHash.Clear();
 }
--- a/uriloader/base/nsDocLoader.h
+++ b/uriloader/base/nsDocLoader.h
@@ -320,17 +320,17 @@ private:
     // loadgroup has no active requests before checking for "real" emptiness if
     // aFlushLayout is true.
     void DocLoaderIsEmpty(bool aFlushLayout);
 
     int64_t GetMaxTotalProgress();
 
     nsresult AddRequestInfo(nsIRequest* aRequest);
     void RemoveRequestInfo(nsIRequest* aRequest);
-    nsRequestInfo *GetRequestInfo(nsIRequest* aRequest);
+    nsRequestInfo *GetRequestInfo(nsIRequest* aRequest) const;
     void ClearRequestInfoHash();
     int64_t CalculateMaxProgress();
 ///    void DumpChannelInfo(void);
 
     // used to clear our internal progress state between loads...
     void ClearInternalProgress();
 };
 
--- a/xpcom/base/nsCycleCollector.cpp
+++ b/xpcom/base/nsCycleCollector.cpp
@@ -951,17 +951,17 @@ public:
     n += mWeakMaps.ShallowSizeOfExcludingThis(aMallocSizeOf);
 
     n += mPtrToNodeMap.ShallowSizeOfExcludingThis(aMallocSizeOf);
 
     return n;
   }
 
 private:
-  PtrToNodeEntry* FindNodeEntry(void* aPtr)
+  PtrToNodeEntry* FindNodeEntry(void* aPtr) const
   {
     return static_cast<PtrToNodeEntry*>(mPtrToNodeMap.Search(aPtr));
   }
 };
 
 PtrInfo*
 CCGraph::FindNode(void* aPtr)
 {
--- a/xpcom/ds/nsAtomTable.cpp
+++ b/xpcom/ds/nsAtomTable.cpp
@@ -238,17 +238,17 @@ class nsAtomSubTable
   friend class nsAtomTable;
   Mutex mLock;
   PLDHashTable mTable;
   nsAtomSubTable();
   void GCLocked(GCKind aKind);
   void AddSizeOfExcludingThisLocked(MallocSizeOf aMallocSizeOf,
                                     AtomsSizes& aSizes);
 
-  AtomTableEntry* Search(AtomTableKey& aKey)
+  AtomTableEntry* Search(AtomTableKey& aKey) const
   {
     mLock.AssertCurrentThreadOwns();
     return static_cast<AtomTableEntry*>(mTable.Search(&aKey));
   }
 
   AtomTableEntry* Add(AtomTableKey& aKey)
   {
     mLock.AssertCurrentThreadOwns();
--- a/xpcom/ds/nsStaticNameTable.cpp
+++ b/xpcom/ds/nsStaticNameTable.cpp
@@ -158,30 +158,30 @@ nsStaticCaseInsensitiveNameTable::~nsSta
   for (uint32_t index = 0; index < mNameTable.EntryCount(); index++) {
     mNameArray[index].~nsDependentCString();
   }
   free((void*)mNameArray);
   MOZ_COUNT_DTOR(nsStaticCaseInsensitiveNameTable);
 }
 
 int32_t
-nsStaticCaseInsensitiveNameTable::Lookup(const nsACString& aName)
+nsStaticCaseInsensitiveNameTable::Lookup(const nsACString& aName) const
 {
   NS_ASSERTION(mNameArray, "not inited");
 
   const nsCString& str = PromiseFlatCString(aName);
 
   NameTableKey key(mNameArray, &str);
   auto entry = static_cast<NameTableEntry*>(mNameTable.Search(&key));
 
   return entry ? entry->mIndex : nsStaticCaseInsensitiveNameTable::NOT_FOUND;
 }
 
 int32_t
-nsStaticCaseInsensitiveNameTable::Lookup(const nsAString& aName)
+nsStaticCaseInsensitiveNameTable::Lookup(const nsAString& aName) const
 {
   NS_ASSERTION(mNameArray, "not inited");
 
   const nsString& str = PromiseFlatString(aName);
 
   NameTableKey key(mNameArray, &str);
   auto entry = static_cast<NameTableEntry*>(mNameTable.Search(&key));
 
--- a/xpcom/ds/nsStaticNameTable.h
+++ b/xpcom/ds/nsStaticNameTable.h
@@ -28,18 +28,18 @@
  *    as long as this table object - typically a static string array.
  */
 
 class nsStaticCaseInsensitiveNameTable
 {
 public:
   enum { NOT_FOUND = -1 };
 
-  int32_t          Lookup(const nsACString& aName);
-  int32_t          Lookup(const nsAString& aName);
+  int32_t          Lookup(const nsACString& aName) const;
+  int32_t          Lookup(const nsAString& aName) const;
   const nsCString& GetStringValue(int32_t aIndex);
 
   nsStaticCaseInsensitiveNameTable(const char* const aNames[], int32_t aLength);
   ~nsStaticCaseInsensitiveNameTable();
 
 private:
   nsDependentCString*   mNameArray;
   PLDHashTable          mNameTable;
--- a/xpcom/ds/nsTHashtable.h
+++ b/xpcom/ds/nsTHashtable.h
@@ -127,17 +127,17 @@ public:
    * Get the entry associated with a key.
    * @param     aKey the key to retrieve
    * @return    pointer to the entry class, if the key exists; nullptr if the
    *            key doesn't exist
    */
   EntryType* GetEntry(KeyType aKey) const
   {
     return static_cast<EntryType*>(
-      const_cast<PLDHashTable*>(&mTable)->Search(EntryType::KeyToPointer(aKey)));
+      mTable.Search(EntryType::KeyToPointer(aKey)));
   }
 
   /**
    * Return true if an entry for the given key exists, false otherwise.
    * @param     aKey the key to retrieve
    * @return    true if the key exists, false if the key doesn't exist
    */
   bool Contains(KeyType aKey) const { return !!GetEntry(aKey); }