From 0788be3644724b8c05d86614734eb3dccc7c2025 Mon Sep 17 00:00:00 2001 From: "warren%netscape.com" Date: Fri, 30 Apr 1999 22:54:28 +0000 Subject: [PATCH] Added size argument to Free and Realloc (for implementations that can't determine the memory block's size. Needed for nsPageMgr. Hooked allocator up to xpcom initialization. git-svn-id: svn://10.0.0.236/trunk@29899 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/xpcom/base/nsAllocator.cpp | 6 +++-- mozilla/xpcom/base/nsAllocator.h | 8 ++++-- mozilla/xpcom/base/nsIAllocator.h | 11 ++++++-- mozilla/xpcom/components/nsServiceManager.cpp | 25 +++++++++++++------ mozilla/xpcom/public/nsIAllocator.h | 11 ++++++-- mozilla/xpcom/src/nsAllocator.cpp | 6 +++-- mozilla/xpcom/src/nsAllocator.h | 8 ++++-- mozilla/xpcom/src/nsServiceManager.cpp | 25 +++++++++++++------ 8 files changed, 74 insertions(+), 26 deletions(-) diff --git a/mozilla/xpcom/base/nsAllocator.cpp b/mozilla/xpcom/base/nsAllocator.cpp index 1983b36414b..bc86dd87a67 100644 --- a/mozilla/xpcom/base/nsAllocator.cpp +++ b/mozilla/xpcom/base/nsAllocator.cpp @@ -78,13 +78,14 @@ nsAllocatorImpl::Alloc(PRUint32 size) } NS_METHOD_(void*) -nsAllocatorImpl::Realloc(void* ptr, PRUint32 size) +nsAllocatorImpl::Realloc(void* ptr, PRUint32 size, + PRInt32 oldSize) { return PR_Realloc(ptr, size); } NS_METHOD -nsAllocatorImpl::Free(void* ptr) +nsAllocatorImpl::Free(void* ptr, PRInt32 size) { PR_Free(ptr); return NS_OK; @@ -105,6 +106,7 @@ nsAllocatorImpl::HeapMinimize(void) nsAllocatorFactory::nsAllocatorFactory(void) { + NS_INIT_REFCNT(); } nsAllocatorFactory::~nsAllocatorFactory(void) diff --git a/mozilla/xpcom/base/nsAllocator.h b/mozilla/xpcom/base/nsAllocator.h index 8d0baad2b7c..e7277c8df76 100644 --- a/mozilla/xpcom/base/nsAllocator.h +++ b/mozilla/xpcom/base/nsAllocator.h @@ -45,16 +45,20 @@ public: * * @param ptr - the block of memory to reallocate * @param size - the new size + * @param oldSize - the current size of the block. If -1 (the default), + * the implementation must be able to determine the block size by + * examining the block pointer. * @result the rellocated block of memory */ - NS_IMETHOD_(void*) Realloc(void* ptr, PRUint32 size); + NS_IMETHOD_(void*) Realloc(void* ptr, PRUint32 size, + PRInt32 oldSize = -1); /** * Frees a block of memory. * * @param ptr - the block of memory to free */ - NS_IMETHOD Free(void* ptr); + NS_IMETHOD Free(void* ptr, PRInt32 size = -1); /** * Attempts to shrink the heap. diff --git a/mozilla/xpcom/base/nsIAllocator.h b/mozilla/xpcom/base/nsIAllocator.h index 8ff1d729326..0d32d9fbeea 100644 --- a/mozilla/xpcom/base/nsIAllocator.h +++ b/mozilla/xpcom/base/nsIAllocator.h @@ -51,16 +51,23 @@ public: * * @param ptr - the block of memory to reallocate * @param size - the new size + * @param oldSize - the current size of the block. If -1 (the default), + * the implementation must be able to determine the block size by + * examining the block pointer. * @result the rellocated block of memory */ - NS_IMETHOD_(void*) Realloc(void* ptr, PRUint32 size) = 0; + NS_IMETHOD_(void*) Realloc(void* ptr, PRUint32 size, + PRInt32 oldSize = -1) = 0; /** * Frees a block of memory. * * @param ptr - the block of memory to free + * @param size - the size of the block to be freed. If -1 (the default), + * the implementation must be able to determine the block size by + * examining the block pointer. */ - NS_IMETHOD Free(void* ptr) = 0; + NS_IMETHOD Free(void* ptr, PRInt32 size = -1) = 0; /** * Attempts to shrink the heap. diff --git a/mozilla/xpcom/components/nsServiceManager.cpp b/mozilla/xpcom/components/nsServiceManager.cpp index 10510f9d8b6..521608db547 100644 --- a/mozilla/xpcom/components/nsServiceManager.cpp +++ b/mozilla/xpcom/components/nsServiceManager.cpp @@ -536,9 +536,12 @@ nsServiceManager::UnregisterService(const char* aProgID) // // - There exists no global Registry. Registry can be created from the component manager. // + #include "nsComponentManager.h" #include "nsIRegistry.h" #include "nsXPComCIID.h" +#include "nsAllocator.h" + extern "C" NS_EXPORT nsresult NS_RegistryGetFactory(nsISupports* servMgr, nsIFactory** aFactory); @@ -579,17 +582,13 @@ nsresult NS_InitXPCOM(nsIServiceManager* *result) } rv = servMgr->RegisterService(kComponentManagerCID, compMgr); - if (NS_FAILED(rv)) - { - return rv; - } - + if (NS_FAILED(rv)) return rv; // 3. Register the RegistryFactory with the component manager so that // clients can create new registry objects. nsIFactory *registryFactory = NULL; rv = NS_RegistryGetFactory(servMgr, ®istryFactory); - if (NS_FAILED(rv)) return (rv); + if (NS_FAILED(rv)) return rv; NS_DEFINE_CID(kRegistryCID, NS_REGISTRY_CID); @@ -597,6 +596,18 @@ nsresult NS_InitXPCOM(nsIServiceManager* *result) NS_REGISTRY_PROGID, registryFactory, PR_TRUE); NS_RELEASE(registryFactory); + if (NS_FAILED(rv)) return rv; - return (rv); + // Register nsAllocator + nsAllocatorFactory* alloc = new nsAllocatorFactory(); + if (alloc == NULL) + return NS_ERROR_OUT_OF_MEMORY; + NS_ADDREF(alloc); + static NS_DEFINE_CID(kAllocatorCID, NS_ALLOCATOR_CID); + rv = compMgr->RegisterFactory(kAllocatorCID, "Malloc/Free Allocator", + NULL, alloc, PR_TRUE); + NS_RELEASE(alloc); + if (NS_FAILED(rv)) return rv; + + return rv; } diff --git a/mozilla/xpcom/public/nsIAllocator.h b/mozilla/xpcom/public/nsIAllocator.h index 8ff1d729326..0d32d9fbeea 100644 --- a/mozilla/xpcom/public/nsIAllocator.h +++ b/mozilla/xpcom/public/nsIAllocator.h @@ -51,16 +51,23 @@ public: * * @param ptr - the block of memory to reallocate * @param size - the new size + * @param oldSize - the current size of the block. If -1 (the default), + * the implementation must be able to determine the block size by + * examining the block pointer. * @result the rellocated block of memory */ - NS_IMETHOD_(void*) Realloc(void* ptr, PRUint32 size) = 0; + NS_IMETHOD_(void*) Realloc(void* ptr, PRUint32 size, + PRInt32 oldSize = -1) = 0; /** * Frees a block of memory. * * @param ptr - the block of memory to free + * @param size - the size of the block to be freed. If -1 (the default), + * the implementation must be able to determine the block size by + * examining the block pointer. */ - NS_IMETHOD Free(void* ptr) = 0; + NS_IMETHOD Free(void* ptr, PRInt32 size = -1) = 0; /** * Attempts to shrink the heap. diff --git a/mozilla/xpcom/src/nsAllocator.cpp b/mozilla/xpcom/src/nsAllocator.cpp index 1983b36414b..bc86dd87a67 100644 --- a/mozilla/xpcom/src/nsAllocator.cpp +++ b/mozilla/xpcom/src/nsAllocator.cpp @@ -78,13 +78,14 @@ nsAllocatorImpl::Alloc(PRUint32 size) } NS_METHOD_(void*) -nsAllocatorImpl::Realloc(void* ptr, PRUint32 size) +nsAllocatorImpl::Realloc(void* ptr, PRUint32 size, + PRInt32 oldSize) { return PR_Realloc(ptr, size); } NS_METHOD -nsAllocatorImpl::Free(void* ptr) +nsAllocatorImpl::Free(void* ptr, PRInt32 size) { PR_Free(ptr); return NS_OK; @@ -105,6 +106,7 @@ nsAllocatorImpl::HeapMinimize(void) nsAllocatorFactory::nsAllocatorFactory(void) { + NS_INIT_REFCNT(); } nsAllocatorFactory::~nsAllocatorFactory(void) diff --git a/mozilla/xpcom/src/nsAllocator.h b/mozilla/xpcom/src/nsAllocator.h index 8d0baad2b7c..e7277c8df76 100644 --- a/mozilla/xpcom/src/nsAllocator.h +++ b/mozilla/xpcom/src/nsAllocator.h @@ -45,16 +45,20 @@ public: * * @param ptr - the block of memory to reallocate * @param size - the new size + * @param oldSize - the current size of the block. If -1 (the default), + * the implementation must be able to determine the block size by + * examining the block pointer. * @result the rellocated block of memory */ - NS_IMETHOD_(void*) Realloc(void* ptr, PRUint32 size); + NS_IMETHOD_(void*) Realloc(void* ptr, PRUint32 size, + PRInt32 oldSize = -1); /** * Frees a block of memory. * * @param ptr - the block of memory to free */ - NS_IMETHOD Free(void* ptr); + NS_IMETHOD Free(void* ptr, PRInt32 size = -1); /** * Attempts to shrink the heap. diff --git a/mozilla/xpcom/src/nsServiceManager.cpp b/mozilla/xpcom/src/nsServiceManager.cpp index 10510f9d8b6..521608db547 100644 --- a/mozilla/xpcom/src/nsServiceManager.cpp +++ b/mozilla/xpcom/src/nsServiceManager.cpp @@ -536,9 +536,12 @@ nsServiceManager::UnregisterService(const char* aProgID) // // - There exists no global Registry. Registry can be created from the component manager. // + #include "nsComponentManager.h" #include "nsIRegistry.h" #include "nsXPComCIID.h" +#include "nsAllocator.h" + extern "C" NS_EXPORT nsresult NS_RegistryGetFactory(nsISupports* servMgr, nsIFactory** aFactory); @@ -579,17 +582,13 @@ nsresult NS_InitXPCOM(nsIServiceManager* *result) } rv = servMgr->RegisterService(kComponentManagerCID, compMgr); - if (NS_FAILED(rv)) - { - return rv; - } - + if (NS_FAILED(rv)) return rv; // 3. Register the RegistryFactory with the component manager so that // clients can create new registry objects. nsIFactory *registryFactory = NULL; rv = NS_RegistryGetFactory(servMgr, ®istryFactory); - if (NS_FAILED(rv)) return (rv); + if (NS_FAILED(rv)) return rv; NS_DEFINE_CID(kRegistryCID, NS_REGISTRY_CID); @@ -597,6 +596,18 @@ nsresult NS_InitXPCOM(nsIServiceManager* *result) NS_REGISTRY_PROGID, registryFactory, PR_TRUE); NS_RELEASE(registryFactory); + if (NS_FAILED(rv)) return rv; - return (rv); + // Register nsAllocator + nsAllocatorFactory* alloc = new nsAllocatorFactory(); + if (alloc == NULL) + return NS_ERROR_OUT_OF_MEMORY; + NS_ADDREF(alloc); + static NS_DEFINE_CID(kAllocatorCID, NS_ALLOCATOR_CID); + rv = compMgr->RegisterFactory(kAllocatorCID, "Malloc/Free Allocator", + NULL, alloc, PR_TRUE); + NS_RELEASE(alloc); + if (NS_FAILED(rv)) return rv; + + return rv; }