/*---------------------------------------------------------------------------* Project: Horizon File: os_StackMemoryBlock.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: 38846 $ *---------------------------------------------------------------------------*/ #include #if NN_PLATFORM_HAS_MMU #include #include #include #include #include #include #include #include #include #include #include #include "os_AddressSpaceManager.h" //--------------------------------------------------------------------------- using namespace nn; using namespace nn::svc; namespace nn{ namespace os{ namespace { size_t GetPageAlignedSize(size_t size) { return (size + NN_OS_MEMORY_PAGE_SIZE - 1) & ~(NN_OS_MEMORY_PAGE_SIZE - 1); } } namespace detail { void Switch(StackMemoryBlock* pTo, StackMemoryBlock* pFrom) { Switch( reinterpret_cast(pTo), reinterpret_cast(pFrom) ); } } // Allocate a memory block from the stack region. void StackMemoryBlock::Initialize(size_t size) { // Check that the memory block has been initialized and that this instance is not initialized. NN_TASSERTMSG_(detail::IsMemoryBlockEnabled(), "InitializeMemoryBlock is not called.\n"); NN_TASSERTMSG_(GetAddress() == 0, "This StackMemoryBlock instance has been already initialized.\n"); if ( !detail::IsMemoryBlockEnabled() || GetAddress() != 0 ) { return; } size = GetPageAlignedSize(size); // Get memory from the virtual address space. uptr addr = detail::AllocateFromMemoryBlockSpace(this, size); if (addr == NULL) { NN_OS_ERROR_IF_FAILED(ResultNoAddressSpace()); } } void StackMemoryBlock::Finalize() { if ( GetAddress() != NULL ) { detail::FreeToMemoryBlockSpace(this); } } }} // namespace nn::os #include using namespace nn::os; extern "C" { // StackMemoryBlock void nnosStackMemoryBlockInitialize(nnosStackMemoryBlock* p) { new (p) StackMemoryBlock(); } void nnosStackMemoryBlockAllocate(nnosStackMemoryBlock* p, size_t size) { NN_TASSERT_(detail::IsMemoryBlockEnabled()); new (p) StackMemoryBlock(size); } void nnosStackMemoryBlockFree(nnosStackMemoryBlock* p) { StackMemoryBlock* pStackMemoryBlock = reinterpret_cast(p); pStackMemoryBlock->~StackMemoryBlock(); } uptr nnosStackMemoryBlockGetAddress(nnosStackMemoryBlock* p) { StackMemoryBlock* pStackMemoryBlock = reinterpret_cast(p); return pStackMemoryBlock->GetAddress(); } size_t nnosStackMemoryBlockGetSize(nnosStackMemoryBlock* p) { StackMemoryBlock* pStackMemoryBlock = reinterpret_cast(p); return pStackMemoryBlock->GetSize(); } uptr nnosStackMemoryBlockGetStackBottom(nnosStackMemoryBlock* p) { StackMemoryBlock* pStackMemoryBlock = reinterpret_cast(p); return pStackMemoryBlock->GetStackBottom(); } } #endif // if NN_PLATFORM_HAS_MMU