mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-03-03 15:26:07 +00:00
expose directly callable shared allocator methods
This commit is contained in:
parent
6f8c90d095
commit
e01d326056
@ -21,6 +21,8 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "nsAllocator.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include <string.h> /* for memcpy */
|
||||
|
||||
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
||||
static NS_DEFINE_IID(kIAllocatorIID, NS_IALLOCATOR_IID);
|
||||
@ -127,3 +129,55 @@ nsAllocatorFactory::LockFactory(PRBool aLock)
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
* Public shortcuts to the shared allocator's methods
|
||||
* (all these methods are class statics)
|
||||
*/
|
||||
|
||||
// public:
|
||||
void* NSTaskMem::Alloc(PRUint32 size)
|
||||
{
|
||||
if(!EnsureAllocator()) return NULL;
|
||||
return mAllocator->Alloc(size);
|
||||
}
|
||||
|
||||
void* NSTaskMem::Realloc(void* ptr, PRUint32 size)
|
||||
{
|
||||
if(!EnsureAllocator()) return NULL;
|
||||
return mAllocator->Realloc(ptr, size);
|
||||
}
|
||||
|
||||
void NSTaskMem::Free(void* ptr)
|
||||
{
|
||||
if(!EnsureAllocator()) return;
|
||||
mAllocator->Free(ptr);
|
||||
}
|
||||
|
||||
void NSTaskMem::HeapMinimize()
|
||||
{
|
||||
if(!EnsureAllocator()) return;
|
||||
mAllocator->HeapMinimize();
|
||||
}
|
||||
|
||||
void* NSTaskMem::Clone(const void* ptr, PRUint32 size)
|
||||
{
|
||||
if(!ptr || !EnsureAllocator()) return NULL;
|
||||
void* p = mAllocator->Alloc(size);
|
||||
if(p) memcpy(p, ptr, size);
|
||||
return p;
|
||||
}
|
||||
|
||||
// private:
|
||||
|
||||
nsIAllocator* NSTaskMem::mAllocator = NULL;
|
||||
|
||||
PRBool NSTaskMem::FetchAllocator()
|
||||
{
|
||||
NS_DEFINE_IID(kAllocatorCID, NS_ALLOCATOR_CID);
|
||||
NS_DEFINE_IID(kIAllocatorIID, NS_IALLOCATOR_IID);
|
||||
nsServiceManager::GetService(kAllocatorCID, kIAllocatorIID,
|
||||
(nsISupports **)&mAllocator);
|
||||
NS_ASSERTION(mAllocator, "failed to get Allocator!");
|
||||
return (PRBool) mAllocator;
|
||||
}
|
||||
|
@ -78,6 +78,26 @@ public:
|
||||
{0x81, 0x77, 0x00, 0x60, 0x08, 0x11, 0x9d, 0x7a} \
|
||||
}
|
||||
|
||||
/*
|
||||
* Public shortcuts to the shared allocator's methods
|
||||
*/
|
||||
|
||||
class NSTaskMem
|
||||
{
|
||||
public:
|
||||
static NS_EXPORT void* Alloc(PRUint32 size);
|
||||
static NS_EXPORT void* Realloc(void* ptr, PRUint32 size);
|
||||
static NS_EXPORT void Free(void* ptr);
|
||||
static NS_EXPORT void HeapMinimize();
|
||||
static NS_EXPORT void* Clone(const void* ptr, PRUint32 size);
|
||||
private:
|
||||
NSTaskMem(); // not implemented
|
||||
static PRBool EnsureAllocator() {return mAllocator || FetchAllocator();}
|
||||
static PRBool FetchAllocator();
|
||||
static nsIAllocator* mAllocator;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#endif /* nsIAllocator_h___ */
|
||||
|
@ -78,6 +78,26 @@ public:
|
||||
{0x81, 0x77, 0x00, 0x60, 0x08, 0x11, 0x9d, 0x7a} \
|
||||
}
|
||||
|
||||
/*
|
||||
* Public shortcuts to the shared allocator's methods
|
||||
*/
|
||||
|
||||
class NSTaskMem
|
||||
{
|
||||
public:
|
||||
static NS_EXPORT void* Alloc(PRUint32 size);
|
||||
static NS_EXPORT void* Realloc(void* ptr, PRUint32 size);
|
||||
static NS_EXPORT void Free(void* ptr);
|
||||
static NS_EXPORT void HeapMinimize();
|
||||
static NS_EXPORT void* Clone(const void* ptr, PRUint32 size);
|
||||
private:
|
||||
NSTaskMem(); // not implemented
|
||||
static PRBool EnsureAllocator() {return mAllocator || FetchAllocator();}
|
||||
static PRBool FetchAllocator();
|
||||
static nsIAllocator* mAllocator;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#endif /* nsIAllocator_h___ */
|
||||
|
@ -21,6 +21,8 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "nsAllocator.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include <string.h> /* for memcpy */
|
||||
|
||||
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
||||
static NS_DEFINE_IID(kIAllocatorIID, NS_IALLOCATOR_IID);
|
||||
@ -127,3 +129,55 @@ nsAllocatorFactory::LockFactory(PRBool aLock)
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
* Public shortcuts to the shared allocator's methods
|
||||
* (all these methods are class statics)
|
||||
*/
|
||||
|
||||
// public:
|
||||
void* NSTaskMem::Alloc(PRUint32 size)
|
||||
{
|
||||
if(!EnsureAllocator()) return NULL;
|
||||
return mAllocator->Alloc(size);
|
||||
}
|
||||
|
||||
void* NSTaskMem::Realloc(void* ptr, PRUint32 size)
|
||||
{
|
||||
if(!EnsureAllocator()) return NULL;
|
||||
return mAllocator->Realloc(ptr, size);
|
||||
}
|
||||
|
||||
void NSTaskMem::Free(void* ptr)
|
||||
{
|
||||
if(!EnsureAllocator()) return;
|
||||
mAllocator->Free(ptr);
|
||||
}
|
||||
|
||||
void NSTaskMem::HeapMinimize()
|
||||
{
|
||||
if(!EnsureAllocator()) return;
|
||||
mAllocator->HeapMinimize();
|
||||
}
|
||||
|
||||
void* NSTaskMem::Clone(const void* ptr, PRUint32 size)
|
||||
{
|
||||
if(!ptr || !EnsureAllocator()) return NULL;
|
||||
void* p = mAllocator->Alloc(size);
|
||||
if(p) memcpy(p, ptr, size);
|
||||
return p;
|
||||
}
|
||||
|
||||
// private:
|
||||
|
||||
nsIAllocator* NSTaskMem::mAllocator = NULL;
|
||||
|
||||
PRBool NSTaskMem::FetchAllocator()
|
||||
{
|
||||
NS_DEFINE_IID(kAllocatorCID, NS_ALLOCATOR_CID);
|
||||
NS_DEFINE_IID(kIAllocatorIID, NS_IALLOCATOR_IID);
|
||||
nsServiceManager::GetService(kAllocatorCID, kIAllocatorIID,
|
||||
(nsISupports **)&mAllocator);
|
||||
NS_ASSERTION(mAllocator, "failed to get Allocator!");
|
||||
return (PRBool) mAllocator;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user