/*---------------------------------------------------------------------------* 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; //! Allocator for the system heap #if NN_PLATFORM_HAS_MMU nnosMemoryBlock s_HeapMemoryBlock; #endif // if NN_PLATFORM_HAS_MMU } extern "C"{ // ABI override NN_WEAK_SYMBOL void* malloc(size_t size) { return s_pSystemHeap->Allocate(size); } // ABI override 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 { // Note // Some functions around here are called from initStartup. // Since initStartup is called before static initializer, if a static object of the non-POD class is initialized with a function here, the static initializer is reinitialized causing a malfunction. // // // Implement with aligned_storage + placement new. // Create system heap 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