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
Родитель 3ba6867d97
Коммит 28302b9846
3 изменённых файлов: 23 добавлений и 15 удалений

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

@ -56,24 +56,12 @@ NS_IMPL_QUERY_INTERFACE1_CI(nsConsoleService, nsIConsoleService)
NS_IMPL_CI_INTERFACE_GETTER1(nsConsoleService, nsIConsoleService) NS_IMPL_CI_INTERFACE_GETTER1(nsConsoleService, nsIConsoleService)
nsConsoleService::nsConsoleService() 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! // XXX grab this from a pref!
// hm, but worry about circularity, bc we want to be able to report // hm, but worry about circularity, bc we want to be able to report
// prefs errs... // prefs errs...
mBufferSize = 250; 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() nsConsoleService::~nsConsoleService()
@ -94,11 +82,30 @@ nsConsoleService::~nsConsoleService()
#endif #endif
if (mMessages)
nsMemory::Free(mMessages); nsMemory::Free(mMessages);
if (mLock) if (mLock)
PR_DestroyLock(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) static PRBool PR_CALLBACK snapshot_enum_func(nsHashKey *key, void *data, void* closure)
{ {
nsISupportsArray *array = (nsISupportsArray *)closure; nsISupportsArray *array = (nsISupportsArray *)closure;

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

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

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

@ -176,7 +176,7 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsVoidImpl)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsInterfacePointerImpl) NS_GENERIC_FACTORY_CONSTRUCTOR(nsSupportsInterfacePointerImpl)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsArray) NS_GENERIC_FACTORY_CONSTRUCTOR(nsArray)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsConsoleService) NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsConsoleService, Init)
NS_DECL_CLASSINFO(nsConsoleService) NS_DECL_CLASSINFO(nsConsoleService)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsAtomService) NS_GENERIC_FACTORY_CONSTRUCTOR(nsAtomService)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsExceptionService) NS_GENERIC_FACTORY_CONSTRUCTOR(nsExceptionService)