1 /*---------------------------------------------------------------------------* 2 Project: Horizon 3 File: os_StackMemoryAutoStackManager.cpp 4 5 Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved. 6 7 These coded instructions, statements, and computer programs contain 8 proprietary information of Nintendo of America Inc. and/or Nintendo 9 Company Ltd., and are protected by Federal copyright law. They may 10 not be disclosed to third parties or copied or duplicated in any form, 11 in whole or in part, without the prior written consent of Nintendo. 12 13 $Rev:$ 14 *---------------------------------------------------------------------------*/ 15 16 #include <nn/config.h> 17 #if NN_PLATFORM_HAS_MMU 18 19 #include <nn/os.h> 20 #include <nn/fnd.h> 21 #include <nn/math.h> 22 23 namespace nn { namespace os { 24 25 namespace 26 { 27 struct DataOnStack 28 { 29 StackMemory stackMemory; 30 void* pHead; 31 }; 32 } 33 Construct(size_t stackSize)34 void* StackMemoryAutoStackManager::Construct(size_t stackSize) 35 { 36 NN_TASSERT_(IsInitialized()); 37 NN_ALIGN_TASSERT_(stackSize, 0x1000); 38 39 void* const pMemory = GetAllocator()->Allocate(stackSize, 0x1000); 40 NN_POINTER_TASSERT_(pMemory); 41 42 StackMemory stackMemory; 43 stackMemory.Initialize(pMemory, stackSize); 44 45 uptr stackBottom = stackMemory.GetStackBottom(); 46 stackBottom -= math::RoundUp(sizeof(DataOnStack), 8); 47 DataOnStack* const pStackBottom = reinterpret_cast<DataOnStack*>(stackBottom); 48 49 new(&pStackBottom->stackMemory) StackMemory(); 50 pStackBottom->stackMemory.MoveFrom(&stackMemory); 51 pStackBottom->pHead = pMemory; 52 53 return pStackBottom; 54 } 55 FreeStack(void * pStackBottom)56 void StackMemoryAutoStackManager::FreeStack(void* pStackBottom) 57 { 58 DataOnStack& dos = *reinterpret_cast<DataOnStack*>(pStackBottom); 59 60 void* volatile pHead = dos.pHead; 61 62 StackMemory stackMemory; 63 stackMemory.MoveFrom(&dos.stackMemory); 64 stackMemory.Finalize(); 65 GetAllocator()->Free(pHead); 66 } 67 68 69 }} // namespace nn::os 70 71 #endif // if NN_PLATFORM_HAS_MMU 72