OSAllocatorSymbian.cpp [plain text]
#include "config.h"
#include "OSAllocator.h"
#include "PageAllocatorSymbian.h"
namespace WTF {
static RPointerArray<SymbianChunk> codeChunksContainer;
static PageAllocatorSymbian dataAllocator;
_LIT(KErrorStringInternalConsistency, "OSAllocator:ConsistencyError");
_LIT(KErrorStringChunkCreation, "OSAllocator:ChunkInitError");
_LIT(KErrorStringPageSize, "OSAllocator:WrongPageSize");
static void* allocateCodeChunk(size_t bytes)
{
RChunk c;
TInt error = c.CreateLocalCode(bytes, bytes);
__ASSERT_ALWAYS(error == KErrNone, User::Panic(KErrorStringChunkCreation, error));
codeChunksContainer.Append(new SymbianChunk(c.Handle()));
return static_cast<void*>(c.Base());
}
static bool deallocateCodeChunk(void* address)
{
bool found = false;
for (int i = 0; i < codeChunksContainer.Count(); i++) {
SymbianChunk* p = codeChunksContainer[i];
if (p && p->contains(address)) {
codeChunksContainer.Remove(i);
delete p;
found = true;
}
}
return found;
}
static PageAllocatorSymbian* dataAllocatorInstance()
{
return &dataAllocator;
}
void* OSAllocator::reserveUncommitted(size_t reservationSize, Usage usage, bool , bool executable, bool)
{
void* base = 0;
if (executable)
base = allocateCodeChunk(reservationSize);
else
base = dataAllocatorInstance()->reserve(reservationSize);
return base;
}
void OSAllocator::releaseDecommitted(void* parkedBase, size_t bytes)
{
if (dataAllocatorInstance()->contains(parkedBase))
dataAllocatorInstance()->release(parkedBase, bytes);
}
void OSAllocator::commit(void* address, size_t bytes, bool, bool executable)
{
if (!executable)
dataAllocatorInstance()->commit(address, bytes);
}
void OSAllocator::decommit(void* address, size_t bytes)
{
if (dataAllocatorInstance()->contains(address))
dataAllocatorInstance()->decommit(address, bytes);
else
deallocateCodeChunk(address); }
void* OSAllocator::reserveAndCommit(size_t bytes, Usage usage, bool writable, bool executable, bool)
{
void* base = reserveUncommitted(bytes, usage, writable, executable);
commit(base, bytes, writable, executable);
return base;
}
PageAllocatorSymbian::PageAllocatorSymbian()
{
__ASSERT_ALWAYS(m_pageSize == WTF::pageSize(), User::Panic(KErrorStringPageSize, m_pageSize));
RChunk chunk;
TInt error = chunk.CreateDisconnectedLocal(0, 0, TInt(largeReservationSize));
__ASSERT_ALWAYS(error == KErrNone, User::Panic(KErrorStringChunkCreation, error));
m_chunk = new SymbianChunk(chunk.Handle()); }
PageAllocatorSymbian::~PageAllocatorSymbian()
{
delete m_chunk;
}
void* PageAllocatorSymbian::reserve(size_t bytes)
{
const size_t nPages = bytes / m_pageSize;
const int64_t startIdx = m_map.findRunOfZeros(nPages);
if (startIdx < 0)
return 0;
for (size_t i = startIdx; i < startIdx + nPages ; i++)
m_map.set(i);
return static_cast<void*>( m_chunk->m_base + (TUint)(m_pageSize * startIdx) );
}
void PageAllocatorSymbian::release(void* address, size_t bytes)
{
const size_t startIdx = (static_cast<char*>(address) - m_chunk->m_base) / m_pageSize;
const size_t nPages = bytes / m_pageSize;
for (size_t i = startIdx; i < startIdx + nPages ; i++)
m_map.clear(i);
}
bool PageAllocatorSymbian::commit(void* address, size_t bytes)
{
const size_t idx = (static_cast<char*>(address) - m_chunk->m_base) / m_pageSize;
const size_t nPages = bytes / m_pageSize;
__ASSERT_ALWAYS(m_map.get(idx), User::Panic(KErrorStringInternalConsistency, idx));
__ASSERT_ALWAYS(m_map.get(idx+nPages-1), User::Panic(KErrorStringInternalConsistency, idx+nPages-1));
TInt error = m_chunk->Commit(static_cast<char*>(address) - m_chunk->m_base, bytes);
return (error == KErrNone);
}
bool PageAllocatorSymbian::decommit(void* address, size_t bytes)
{
TInt error = m_chunk->Decommit(static_cast<char*>(address) - m_chunk->m_base, bytes);
return (error == KErrNone);
}
bool PageAllocatorSymbian::contains(const void* address) const
{
return m_chunk->contains(address);
}
}