Bug 605362, part 5: Centralize more of the accounting. r=joe

This commit is contained in:
Chris Jones 2010-11-05 02:17:07 -05:00
parent 9119868099
commit 3a6c74c8d5
6 changed files with 35 additions and 86 deletions

View File

@ -63,6 +63,8 @@ NS_MEMORY_REPORTER_IMPLEMENT(ShmemMapped,
nsnull)
SharedMemory::SharedMemory()
: mAllocSize(0)
, mMappedSize(0)
{
// NB: SharedMemory is main-thread-only at the moment, but that may
// change soon
@ -82,32 +84,36 @@ SharedMemory::PageAlignedSize(size_t aSize)
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;
}
/*static*/ void
SharedMemory::Unmapped(size_t aNBytes)
void
SharedMemory::Unmapped()
{
NS_ABORT_IF_FALSE(gShmemMapped >= PRInt64(aNBytes),
NS_ABORT_IF_FALSE(gShmemMapped >= PRInt64(mMappedSize),
"Can't unmap more than mapped");
gShmemMapped -= aNBytes;
gShmemMapped -= mMappedSize;
mMappedSize = 0;
}
/*static*/ void
SharedMemory::Destroyed(size_t aNBytes)
SharedMemory::Destroyed()
{
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

View File

@ -67,9 +67,9 @@ public:
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;
@ -115,10 +115,19 @@ protected:
// 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

View File

@ -70,15 +70,11 @@ LogError(const char* what)
SharedMemoryBasic::SharedMemoryBasic()
: mShmFd(-1)
, mAllocSize(0)
, mSize(0)
, mMemory(nsnull)
{ }
SharedMemoryBasic::SharedMemoryBasic(const Handle& aHandle)
: mShmFd(aHandle.fd)
, mAllocSize(0)
, mSize(0)
, mMemory(nsnull)
{ }
@ -107,7 +103,6 @@ SharedMemoryBasic::Create(size_t aNbytes)
}
mShmFd = shmfd;
mAllocSize = aNbytes;
Created(aNbytes);
return true;
}
@ -128,7 +123,6 @@ SharedMemoryBasic::Map(size_t nBytes)
return false;
}
mSize = nBytes;
Mapped(nBytes);
return true;
}
@ -157,12 +151,10 @@ SharedMemoryBasic::Unmap()
return;
}
if (munmap(mMemory, mSize)) {
if (munmap(mMemory, Size())) {
LogError("ShmemAndroid::Unmap()");
}
mMemory = nsnull;
mSize = 0;
Unmapped(mSize);
}
void
@ -170,9 +162,6 @@ SharedMemoryBasic::Destroy()
{
if (mShmFd > 0) {
close(mShmFd);
if (mAllocSize) {
Destroyed(mAllocSize);
}
}
}

View File

@ -70,12 +70,6 @@ public:
NS_OVERRIDE
virtual bool Map(size_t nBytes);
NS_OVERRIDE
virtual size_t Size() const
{
return mSize;
}
NS_OVERRIDE
virtual void* memory() const
{
@ -107,10 +101,6 @@ private:
// 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;
};

View File

@ -59,33 +59,19 @@ 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;
@ -96,18 +82,11 @@ public:
{
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
{
@ -142,9 +121,6 @@ public:
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

View File

@ -74,33 +74,21 @@ public:
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
@ -143,7 +131,6 @@ public:
shmctl(mHandle, IPC_RMID, 0);
mData = mem;
mSize = nBytes;
#ifdef NS_DEBUG
struct shmid_ds info;
@ -158,12 +145,6 @@ public:
return true;
}
NS_OVERRIDE
virtual size_t Size() const
{
return mSize;
}
NS_OVERRIDE
virtual void* memory() const
{
@ -195,8 +176,6 @@ public:
private:
Handle mHandle;
void* mData;
size_t mAllocSize;
size_t mSize;
};
} // namespace ipc