Bug 1223808 - part 2 - use UniquePtr<uint8_t[]> instead of nsAutoArrayPtr<SHA1Sum::Hash> in HandleHashKey; r=michal
authorNathan Froyd <froydnj@mozilla.com>
Wed, 18 Nov 2015 15:56:47 -0500
changeset 309479 37997f6e43494a557ade83c7713ea59f78b65087
parent 309478 689d9f449560c540ba39d9c7c346c6666c3fb480
child 309480 cc847f227d9a2736a62ec10b3c2dc147b69c6d34
push id5513
push userraliiev@mozilla.com
push dateMon, 25 Jan 2016 13:55:34 +0000
treeherdermozilla-beta@5ee97dd05b5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmichal
bugs1223808
milestone45.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1223808 - part 2 - use UniquePtr<uint8_t[]> instead of nsAutoArrayPtr<SHA1Sum::Hash> in HandleHashKey; r=michal
netwerk/cache2/CacheFileIOManager.cpp
netwerk/cache2/CacheFileIOManager.h
--- a/netwerk/cache2/CacheFileIOManager.cpp
+++ b/netwerk/cache2/CacheFileIOManager.cpp
@@ -315,17 +315,17 @@ CacheFileHandles::HandleHashKey::AssertH
 #endif
 
 size_t
 CacheFileHandles::HandleHashKey::SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const
 {
   MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
 
   size_t n = 0;
-  n += mallocSizeOf(mHash);
+  n += mallocSizeOf(mHash.get());
   for (uint32_t i = 0; i < mHandles.Length(); ++i) {
     n += mHandles[i]->SizeOfIncludingThis(mallocSizeOf);
   }
 
   return n;
 }
 
 /******************************************************************************
--- a/netwerk/cache2/CacheFileIOManager.h
+++ b/netwerk/cache2/CacheFileIOManager.h
@@ -137,61 +137,68 @@ public:
   {
   public:
     typedef const SHA1Sum::Hash& KeyType;
     typedef const SHA1Sum::Hash* KeyTypePointer;
 
     explicit HandleHashKey(KeyTypePointer aKey)
     {
       MOZ_COUNT_CTOR(HandleHashKey);
-      mHash = (SHA1Sum::Hash*)new uint8_t[SHA1Sum::kHashSize];
-      memcpy(mHash, aKey, sizeof(SHA1Sum::Hash));
+      mHash = MakeUnique<uint8_t[]>(SHA1Sum::kHashSize);
+      memcpy(mHash.get(), aKey, sizeof(SHA1Sum::Hash));
     }
     HandleHashKey(const HandleHashKey& aOther)
     {
       NS_NOTREACHED("HandleHashKey copy constructor is forbidden!");
     }
     ~HandleHashKey()
     {
       MOZ_COUNT_DTOR(HandleHashKey);
     }
 
     bool KeyEquals(KeyTypePointer aKey) const
     {
-      return memcmp(mHash, aKey, sizeof(SHA1Sum::Hash)) == 0;
+      return memcmp(mHash.get(), aKey, sizeof(SHA1Sum::Hash)) == 0;
     }
     static KeyTypePointer KeyToPointer(KeyType aKey)
     {
       return &aKey;
     }
     static PLDHashNumber HashKey(KeyTypePointer aKey)
     {
       return (reinterpret_cast<const uint32_t *>(aKey))[0];
     }
 
     void AddHandle(CacheFileHandle* aHandle);
     void RemoveHandle(CacheFileHandle* aHandle);
     already_AddRefed<CacheFileHandle> GetNewestHandle();
     void GetHandles(nsTArray<RefPtr<CacheFileHandle> > &aResult);
 
-    SHA1Sum::Hash *Hash() const { return mHash; }
+    SHA1Sum::Hash *Hash() const
+    {
+      return reinterpret_cast<SHA1Sum::Hash*>(mHash.get());
+    }
     bool IsEmpty() const { return mHandles.Length() == 0; }
 
     enum { ALLOW_MEMMOVE = true };
 
 #ifdef DEBUG
     void AssertHandlesState();
 #endif
 
     // Memory reporting
     size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
     size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
 
   private:
-    nsAutoArrayPtr<SHA1Sum::Hash> mHash;
+    // We can't make this UniquePtr<SHA1Sum::Hash>, because you can't have
+    // UniquePtrs with known bounds.  So we settle for this representation
+    // and using appropriate casts when we need to access it as a
+    // SHA1Sum::Hash.
+    UniquePtr<uint8_t[]> mHash;
     // Use weak pointers since the hash table access is on a single thread
     // only and CacheFileHandle removes itself from this table in its dtor
     // that may only be called on the same thread as we work with the hashtable
     // since we dispatch its Release() to this thread.
     nsTArray<CacheFileHandle*> mHandles;
   };
 
 private: