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