Bug 1303275 - Fix ShmemPool code to resolve 'not declared' compiler error. r=gcp
authorJulian Hector <julian.r.hector@gmail.com>
Fri, 16 Sep 2016 06:44:00 -0400
changeset 355615 a4754f5f3a007f666f5319046ece2c18c6b546e3
parent 355614 5471f6af44ac807761a1ca0f3c786a37e1a045e7
child 355616 32bb28f7009253b367e4e9bbcda234f0f92c1e58
push id6570
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:26:13 +0000
treeherdermozilla-beta@f455459b2ae5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgcp
bugs1303275
milestone51.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 1303275 - Fix ShmemPool code to resolve 'not declared' compiler error. r=gcp
dom/media/systemservices/ShmemPool.cpp
dom/media/systemservices/ShmemPool.h
--- a/dom/media/systemservices/ShmemPool.cpp
+++ b/dom/media/systemservices/ShmemPool.cpp
@@ -4,22 +4,16 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/Assertions.h"
 #include "mozilla/Logging.h"
 #include "mozilla/ShmemPool.h"
 #include "mozilla/Move.h"
 
-#undef LOG
-#undef LOG_ENABLED
-extern mozilla::LazyLogModule gCamerasParentLog;
-#define LOG(args) MOZ_LOG(gCamerasParentLog, mozilla::LogLevel::Debug, args)
-#define LOG_ENABLED() MOZ_LOG_TEST(gCamerasParentLog, mozilla::LogLevel::Debug)
-
 namespace mozilla {
 
 ShmemPool::ShmemPool(size_t aPoolSize)
   : mMutex("mozilla::ShmemPool"),
     mPoolFree(aPoolSize)
 #ifdef DEBUG
     ,mMaxPoolUse(0)
 #endif
@@ -57,94 +51,30 @@ mozilla::ShmemBuffer ShmemPool::GetIfAva
   if (poolUse > mMaxPoolUse) {
     mMaxPoolUse = poolUse;
     LOG(("Maximum ShmemPool use increased: %d buffers", mMaxPoolUse));
   }
 #endif
   return Move(res);
 }
 
-template <class T>
-mozilla::ShmemBuffer ShmemPool::Get(T* aInstance, size_t aSize)
-{
-  MutexAutoLock lock(mMutex);
-
-  // Pool is empty, don't block caller.
-  if (mPoolFree == 0) {
-    // This isn't initialized, so will be understood as an error.
-    return ShmemBuffer();
-  }
-
-  ShmemBuffer& res = mShmemPool[mPoolFree - 1];
-
-  if (!res.mInitialized) {
-    LOG(("Initializing new Shmem in pool"));
-    if (!aInstance->AllocShmem(aSize, SharedMemory::TYPE_BASIC, &res.mShmem)) {
-      LOG(("Failure allocating new Shmem buffer"));
-      return ShmemBuffer();
-    }
-    res.mInitialized = true;
-  }
-
-  MOZ_ASSERT(res.mShmem.IsWritable(), "Shmem in Pool is not writable?");
-
-  // Prepare buffer, increase size if needed (we never shrink as we don't
-  // maintain seperate sized pools and we don't want to keep reallocating)
-  if (res.mShmem.Size<char>() < aSize) {
-    LOG(("Size change/increase in Shmem Pool"));
-    aInstance->DeallocShmem(res.mShmem);
-    res.mInitialized = false;
-    // this may fail; always check return value
-    if (!aInstance->AllocShmem(aSize, SharedMemory::TYPE_BASIC, &res.mShmem)) {
-      LOG(("Failure allocating resized Shmem buffer"));
-      return ShmemBuffer();
-    } else {
-      res.mInitialized = true;
-    }
-  }
-
-  MOZ_ASSERT(res.mShmem.IsWritable(), "Shmem in Pool is not writable post resize?");
-
-  mPoolFree--;
-#ifdef DEBUG
-  size_t poolUse = mShmemPool.Length() - mPoolFree;
-  if (poolUse > mMaxPoolUse) {
-    mMaxPoolUse = poolUse;
-    LOG(("Maximum ShmemPool use increased: %d buffers", mMaxPoolUse));
-  }
-#endif
-  return Move(res);
-}
-
 void ShmemPool::Put(ShmemBuffer&& aShmem)
 {
   MutexAutoLock lock(mMutex);
   MOZ_ASSERT(mPoolFree < mShmemPool.Length());
   mShmemPool[mPoolFree] = Move(aShmem);
   mPoolFree++;
 #ifdef DEBUG
   size_t poolUse = mShmemPool.Length() - mPoolFree;
   if (poolUse > 0) {
     LOG(("ShmemPool usage reduced to %d buffers", poolUse));
   }
 #endif
 }
 
