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.

This commit is contained in:
warren%netscape.com 1999-04-30 22:54:28 +00:00
parent 4ceccb4a80
commit dbb21cf3e6
8 changed files with 74 additions and 26 deletions

View File

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

View File

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

View File

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

View File

@ -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, &registryFactory);
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;
}

View File

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

View File

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

View File

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

View File

@ -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, &registryFactory);
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;
}