1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_FrameHeap.h
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: 13145 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_SND_FRAME_HEAP_H_
17 #define NW_SND_FRAME_HEAP_H_
18 
19 #include <nw/ut/ut_LinkList.h>
20 #include <nw/ut/ut_FrameHeap.h>
21 #include <nw/snd/snd_SoundDataManager.h>
22 
23 namespace nw {
24 namespace snd {
25 namespace internal {
26 
27 /* ========================================================================
28         class definition
29    ======================================================================== */
30 
31 class FrameHeap
32 {
33     /* ------------------------------------------------------------------------
34             type definition
35        ------------------------------------------------------------------------ */
36   public:
37     typedef void (*DisposeCallback)( void* mem, unsigned long size, void* userArg );
38 
39     struct Block
40     {
41       public:
BlockBlock42         Block( void* buffer, u32 size, DisposeCallback callback, void* callbackArg )
43         : m_pBuffer( buffer ), m_Size( size ), m_Callback( callback ), m_pCallbackArg( callbackArg ) {}
~BlockBlock44         ~Block() { if ( m_Callback != NULL ) m_Callback( m_pBuffer, m_Size, m_pCallbackArg ); }
45 
GetBufferAddrBlock46         void* GetBufferAddr(){ return m_pBuffer; }
GetBufferAddrBlock47         const void* GetBufferAddr() const { return m_pBuffer; }
GetBufferSizeBlock48         u32 GetBufferSize() const { return m_Size; }
GetDisposeCallbackBlock49         DisposeCallback GetDisposeCallback() const { return m_Callback; }
50             /* const DisposeCallback を返そうとすると、RVCT で、
51                "type qualifier on return type is meaningless" の警告 */
GetDisposeCallbackArgBlock52         const void* GetDisposeCallbackArg() const { return m_pCallbackArg; }
53 
54       public:
55         ut::LinkListNode m_Link;
56       private:
57         void* m_pBuffer;
58         u32 m_Size;
59         DisposeCallback m_Callback;
60         void* m_pCallbackArg;
61     };
62 
63     typedef ut::LinkList< Block, offsetof(Block,m_Link)> BlockList;
64 
65     class Section
66     {
67       public:
68         ~Section();
AppendBlock(Block * block)69         void AppendBlock( Block* block ) { m_BlockList.PushBack( block ); }
GetBlockList()70         const BlockList& GetBlockList() const { return m_BlockList; }
71 
72         void Dump( nw::snd::SoundDataManager& mgr, nw::snd::SoundArchive& arc );
73 
74       public:
75         ut::LinkListNode m_Link;
76       private:
77         BlockList m_BlockList;
78     };
79 
80     typedef ut::LinkList< Section, offsetof(Section,m_Link)> SectionList;
81 
82     /* ------------------------------------------------------------------------
83             constant declaration
84        ------------------------------------------------------------------------ */
85   private:
86     static const int HEAP_ALIGN = 32;
87 
88     /* ------------------------------------------------------------------------
89             static member
90        ------------------------------------------------------------------------ */
91   private:
92     static void InitHeapSection( FrameHeap::Section* section );
93 
94     /* ------------------------------------------------------------------------
95             class member
96        ------------------------------------------------------------------------ */
97   public:
98     FrameHeap();
99     ~FrameHeap();
100     bool  Create( void* startAddress, u32 size );
101     void  Destroy();
102 
103     void* Alloc(
104         u32 size,
105         FrameHeap::DisposeCallback callback,
106         void* callbackArg
107     );
108     void  Clear();
109 
110     int   SaveState();
111     void  LoadState( int level );
112     int   GetCurrentLevel() const;
113 
114     u32   GetSize() const;
115     u32   GetFreeSize() const;
116 
IsValid()117     bool  IsValid() const { return m_pHeap != NULL; }
118 
119     // TODO: 不要?
120     // const SectionList& GetSectionList() const { return m_SectionList; }
121 
122     void Dump( nw::snd::SoundDataManager& mgr, nw::snd::SoundArchive& arc );
123 
124   private:
125     ut::FrameHeap* m_pHeap;
126     SectionList m_SectionList;
127 
128     bool NewSection();
129     void ClearSection();
130 };
131 
132 } // namespace nw::snd::internal
133 } // namespace nw::snd
134 } // namespace nw
135 
136 
137 #endif /* NW_SND_FRAME_HEAP_H_ */
138 
139