Bug 728171 - Use Scoped.h throughout the code. r=cjones

This commit is contained in:
David Rajchenbach-Teller 2012-04-12 12:21:24 +02:00
parent 5261b387f7
commit 457f40baba
11 changed files with 74 additions and 92 deletions

View File

@ -558,9 +558,11 @@ PluginModuleParent::RecvBackUpXResources(const FileDescriptor& aXSocketFd)
#ifndef MOZ_X11
NS_RUNTIMEABORT("This message only makes sense on X11 platforms");
#else
NS_ABORT_IF_FALSE(0 > mPluginXSocketFdDup.mFd,
NS_ABORT_IF_FALSE(0 > mPluginXSocketFdDup.get(),
"Already backed up X resources??");
mPluginXSocketFdDup.mFd = aXSocketFd.fd;
int fd = aXSocketFd.fd; // Copy to discard |const| qualifier
mPluginXSocketFdDup.forget();
mPluginXSocketFdDup.reset(fd);
#endif
return true;
}

View File

@ -52,6 +52,8 @@
# error Unknown toolkit
#endif
#include "mozilla/Scoped.h"
#include "gfxCore.h"
#include "nsDebug.h"
@ -85,38 +87,14 @@ XVisualIDToInfo(Display* aDisplay, VisualID aVisualID,
* Invoke XFree() on a pointer to memory allocated by Xlib (if the
* pointer is nonnull) when this class goes out of scope.
*/
template<typename T>
struct ScopedXFree
template <typename T>
struct ScopedXFreePtrTraits
{
ScopedXFree() : mPtr(NULL) {}
ScopedXFree(T* aPtr) : mPtr(aPtr) {}
~ScopedXFree() { Assign(NULL); }
ScopedXFree& operator=(T* aPtr) { Assign(aPtr); return *this; }
operator T*() const { return get(); }
T* operator->() const { return get(); }
T* get() const { return mPtr; }
private:
void Assign(T* aPtr)
{
NS_ASSERTION(!mPtr || mPtr != aPtr, "double-XFree() imminent");
if (mPtr)
XFree(mPtr);
mPtr = aPtr;
}
T* mPtr;
// disable these
ScopedXFree(const ScopedXFree&);
ScopedXFree& operator=(const ScopedXFree&);
static void* operator new (size_t);
static void operator delete (void*);
typedef T *type;
static T *empty() { return NULL; }
static void release(T *ptr) { if (ptr!=NULL) XFree(ptr); }
};
SCOPED_TEMPLATE(ScopedXFree, ScopedXFreePtrTraits);
/**
* On construction, set a graceful X error handler that doesn't crash the application and records X errors.

View File

@ -174,7 +174,7 @@ class RilWriteTask : public Task {
};
void RilWriteTask::Run() {
sClient->OnFileCanWriteWithoutBlocking(sClient->mSocket.mFd);
sClient->OnFileCanWriteWithoutBlocking(sClient->mSocket.rwget());
}
static void
@ -205,7 +205,7 @@ RilClient::OpenSocket()
memset(&addr, 0, sizeof(addr));
strcpy(addr.sun_path, RIL_SOCKET_NAME);
addr.sun_family = AF_LOCAL;
mSocket.mFd = socket(AF_LOCAL, SOCK_STREAM, 0);
mSocket.reset(socket(AF_LOCAL, SOCK_STREAM, 0));
alen = strlen(RIL_SOCKET_NAME) + offsetof(struct sockaddr_un, sun_path) + 1;
#else
struct hostent *hp;
@ -219,39 +219,39 @@ RilClient::OpenSocket()
addr.sin_family = hp->h_addrtype;
addr.sin_port = htons(RIL_TEST_PORT);
memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
mSocket.mFd = socket(hp->h_addrtype, SOCK_STREAM, 0);
mSocket.reset(socket(hp->h_addrtype, SOCK_STREAM, 0));
alen = sizeof(addr);
#endif
if (mSocket.mFd < 0) {
if (mSocket.get() < 0) {
LOG("Cannot create socket for RIL!\n");
return false;
}
if (connect(mSocket.mFd, (struct sockaddr *) &addr, alen) < 0) {
if (connect(mSocket.get(), (struct sockaddr *) &addr, alen) < 0) {
#if defined(MOZ_WIDGET_GONK)
LOG("Cannot open socket for RIL!\n");
#endif
close(mSocket.mFd);
mSocket.dispose();
return false;
}
// Set close-on-exec bit.
int flags = fcntl(mSocket.mFd, F_GETFD);
int flags = fcntl(mSocket.get(), F_GETFD);
if (-1 == flags) {
return false;
}
flags |= FD_CLOEXEC;
if (-1 == fcntl(mSocket.mFd, F_SETFD, flags)) {
if (-1 == fcntl(mSocket.get(), F_SETFD, flags)) {
return false;
}
// Select non-blocking IO.
if (-1 == fcntl(mSocket.mFd, F_SETFL, O_NONBLOCK)) {
if (-1 == fcntl(mSocket.get(), F_SETFL, O_NONBLOCK)) {
return false;
}
if (!mIOLoop->WatchFileDescriptor(mSocket.mFd,
if (!mIOLoop->WatchFileDescriptor(mSocket.get(),
true,
MessageLoopForIO::WATCH_READ,
&mReadWatcher,
@ -274,7 +274,7 @@ RilClient::OnFileCanReadWithoutBlocking(int fd)
// data available on the socket
// If so, break;
MOZ_ASSERT(fd == mSocket.mFd);
MOZ_ASSERT(fd == mSocket.get());
while (true) {
if (!mIncoming) {
mIncoming = new RilRawData();
@ -295,7 +295,7 @@ RilClient::OnFileCanReadWithoutBlocking(int fd)
mIncoming.forget();
mReadWatcher.StopWatchingFileDescriptor();
mWriteWatcher.StopWatchingFileDescriptor();
close(mSocket.mFd);
close(mSocket.get());
RilReconnectTask::Enqueue();
return;
}
@ -318,7 +318,7 @@ RilClient::OnFileCanWriteWithoutBlocking(int fd)
// system won't block.
//
MOZ_ASSERT(fd == mSocket.mFd);
MOZ_ASSERT(fd == mSocket.get());
while (!mOutgoingQ.empty() || mCurrentRilRawData != NULL) {
if(!mCurrentRilRawData) {

View File

@ -140,7 +140,7 @@ NS_IMPL_THREADSAFE_RELEASE(nsZipHandle)
nsresult nsZipHandle::Init(nsILocalFile *file, nsZipHandle **ret)
{
mozilla::AutoFDClose fd;
nsresult rv = file->OpenNSPRFileDesc(PR_RDONLY, 0000, &fd);
nsresult rv = file->OpenNSPRFileDesc(PR_RDONLY, 0000, &fd.rwget());
if (NS_FAILED(rv))
return rv;

View File

@ -1352,7 +1352,7 @@ TelemetrySessionData::LoadFromDisk(nsIFile *file, TelemetrySessionData **ptr)
}
AutoFDClose fd;
rv = f->OpenNSPRFileDesc(PR_RDONLY, 0, &fd);
rv = f->OpenNSPRFileDesc(PR_RDONLY, 0, &fd.rwget());
if (NS_FAILED(rv)) {
return NS_ERROR_FAILURE;
}
@ -1439,7 +1439,7 @@ TelemetrySessionData::SaveToDisk(nsIFile *file, const nsACString &uuid)
}
AutoFDClose fd;
rv = f->OpenNSPRFileDesc(PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0600, &fd);
rv = f->OpenNSPRFileDesc(PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0600, &fd.rwget());
if (NS_FAILED(rv)) {
return rv;
}

View File

@ -498,7 +498,8 @@ nsUrlClassifierPrefixSet::LoadFromFile(nsIFile* aFile)
NS_ENSURE_SUCCESS(rv, rv);
AutoFDClose fileFd;
rv = file->OpenNSPRFileDesc(PR_RDONLY | nsILocalFile::OS_READAHEAD, 0, &fileFd);
rv = file->OpenNSPRFileDesc(PR_RDONLY | nsILocalFile::OS_READAHEAD,
0, &fileFd.rwget());
NS_ENSURE_SUCCESS(rv, rv);
return LoadFromFd(fileFd);
@ -556,7 +557,7 @@ nsUrlClassifierPrefixSet::StoreToFile(nsIFile* aFile)
AutoFDClose fileFd;
rv = file->OpenNSPRFileDesc(PR_RDWR | PR_TRUNCATE | PR_CREATE_FILE,
0644, &fileFd);
0644, &fileFd.rwget());
NS_ENSURE_SUCCESS(rv, rv);
MutexAutoLock lock(mPrefixSetLock);

View File

@ -79,10 +79,10 @@ bool
SetGraphicsMode()
{
ScopedClose fd(open("/dev/tty0", O_RDWR | O_SYNC));
if (0 > fd.mFd) {
if (0 > fd.get()) {
// This is non-fatal; post-Cupcake kernels don't have tty0.
LOG("No /dev/tty0?");
} else if (ioctl(fd.mFd, KDSETMODE, (void*) KD_GRAPHICS)) {
} else if (ioctl(fd.get(), KDSETMODE, (void*) KD_GRAPHICS)) {
LOG("Error setting graphics mode on /dev/tty0");
return false;
}
@ -99,32 +99,32 @@ Open(nsIntSize* aScreenSize)
return false;
ScopedClose fd(open("/dev/graphics/fb0", O_RDWR));
if (0 > fd.mFd) {
if (0 > fd.get()) {
LOG("Error opening framebuffer device");
return false;
}
struct fb_fix_screeninfo fi;
if (0 > ioctl(fd.mFd, FBIOGET_FSCREENINFO, &fi)) {
if (0 > ioctl(fd.get(), FBIOGET_FSCREENINFO, &fi)) {
LOG("Error getting fixed screeninfo");
return false;
}
if (0 > ioctl(fd.mFd, FBIOGET_VSCREENINFO, &sVi)) {
if (0 > ioctl(fd.get(), FBIOGET_VSCREENINFO, &sVi)) {
LOG("Error getting variable screeninfo");
return false;
}
sMappedSize = fi.smem_len;
void* mem = mmap(0, sMappedSize, PROT_READ | PROT_WRITE, MAP_SHARED,
fd.mFd, 0);
fd.rwget(), 0);
if (MAP_FAILED == mem) {
LOG("Error mmap'ing framebuffer");
return false;
}
sFd = fd.mFd;
fd.mFd = -1;
sFd = fd.get();
fd.forget();
// The android porting doc requires a /dev/graphics/fb0 device
// that's double buffered with r5g6b5 format. Hence the
@ -155,12 +155,12 @@ GetSize(nsIntSize *aScreenSize) {
return true;
ScopedClose fd(open("/dev/graphics/fb0", O_RDWR));
if (0 > fd.mFd) {
if (0 > fd.get()) {
LOG("Error opening framebuffer device");
return false;
}
if (0 > ioctl(fd.mFd, FBIOGET_VSCREENINFO, &sVi)) {
if (0 > ioctl(fd.get(), FBIOGET_VSCREENINFO, &sVi)) {
LOG("Error getting variable screeninfo");
return false;
}

View File

@ -128,7 +128,7 @@ public:
{
ScopedClose fd(open(kSleepFile, O_RDONLY, 0));
do {
len = read(fd.mFd, &buf, 1);
len = read(fd.get(), &buf, 1);
} while (len < 0 && errno == EINTR);
NS_WARN_IF_FALSE(len >= 0, "WAIT_FOR_FB_SLEEP failed");
NS_DispatchToMainThread(mScreenOffEvent);
@ -137,7 +137,7 @@ public:
{
ScopedClose fd(open(kWakeFile, O_RDONLY, 0));
do {
len = read(fd.mFd, &buf, 1);
len = read(fd.get(), &buf, 1);
} while (len < 0 && errno == EINTR);
NS_WARN_IF_FALSE(len >= 0, "WAIT_FOR_FB_WAKE failed");
NS_DispatchToMainThread(mScreenOnEvent);

View File

@ -293,7 +293,8 @@ NS_IMETHODIMP nsSound::OnStreamComplete(nsIStreamLoader *aLoader,
ScopedCanberraFile canberraFile(tmpFile);
mozilla::AutoFDClose fd;
rv = canberraFile->OpenNSPRFileDesc(PR_WRONLY, PR_IRUSR | PR_IWUSR, &fd);
rv = canberraFile->OpenNSPRFileDesc(PR_WRONLY, PR_IRUSR | PR_IWUSR,
&fd.rwget());
if (NS_FAILED(rv)) {
return rv;
}

View File

@ -141,7 +141,7 @@ nsresult
FileLocation::GetData(Data &data)
{
if (!IsZip()) {
return mBaseFile->OpenNSPRFileDesc(PR_RDONLY, 0444, &data.mFd);
return mBaseFile->OpenNSPRFileDesc(PR_RDONLY, 0444, &data.mFd.rwget());
}
data.mZip = mBaseZip;
if (!data.mZip) {

View File

@ -47,43 +47,43 @@
#endif
#include "prio.h"
#include "mozilla/Scoped.h"
namespace mozilla {
/**
* AutoFDClose is a RAII wrapper for PRFileDesc.
*
* Instances |PR_Close| their fds when they go out of scope.
**/
class AutoFDClose
struct ScopedClosePRFDTraits
{
public:
AutoFDClose(PRFileDesc* fd = nsnull) : mFD(fd) { }
~AutoFDClose() { if (mFD) PR_Close(mFD); }
PRFileDesc* operator= (PRFileDesc *fd) {
if (mFD) PR_Close(mFD);
mFD = fd;
return fd;
}
operator PRFileDesc* () { return mFD; }
PRFileDesc** operator &() { *this = nsnull; return &mFD; }
private:
PRFileDesc *mFD;
};
/**
* Instances close() their fds when they go out of scope.
*/
struct ScopedClose
{
ScopedClose(int aFd=-1) : mFd(aFd) {}
~ScopedClose() {
if (0 <= mFd) {
close(mFd);
typedef PRFileDesc* type;
static type empty() { return NULL; }
static void release(type fd) {
if (fd != NULL) {
PR_Close(fd);
}
}
int mFd;
};
typedef Scoped<ScopedClosePRFDTraits> AutoFDClose;
/**
* ScopedCloseFD is a RAII wrapper for POSIX file descriptors
*
* Instances |close()| their fds when they go out of scope.
*/
struct ScopedCloseFDTraits
{
typedef int type;
static type empty() { return -1; }
static void release(type fd) {
if (fd != -1) {
close(fd);
}
}
};
typedef Scoped<ScopedCloseFDTraits> ScopedClose;
/**
* Fallocate efficiently and continuously allocates files via fallocate-type APIs.