/*---------------------------------------------------------------------------* Project: Horizon File: os_TransferMemoryBlock.cpp Copyright (C)2009 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 31762 $ *---------------------------------------------------------------------------*/ #include #if NN_PLATFORM_HAS_MMU #include #include #include #include #include #include #include "os_AddressSpaceManager.h" namespace nn { namespace os { void TransferMemoryBlock::Initialize( void* p, size_t size, bit32 myPermission, bit32 otherPermission ) { // Check that it is not initialized. NN_TASSERT_( GetAddress() == NULL ); NN_ALIGN_TASSERT_( p, NN_OS_MEMORY_PAGE_SIZE ); NN_ALIGN_TASSERT_( size, NN_OS_MEMORY_PAGE_SIZE ); Handle handle; Result result; result = nn::svc::CreateMemoryBlock(&handle, reinterpret_cast(p), size, myPermission, otherPermission); NN_ERR_THROW_FATAL(result); this->SetHandle(handle); SetAddressAndSize(reinterpret_cast(p), size); } Result TransferMemoryBlock::TryInitialize( void* p, size_t size, bit32 myPermission, bit32 otherPermission ) { // Check that it is not initialized. NN_TASSERT_( GetAddress() == NULL ); NN_ALIGN_TASSERT_( p, NN_OS_MEMORY_PAGE_SIZE ); NN_ALIGN_TASSERT_( size, NN_OS_MEMORY_PAGE_SIZE ); Handle handle; Result result; result = nn::svc::CreateMemoryBlock(&handle, reinterpret_cast(p), size, myPermission, otherPermission); NN_UTIL_RETURN_IF_FAILED(result); this->SetHandle(handle); SetAddressAndSize(reinterpret_cast(p), size); return result; } void TransferMemoryBlock::Finalize() { if (this->IsValid()) { Unmap(); this->HandleObject::Close(); } } Result TransferMemoryBlock::AttachAndMap( Handle handle, size_t size, bit32 otherPermission, bit32 myPermission ) { NN_ALIGN_TASSERT_( size, NN_OS_MEMORY_PAGE_SIZE ); this->SetHandle(handle); return Map(size, otherPermission, myPermission); } Result TransferMemoryBlock::Map( size_t size, bit32 otherPermission, bit32 myPermission ) { // Check that it is not initialized. NN_TASSERT_( GetAddress() == NULL ); NN_ALIGN_TASSERT_( size, NN_OS_MEMORY_PAGE_SIZE ); // Get memory from the virtual address space. uptr addr = detail::AllocateFromSharedMemorySpace(this, size); if (addr == NULL) { NN_TPANIC_("failed to allocate address space."); } this->MemoryBlockBase::SetReadOnly((myPermission & os::MEMORY_PERMISSION_WRITE) == 0); Result result = nn::svc::MapMemoryBlock(GetHandle(), addr, myPermission, otherPermission); if( result.IsFailure() ) { return result; } m_SpaceAllocated = true; return result; } void TransferMemoryBlock::Unmap() { if (GetAddress() != NULL) { if( m_SpaceAllocated ) { nn::svc::UnmapMemoryBlock(GetHandle(), GetAddress()); detail::FreeToSharedMemorySpace(this); } else { SetAddressAndSize(0, 0); } } } }} #endif // if NN_PLATFORM_HAS_MMU