1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     demo_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 "demo/Utility/demo_Utility.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 Arguments, allocates the buffers needed for DMPGL initialization and rendering.
49                           This function must be called once in advance.
50 
51                           Starting from the second call, all further calls are ignored if the class was not destroyed by a call of 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 argument 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 space and maximum space 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     } // namespace detail
103 
104     /*!
105         @brief    Initializes the memory manager class.
106 
107         @param[in]   fcramAddress      Start address of the memory region in FCRAM used for graphics
108         @param[in]   memorySize        Size of the memory region used for graphics
109     */
110     void InitializeMemoryManager(const uptr fcramAddress, const size_t memorySize);
111 
112     /*!
113         @brief    Terminates the memory manager.
114     */
115     void FinalizeMemoryManager(void);
116 
117     /*!
118         @brief    The current free space and maximum space of main memory and VRAM are sent to debug output.
119     */
120     void PrintMemoryManagerInfo(void);
121 
122     /*!
123         @brief    Memory allocator specified as an argument to the nngxInitialize function
124 
125         @param[in]   area    Memory region to allocate
126         @param[in]   aim     Purpose of the allocated buffer
127         @param[in]   id      ID of the buffer to allocate
128         @param[in]   size    Size of the buffer to allocate
129 
130         @return      Returns the address of the allocated buffer.
131     */
132     void* GetAllocator(GLenum area, GLenum aim, GLuint id, GLsizei size);
133 
134     /*!
135         @brief    Memory deallocator specified as an argument to the nngxInitialize function
136 
137         @param[in]   area    Memory region that contains the buffer to free
138         @param[in]   aim     Purpose of the buffer to free
139         @param[in]   id      ID of the buffer to free
140         @param[in]   addr    Address of the buffer to free
141     */
142     void GetDeallocator(GLenum area, GLenum aim, GLuint id, void* addr);
143 
144     /*!
145         @brief    Function that allocates the buffer from the heap in main memory managed by the memory manager
146 
147         :private
148 
149         For internal processing by the demo library.
150         Calls the allocator as a system application.
151 
152         @param[in]   size    Size of the buffer to allocate
153 
154         @return      Returns the address of the allocated buffer.
155     */
156     void* Alloc(size_t size);
157 
158     /*!
159         @brief    Frees the buffer allocated from the heap in main memory.
160 
161         :private
162 
163         For internal processing by the demo library.
164 
165         @param[in]   Address of the buffer to free
166     */
167     void Free(void* ptr);
168 }
169 
170 #endif
171