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