Bug 605362, part 5: Centralize more of the accounting. r=joe
authorChris Jones <jones.chris.g@gmail.com>
Fri, 05 Nov 2010 02:17:07 -0500
changeset 56910 ffb9040a013b85de49d83fe36d8b52609932b0b9
parent 56909 e390c6cf96b1adffdfef04298d6ea95e82073b25
child 56911 0aa98eae87ed8a874b75ab692c7afa8c2b8f1010
push idunknown
push userunknown
push dateunknown
reviewersjoe
bugs605362
milestone2.0b8pre
Bug 605362, part 5: Centralize more of the accounting. r=joe
ipc/glue/SharedMemory.cpp
ipc/glue/SharedMemory.h
ipc/glue/SharedMemoryBasic_android.cpp
ipc/glue/SharedMemoryBasic_android.h
ipc/glue/SharedMemoryBasic_chromium.h
ipc/glue/SharedMemorySysV.h
--- a/ipc/glue/SharedMemory.cpp
+++ b/ipc/glue/SharedMemory.cpp
@@ -58,16 +58,18 @@ NS_MEMORY_REPORTER_IMPLEMENT(ShmemAlloca
                              nsnull)
 NS_MEMORY_REPORTER_IMPLEMENT(ShmemMapped,
                              "shmem/mapped",
                              "Shmem bytes mapped into address space",
                              GetShmemMapped,
                              nsnull)
 
 SharedMemory::SharedMemory()
+  : mAllocSize(0)
+  , mMappedSize(0)
 {
   // NB: SharedMemory is main-thread-only at the moment, but that may
   // change soon
   static bool registered;
   if (!registered) {
     NS_RegisterMemoryReporter(new NS_MEMORY_REPORTER_NAME(ShmemAllocated));
     NS_RegisterMemoryReporter(new NS_MEMORY_REPORTER_NAME(ShmemMapped));
     registered = true;
@@ -77,38 +79,42 @@ SharedMemory::SharedMemory()
 /*static*/ size_t
 SharedMemory::PageAlignedSize(size_t aSize)
 {
   size_t pageSize = SystemPageSize();
   size_t nPagesNeeded = size_t(ceil(double(aSize) / double(pageSize)));
   return pageSize * nPagesNeeded;
 }
 
-/*static*/ void
+void
 SharedMemory::Created(size_t aNBytes)
 {
-  gShmemAllocated += aNBytes;
+  mAllocSize = aNBytes;
+  gShmemAllocated += mAllocSize;
 }
 
-/*static*/ void
+void
 SharedMemory::Mapped(size_t aNBytes)
 {
-  gShmemMapped += aNBytes;
+  mMappedSize = aNBytes;
+  gShmemMapped += mMappedSize;
+}
+
+void
+SharedMemory::Unmapped()
+{
+  NS_ABORT_IF_FALSE(gShmemMapped >= PRInt64(mMappedSize),
+                    "Can't unmap more than mapped");
+  gShmemMapped -= mMappedSize;
+  mMappedSize = 0;
 }
 
 /*static*/ void
-SharedMemory::Unmapped(size_t aNBytes)
+SharedMemory::Destroyed()
 {
-  NS_ABORT_IF_FALSE(gShmemMapped >= PRInt64(aNBytes),
-                    "Can't unmap more than mapped");
-  gShmemMapped -= aNBytes;
-}
-
-/*static*/ void
-SharedMemory::Destroyed(size_t aNBytes)
-{
-  NS_ABORT_IF_FALSE(gShmemAllocated >= PRInt64(aNBytes),
+  NS_ABORT_IF_FALSE(gShmemAllocated >= PRInt64(mAllocSize),
                     "Can't destroy more than allocated");
-  gShmemAllocated -= aNBytes;
+  gShmemAllocated -= mAllocSize;
+  mAllocSize = 0;
 }
 
 } // namespace ipc
 } // namespace mozilla
--- a/ipc/glue/SharedMemory.h
+++ b/ipc/glue/SharedMemory.h
@@ -62,19 +62,19 @@ class SharedMemory
 {
 public:
   enum SharedMemoryType {
     TYPE_BASIC,
     TYPE_SYSV,
     TYPE_UNKNOWN
   };
 
-  virtual ~SharedMemory() { }
+  virtual ~SharedMemory() { Unmapped(); Destroyed(); }
 
-  virtual size_t Size() const = 0;
+  size_t Size() const { return mMappedSize; }
 
   virtual void* memory() const = 0;
 
   virtual bool Create(size_t size) = 0;
   virtual bool Map(size_t nBytes) = 0;
 
   virtual SharedMemoryType Type() const = 0;
 
@@ -110,19 +110,28 @@ protected:
 
   // Implementations should call these methods on shmem usage changes,
   // but *only if* the OS-specific calls are known to have succeeded.
   // The methods are expected to be called in the pattern
   //
   //   Created (Mapped Unmapped)* Destroy
   //
   // but this isn't checked.
-  static void Created(size_t aNBytes);
-  static void Mapped(size_t aNBytes);
-  static void Unmapped(size_t aNBytes);
-  static void Destroyed(size_t aNBytes);
+  void Created(size_t aNBytes);
+  void Mapped(size_t aNBytes);
+  void Unmapped();
+  void Destroyed();
+
+  // The size of the shmem region requested in Create(), if
+  // successful.  SharedMemorys that are opened from a foreign handle
+  // have an alloc size of 0, even though they have access to the
+  // alloc-size information.
+  size_t mAllocSize;
+  // The size of the region mapped in Map(), if successful.  All
+  // SharedMemorys that are mapped have a non-zero mapped size.
+  size_t mMappedSize;
 };
 
 } // namespace ipc
 } // namespace mozilla
 
 
 #endif // ifndef mozilla_ipc_SharedMemory_h
