/*---------------------------------------------------------------------------* Project: NintendoWare File: snd_InstancePool.cpp Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc. 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. $Revision: 24859 $ *---------------------------------------------------------------------------*/ #include "precompiled.h" #include #include #include namespace nw { namespace snd { namespace internal { /*---------------------------------------------------------------------------* Name: CreateImpl Description: メモリプールの作成 Arguments: buffer - バッファの開始アドレス size - バッファのサイズ objSize - オブジェクトのサイズ Returns: None. *---------------------------------------------------------------------------*/ u32 PoolImpl::CreateImpl( void* buffer, size_t size, u32 objSize ) { NW_NULL_ASSERT( buffer ); char* ptr = static_cast( ut::RoundUp( buffer, 4 ) ); objSize = ut::RoundUp( objSize, 4 ); u32 numObjects = ( size - ( ptr - static_cast(buffer) ) ) / objSize; for ( u32 i=0; i < numObjects; i++ ) { PoolImpl *head = reinterpret_cast( ptr ); head->m_pNext = m_pNext; m_pNext = head; ptr += objSize; } return numObjects; } /*---------------------------------------------------------------------------* Name: DestroyImpl Description: メモリプールの破棄 Arguments: buffer - バッファの開始アドレス size - バッファのサイズ Returns: None. *---------------------------------------------------------------------------*/ void PoolImpl::DestroyImpl( void* buffer, unsigned long size ) { NW_NULL_ASSERT( buffer ); void* begin = buffer; void* end = static_cast( static_cast( buffer ) + size ); PoolImpl* ptr = m_pNext; PoolImpl* prev = this; while ( ptr != NULL ) { if ( ( begin <= ptr ) && ( ptr < end ) ) { prev->m_pNext = ptr->m_pNext; } else { prev = ptr; } ptr = ptr->m_pNext; } } /*---------------------------------------------------------------------------* Name: CountImpl Description: 空き領域数をカウント Arguments: None. Returns: 空き領域数 *---------------------------------------------------------------------------*/ int PoolImpl::CountImpl() const { int count = 0; for ( PoolImpl* ptr = m_pNext; ptr != NULL; ptr = ptr->m_pNext ) { ++count; } return count; } /*---------------------------------------------------------------------------* Name: AllocImpl Description: プールからメモリ領域を確保 Arguments: None. Returns: メモリ領域の開始アドレス *---------------------------------------------------------------------------*/ void* PoolImpl::AllocImpl() { if ( m_pNext == NULL ) return NULL; PoolImpl* head = m_pNext; m_pNext = head->m_pNext; return head; } /*---------------------------------------------------------------------------* Name: FreeImpl Description: メモリ領域をプールに開放 Arguments: ptr - メモリ領域の開始アドレス Returns: None. *---------------------------------------------------------------------------*/ void PoolImpl::FreeImpl( void* ptr ) { PoolImpl* head = reinterpret_cast( ptr ); head->m_pNext = m_pNext; m_pNext = head; } } // namespace nw::snd::internal } // namespace nw::snd } // namespace nw