1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_InstancePool.cpp
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: 31311 $
16  *---------------------------------------------------------------------------*/
17 
18 #include "precompiled.h"
19 
20 #include <nw/snd/snd_InstancePool.h>
21 #include <nw/assert.h>
22 #include <nw/ut/ut_Inlines.h>
23 
24 namespace nw {
25 namespace snd {
26 namespace internal {
27 
28 /*---------------------------------------------------------------------------*
29   Name:         CreateImpl
30 
31   Description:  メモリプールの作成
32 
33   Arguments:    buffer - バッファの開始アドレス
34                 size - バッファのサイズ
35                 objSize - オブジェクトのサイズ
36 
37   Returns:      None.
38  *---------------------------------------------------------------------------*/
CreateImpl(void * buffer,size_t size,u32 objSize)39 u32 PoolImpl::CreateImpl( void* buffer, size_t size, u32 objSize )
40 {
41     NW_NULL_ASSERT( buffer );
42 
43     char* ptr = static_cast<char*>( ut::RoundUp( buffer, 4 ) );
44     objSize = ut::RoundUp( objSize, 4 );
45     u32 numObjects = ( size - ( ptr - static_cast<char*>(buffer) ) ) / objSize;
46 
47     for ( u32 i=0; i < numObjects; i++ )
48     {
49         PoolImpl *head = reinterpret_cast<PoolImpl*>( ptr );
50         head->m_pNext = m_pNext;
51         m_pNext = head;
52         ptr += objSize;
53     }
54     return numObjects;
55 }
56 
57 /*---------------------------------------------------------------------------*
58   Name:         DestroyImpl
59 
60   Description:  メモリプールの破棄
61 
62   Arguments:    buffer - バッファの開始アドレス
63                 size - バッファのサイズ
64 
65   Returns:      None.
66  *---------------------------------------------------------------------------*/
DestroyImpl(void * buffer,unsigned long size)67 void PoolImpl::DestroyImpl( void* buffer, unsigned long size )
68 {
69     NW_NULL_ASSERT( buffer );
70 
71     void* begin = buffer;
72     void* end = static_cast<void*>( static_cast<char*>( buffer ) + size );
73     PoolImpl* ptr = m_pNext;
74     PoolImpl* prev = this;
75     while ( ptr != NULL )
76     {
77         if ( ( begin <= ptr ) && ( ptr < end ) )
78         {
79             prev->m_pNext = ptr->m_pNext;
80         }
81         else
82         {
83             prev = ptr;
84         }
85         ptr = ptr->m_pNext;
86     }
87 }
88 
89 /*---------------------------------------------------------------------------*
90   Name:         CountImpl
91 
92   Description:  空き領域数をカウント
93 
94   Arguments:    None.
95 
96   Returns:      空き領域数
97  *---------------------------------------------------------------------------*/
CountImpl() const98 int PoolImpl::CountImpl() const
99 {
100     int count = 0;
101     for ( PoolImpl* ptr = m_pNext; ptr != NULL; ptr = ptr->m_pNext )
102     {
103         ++count;
104     }
105     return count;
106 }
107 
108 /*---------------------------------------------------------------------------*
109   Name:         AllocImpl
110 
111   Description:  プールからメモリ領域を確保
112 
113   Arguments:    None.
114 
115   Returns:      メモリ領域の開始アドレス
116  *---------------------------------------------------------------------------*/
AllocImpl()117 void* PoolImpl::AllocImpl()
118 {
119     if ( m_pNext == NULL ) return NULL;
120     PoolImpl* head = m_pNext;
121     m_pNext = head->m_pNext;
122     return head;
123 }
124 
125 /*---------------------------------------------------------------------------*
126   Name:         FreeImpl
127 
128   Description:  メモリ領域をプールに開放
129 
130   Arguments:    ptr - メモリ領域の開始アドレス
131 
132   Returns:      None.
133  *---------------------------------------------------------------------------*/
FreeImpl(void * ptr)134 void PoolImpl::FreeImpl( void* ptr )
135 {
136     PoolImpl* head = reinterpret_cast<PoolImpl*>( ptr );
137     head->m_pNext = m_pNext;
138     m_pNext = head;
139 }
140 
141 } // namespace nw::snd::internal
142 } // namespace nw::snd
143 } // namespace nw
144 
145