Bug 112262. Move nsMemory into the glue library. sr=rpotts, sr=jband.

This commit is contained in:
dougt%netscape.com 2001-12-07 19:54:16 +00:00
parent cfde953c9f
commit 5cca684efc
14 changed files with 374 additions and 81 deletions

View File

@ -1,6 +1,5 @@
nsAgg.h
nsIAllocator.h
nsMemory.h
nsCom.h
nsComObsolete.h
nsCWeakReference.h

View File

@ -57,7 +57,6 @@ endif
EXPORTS = \
nsAgg.h \
nsIAllocator.h \
nsMemory.h \
nsCom.h \
nsComObsolete.h \
nsCWeakReference.h \

View File

@ -36,7 +36,6 @@ include <$(DEPTH)\config\config.mak>
EXPORTS = \
nsAgg.h \
nsIAllocator.h \
nsMemory.h \
nsCom.h \
nsComObsolete.h \
nsCWeakReference.h \

View File

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

View File

@ -45,8 +45,6 @@
#include "nsCOMPtr.h"
#include "plevent.h"
#define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1"
struct PRLock;
class MemoryFlusher;

View File

@ -44,6 +44,7 @@ DEFFILE = xpcom_alpha.def
LINCS = \
-I..\base \
-I..\glue \
-I..\ds \
-I..\io \
-I..\components \

View File

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

View File

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

View File

@ -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<nsIMemory> memory = getter_AddRefs(nsMemory::GetGlobalMemoryService());
nsCOMPtr<nsIMemory> 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;
}

View File

@ -1 +1,2 @@
nsCOMPtr.h
nsMemory.h

View File

@ -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 = \

View File

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

135
xpcom/glue/nsMemory.cpp Normal file
View File

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

75
xpcom/glue/nsMemory.h Normal file
View File

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