Bug 605362, part 2: Track allocated sizes (in the allocating process) in all shmem backends. r=joe
authorChris Jones <jones.chris.g@gmail.com>
Fri, 05 Nov 2010 02:17:07 -0500
changeset 56907 62bbf0aed452646c77fcf9074c31545cc37154e7
parent 56906 f5e82d1422e6a32df20373b16533c9868cdbf679
child 56908 e25e8b0bb4d0929468b0fd595571c51ecd2a8564
push id16725
push usercjones@mozilla.com
push dateFri, 05 Nov 2010 07:21:46 +0000
treeherdermozilla-central@0aa98eae87ed [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjoe
bugs605362
milestone2.0b8pre
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 605362, part 2: Track allocated sizes (in the allocating process) in all shmem backends. r=joe
ipc/glue/SharedMemoryBasic_android.cpp
ipc/glue/SharedMemoryBasic_android.h
ipc/glue/SharedMemoryBasic_chromium.h
ipc/glue/SharedMemorySysV.h
--- a/ipc/glue/SharedMemoryBasic_android.cpp
+++ b/ipc/glue/SharedMemoryBasic_android.cpp
@@ -65,32 +65,32 @@ static void
 LogError(const char* what)
 {
   __android_log_print(ANDROID_LOG_ERROR, "Gecko",
                       "%s: %s (%d)", what, strerror(errno), errno);
 }
 
 SharedMemoryBasic::SharedMemoryBasic()
   : mShmFd(-1)
+  , mAllocSize(0)
   , mSize(0)
   , mMemory(nsnull)
 { }
 
 SharedMemoryBasic::SharedMemoryBasic(const Handle& aHandle)
   : mShmFd(aHandle.fd)
+  , mAllocSize(0)
   , mSize(0)
   , mMemory(nsnull)
 { }
 
 SharedMemoryBasic::~SharedMemoryBasic()
 {
   Unmap();
-  if (mShmFd > 0) {
-    close(mShmFd);
-  }
+  Destroy();
 }
 
 bool
 SharedMemoryBasic::Create(size_t aNbytes)
 {
   NS_ABORT_IF_FALSE(-1 == mShmFd, "Already Create()d");
 
   // Carve a new instance off of /dev/ashmem
@@ -102,16 +102,17 @@ SharedMemoryBasic::Create(size_t aNbytes
 
   if (ioctl(shmfd, ASHMEM_SET_SIZE, aNbytes)) {
     LogError("ShmemAndroid::Unmap():ioctl(SET_SIZE)");
     close(shmfd);
     return false;
   }
 
   mShmFd = shmfd;
+  mAllocSize = aNbytes;
   return true;
 }
 
 bool
 SharedMemoryBasic::Map(size_t nBytes)
 {
   NS_ABORT_IF_FALSE(nsnull == mMemory, "Already Map()d");
 
@@ -156,10 +157,18 @@ SharedMemoryBasic::Unmap()
 
   if (munmap(mMemory, mSize)) {
     LogError("ShmemAndroid::Unmap()");
   }
   mMemory = nsnull;
   mSize = 0;
 }
 
+void
+SharedMemoryBasic::Destroy()
+{
+  if (mShmFd > 0) {
+    close(mShmFd);
+  }
+}
+
 } // namespace ipc
 } // namespace mozilla
--- a/ipc/glue/SharedMemoryBasic_android.h
+++ b/ipc/glue/SharedMemoryBasic_android.h
@@ -98,19 +98,22 @@ public:
     return aHandle.fd >= 0;
   }
 
   bool ShareToProcess(base::ProcessHandle aProcess,
                       Handle* aNewHandle);
 
 private:
   void Unmap();
+  void Destroy();
 
   // The /dev/ashmem fd we allocate.
   int mShmFd;
+  // Allocated size, 0 if unallocated
+  size_t mAllocSize;
   // Mapped size, 0 if unmapped.
   size_t mSize;
   // Pointer to mapped region, null if unmapped.
   void *mMemory;
 };
 
 } // namespace ipc
 } // namespace mozilla
--- a/ipc/glue/SharedMemoryBasic_chromium.h
+++ b/ipc/glue/SharedMemoryBasic_chromium.h
@@ -53,31 +53,37 @@
 namespace mozilla {
 namespace ipc {
 
 class SharedMemoryBasic : public SharedMemory
 {
 public:
   typedef base::SharedMemoryHandle Handle;
 
-  SharedMemoryBasic() :
-    mSize(0)
+  SharedMemoryBasic()
+    : mAllocSize(0)
+    , mSize(0)
   {
   }
 
-  SharedMemoryBasic(const Handle& aHandle) :
-    mSharedMemory(aHandle, false),
-    mSize(0)
+  SharedMemoryBasic(const Handle& aHandle)
+    : mSharedMemory(aHandle, false)
+    , mAllocSize(0)
+    , mSize(0)
   {
   }
 
   NS_OVERRIDE
   virtual bool Create(size_t aNbytes)
   {
-    return mSharedMemory.Create("", false, false, aNbytes);
+    bool ok = mSharedMemory.Create("", false, false, aNbytes);
+    if (ok) {
+      mAllocSize = aNbytes;
+    }
+    return ok;
   }
 
   NS_OVERRIDE
   virtual bool Map(size_t nBytes)
   {
     bool ok = mSharedMemory.Map(nBytes);
     if (ok)
       mSize = nBytes;
@@ -119,16 +125,17 @@ public:
     bool ret = mSharedMemory.ShareToProcess(process, &handle);
     if (ret)
       *new_handle = handle;
     return ret;
   }
 
 private:
   base::SharedMemory mSharedMemory;
+  size_t mAllocSize;
   // NB: we have to track this because shared_memory_win.cc doesn't
   size_t mSize;
 };
 
 } // namespace ipc
 } // namespace mozilla
 
 
--- a/ipc/glue/SharedMemorySysV.h
+++ b/ipc/glue/SharedMemorySysV.h
@@ -70,23 +70,25 @@ namespace ipc {
 class SharedMemorySysV : public SharedMemory
 {
 public:
   typedef int Handle;
 
   SharedMemorySysV() :
     mHandle(-1),
     mData(nsnull),
+    mAllocSize(0),
     mSize(0)
   {
   }
 
   SharedMemorySysV(Handle aHandle) :
     mHandle(aHandle),
     mData(nsnull),
+    mAllocSize(0),
     mSize(0)
   {
   }
 
   virtual ~SharedMemorySysV()
   {
     shmdt(mData);
     mHandle = -1;
@@ -98,16 +100,17 @@ public:
   NS_OVERRIDE
   virtual bool Create(size_t aNbytes)
   {
     int id = shmget(IPC_PRIVATE, aNbytes, IPC_CREAT | 0600);
     if (id == -1)
       return false;
 
     mHandle = id;
+    mAllocSize = aNbytes;
 
     if (!Map(aNbytes))
       return false;
 
     return true;
   }
 
   NS_OVERRIDE
@@ -182,16 +185,17 @@ public:
   static bool IsHandleValid(Handle aHandle)
   {
     return aHandle != -1;
   }
 
 private:
   Handle mHandle;
   void* mData;
+  size_t mAllocSize;
   size_t mSize;
 };
 
 } // namespace ipc
 } // namespace mozilla
 
 #endif // OS_LINUX