/*---------------------------------------------------------------------------* Project: Horizon File: fnd_Allocator.h 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: 16565 $ *---------------------------------------------------------------------------*/ /*! @file @brief アロケータに関するAPIの宣言 */ #ifndef NN_FND_ALLOCATOR_H_ #define NN_FND_ALLOCATOR_H_ #ifdef __cplusplus #include #include #include namespace nn { namespace fnd { /*! @brief ライブラリで使う汎用アロケータインターフェイスです。 */ class IAllocator { public: /*! @brief 指定したサイズとアラインメントでメモリ領域を確保します。 @param[in] size 確保するメモリのサイズ @param[in] alignment 確保するメモリのアラインメント @return 確保したメモリ領域の先頭へのポインタ */ virtual void* Allocate(size_t size, s32 alignment) = 0; /*! @brief メモリ領域を解放します。 @param[in] p 確保されているメモリ領域の先頭へのポインタ */ virtual void Free(void* p) = 0; }; template class StdAllocatorAdapterHolder { public: static void SetAllocator(Allocator allocator) { s_Allocator = allocator; } static Allocator GetAllocator() { return s_Allocator; } private: static Allocator s_Allocator; }; template class StdAllocatorAdapter : public StdAllocatorAdapterHolder { public: typedef size_t size_type; typedef sptr difference_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef T value_type; template struct rebind { typedef StdAllocatorAdapter other; }; StdAllocatorAdapter() throw() {} StdAllocatorAdapter(const StdAllocatorAdapter&) throw() {} template StdAllocatorAdapter(const StdAllocatorAdapter&) throw() {} ~StdAllocatorAdapter() throw() {} pointer address(reference& x) const { return &x; } const_pointer address(const_reference& x) const { return &x; } pointer allocate(size_type n, void* = 0) { return static_cast(StdAllocatorAdapterHolder::GetAllocator()->Allocate(n * sizeof(T), nn::util::alignment_of::value)); } void deallocate(pointer p, size_type) { StdAllocatorAdapterHolder::GetAllocator()->Free(static_cast(p)); } size_type max_size() const throw() { return ::std::numeric_limits::max() / sizeof(T); } void construct(pointer p, const T& val) { new (static_cast(p)) T(val); } void destroy(pointer p) { p->~T(); } }; template class StdUnitAllocatorAdapter { public: static void SetAllocator(Allocator allocator) { s_Allocator = allocator; } static Allocator GetAllocator() { return s_Allocator; } typedef size_t size_type; typedef sptr difference_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef T value_type; template struct rebind { typedef StdUnitAllocatorAdapter other; }; StdUnitAllocatorAdapter() throw() {} StdUnitAllocatorAdapter(const StdUnitAllocatorAdapter&) throw() {} template StdUnitAllocatorAdapter(const StdUnitAllocatorAdapter&) throw() {} ~StdUnitAllocatorAdapter() throw() {} pointer address(reference& x) const { return &x; } const_pointer address(const_reference& x) const { return &x; } pointer allocate(size_type n, void* = 0) { return static_cast(s_Allocator->Allocate(n * sizeof(T), nn::util::alignment_of::value)); } void deallocate(pointer p, size_type) { s_Allocator->Free(static_cast(p)); } size_type max_size() const throw() { return ::std::numeric_limits::max() / sizeof(T); } void construct(pointer p, const T& val) { new (static_cast(p)) T(val); } void destroy(pointer p) { p->~T(); } private: static Allocator s_Allocator; }; template Allocator StdAllocatorAdapterHolder::s_Allocator; template Allocator StdUnitAllocatorAdapter::s_Allocator; }} // end of namespace nn #endif // __cplusplus #endif