SharedMemorySymbian.cpp [plain text]
#include "config.h"
#if PLATFORM(QT) && OS(SYMBIAN)
#include "SharedMemory.h"
#include "ArgumentDecoder.h"
#include "ArgumentEncoder.h"
#include <e32math.h>
#include <qdebug.h>
#include <qglobal.h>
#include <sys/param.h>
namespace WebKit {
SharedMemory::Handle::Handle()
: m_chunkID(0)
, m_size(0)
{
}
SharedMemory::Handle::~Handle()
{
}
bool SharedMemory::Handle::isNull() const
{
return !m_chunkID;
}
void SharedMemory::Handle::encode(CoreIPC::ArgumentEncoder* encoder) const
{
ASSERT(!isNull());
encoder->encodeUInt32(m_size);
encoder->encodeUInt32(m_chunkID);
}
bool SharedMemory::Handle::decode(CoreIPC::ArgumentDecoder* decoder, Handle& handle)
{
size_t size;
if (!decoder->decodeUInt32(size))
return false;
uint32_t chunkID;
if (!decoder->decodeUInt32(chunkID))
return false;
handle.m_size = size;
handle.m_chunkID = chunkID;
return true;
}
CoreIPC::Attachment SharedMemory::Handle::releaseToAttachment() const
{
return CoreIPC::Attachment(-1, 0);
}
void SharedMemory::Handle::adoptFromAttachment(int, size_t)
{
}
PassRefPtr<SharedMemory> SharedMemory::create(size_t size)
{
uint32_t random = Math::Random();
TBuf<KMaxKernelName> chunkName;
chunkName.Format(_L("%d"), random);
RChunk chunk;
TInt error = chunk.CreateGlobal(chunkName, size, size);
if (error) {
qCritical() << "Failed to create WK2 shared memory of size " << size << " with error " << error;
return 0;
}
RefPtr<SharedMemory> sharedMemory(adoptRef(new SharedMemory));
sharedMemory->m_handle = chunk.Handle();
sharedMemory->m_size = chunk.Size();
sharedMemory->m_data = static_cast<void*>(chunk.Base());
return sharedMemory.release();
}
PassRefPtr<SharedMemory> SharedMemory::create(const Handle& handle, Protection protection)
{
if (handle.isNull())
return 0;
TBuf<KMaxKernelName> chunkName;
chunkName.Format(_L("%d"), handle.m_chunkID);
RChunk chunk;
TInt error = chunk.OpenGlobal(chunkName, false);
if (error) {
qCritical() << "Failed to create WK2 shared memory from handle " << error;
return 0;
}
chunk.Adjust(chunk.MaxSize());
RefPtr<SharedMemory> sharedMemory(adoptRef(new SharedMemory));
sharedMemory->m_handle = chunk.Handle();
sharedMemory->m_size = chunk.Size();
sharedMemory->m_data = static_cast<void*>(chunk.Base());
return sharedMemory.release();
}
SharedMemory::~SharedMemory()
{
}
bool SharedMemory::createHandle(Handle& handle, Protection protection)
{
ASSERT_ARG(handle, handle.isNull());
RChunk chunk;
if (chunk.SetReturnedHandle(m_handle))
return false;
TName globalChunkName = chunk.Name();
TLex lexer(globalChunkName);
TUint32 nameAsInt = 0;
if (lexer.Val(nameAsInt, EDecimal))
return false;
handle.m_chunkID = nameAsInt;
handle.m_size = m_size;
return true;
}
unsigned SharedMemory::systemPageSize()
{
return PAGE_SIZE;
}
}
#endif