1999-01-27 05:02:22 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* 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/
|
1999-01-27 05:02:22 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* 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.
|
1999-01-27 05:02:22 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-01-27 05:02:22 +03:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-12-01 02:36:32 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
1999-01-27 05:02:22 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Implementation of nsIAllocator using NSPR
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "nsAllocator.h"
|
1999-04-02 03:12:49 +04:00
|
|
|
#include <string.h> /* for memcpy */
|
1999-01-27 05:02:22 +03:00
|
|
|
|
1999-04-02 10:17:28 +04:00
|
|
|
nsAllocatorImpl::nsAllocatorImpl(nsISupports* outer)
|
1999-01-27 05:02:22 +03:00
|
|
|
{
|
|
|
|
NS_INIT_AGGREGATED(outer);
|
|
|
|
}
|
|
|
|
|
1999-04-02 10:17:28 +04:00
|
|
|
nsAllocatorImpl::~nsAllocatorImpl(void)
|
1999-01-27 05:02:22 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-04-02 10:17:28 +04:00
|
|
|
NS_IMPL_AGGREGATED(nsAllocatorImpl);
|
1999-01-27 05:02:22 +03:00
|
|
|
|
|
|
|
NS_METHOD
|
1999-04-02 10:17:28 +04:00
|
|
|
nsAllocatorImpl::AggregatedQueryInterface(const nsIID& aIID, void** aInstancePtr)
|
1999-01-27 05:02:22 +03:00
|
|
|
{
|
1999-08-19 09:36:30 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aInstancePtr);
|
|
|
|
|
1999-12-01 02:36:32 +03:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsISupports)))
|
1999-08-19 09:36:30 +04:00
|
|
|
*aInstancePtr = GetInner();
|
1999-12-01 02:36:32 +03:00
|
|
|
else if (aIID.Equals(NS_GET_IID(nsIAllocator)))
|
1999-08-19 09:36:30 +04:00
|
|
|
*aInstancePtr = NS_STATIC_CAST(nsIAllocator*, this);
|
|
|
|
else {
|
|
|
|
*aInstancePtr = nsnull;
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF((nsISupports*)*aInstancePtr);
|
|
|
|
return NS_OK;
|
1999-01-27 05:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
1999-04-02 10:17:28 +04:00
|
|
|
nsAllocatorImpl::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
|
1999-01-27 05:02:22 +03:00
|
|
|
{
|
1999-08-19 09:36:30 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aInstancePtr);
|
|
|
|
NS_ENSURE_PROPER_AGGREGATION(outer, aIID);
|
|
|
|
|
1999-04-02 10:17:28 +04:00
|
|
|
nsAllocatorImpl* mm = new nsAllocatorImpl(outer);
|
1999-01-27 05:02:22 +03:00
|
|
|
if (mm == NULL)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
1999-08-19 09:36:30 +04:00
|
|
|
|
|
|
|
nsresult rv = mm->AggregatedQueryInterface(aIID, aInstancePtr);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
delete mm;
|
|
|
|
return rv;
|
1999-01-27 05:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_METHOD_(void*)
|
1999-04-02 10:17:28 +04:00
|
|
|
nsAllocatorImpl::Alloc(PRUint32 size)
|
1999-01-27 05:02:22 +03:00
|
|
|
{
|
|
|
|
return PR_Malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD_(void*)
|
1999-05-07 10:22:11 +04:00
|
|
|
nsAllocatorImpl::Realloc(void* ptr, PRUint32 size)
|
1999-01-27 05:02:22 +03:00
|
|
|
{
|
|
|
|
return PR_Realloc(ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
1999-05-07 10:22:11 +04:00
|
|
|
nsAllocatorImpl::Free(void* ptr)
|
1999-01-27 05:02:22 +03:00
|
|
|
{
|
|
|
|
PR_Free(ptr);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
1999-04-02 10:17:28 +04:00
|
|
|
nsAllocatorImpl::HeapMinimize(void)
|
1999-01-27 05:02:22 +03:00
|
|
|
{
|
|
|
|
#ifdef XP_MAC
|
|
|
|
// This used to live in the memory allocators no Mac, but does no more
|
|
|
|
// Needs to be hooked up in the new world.
|
|
|
|
// CallCacheFlushers(0x7fffffff);
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-05-26 05:38:36 +04:00
|
|
|
#if 0
|
1999-01-27 05:02:22 +03:00
|
|
|
nsAllocatorFactory::nsAllocatorFactory(void)
|
|
|
|
{
|
1999-05-01 02:54:28 +04:00
|
|
|
NS_INIT_REFCNT();
|
1999-01-27 05:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAllocatorFactory::~nsAllocatorFactory(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-08-23 14:14:16 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsAllocatorFactory, nsIFactory)
|
1999-01-27 05:02:22 +03:00
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsAllocatorFactory::CreateInstance(nsISupports *aOuter,
|
|
|
|
REFNSIID aIID,
|
|
|
|
void **aResult)
|
|
|
|
{
|
1999-04-02 10:17:28 +04:00
|
|
|
return nsAllocatorImpl::Create(aOuter, aIID, aResult);
|
1999-01-27 05:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsAllocatorFactory::LockFactory(PRBool aLock)
|
|
|
|
{
|
|
|
|
return NS_OK; // XXX what?
|
|
|
|
}
|
1999-05-26 05:38:36 +04:00
|
|
|
#endif
|
1999-01-27 05:02:22 +03:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-04-02 03:12:49 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Public shortcuts to the shared allocator's methods
|
|
|
|
* (all these methods are class statics)
|
|
|
|
*/
|
|
|
|
|
|
|
|
// public:
|
1999-04-02 10:17:28 +04:00
|
|
|
void* nsAllocator::Alloc(PRUint32 size)
|
1999-04-02 03:12:49 +04:00
|
|
|
{
|
|
|
|
if(!EnsureAllocator()) return NULL;
|
|
|
|
return mAllocator->Alloc(size);
|
|
|
|
}
|
|
|
|
|
1999-04-02 10:17:28 +04:00
|
|
|
void* nsAllocator::Realloc(void* ptr, PRUint32 size)
|
1999-04-02 03:12:49 +04:00
|
|
|
{
|
|
|
|
if(!EnsureAllocator()) return NULL;
|
|
|
|
return mAllocator->Realloc(ptr, size);
|
|
|
|
}
|
|
|
|
|
1999-04-02 10:17:28 +04:00
|
|
|
void nsAllocator::Free(void* ptr)
|
1999-04-02 03:12:49 +04:00
|
|
|
{
|
|
|
|
if(!EnsureAllocator()) return;
|
|
|
|
mAllocator->Free(ptr);
|
|
|
|
}
|
|
|
|
|
1999-04-02 10:17:28 +04:00
|
|
|
void nsAllocator::HeapMinimize()
|
1999-04-02 03:12:49 +04:00
|
|
|
{
|
|
|
|
if(!EnsureAllocator()) return;
|
|
|
|
mAllocator->HeapMinimize();
|
|
|
|
}
|
|
|
|
|
1999-04-02 10:17:28 +04:00
|
|
|
void* nsAllocator::Clone(const void* ptr, PRUint32 size)
|
1999-04-02 03:12:49 +04:00
|
|
|
{
|
|
|
|
if(!ptr || !EnsureAllocator()) return NULL;
|
|
|
|
void* p = mAllocator->Alloc(size);
|
|
|
|
if(p) memcpy(p, ptr, size);
|
|
|
|
return p;
|
1999-08-10 23:54:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_EXPORT nsIAllocator*
|
|
|
|
nsAllocator::GetGlobalAllocator()
|
|
|
|
{
|
|
|
|
if(!EnsureAllocator()) return nsnull;
|
|
|
|
NS_ADDREF(mAllocator);
|
|
|
|
return mAllocator;
|
|
|
|
}
|
1999-04-02 03:12:49 +04:00
|
|
|
|
|
|
|
// private:
|
|
|
|
|
1999-04-02 10:17:28 +04:00
|
|
|
nsIAllocator* nsAllocator::mAllocator = NULL;
|
1999-04-02 03:12:49 +04:00
|
|
|
|
1999-04-02 10:17:28 +04:00
|
|
|
PRBool nsAllocator::FetchAllocator()
|
1999-04-02 03:12:49 +04:00
|
|
|
{
|
1999-12-01 02:36:32 +03:00
|
|
|
nsAllocatorImpl::Create(NULL, NS_GET_IID(nsIAllocator), (void**)&mAllocator);
|
1999-04-02 03:12:49 +04:00
|
|
|
NS_ASSERTION(mAllocator, "failed to get Allocator!");
|
1999-11-19 04:14:07 +03:00
|
|
|
return (mAllocator ? PR_TRUE : PR_FALSE);
|
1999-04-02 03:12:49 +04:00
|
|
|
}
|