--- a/ipc/glue/SharedMemoryBasic_android.cpp
+++ b/ipc/glue/SharedMemoryBasic_android.cpp
@@ -65,25 +65,21 @@ 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();
   Destroy();
 }
@@ -102,17 +98,16 @@ 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;
   Created(aNbytes);
   return true;
 }
 
 bool
 SharedMemoryBasic::Map(size_t nBytes)
 {
   NS_ABORT_IF_FALSE(nsnull == mMemory, "Already Map()d");
@@ -123,17 +118,16 @@ SharedMemoryBasic::Map(size_t nBytes)
                  mShmFd,
                  0);
   if (MAP_FAILED == mMemory) {
     LogError("ShmemAndroid::Map()");
     mMemory = nsnull;
     return false;
   }
 
-  mSize = nBytes;
   Mapped(nBytes);
   return true;
 }
 
 bool
 SharedMemoryBasic::ShareToProcess(base::ProcessHandle/*unused*/,
                                   Handle* aNewHandle)
 {
@@ -152,29 +146,24 @@ SharedMemoryBasic::ShareToProcess(base::
 
 void
 SharedMemoryBasic::Unmap()
 {
   if (!mMemory) {
     return;
   }
 
-  if (munmap(mMemory, mSize)) {
+  if (munmap(mMemory, Size())) {
     LogError("ShmemAndroid::Unmap()");
   }
   mMemory = nsnull;
-  mSize = 0;
-  Unmapped(mSize);
 }
 
 void
 SharedMemoryBasic::Destroy()
 {
   if (mShmFd > 0) {
     close(mShmFd);
-    if (mAllocSize) {
-      Destroyed(mAllocSize);
-    }
   }
 }
 
 } // namespace ipc
 } // namespace mozilla
--- a/ipc/glue/SharedMemoryBasic_android.h
+++ b/ipc/glue/SharedMemoryBasic_android.h
@@ -66,22 +66,16 @@ public:
 
   NS_OVERRIDE
   virtual bool Create(size_t aNbytes);
 
   NS_OVERRIDE
   virtual bool Map(size_t nBytes);
 
   NS_OVERRIDE
-  virtual size_t Size() const
-  {
-    return mSize;
-  }
-
-  NS_OVERRIDE
   virtual void* memory() const
   {
     return mMemory;
   }
 
   NS_OVERRIDE
   virtual SharedMemoryType Type() const
   {
@@ -102,20 +96,16 @@ public:
                       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
 
 #endif // ifndef mozilla_ipc_SharedMemoryBasic_android_h
--- a/ipc/glue/SharedMemoryBasic_chromium.h
+++ b/ipc/glue/SharedMemoryBasic_chromium.h
@@ -54,66 +54,45 @@ namespace mozilla {
 namespace ipc {
 
 class SharedMemoryBasic : public SharedMemory
 {
 public:
   typedef base::SharedMemoryHandle Handle;
 
   SharedMemoryBasic()
-    : mAllocSize(0)
-    , mSize(0)
   {
   }
 
   SharedMemoryBasic(const Handle& aHandle)
     : mSharedMemory(aHandle, false)
-    , mAllocSize(0)
-    , mSize(0)
   {
   }
 
-  virtual ~SharedMemoryBasic() {
-    if (memory()) {
-      Unmapped(mSize);
-    }
-    if (mAllocSize) {
-      Destroyed(mAllocSize);
-    }
-  }
-
   NS_OVERRIDE
   virtual bool Create(size_t aNbytes)
   {
     bool ok = mSharedMemory.Create("", false, false, aNbytes);
     if (ok) {
-      mAllocSize = aNbytes;
       Created(aNbytes);
     }
     return ok;
   }
 
   NS_OVERRIDE
   virtual bool Map(size_t nBytes)
   {
     bool ok = mSharedMemory.Map(nBytes);
     if (ok) {
-      mSize = nBytes;
       Mapped(nBytes);
     }
     return ok;
   }
 
   NS_OVERRIDE
-  virtual size_t Size() const
-  {
-    return mSize;
-  }
-
-  NS_OVERRIDE
   virtual void* memory() const
   {
     return mSharedMemory.memory();
   }
 
   NS_OVERRIDE
   virtual SharedMemoryType Type() const
   {
@@ -137,18 +116,15 @@ 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
 
 
 #endif // ifndef mozilla_ipc_SharedMemoryBasic_chromium_h
--- a/ipc/glue/SharedMemorySysV.h
+++ b/ipc/glue/SharedMemorySysV.h
@@ -69,43 +69,31 @@ namespace ipc {
 
 class SharedMemorySysV : public SharedMemory
 {
 public:
   typedef int Handle;
 
   SharedMemorySysV() :
     mHandle(-1),
-    mData(nsnull),
-    mAllocSize(0),
-    mSize(0)
+    mData(nsnull)
   {
   }
 
   SharedMemorySysV(Handle aHandle) :
     mHandle(aHandle),
-    mData(nsnull),
-    mAllocSize(0),
-    mSize(0)
+    mData(nsnull)
   {
   }
 
   virtual ~SharedMemorySysV()
   {
-    if (memory()) {
-      Unmapped(mSize);
-    }
-    if (mAllocSize) {
-      Destroyed(mAllocSize);
-    }
-
     shmdt(mData);
     mHandle = -1;
     mData = nsnull;
-    mSize = 0;
   }
 
   NS_OVERRIDE
   virtual bool Create(size_t aNbytes)
   {
     int id = shmget(IPC_PRIVATE, aNbytes, IPC_CREAT | 0600);
     if (id == -1)
       return false;
@@ -138,38 +126,31 @@ public:
       return false;
     }
 
     // Mark the handle as deleted so that, should this process go away, the
     // segment is cleaned up.
     shmctl(mHandle, IPC_RMID, 0);
 
     mData = mem;
-    mSize = nBytes;
 
 #ifdef NS_DEBUG
     struct shmid_ds info;
     if (shmctl(mHandle, IPC_STAT, &info) < 0)
       return false;
 
     NS_ABORT_IF_FALSE(nBytes <= info.shm_segsz,
                       "Segment doesn't have enough space!");
 #endif
 
     Mapped(nBytes);
     return true;
   }
 
   NS_OVERRIDE
-  virtual size_t Size() const
-  {
-    return mSize;
-  }
-
-  NS_OVERRIDE
   virtual void* memory() const
   {
     return mData;
   }
 
   NS_OVERRIDE
   virtual SharedMemoryType Type() const
   {
@@ -190,18 +171,16 @@ 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
 
 #endif // ifndef mozilla_ipc_SharedMemorySysV_h