Bug 1038966, part 3 - Shmem should use refcounted pointer classes when creating SharedMemory. r=bent

This commit is contained in:
Andrew McCreight 2014-07-25 16:41:25 -07:00
parent 018402f71c
commit c95e487200
3 changed files with 47 additions and 43 deletions

View File

@ -11,7 +11,6 @@
#include "SharedMemoryBasic.h"
#include "SharedMemorySysV.h"
#include "nsAutoPtr.h"
#include "mozilla/unused.h"
@ -111,10 +110,10 @@ public:
#ifdef MOZ_HAVE_SHAREDMEMORYSYSV
static Shmem::SharedMemory*
static already_AddRefed<Shmem::SharedMemory>
CreateSegment(size_t aNBytes, SharedMemorySysV::Handle aHandle)
{
nsAutoPtr<SharedMemory> segment;
nsRefPtr<SharedMemory> segment;
if (SharedMemorySysV::IsHandleValid(aHandle)) {
segment = new SharedMemorySysV(aHandle);
@ -128,15 +127,14 @@ CreateSegment(size_t aNBytes, SharedMemorySysV::Handle aHandle)
if (!segment->Map(aNBytes))
return nullptr;
segment->AddRef();
return segment.forget();
}
#endif
static Shmem::SharedMemory*
static already_AddRefed<Shmem::SharedMemory>
CreateSegment(size_t aNBytes, SharedMemoryBasic::Handle aHandle)
{
nsAutoPtr<SharedMemory> segment;
nsRefPtr<SharedMemory> segment;
if (SharedMemoryBasic::IsHandleValid(aHandle)) {
segment = new SharedMemoryBasic(aHandle);
@ -150,7 +148,6 @@ CreateSegment(size_t aNBytes, SharedMemoryBasic::Handle aHandle)
if (!segment->Map(aNBytes))
return nullptr;
segment->AddRef();
return segment.forget();
}
@ -349,7 +346,7 @@ Shmem::RevokeRights(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead)
}
// static
Shmem::SharedMemory*
already_AddRefed<Shmem::SharedMemory>
Shmem::Alloc(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead,
size_t aNBytes,
SharedMemoryType aType,
@ -360,7 +357,7 @@ Shmem::Alloc(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead,
NS_ABORT_IF_FALSE(!aProtect || !aUnsafe, "protect => !unsafe");
size_t pageSize = SharedMemory::SystemPageSize();
SharedMemory* segment = nullptr;
nsRefPtr<SharedMemory> segment;
// |2*pageSize| is for the front and back sentinel
size_t segmentSize = SharedMemory::PageAlignedSize(aNBytes + 2*pageSize);
@ -398,11 +395,11 @@ Shmem::Alloc(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead,
if (aProtect)
Protect(segment);
return segment;
return segment.forget();
}
// static
Shmem::SharedMemory*
already_AddRefed<Shmem::SharedMemory>
Shmem::OpenExisting(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead,
const IPC::Message& aDescriptor,
id_t* aId,
@ -419,7 +416,7 @@ Shmem::OpenExisting(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead,
if (!ShmemCreated::ReadInfo(&aDescriptor, &iter, aId, &size, &type))
return nullptr;
SharedMemory* segment = nullptr;
nsRefPtr<SharedMemory> segment;
size_t pageSize = SharedMemory::SystemPageSize();
// |2*pageSize| is for the front and back sentinels
size_t segmentSize = SharedMemory::PageAlignedSize(size + 2*pageSize);
@ -460,7 +457,6 @@ Shmem::OpenExisting(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead,
if (size != header->mSize) {
NS_ERROR("Wrong size for this Shmem!");
delete segment;
return nullptr;
}
@ -469,7 +465,7 @@ Shmem::OpenExisting(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead,
if (!header->mUnsafe && aProtect)
Protect(segment);
return segment;
return segment.forget();
}
// static
@ -499,14 +495,14 @@ Shmem::Dealloc(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead,
#else // !defined(DEBUG)
// static
Shmem::SharedMemory*
already_AddRefed<Shmem::SharedMemory>
Shmem::Alloc(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead,
size_t aNBytes,
SharedMemoryType aType,
bool /*unused*/,
bool /*unused*/)
{
SharedMemory *segment = nullptr;
nsRefPtr<SharedMemory> segment;
if (aType == SharedMemory::TYPE_BASIC)
segment = CreateSegment(SharedMemory::PageAlignedSize(aNBytes + sizeof(uint32_t)),
@ -525,11 +521,11 @@ Shmem::Alloc(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead,
*PtrToSize(segment) = static_cast<uint32_t>(aNBytes);
return segment;
return segment.forget();
}
// static
Shmem::SharedMemory*
already_AddRefed<Shmem::SharedMemory>
Shmem::OpenExisting(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead,
const IPC::Message& aDescriptor,
id_t* aId,
@ -545,7 +541,7 @@ Shmem::OpenExisting(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead,
if (!ShmemCreated::ReadInfo(&aDescriptor, &iter, aId, &size, &type))
return nullptr;
SharedMemory* segment = nullptr;
nsRefPtr<SharedMemory> segment;
size_t segmentSize = SharedMemory::PageAlignedSize(size + sizeof(uint32_t));
if (SharedMemory::TYPE_BASIC == type) {
@ -580,11 +576,10 @@ Shmem::OpenExisting(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead,
// this is the only validity check done in non-DEBUG builds
if (size != static_cast<size_t>(*PtrToSize(segment))) {
delete segment;
return nullptr;
}
return segment;
return segment.forget();
}
// static

View File

@ -15,6 +15,7 @@
#include "nscore.h"
#include "nsDebug.h"
#include "nsAutoPtr.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/ipc/SharedMemory.h"
@ -201,7 +202,7 @@ public:
mId = 0;
}
static SharedMemory*
static already_AddRefed<Shmem::SharedMemory>
Alloc(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead,
size_t aNBytes,
SharedMemoryType aType,
@ -230,7 +231,7 @@ public:
// descriptor shared to us by the process that created the
// underlying OS shmem resource. The contents of the descriptor
// depend on the type of SharedMemory that was passed to us.
static SharedMemory*
static already_AddRefed<SharedMemory>
OpenExisting(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead,
const IPC::Message& aDescriptor,
id_t* aId,

View File

@ -320,7 +320,7 @@ def _runtimeAbort(msg):
ExprCall(ExprVar('NS_RUNTIMEABORT'), args=[ msg ]))
def _refptr(T):
return Type('nsAutoPtr', T=T)
return Type('nsRefPtr', T=T)
def _refptrGet(expr):
return ExprCall(ExprSelect(expr, '.', 'get'))
@ -328,6 +328,9 @@ def _refptrGet(expr):
def _refptrForget(expr):
return ExprCall(ExprSelect(expr, '.', 'forget'))
def _refptrTake(expr):
return ExprCall(ExprSelect(expr, '.', 'take'))
def _cxxArrayType(basetype, const=0, ref=0):
return Type('InfallibleTArray', T=basetype, const=const, ref=ref)
@ -3606,7 +3609,7 @@ class _GenerateProtocolActorCode(ipdl.ast.Visitor):
if p.decl.type.isToplevel():
tmpvar = ExprVar('tmp')
register.addstmts([
StmtDecl(Decl(_actorIdType(), tmpvar.name),
p.nextActorIdExpr(self.side)),
@ -3628,17 +3631,19 @@ class _GenerateProtocolActorCode(ipdl.ast.Visitor):
ExprCall(ExprSelect(p.actorMapVar(), '.', 'Remove'),
[ idvar ])))
# SharedMemory* CreateSharedMemory(size, type, bool, id_t*):
# nsAutoPtr<SharedMemory> seg(Shmem::Alloc(size, type, unsafe));
# if (!shmem)
# return null;
# Shmem s(seg, [nextshmemid]);
# Message descriptor;
# if (!s->ShareTo(subprocess, mId, descriptor) ||
# !Send(descriptor))
# return null;
# mShmemMap.Add(seg, id);
# return shmem.forget();
# SharedMemory* CreateSharedMemory(size_t aSize, Type aType, bool aUnsafe, id_t* aId):
# nsRefPtr<SharedMemory> segment(Shmem::Alloc(aSize, aType, aUnsafe));
# if (!segment)
# return nullptr;
# Shmem shmem(segment.get(), [nextshmemid]);
# Message descriptor = shmem.ShareTo(subprocess, mId, descriptor);
# if (!descriptor)
# return nullptr;
# mChannel.Send(descriptor);
# *aId = shmem.Id();
# SharedMemory* rawSegment = segment.get();
# mShmemMap.Add(segment.forget().take(), *aId);
# return rawSegment;
createshmem.addstmt(StmtDecl(
Decl(_refptr(_rawShmemType()), rawvar.name),
initargs=[ _shmemAlloc(sizevar, typevar, unsafevar) ]))
@ -3667,12 +3672,15 @@ class _GenerateProtocolActorCode(ipdl.ast.Visitor):
args=[ descriptorvar ])))
createshmem.addstmt(failif)
rawsegmentvar = ExprVar('rawSegment')
createshmem.addstmts([
StmtExpr(ExprAssn(ExprDeref(idvar), _shmemId(shmemvar))),
StmtDecl(Decl(_rawShmemType(ptr=1), rawsegmentvar.name),
init=_refptrGet(rawvar)),
StmtExpr(ExprCall(
ExprSelect(p.shmemMapVar(), '.', 'AddWithID'),
args=[ rawvar, ExprDeref(idvar) ])),
StmtReturn(_refptrForget(rawvar))
args=[ _refptrTake(_refptrForget(rawvar)), ExprDeref(idvar) ])),
StmtReturn(rawsegmentvar)
])
# SharedMemory* AdoptSharedMemory(SharedMemory*, id_t*):
@ -4123,8 +4131,8 @@ class _GenerateProtocolActorCode(ipdl.ast.Visitor):
def genShmemCreatedHandler(self):
p = self.protocol
assert p.decl.type.isToplevel()
case = StmtBlock()
case = StmtBlock()
rawvar = ExprVar('rawmem')
idvar = ExprVar('id')
@ -4141,7 +4149,7 @@ class _GenerateProtocolActorCode(ipdl.ast.Visitor):
failif,
StmtExpr(ExprCall(
ExprSelect(p.shmemMapVar(), '.', 'AddWithID'),
args=[ _refptrForget(rawvar), idvar ])),
args=[ _refptrTake(_refptrForget(rawvar)), idvar ])),
Whitespace.NL,
StmtReturn(_Result.Processed)
])
@ -4151,8 +4159,8 @@ class _GenerateProtocolActorCode(ipdl.ast.Visitor):
def genShmemDestroyedHandler(self):
p = self.protocol
assert p.decl.type.isToplevel()
case = StmtBlock()
case = StmtBlock()
rawvar = ExprVar('rawmem')
idvar = ExprVar('id')