-template <class T>
-void ShmemPool::Cleanup(T* aInstance)
-{
-  MutexAutoLock lock(mMutex);
-  for (size_t i = 0; i < mShmemPool.Length(); i++) {
-    if (mShmemPool[i].mInitialized) {
-      aInstance->DeallocShmem(mShmemPool[i].Get());
-      mShmemPool[i].mInitialized = false;
-    }
-  }
-}
-
 ShmemPool::~ShmemPool()
 {
 #ifdef DEBUG
   for (size_t i = 0; i < mShmemPool.Length(); i++) {
     MOZ_ASSERT(!mShmemPool[i].Valid());
   }
 #endif
 }
--- a/dom/media/systemservices/ShmemPool.h
+++ b/dom/media/systemservices/ShmemPool.h
@@ -5,16 +5,22 @@
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_ShmemPool_h
 #define mozilla_ShmemPool_h
 
 #include "mozilla/ipc/Shmem.h"
 #include "mozilla/Mutex.h"
 
+#undef LOG
+#undef LOG_ENABLED
+extern mozilla::LazyLogModule gCamerasParentLog;
+#define LOG(args) MOZ_LOG(gCamerasParentLog, mozilla::LogLevel::Debug, args)
+#define LOG_ENABLED() MOZ_LOG_TEST(gCamerasParentLog, mozilla::LogLevel::Debug)
+
 namespace mozilla {
 
 class ShmemPool;
 
 class ShmemBuffer {
 public:
   ShmemBuffer() : mInitialized(false) {}
   explicit ShmemBuffer(mozilla::ipc::Shmem aShmem) {
@@ -56,24 +62,86 @@ private:
   bool mInitialized;
   mozilla::ipc::Shmem mShmem;
 };
 
 class ShmemPool {
 public:
   explicit ShmemPool(size_t aPoolSize);
   ~ShmemPool();
+  // Get/GetIfAvailable differ in what thread they can run on. GetIfAvailable
+  // can run anywhere but won't allocate if the right size isn't available.
+  ShmemBuffer GetIfAvailable(size_t aSize);
+  void Put(ShmemBuffer&& aShmem);
+
   // We need to use the allocation/deallocation functions
   // of a specific IPC child/parent instance.
-  template <class T> void Cleanup(T* aInstance);
-  // These 2 differ in what thread they can run on. GetIfAvailable
-  // can run anywhere but won't allocate if the right size isn't available.
-  ShmemBuffer GetIfAvailable(size_t aSize);
-  template <class T> ShmemBuffer Get(T* aInstance, size_t aSize);
-  void Put(ShmemBuffer&& aShmem);
+  template <class T>
+  void Cleanup(T* aInstance)
+  {
+    MutexAutoLock lock(mMutex);
+    for (size_t i = 0; i < mShmemPool.Length(); i++) {
+      if (mShmemPool[i].mInitialized) {
+        aInstance->DeallocShmem(mShmemPool[i].Get());
+        mShmemPool[i].mInitialized = false;
+      }
+    }
+  }
+
+  template <class T>
+  ShmemBuffer Get(T* aInstance, size_t aSize)
+  {
+    MutexAutoLock lock(mMutex);
+
+    // Pool is empty, don't block caller.
+    if (mPoolFree == 0) {
+      // This isn't initialized, so will be understood as an error.
+      return ShmemBuffer();
+    }
+
+    ShmemBuffer& res = mShmemPool[mPoolFree - 1];
+
+    if (!res.mInitialized) {
+      LOG(("Initializing new Shmem in pool"));
+      if (!aInstance->AllocShmem(aSize, ipc::SharedMemory::TYPE_BASIC, &res.mShmem)) {
+        LOG(("Failure allocating new Shmem buffer"));
+        return ShmemBuffer();
+      }
+      res.mInitialized = true;
+    }
+
+    MOZ_ASSERT(res.mShmem.IsWritable(), "Shmem in Pool is not writable?");
+
+    // Prepare buffer, increase size if needed (we never shrink as we don't
+    // maintain seperate sized pools and we don't want to keep reallocating)
+    if (res.mShmem.Size<char>() < aSize) {
+      LOG(("Size change/increase in Shmem Pool"));
+      aInstance->DeallocShmem(res.mShmem);
+      res.mInitialized = false;
+      // this may fail; always check return value
+      if (!aInstance->AllocShmem(aSize, ipc::SharedMemory::TYPE_BASIC, &res.mShmem)) {
+        LOG(("Failure allocating resized Shmem buffer"));
+        return ShmemBuffer();
+      } else {
+        res.mInitialized = true;
+      }
+    }
+
+    MOZ_ASSERT(res.mShmem.IsWritable(), "Shmem in Pool is not writable post resize?");
+
+    mPoolFree--;
+#ifdef DEBUG
+    size_t poolUse = mShmemPool.Length() - mPoolFree;
+    if (poolUse > mMaxPoolUse) {
+      mMaxPoolUse = poolUse;
+      LOG(("Maximum ShmemPool use increased: %d buffers", mMaxPoolUse));
+    }
+#endif
+    return Move(res);
+  }
 
 private:
   Mutex mMutex;
   size_t mPoolFree;
 #ifdef DEBUG
   size_t mMaxPoolUse;
 #endif
   nsTArray<ShmemBuffer> mShmemPool;