Make console service init handle out-of-memory. Bug 281093, patch by Daniel de

Wildt <dewildt@gmail.com>, r+sr=bzbarsky
This commit is contained in:
bzbarsky%mit.edu 2005-03-02 16:37:53 +00:00
Родитель 9f42f8a2e4
Коммит e4fa0ae687
3 изменённых файлов: 23 добавлений и 15 удалений

Просмотреть файл

@ -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;

Просмотреть файл

@ -52,6 +52,7 @@ class nsConsoleService : public nsIConsoleService
{
public:
nsConsoleService();
nsresult Init();
NS_DECL_ISUPPORTS
NS_DECL_NSICONSOLESERVICE

Просмотреть файл

@ -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)