From 5cca684efcf4b4194229ad12cc4b5bbdc4010c12 Mon Sep 17 00:00:00 2001 From: "dougt%netscape.com" Date: Fri, 7 Dec 2001 19:54:16 +0000 Subject: [PATCH] Bug 112262. Move nsMemory into the glue library. sr=rpotts, sr=jband. --- xpcom/base/MANIFEST | 1 - xpcom/base/Makefile.in | 1 - xpcom/base/makefile.win | 1 - xpcom/base/nsMemoryImpl.cpp | 64 ----------------- xpcom/base/nsMemoryImpl.h | 2 - xpcom/build/makefile.win | 1 + xpcom/build/nsXPCOM.h | 21 ++++-- xpcom/build/nsXPCOMPrivate.h | 70 ++++++++++++++++++ xpcom/build/nsXPComInit.cpp | 66 ++++++++++++++--- xpcom/glue/MANIFEST | 1 + xpcom/glue/Makefile.in | 6 ++ xpcom/glue/makefile.win | 11 +++ xpcom/glue/nsMemory.cpp | 135 +++++++++++++++++++++++++++++++++++ xpcom/glue/nsMemory.h | 75 +++++++++++++++++++ 14 files changed, 374 insertions(+), 81 deletions(-) create mode 100644 xpcom/build/nsXPCOMPrivate.h create mode 100644 xpcom/glue/nsMemory.cpp create mode 100644 xpcom/glue/nsMemory.h diff --git a/xpcom/base/MANIFEST b/xpcom/base/MANIFEST index ca67f5dd8825..77259070b013 100644 --- a/xpcom/base/MANIFEST +++ b/xpcom/base/MANIFEST @@ -1,6 +1,5 @@ nsAgg.h nsIAllocator.h -nsMemory.h nsCom.h nsComObsolete.h nsCWeakReference.h diff --git a/xpcom/base/Makefile.in b/xpcom/base/Makefile.in index 91f967b1e6c3..75253db42a8b 100644 --- a/xpcom/base/Makefile.in +++ b/xpcom/base/Makefile.in @@ -57,7 +57,6 @@ endif EXPORTS = \ nsAgg.h \ nsIAllocator.h \ - nsMemory.h \ nsCom.h \ nsComObsolete.h \ nsCWeakReference.h \ diff --git a/xpcom/base/makefile.win b/xpcom/base/makefile.win index 45c54b2acf9e..f830b946d88b 100644 --- a/xpcom/base/makefile.win +++ b/xpcom/base/makefile.win @@ -36,7 +36,6 @@ include <$(DEPTH)\config\config.mak> EXPORTS = \ nsAgg.h \ nsIAllocator.h \ - nsMemory.h \ nsCom.h \ nsComObsolete.h \ nsCWeakReference.h \ diff --git a/xpcom/base/nsMemoryImpl.cpp b/xpcom/base/nsMemoryImpl.cpp index efd6ba5b1473..2ab6bb714280 100644 --- a/xpcom/base/nsMemoryImpl.cpp +++ b/xpcom/base/nsMemoryImpl.cpp @@ -543,67 +543,3 @@ nsMemoryImpl::Shutdown() return NS_OK; } - -//////////////////////////////////////////////////////////////////////////////// -// nsMemory static helper routines - -NS_EXPORT void* -nsMemory::Alloc(PRSize size) -{ - if (gMemory == nsnull) { - EnsureGlobalMemoryService(); - } - return gMemory->Alloc(size); -} - -NS_EXPORT void* -nsMemory::Realloc(void* ptr, PRSize size) -{ - if (gMemory == nsnull) { - EnsureGlobalMemoryService(); - } - return gMemory->Realloc(ptr, size); -} - -NS_EXPORT void -nsMemory::Free(void* ptr) -{ - if (gMemory == nsnull) { - EnsureGlobalMemoryService(); - } - gMemory->Free(ptr); -} - -NS_EXPORT nsresult -nsMemory::HeapMinimize(PRBool aImmediate) -{ - if (gMemory == nsnull) { - EnsureGlobalMemoryService(); - } - return gMemory->HeapMinimize(aImmediate); -} - -NS_EXPORT void* -nsMemory::Clone(const void* ptr, PRSize size) -{ - if (gMemory == nsnull) { - EnsureGlobalMemoryService(); - } - void* newPtr = gMemory->Alloc(size); - if (newPtr) - memcpy(newPtr, ptr, size); - return newPtr; -} - -NS_EXPORT nsIMemory* -nsMemory::GetGlobalMemoryService() -{ - if (gMemory == nsnull) { - EnsureGlobalMemoryService(); - } - NS_ADDREF(gMemory); - return gMemory; -} - -//---------------------------------------------------------------------- - diff --git a/xpcom/base/nsMemoryImpl.h b/xpcom/base/nsMemoryImpl.h index c5c27fc659da..f1625734f1b5 100644 --- a/xpcom/base/nsMemoryImpl.h +++ b/xpcom/base/nsMemoryImpl.h @@ -45,8 +45,6 @@ #include "nsCOMPtr.h" #include "plevent.h" -#define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1" - struct PRLock; class MemoryFlusher; diff --git a/xpcom/build/makefile.win b/xpcom/build/makefile.win index c3faed3ae137..da8f8b0b8867 100644 --- a/xpcom/build/makefile.win +++ b/xpcom/build/makefile.win @@ -44,6 +44,7 @@ DEFFILE = xpcom_alpha.def LINCS = \ -I..\base \ + -I..\glue \ -I..\ds \ -I..\io \ -I..\components \ diff --git a/xpcom/build/nsXPCOM.h b/xpcom/build/nsXPCOM.h index 16f91b51f8ba..82df7e6a6ab9 100644 --- a/xpcom/build/nsXPCOM.h +++ b/xpcom/build/nsXPCOM.h @@ -35,15 +35,15 @@ * * ***** END LICENSE BLOCK ***** */ -#ifndef nsXPComInit_h__ -#define nsXPComInit_h__ +#ifndef nsXPCom_h__ +#define nsXPCom_h__ #include "nscore.h" class nsIServiceManager; class nsIFile; class nsIDirectoryServiceProvider; - +class nsIMemory; /** * Initialises XPCOM. You must call this method before proceeding * to use xpcom. The one exception is that you may call @@ -101,7 +101,7 @@ NS_ShutdownXPCOM(nsIServiceManager* servMgr); * Public Method to access to the service manager. * * @status FROZEN - * @param result Interface pointer to the service + * @param result Interface pointer to the service manager * * @return NS_OK for success; * other error codes indicate a failure during initialisation. @@ -110,4 +110,17 @@ NS_ShutdownXPCOM(nsIServiceManager* servMgr); extern "C" NS_COM nsresult NS_GetServiceManager(nsIServiceManager* *result); +/** + * Public Method to access to the memory manager. See nsIMemory + * + * @status FROZEN + * @param result Interface pointer to the memory manager + * + * @return NS_OK for success; + * other error codes indicate a failure during initialisation. + * + */ +extern "C" NS_COM nsresult +NS_GetMemoryManager(nsIMemory* *result); + #endif diff --git a/xpcom/build/nsXPCOMPrivate.h b/xpcom/build/nsXPCOMPrivate.h new file mode 100644 index 000000000000..4d95c24d74a7 --- /dev/null +++ b/xpcom/build/nsXPCOMPrivate.h @@ -0,0 +1,70 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: NPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Netscape Public License + * Version 1.1 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the NPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the NPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsXPComPrivate_h__ +#define nsXPComPrivate_h__ + +#include "nscore.h" +/** + * Private Method to register an exit routine. This method + * allows you to setup a callback that will be called from + * the NS_ShutdownXPCOM function after all services and + * components have gone away. + * + * This API is for the exclusive use of the xpcom glue library. + * + * @status FROZEN + * @param exitRoutine pointer to user defined callback function + * of type XPCOMExitRoutine. + * @param priority higher priorities are called before lower + * priorities. + * + * @return NS_OK for success; + * other error codes indicate a failure. + * + */ +typedef NS_CALLBACK(XPCOMExitRoutine)(void); + +extern "C" NS_COM nsresult +NS_RegisterXPCOMExitRoutine(XPCOMExitRoutine exitRoutine, PRUint32 priority); + +extern "C" NS_COM nsresult +NS_UnregisterXPCOMExitRoutine(XPCOMExitRoutine exitRoutine); + +#endif + + diff --git a/xpcom/build/nsXPComInit.cpp b/xpcom/build/nsXPComInit.cpp index 6e857b853d52..15189e982e33 100644 --- a/xpcom/build/nsXPComInit.cpp +++ b/xpcom/build/nsXPComInit.cpp @@ -36,6 +36,7 @@ * ***** END LICENSE BLOCK ***** */ #include "nsXPCOM.h" +#include "nsXPCOMPrivate.h" #include "nsIRegistry.h" #include "nscore.h" #include "nsCOMPtr.h" @@ -51,9 +52,7 @@ #include "nsErrorService.h" #include "nsArena.h" #include "nsByteBuffer.h" -#ifdef PAGE_MANAGER -#include "nsPageMgr.h" -#endif + #include "nsSupportsArray.h" #include "nsSupportsPrimitives.h" #include "nsConsoleService.h" @@ -203,6 +202,9 @@ PRBool gXPCOMShuttingDown = PR_FALSE; &NS_CLASSINFO_NAME(Class) } static nsModuleComponentInfo components[] = { +// ugh +#define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1" +#define NS_MEMORY_CLASSNAME "Global Memory Service" COMPONENT(MEMORY, nsMemoryImpl::Create), #define NS_ERRORSERVICE_CLASSNAME NS_ERRORSERVICE_NAME COMPONENT(ERRORSERVICE, nsErrorService::Create), @@ -211,10 +213,6 @@ static nsModuleComponentInfo components[] = { COMPONENT(BYTEBUFFER, ByteBufferImpl::Create), COMPONENT(SCRIPTABLEINPUTSTREAM, nsScriptableInputStream::Create), -#ifdef PAGE_MANAGER - COMPONENT(PAGEMANAGER, nsPageMgr::Create), -#endif - COMPONENT(PROPERTIES, nsProperties::Create), #define NS_PERSISTENTPROPERTIES_CID NS_IPERSISTENTPROPERTIES_CID /* sigh */ @@ -278,6 +276,21 @@ static nsModuleComponentInfo components[] = { const int components_length = sizeof(components) / sizeof(components[0]); +// gMemory will be freed during shutdown. +static nsIMemory* gMemory = nsnull; +nsresult NS_COM NS_GetMemoryManager(nsIMemory* *result) +{ + nsresult rv = NS_OK; + if (!gMemory) + { + rv = nsMemoryImpl::Create(nsnull, + NS_GET_IID(nsIMemory), + (void**)&gMemory); + } + NS_IF_ADDREF(*result = gMemory); + return rv; +} + nsresult NS_COM NS_InitXPCOM(nsIServiceManager* *result, nsIFile* binDirectory) { @@ -373,8 +386,10 @@ nsresult NS_COM NS_InitXPCOM2(nsIServiceManager* *result, } nsIServiceManager *serviceManager = NS_STATIC_CAST(nsIServiceManager*, compMgr); - nsCOMPtr memory = getter_AddRefs(nsMemory::GetGlobalMemoryService()); + + nsCOMPtr memory; + NS_GetMemoryManager(getter_AddRefs(memory)); // dougt - these calls will be moved into a new interface when nsIComponentManager is frozen. rv = compMgr->RegisterService(kMemoryCID, memory); if (NS_FAILED(rv)) return rv; @@ -435,6 +450,34 @@ nsresult NS_COM NS_InitXPCOM2(nsIServiceManager* *result, return rv; } +static nsVoidArray gExitRoutines; +static void CallExitRoutines() +{ + PRInt32 count = gExitRoutines.Count(); + for (PRInt32 i = 0; i < count; i++) { + XPCOMExitRoutine func = (XPCOMExitRoutine) gExitRoutines.ElementAt(i); + func(); + } + gExitRoutines.Clear(); +} + +nsresult NS_COM +NS_RegisterXPCOMExitRoutine(XPCOMExitRoutine exitRoutine, PRUint32 priority) +{ + // priority are not used right now. It will need to be implemented as more + // classes are moved into the glue library --dougt + PRBool okay = gExitRoutines.AppendElement((void*)exitRoutine); + return okay ? NS_OK : NS_ERROR_FAILURE; +} + +nsresult NS_COM +NS_UnregisterXPCOMExitRoutine(XPCOMExitRoutine exitRoutine) +{ + PRBool okay = gExitRoutines.RemoveElement((void*)exitRoutine); + return okay ? NS_OK : NS_ERROR_FAILURE; +} + + // // NS_ShutdownXPCOM() // @@ -535,9 +578,13 @@ nsresult NS_COM NS_ShutdownXPCOM(nsIServiceManager* servMgr) EmptyEnumeratorImpl::Shutdown(); nsMemoryImpl::Shutdown(); + NS_IF_RELEASE(gMemory); + nsThread::Shutdown(); NS_PurgeAtomTable(); + CallExitRoutines(); + #ifdef NS_BUILD_REFCNT_LOGGING nsTraceRefcnt::DumpStatistics(); nsTraceRefcnt::ResetStatistics(); @@ -551,3 +598,6 @@ nsresult NS_COM NS_ShutdownXPCOM(nsIServiceManager* servMgr) return NS_OK; } + + + diff --git a/xpcom/glue/MANIFEST b/xpcom/glue/MANIFEST index 87b025fbcb6e..b0c48157cbf0 100644 --- a/xpcom/glue/MANIFEST +++ b/xpcom/glue/MANIFEST @@ -1 +1,2 @@ nsCOMPtr.h +nsMemory.h diff --git a/xpcom/glue/Makefile.in b/xpcom/glue/Makefile.in index 0e115b50097e..b95e6b29246a 100644 --- a/xpcom/glue/Makefile.in +++ b/xpcom/glue/Makefile.in @@ -34,10 +34,16 @@ REQUIRES = $(NULL) CPPSRCS = \ nsCOMPtr.cpp \ + nsMemory.cpp \ + $(NULL) + +LOCAL_INCLUDES = \ + -I$(srcdir)/../build \ $(NULL) EXPORTS = \ nsCOMPtr.h \ + nsMemory.h \ $(NULL) XPIDLSRCS = \ diff --git a/xpcom/glue/makefile.win b/xpcom/glue/makefile.win index 804e21825793..d03c129edaba 100755 --- a/xpcom/glue/makefile.win +++ b/xpcom/glue/makefile.win @@ -29,6 +29,7 @@ LIBRARY_NAME=xpcomglue EXPORTS = \ nsCOMPtr.h \ + nsMemory.h \ $(NULL) XPIDLSRCS = \ @@ -36,10 +37,17 @@ XPIDLSRCS = \ ################################################################################ + +#For nsXPCOMPrivate.h +LINCS = \ + -I..\build \ + $(NULL) + LCFLAGS = -D_IMPL_NS_COM -D_IMPL_NS_BASE -DWIN32_LEAN_AND_MEAN CPP_OBJS = \ .\$(OBJDIR)\nsCOMPtr.obj \ + .\$(OBJDIR)\nsMemory.obj \ $(NULL) include <$(DEPTH)\config\rules.mak> @@ -49,3 +57,6 @@ libs:: $(LIBRARY) clobber:: rm -f $(DIST)\lib\$(LIBRARY_NAME).lib + + + diff --git a/xpcom/glue/nsMemory.cpp b/xpcom/glue/nsMemory.cpp new file mode 100644 index 000000000000..e26c79e36a20 --- /dev/null +++ b/xpcom/glue/nsMemory.cpp @@ -0,0 +1,135 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: NPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Netscape Public License + * Version 1.1 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the NPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the NPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsCOMPtr.h" +#include "nsMemory.h" +#include "nsIObserver.h" +#include "nsIObserverService.h" +#include "nsIServiceManager.h" + +#include "nsXPCOMPrivate.h" + + + +static nsIMemory* gMemory = nsnull; +static PRBool gHasMemoryShutdown = PR_FALSE; + +static NS_METHOD FreeGlobalMemory(void) +{ + NS_IF_RELEASE(gMemory); + gHasMemoryShutdown = PR_TRUE; + return NS_OK; +} + +#define ENSURE_ALLOCATOR \ + (gMemory ? PR_TRUE : !gHasMemoryShutdown && SetupGlobalMemory()) + +static nsIMemory* +SetupGlobalMemory() +{ + NS_ASSERTION(!gMemory, "bad call"); + NS_GetMemoryManager(&gMemory); + NS_ASSERTION(gMemory, "can't get memory manager!"); + NS_RegisterXPCOMExitRoutine(FreeGlobalMemory, 0); + return gMemory; +} + + +//////////////////////////////////////////////////////////////////////////////// +// nsMemory static helper routines + +NS_EXPORT void* +nsMemory::Alloc(PRSize size) +{ + if (!ENSURE_ALLOCATOR) + return nsnull; + + return gMemory->Alloc(size); +} + +NS_EXPORT void* +nsMemory::Realloc(void* ptr, PRSize size) +{ + if (!ENSURE_ALLOCATOR) + return nsnull; + + return gMemory->Realloc(ptr, size); +} + +NS_EXPORT void +nsMemory::Free(void* ptr) +{ + if (!ENSURE_ALLOCATOR) + return; + + gMemory->Free(ptr); +} + +NS_EXPORT nsresult +nsMemory::HeapMinimize(PRBool aImmediate) +{ + if (!ENSURE_ALLOCATOR) + return NS_ERROR_FAILURE; + + return gMemory->HeapMinimize(aImmediate); +} + +NS_EXPORT void* +nsMemory::Clone(const void* ptr, PRSize size) +{ + if (!ENSURE_ALLOCATOR) + return nsnull; + + void* newPtr = gMemory->Alloc(size); + if (newPtr) + memcpy(newPtr, ptr, size); + return newPtr; +} + +NS_EXPORT nsIMemory* +nsMemory::GetGlobalMemoryService() +{ + if (!ENSURE_ALLOCATOR) + return nsnull; + + nsIMemory* result = gMemory; + NS_IF_ADDREF(result); + return result; +} + +//---------------------------------------------------------------------- + diff --git a/xpcom/glue/nsMemory.h b/xpcom/glue/nsMemory.h new file mode 100644 index 000000000000..b546808a2989 --- /dev/null +++ b/xpcom/glue/nsMemory.h @@ -0,0 +1,75 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: NPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Netscape Public License + * Version 1.1 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the NPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the NPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsMemory_h__ +#define nsMemory_h__ + +#include "nsIMemory.h" + +#define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1" +#define NS_MEMORY_CLASSNAME "Global Memory Service" +#define NS_MEMORY_CID \ +{ /* 30a04e40-38e7-11d4-8cf5-0060b0fc14a3 */ \ + 0x30a04e40, \ + 0x38e7, \ + 0x11d4, \ + {0x8c, 0xf5, 0x00, 0x60, 0xb0, 0xfc, 0x14, 0xa3} \ +} + + +/** + * Static helper routines to manage memory. These routines allow easy access + * to xpcom's built-in (global) nsIMemory implementation, without needing + * to go through the service manager to get it. However this requires clients + * to link with the xpcom DLL. + * + * This class is not threadsafe and is intented for use only on the main + * thread. + */ +class nsMemory +{ +public: + static NS_EXPORT void* Alloc(size_t size); + static NS_EXPORT void* Realloc(void* ptr, size_t size); + static NS_EXPORT void Free(void* ptr); + static NS_EXPORT nsresult HeapMinimize(PRBool aImmediate); + static NS_EXPORT void* Clone(const void* ptr, size_t size); + static NS_EXPORT nsIMemory* GetGlobalMemoryService(); // AddRefs +}; + +#endif // nsMemory_h__ +