1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_SoundHeap.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_SoundHeap.h>
21 
22 #include <nw/snd/snd_SoundThread.h>
23 #include <nw/snd/snd_DriverCommand.h>
24 
25 namespace nw {
26 namespace snd {
27 
28 /*---------------------------------------------------------------------------*
29   Name:         SoundHeap
30 
31   Description:  コンストラクタ
32 
33   Arguments:    無し
34 
35   Returns:      無し
36  *---------------------------------------------------------------------------*/
SoundHeap()37 SoundHeap::SoundHeap()
38 : m_FrameHeap()
39 {
40     m_CriticalSection.Initialize();
41 }
42 
43 /*---------------------------------------------------------------------------*
44   Name:         SoundHeap
45 
46   Description:  デストラクタ
47 
48   Arguments:    無し
49 
50   Returns:      無し
51  *---------------------------------------------------------------------------*/
~SoundHeap()52 SoundHeap::~SoundHeap()
53 {
54     Destroy();
55 
56     m_CriticalSection.Finalize();
57 }
58 
59 /*---------------------------------------------------------------------------*
60   Name:         Create
61 
62   Description:  サウンドヒープを作成する
63 
64   Arguments:    startAddress - メインメモリ上のヒープ開始アドレス
65                 size         - メインメモリ上のヒープのサイズ
66 
67   Returns:      成功したらtrue
68  *---------------------------------------------------------------------------*/
Create(void * startAddress,size_t size)69 bool SoundHeap::Create(
70     void* startAddress,
71     size_t size
72 )
73 {
74 	nn::os::CriticalSection::ScopedLock lock( m_CriticalSection );
75 
76     return m_FrameHeap.Create( startAddress, size );
77 }
78 
79 /*---------------------------------------------------------------------------*
80   Name:         Destroy
81 
82   Description:  ヒープを破棄する
83 
84   Arguments:    無し
85 
86   Returns:      無し
87  *---------------------------------------------------------------------------*/
Destroy()88 void SoundHeap::Destroy()
89 {
90 	nn::os::CriticalSection::ScopedLock lock( m_CriticalSection );
91 
92     if ( ! m_FrameHeap.IsValid() ) return;
93 
94     Clear();
95 
96     m_FrameHeap.Destroy();
97 }
98 
99 /*---------------------------------------------------------------------------*
100   Name:         Alloc
101 
102   Description:  メインメモリ上のヒープからメモリ領域を割り当てます
103 
104   Arguments:    size        - メモリ領域のサイズ
105 
106   Returns:      割り当てたメモリ領域の先頭アドレス
107  *---------------------------------------------------------------------------*/
Alloc(size_t size)108 void* SoundHeap::Alloc( size_t size )
109 {
110 	nn::os::CriticalSection::ScopedLock lock( m_CriticalSection );
111 
112     void* buffer = Alloc(
113         size,
114         DisposeCallbackFunc,
115         NULL
116     );
117     // NN_LOG("[%s] free(%d %dKB %.3fMB)\n", __FUNCTION__,
118     //         GetFreeSize(), GetFreeSize()/1024, GetFreeSize()/(1024.f*1024));
119     return buffer;
120 }
121 
122 /*---------------------------------------------------------------------------*
123   Name:         Alloc
124 
125   Description:  メインメモリ上のヒープからメモリ領域を割り当てます
126 
127   Arguments:    size        - メモリ領域のサイズ
128                 callback    - メモリ領域が解放されるときに呼び出されるコールバック
129                 callbackArg - コールバック
130 
131   Returns:      割り当てたメモリ領域の先頭アドレス
132  *---------------------------------------------------------------------------*/
Alloc(size_t size,SoundHeap::DisposeCallback callback,void * callbackArg)133 void* SoundHeap::Alloc(
134     size_t size,
135     SoundHeap::DisposeCallback callback,
136     void* callbackArg
137 )
138 {
139 	nn::os::CriticalSection::ScopedLock lock( m_CriticalSection );
140 
141     return m_FrameHeap.Alloc(
142         size,
143         callback,
144         callbackArg
145     );
146 }
147 
148 // 確保したメモリブロックを全て解放する
Clear()149 void SoundHeap::Clear()
150 {
151     if ( ! m_FrameHeap.IsValid() ) return;
152 
153     {
154         nn::os::CriticalSection::ScopedLock lock( m_CriticalSection );
155 
156         m_FrameHeap.Clear();
157     }
158 
159     internal::DriverCommandManager& cmdmgr = internal::DriverCommandManager::GetInstance();
160     u32 tag = cmdmgr.FlushCommand( true );
161     cmdmgr.WaitCommandReply( tag );
162 }
163 
164 // サウンドヒープの状態を保存する
SaveState()165 int SoundHeap::SaveState()
166 {
167 	nn::os::CriticalSection::ScopedLock lock( m_CriticalSection );
168 
169     return m_FrameHeap.SaveState();
170 }
171 
172 // サウンドヒープの状態を復元する
LoadState(int level)173 void SoundHeap::LoadState( int level )
174 {
175     {
176         nn::os::CriticalSection::ScopedLock lock( m_CriticalSection );
177 
178         m_FrameHeap.LoadState( level );
179 
180         internal::DriverCommandManager& cmdmgr = internal::DriverCommandManager::GetInstance();
181         u32 tag = cmdmgr.FlushCommand( true );
182         cmdmgr.WaitCommandReply( tag );
183     }
184 
185     // NN_LOG("[%s] free(%d %dKB %.3fMB)\n", __FUNCTION__,
186     //         GetFreeSize(), GetFreeSize()/1024, GetFreeSize()/(1024.f*1024));
187 }
188 
189 /* ========================================================================
190         private function
191    ======================================================================== */
192 
DisposeCallbackFunc(void * mem,unsigned long size,void * arg)193 void SoundHeap::DisposeCallbackFunc( void* mem, unsigned long size, void* arg )
194 {
195     NW_UNUSED_VARIABLE(arg);
196 
197     internal::DriverCommandManager& cmdmgr = internal::DriverCommandManager::GetInstance();
198 
199     internal::DriverCommandInvalidateData* command =
200         cmdmgr.AllocCommand<internal::DriverCommandInvalidateData>();
201     command->id = internal::DRIVER_COMMAND_INVALIDATE_DATA;
202     command->mem = mem;
203     command->size = size;
204     cmdmgr.PushCommand(command);
205 }
206 
207 } // namespace nw::snd
208 } // namespace nw
209 
210