From 28302b98462a88b8726cad992139d64e43acc0e1 Mon Sep 17 00:00:00 2001 From: "bzbarsky%mit.edu" Date: Wed, 2 Mar 2005 16:37:53 +0000 Subject: [PATCH] Make console service init handle out-of-memory. Bug 281093, patch by Daniel de Wildt , r+sr=bzbarsky --- xpcom/base/nsConsoleService.cpp | 35 ++++++++++++++++++++------------- xpcom/base/nsConsoleService.h | 1 + xpcom/build/nsXPComInit.cpp | 2 +- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/xpcom/base/nsConsoleService.cpp b/xpcom/base/nsConsoleService.cpp index 7e17d07ba26..0f4c1eba0a4 100644 --- a/xpcom/base/nsConsoleService.cpp +++ b/xpcom/base/nsConsoleService.cpp @@ -56,24 +56,12 @@ NS_IMPL_QUERY_INTERFACE1_CI(nsConsoleService, nsIConsoleService) NS_IMPL_CI_INTERFACE_GETTER1(nsConsoleService, nsIConsoleService) nsConsoleService::nsConsoleService() - : mCurrent(0), mFull(PR_FALSE), mListening(PR_FALSE), mLock(nsnull) + : mMessages(nsnull), mCurrent(0), mFull(PR_FALSE), mListening(PR_FALSE), mLock(nsnull) { // XXX grab this from a pref! // hm, but worry about circularity, bc we want to be able to report // prefs errs... mBufferSize = 250; - - // XXX deal with these two allocations by detecting null mLock in factory? - mMessages = (nsIConsoleMessage **) - nsMemory::Alloc(mBufferSize * sizeof(nsIConsoleMessage *)); - - mLock = PR_NewLock(); - - // Array elements should be 0 initially for circular buffer algorithm. - for (PRUint32 i = 0; i < mBufferSize; i++) { - mMessages[i] = nsnull; - } - } nsConsoleService::~nsConsoleService() @@ -94,11 +82,30 @@ nsConsoleService::~nsConsoleService() #endif - nsMemory::Free(mMessages); + if (mMessages) + nsMemory::Free(mMessages); if (mLock) PR_DestroyLock(mLock); } +nsresult +nsConsoleService::Init() +{ + mMessages = (nsIConsoleMessage **) + nsMemory::Alloc(mBufferSize * sizeof(nsIConsoleMessage *)); + if (!mMessages) + return NS_ERROR_OUT_OF_MEMORY; + + // Array elements should be 0 initially for circular buffer algorithm. + memset(mMessages, 0, mBufferSize * sizeof(nsIConsoleMessage *)); + + mLock = PR_NewLock(); + if (!mLock) + return NS_ERROR_OUT_OF_MEMORY; + + return NS_OK; +} + static PRBool PR_CALLBACK snapshot_enum_func(nsHashKey *key, void *data, void* closure) { nsISupportsArray *array = (nsISupportsArray *)closure; diff --git a/xpcom/base/nsConsoleService.h b/xpcom/base/nsConsoleService.h index 59758f84a30..24a6fe7db19 100644 --- a/xpcom/base/nsConsoleService.h +++ b/xpcom/base/nsConsoleService.h @@ -52,6 +52,7 @@ class nsConsoleService : public nsIConsoleService { public: nsConsoleService(); + nsresult Init(); NS_DECL_ISUPPORTS NS_DECL_NSICONSOLESERVICE diff --git a/xpcom/build/nsXPComInit.cpp b/xpcom/build/nsXPComInit.cpp index a826583e3ac..c2bffcc6d49 100644 --- a/xpcom/build/nsXPComInit.cpp +++ b/xpcom/build/nsXPComInit.cpp @@ -176,7 +176,7 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsVoidImpl) NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsInterfacePointerImpl) NS_GENERIC_FACTORY_CONSTRUCTOR(nsArray) -NS_GENERIC_FACTORY_CONSTRUCTOR(nsConsoleService) +NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsConsoleService, Init) NS_DECL_CLASSINFO(nsConsoleService) NS_GENERIC_FACTORY_CONSTRUCTOR(nsAtomService) NS_GENERIC_FACTORY_CONSTRUCTOR(nsExceptionService)