1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     MemoryManager.h
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  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   $Rev: 46365 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef MEMORY_MANAGER_H_
17 #define MEMORY_MANAGER_H_
18 
19 #include <nn.h>
20 
21 namespace demo
22 {
23     namespace memory_manager
24     {
25 
26         namespace detail
27         {
28             /*!
29                 @brief Class that manages memory regions allocated for graphics.
30 
31                        Allocates or releases regions in main memory or VRAM as needed.
32             */
33 
34             class MemoryManager : private nn::util::NonCopyable<MemoryManager>
35             {
36             public:
37                 /*!
38                     @brief    Constructor.
39                 */
40                 MemoryManager(void);
41 
42                 /*!
43                 @brief    Destructor.
44                 */
45                 virtual ~MemoryManager();
46 
47                 /*!
48                     @brief    Initializes the memory manager class.
49 
50                               Using the memory region specified by the parameters, allocates the buffers needed for DMPGL initialization and rendering.
51                               This function must be called once before use.
52 
53                               Starting from the second call, all further calls are ignored if the class was not destroyed by Finalize function.
54 
55                     @param[in]   fcramAddress      Start address of the memory region in FCRAM used for graphics
56                     @param[in]   memorySize        Size of the memory region used for graphics
57                 */
58                 void Initialize(const uptr fcramAddress, const size_t memorySize);
59 
60                 /*!
61                     @brief    Finalize the memory manager class.
62                 */
63                 void Finalize(void);
64 
65                 /*!
66                     @brief    This allocates a memory region managed by this class.
67 
68                     @param[in]   area    Memory region to allocate
69                     @param[in]   aim     Purpose of the allocated buffer
70                     @param[in]   id      ID of the buffer to allocate
71                     @param[in]   size    Size of the buffer to allocate
72 
73                     @return      Returns the address of the allocated buffer.
74                 */
75                 void* Allocate(GLenum area, GLenum aim, GLuint id, GLsizei size);
76 
77                 /*!
78                     @brief    Frees the memory region allocated by this class.
79 
80                               Note that the region is not freed if the area argument specifies VRAM.
81 
82                     @param[in]   area    Memory region that contains the buffer to free
83                     @param[in]   aim     Purpose of the buffer to free
84                     @param[in]   id      ID of the buffer to free
85                     @param[in]   addr    Address of the buffer to free
86                 */
87                 void    Deallocate(GLenum area, GLenum aim, GLuint id, void* addr);
88 
89 
90                 /*!
91                     @brief    The current free capacity and maximum capacity of main memory and VRAM are sent to debug output.
92                 */
93                 void PrintFreeMemorySize(void);
94 
95             private:
96                 bool            m_Initialized;
97                 u8              m_Pad[3];
98                 uptr            m_pStartAddrFcram;
99                 uptr            m_CurrentAddrVramA;
100                 uptr            m_CurrentAddrVramB;
101                 nn::fnd::ExpHeap    m_HeapOnFcram;
102                 size_t              m_AllocatedBlockSize;
103 
104             public:
105                 bool m_DebugPrint;
106                 NN_PADDING3;
107             };
108         } // namespace detail
109 
110         /*!
111             @brief    Initializes the memory manager class.
112 
113             @param[in]   fcramAddress      Start address of the memory region in FCRAM used for graphics
114             @param[in]   memorySize        Size of the memory region used for graphics
115         */
116         void InitializeMemoryManager(const uptr fcramAddress, const size_t memorySize);
117 
118         /*!
119             @brief    Terminate the memory manager.
120         */
121         void FinalizeMemoryManager(void);
122 
123         /*!
124             @brief    The current free capacity and maximum capacity of main memory and VRAM are sent to debug output.
125         */
126         void PrintMemoryManagerInfo(void);
127 
128         /*!
129             @brief    Memory allocator specified as an argument to the nngxInitialize function
130 
131             @param[in]   area    Memory region to allocate
132             @param[in]   aim     Purpose of the allocated buffer
133             @param[in]   id      ID of the buffer to allocate
134             @param[in]   size    Size of the buffer to allocate
135 
136             @return      Returns the address of the allocated buffer.
137         */
138         void* GetAllocator(GLenum area, GLenum aim, GLuint id, GLsizei size);
139 
140         /*!
141             @brief    Memory deallocator specified as an argument to the nngxInitialize function
142 
143             @param[in]   area    Memory region that contains the buffer to free
144             @param[in]   aim     Purpose of the buffer to free
145             @param[in]   id      ID of the buffer to free
146             @param[in]   addr    Address of the buffer to free
147         */
148         void GetDeallocator(GLenum area, GLenum aim, GLuint id, void* addr);
149 
150         /*!
151             @brief    Function that allocates the buffer from the heap in main memory managed by the memory manager
152 
153             :private
154 
155             For internal processing by the demo library.
156             Call the allocator as a system application.
157 
158             @param[in]   size    Size of the buffer to allocate
159 
160             @return      Returns the address of the allocated buffer.
161         */
162 
163         void* Alloc(size_t size);
164 
165         void* Alloc(GLenum aim, const size_t size);
166         void* Alloc(GLenum area, GLenum aim, const size_t size);
167 
168         /*!
169             @brief    Free the buffer allocated from the heap in main memory.
170 
171             :private
172 
173             For internal processing by the demo library.
174 
175             @param[in]   Address of the buffer to free
176         */
177         void Free(void* ptr);
178 
179     }
180 }
181 
182 #endif
183