/*---------------------------------------------------------------------------* Project: Horizon File: init_Alloc.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: 20442 $ *---------------------------------------------------------------------------*/ //--------------------------------------------------------------------------- #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace nn::os; typedef nn::fnd::ExpHeapTemplate > SystemExpHeap; namespace { SystemExpHeap* s_pSystemHeap = 0; //!< システムヒープ nn::util::aligned_storage::value >::type s_SystemAllocatorBuffer; SystemExpHeap::Allocator* s_pSystemAllocator = 0; //Allocate(size); } // ABI のオーバーライド NN_WEAK_SYMBOL void free(void* p) { if (p) { s_pSystemHeap->Free(p); } } } // extern "C" NN_WEAK_SYMBOL void* operator new (size_t size, const ::std::nothrow_t&) throw() { return malloc(size); } NN_WEAK_SYMBOL void* operator new[] (size_t size, const ::std::nothrow_t&) throw() { return operator new(size, ::std::nothrow_t()); } NN_WEAK_SYMBOL void operator delete (void* p) throw() { free(p); } NN_WEAK_SYMBOL void operator delete[] (void* p) throw() { operator delete(p); } //NN_WEAK_SYMBOL void operator delete (void* p, const ::std::nothrow_t&) throw() //{ // if( p != 0 ) // { // free(p); // } //} //NN_WEAK_SYMBOL void operator delete[] (void* p, const ::std::nothrow_t&) throw() //{ // if( p != 0 ) // { // free(p); // } //} namespace nn { namespace init { // 注意 // この周辺の関数は initStartup から呼ばれるものがある。 // initStartup は static initializer 以前に呼ばれる関数のため、 // この周辺の関数で non-POD クラスの static オブジェクトを初期化すると、 // static initializer で再初期化されてしまい、正常に動作しない。 // aligned_storage + placement new で実装すること。 // システムヒープを作成 void InitializeAllocator(uptr addr, size_t size) { NN_TASSERT_(size >= sizeof(SystemExpHeap)); const size_t alignment = nn::util::alignment_of::value; uptr heapAddr = (((addr - 1) / alignment) + 1) * alignment; uptr headAddr = heapAddr + sizeof(SystemExpHeap); s_pSystemHeap = new (reinterpret_cast(heapAddr)) SystemExpHeap(headAddr, addr + size - headAddr); s_pSystemAllocator = new (&s_SystemAllocatorBuffer) SystemExpHeap::Allocator(*s_pSystemHeap); } #if NN_PLATFORM_HAS_MMU void InitializeAllocator(size_t size) { nnosMemoryBlockAllocate(&s_HeapMemoryBlock, size); NN_TASSERT_(nnosMemoryBlockGetSize(&s_HeapMemoryBlock) == size); InitializeAllocator(nnosMemoryBlockGetAddress(&s_HeapMemoryBlock), size); } #endif // if NN_PLATFORM_HAS_MMU nn::fnd::IAllocator* GetAllocator(void) { return s_pSystemAllocator; } }} // namespace nn::init