/*---------------------------------------------------------------------------* Project: NintendoWare File: demo_Memory.cpp Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo and/or its licensed developers and are protected by national and international copyright laws. 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. The content herein is highly confidential and should be handled accordingly. $Revision: 31311 $ *---------------------------------------------------------------------------*/ #include #include #include #include #include #include #include #include namespace nw { namespace demo { static nn::os::MemoryBlock s_MemoryBlock; static nn::fnd::ExpHeap s_MainMemoryHeap; static bool s_IsInitialized = false; /*!--------------------------------------------------------------------------* @brief 確保したメモリのアライメントを調整します。 @param[in] memory 確保されたメモリの先頭ポインタです。 @param[in] align アライメント値です。 @return アラインされたアドレスを返します。 *---------------------------------------------------------------------------*/ static void* AlignMemory_( void* memory, u8 align ) { void* top = nw::ut::AddOffsetToPtr( memory, 1 ); top = nw::ut::RoundUp( top, align ); u8* head = reinterpret_cast( nw::ut::AddOffsetToPtr( top, - 1 ) ); size_t offset = nw::ut::GetOffsetFromPtr( memory, top ); NW_U8_RANGE_ASSERT( offset ); *head = static_cast( offset ); return top; } //-------------------------------------------------------------------------- void InitializeDemoAllocator( DemoAllocator* allocator, size_t size, bit32 option /* = 0 */ ) { void* memory = nw::demo::Alloc(size); uptr address = reinterpret_cast(memory); allocator->Initialize(address, size, option); } //-------------------------------------------------------------------------- void FinalizeDemoAllocator( DemoAllocator* allocator ) { nw::demo::Free(reinterpret_cast(allocator->GetStartAddress())); allocator->Finalize(); } //-------------------------------------------------------------------------- void InitializeDemoMemory() { if ( s_IsInitialized ) { return; } s_IsInitialized = true; // デバイスメモリのサイズを指定します。 // 現在は 32MB を指定していますが、今後SDKの仕様に合わせて変更します。 const size_t DEMO_MAIN_MEMORY_SIZE = 0x2000000; // 32MB nn::os::SetDeviceMemorySize(DEMO_MAIN_MEMORY_SIZE); uptr address = nn::os::GetDeviceMemoryAddress(); nwosPrintf( "################# Device Memory : 0x%x - 0x%x #################\n", (uint)address, ((uint)address + DEMO_MAIN_MEMORY_SIZE)); s_MainMemoryHeap.Initialize(address, DEMO_MAIN_MEMORY_SIZE, nn::os::ALLOCATE_OPTION_LINEAR); } //-------------------------------------------------------------------------- void* UnAlignMemory( void* memory ) { u8* head = reinterpret_cast( nw::ut::AddOffsetToPtr( memory, -1 ) ); u8 offset = *head; return nw::ut::AddOffsetToPtr( memory, -offset ); } //-------------------------------------------------------------------------- void* Alloc(size_t size, u8 alignment) { // 2のべき乗のチェック NW_ASSERT( (alignment & (alignment - 1)) == 0 ); if ( alignment == 0 ) { alignment = 1; } InitializeDemoMemory(); // 未初期化の場合にはデフォルト設定で初期化。 void* memory = s_MainMemoryHeap.Allocate( size + alignment ); return AlignMemory_( memory, alignment ); } //-------------------------------------------------------------------------- void Free(void* memory) { memory = UnAlignMemory( memory ); s_MainMemoryHeap.Free( memory ); } //-------------------------------------------------------------------------- void Dump() { s_MainMemoryHeap.Dump(); } //-------------------------------------------------------------------------- void DemoAllocator::Initialize(uptr startAddress, size_t size, bit32 option) { m_StartAddress = startAddress; m_Heap.Initialize(m_StartAddress, size, option); } //-------------------------------------------------------------------------- void DemoAllocator::Finalize() { m_Heap.Finalize(); } //-------------------------------------------------------------------------- size_t DemoAllocator::GetFreeSize() { return m_Heap.GetTotalFreeSize(); } //-------------------------------------------------------------------------- size_t DemoAllocator::GetTotalSize() { return m_Heap.GetTotalSize(); } //-------------------------------------------------------------------------- size_t DemoAllocator::GetMemoryBlockSize(void* address) { return m_Heap.GetSizeOf(address); } //-------------------------------------------------------------------------- void DemoAllocator::Dump() { m_Heap.Dump(); } //-------------------------------------------------------------------------- void FrameHeapAllocator::Initialize(uptr startAddress, size_t size, bit32 option) { m_StartAddress = startAddress; m_FrameHeap.Initialize(m_StartAddress, size, option); } //-------------------------------------------------------------------------- void FrameHeapAllocator::Finalize() { m_FrameHeap.Finalize(); } } /* namespace demo */ } /* namespace nw */