1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<HTML>
3<HEAD>
4<META http-equiv="Content-Type" content="text/html; charset=windows-1252">
5<META http-equiv="Content-Style-Type" content="text/css">
6<META name="GENERATOR" content="Microsoft FrontPage 5.0">
7<TITLE>The Expanded Heap Manager</TITLE>
8<LINK rel="stylesheet" type="text/css" href="../CSS/revolution.css">
9</HEAD>
10<BODY>
11
12<H1>The Expanded Heap Manager</H1>
13<p>
14The Expanded Heap Manager is a memory manager that allows you to freely allocate and deallocate memory, similar to the C standard library <CODE>malloc</CODE> and <CODE>free</CODE> functions. In addition to allocating and freeing memory, this manager has additional functions for game programs. The following is an overview of the Expanded Heap Manager.
15</p>
16
17<H2>Creating Heaps</H2>
18<p>
19To use the Expanded Heap Manager, you must first create an expanded heap. The following functions create and destroy expanded heaps.
20</p>
21
22<TABLE class="api_list" border="1">
23<CAPTION>Functions to Create and Destroy Expanded Heaps</CAPTION>
24<TR><TD><B>Functions</B></TD><TD><B>Features</B></TD></TR>
25<TR><TH><A HREF="./ExpHeap/MEMCreateExpHeap.html">MEMCreateExpHeap</A></TH><TD>Creates the expanded heap.</TD></TR>
26<TR><TH><A HREF="./ExpHeap/MEMCreateExpHeapEx.html">MEMCreateExpHeapEx</A></TH><TD>Creates the expanded heap. Heap options can be specified.</TD></TR>
27<TR><TH><A HREF="./ExpHeap/MEMDestroyExpHeap.html">MEMDestroyExpHeap</A></TH><TD>Destroys the expanded heap.</TD></TR>
28</TABLE>
29
30<H2>Allocating Memory Blocks</H2>
31<H3>Allocating and Freeing Memory Blocks</H3>
32<p>
33The following functions allocate and free memory blocks.
34</p>
35
36<TABLE class="api_list" border="1">
37<CAPTION>Functions for Allocating and Freeing Memory Blocks</CAPTION>
38<TR><TD><B>Functions</B></TD><TD><B>Features</B></TD></TR>
39<TR><TH><A HREF="./ExpHeap/MEMAllocFromExpHeap.html">MEMAllocFromExpHeap</A></TH><TD>Allocates a memory block from the expanded heap.</TD></TR>
40<TR><TH><A HREF="./ExpHeap/MEMAllocFromExpHeapEx.html">MEMAllocFromExpHeapEx</A></TH><TD>Allocates a memory block from the expanded heap. The alignment can be specified (described in the next section).</TD></TR>
41<TR><TH><A HREF="./ExpHeap/MEMFreeToExpHeap.html">MEMFreeToExpHeap</A></TH><TD>Deallocates a memory block.</TD></TR>
42</TABLE>
43
44<H3>Allocating the Minimum Memory Block Size</H3>
45<p>
46The Expanded Heap Manager needs 16 bytes to use as a memory block management region. The minimum alignment of allocated memory blocks must be on a 4-byte boundary; therefore, even if a one-byte memory block is allocated, 20 bytes of memory are consumed.
47</p>
48
49<H3>Procedure for Allocating Memory Blocks</H3>
50<p>
51The Expanded Heap Manager has two modes that identify available regions for allocating memory blocks. You can switch between modes. These modes are described below.
52</p>
53
54<TABLE class="api_list" border="1">
55<CAPTION>Memory Block Allocation Modes</CAPTION>
56<TR><TD><B>Mode</B></TD><TD><B>Content</B></TD></TR>
57<TR><TH><CODE>FIRST</CODE> Mode</TH><TD>Allocates a memory block from the first free region that is at least the size of the memory block to allocate.</TD></TR>
58<TR><TH><CODE>NEAR</CODE> Mode</TH><TD>Searches for a free region nearest in size to the memory block to allocate, then allocates the memory block from this free region.</TD></TR>
59</TABLE>
60
61<CENTER>
62<IMG SRC="./fig2-1.gif" width="578" height="210"><BR> <B>Figure 2-1 Procedure for Allocating Expanded Heap Memory Blocks</B><BR>
63</CENTER>
64
65<p>
66<CODE>FIRST</CODE> is the default mode. Unlike <CODE>FIRST</CODE>, <CODE>NEAR</CODE> allocates an available block nearest in size to the memory block to allocate. If an exact match is not found, this mode searches all free regions for the nearest match. Therefore, memory block allocation takes longer if free regions are fragmented. The following functions set and acquire the allocation mode.
67</p>
68
69<TABLE class="api_list" border="1">
70<CAPTION>Functions for Setting and Getting Allocation Modes</CAPTION>
71<TR><TD><B>Functions</B></TD><TD><B>Features</B></TD></TR>
72<TR><TH><A HREF="./ExpHeap/MEMSetAllocModeForExpHeap.html">MEMSetAllocModeForExpHeap</A></TH><TD>Sets the allocation mode.</TD></TR>
73<TR><TH><A HREF="./ExpHeap/MEMGetAllocModeForExpHeap.html">MEMGetAllocModeForExpHeap</A></TH><TD>Gets the current allocation mode.</TD></TR>
74</TABLE>
75
76<p>
77The following are the allocation mode types that different functions use.
78</p>
79
80<TABLE class="api_list" border="1">
81<CAPTION>Allocation Mode Types That Functions Use</CAPTION>
82<TR><TD><B>Mode</B></TD><TD><B>Value Specified by the Function</B></TD></TR>
83<TR><TH><CODE>FIRST</CODE> Mode</TH><TD><CODE><CODE>MEM_EXPHEAP_ALLOC_MODE_FIRST</CODE></CODE></TD></TR>
84<TR><TH><CODE>NEAR</CODE> Mode</TH><TD><CODE><CODE>MEM_EXPHEAP_ALLOC_MODE_NEAR</CODE></CODE></TD></TR>
85</TABLE>
86
87<H3>Allocating Memory Blocks from the Top of the Heap Region</H3>
88<p>
89Generally, the Expanded Heap Manager searches for free regions from the lowest to highest address in the heap region and allocates memory blocks to the free regions starting from the bottom. As an alternative, you can search for free regions from the highest to lowest address in the heap. Using this feature, you can allocate longer&ndash;term memory blocks from the beginning of the heap region, and temporary memory blocks from the end of the heap region to help minimize heap fragmentation. For example, you can use this feature to load compressed data into memory, expand the compressed data, and then delete the compressed data source. In this case, if the expansion process is performed by allocating the memory block from the bottom of the heap region as usual, the free region will be divided into two.
90</p>
91
92<CENTER>
93<IMG SRC="./fig2-2.gif" width="578" height="179"><BR> <B>Figure 2-2 Mechanism for Segmenting Memory Blocks</B><BR>
94</CENTER>
95
96<p>
97In contrast, if you temporarily load the compressed data into a memory block allocated from the top of the heap region, the free region is not divided.
98</p>
99
100<CENTER>
101<IMG SRC="./fig2-3.gif" width="578" height="179"><BR> <B>Figure 2-3 Method for Segmenting Expanded Heap Memory Blocks</B><BR>
102</CENTER>
103
104<p>
105To allocate memory blocks from the top of the heap region, use the memory block allocation function, <A HREF="./ExpHeap/MEMAllocFromExpHeapEx.html"><CODE>MEMAllocFromExpHeapEx</CODE></A>, and pass a negative value to the alignment argument (described below).
106</p>
107
108<H2>Specifying Alignment</H2>
109<p>
110The Expanded Heap Manager specifies alignment during memory block allocation. In the <A HREF="./ExpHeap/MEMAllocFromExpHeapEx.html"><CODE>MEMAllocFromExpHeapEx</CODE></A> function, you can specify 4, 8, 16 or 32 as alignment values. If negative values (i.e., -4, -8, -16 or -32) are specified, memory blocks are allocated from the top of the heap. The <A HREF="./ExpHeap/MEMAllocFromExpHeap.html"><CODE>MEMAllocFromExpHeap</CODE></A> function does not specify an alignment; the alignment value is always 4.
111</p>
112<p>
113By default, the small memory fragments produced by alignment are not reused as allocatable memory. To allocate this fragmentary memory as valid regions, enable its reuse with the <A HREF="./ExpHeap/MEMUseMarginOfAlignmentForExpHeap.html"><CODE>MEMUseMarginOfAlignmentForExpHeap</CODE></A> function. However, note that performance may decline if a great number of memory fragments are registered.
114</p>
115
116<H2>Adjusting the Heap Region Size</H2>
117<p>
118The Expanded Heap Manager can reduce the heap region size to match the heap content. Use this functionality only if memory blocks are not allocated from the top of the heap region. Also note that regions of free memory that are closer to the bottom than the already allocated memory blocks are not reduced in size and remain unchanged.
119</p>
120<p>
121After placing data of undefined size and number in memory, this feature can be used to optimize the heap size when allocating additional memory is not required. First, create an extended heap with a sufficient size, allocate memory blocks from the heap region, and store the data. After storing all the required data and freeing all memory blocks allocated from the top of the heap region, reduce the heap size to match its content.
122<p>
123
124<CENTER>
125<IMG SRC="./fig2-4.gif" width="578" height="256"><BR> <B>Figure 2-4 Adjusting the Size of the Extended Heap</B><BR>
126</CENTER>
127
128<p>
129The following function reduces heap region size.
130</p>
131
132<TABLE class="api_list" border="1">
133<CAPTION>Function for Reducing the Size of the Heap Region</CAPTION>
134<TR><TD><B>Functions</B></TD><TD><B>Features</B></TD></TR>
135<TR><TH><A HREF="./ExpHeap/MEMAdjustExpHeap.html">MEMAdjustExpHeap</A></TH><TD>Reduces the size of the heap region by freeing unused regions at the top of the heap region.</TD></TR>
136</TABLE>
137
138<H2>Changing Memory Block Size</H2>
139<p>
140The Expanded Heap Manager can change the size of the allocated memory blocks without moving them. If a memory block is reduced to a size smaller than it originally was, the leftover region is used as a free region. If a memory block is expanded to a size larger than it originally was, it must have sufficient free space available above it for this to happen. If there is a free region above a memory block, this function merges the free region with the memory block to increase the size of the memory block.
141</p>
142
143<CENTER>
144<IMG SRC="./fig2-5.gif" width="578" height="235"><BR> <B>Figure 2-5 Changing the Size of Expanded Heap Memory Blocks</B><BR>
145</CENTER>
146
147<p>
148Use this function to change memory block size.
149</p>
150
151<TABLE class="api_list" border="1">
152<CAPTION>Function for Changing the Size of Memory Blocks</CAPTION>
153<TR><TD><B>Functions</B></TD><TD><B>Features</B></TD></TR>
154<TR><TH><A HREF="./ExpHeap/MEMResizeForMBlockExpHeap.html">MEMResizeForMBlockExpHeap</A></TH><TD>Expands or reduces memory blocks. Returns the changed memory block size.</TD></TR>
155</TABLE>
156
157<p>
158When reducing memory blocks, if the specified memory block size is only slightly different from the current actual memory block size, then the free region that would be created after this block is reduced sometimes cannot be used effectively. In such cases, the <A HREF="./ExpHeap/MEMResizeForMBlockExpHeap.html"><CODE>MEMResizeForMBlockExpHeap</CODE></A> function leaves the memory block's size unchanged and returns the current actual memory block size. If you attempt to expand a memory block when either there is no free region directly above the memory block, or it was not possible to achieve the desired size even after merging the current memory block with the free region above it, the <A HREF="./ExpHeap/MEMResizeForMBlockExpHeap.html"><CODE>MEMResizeForMBlockExpHeap</CODE></A> function fails and returns zero.
159</p>
160
161<H2>Acquiring Free Space</H2>
162<p>
163The Expanded Heap Manager can get the total amount of available regions. It can also obtain the size of the largest allocatable memory block. These functions are shown in the following table.
164</p>
165
166<TABLE class="api_list" border="1">
167<CAPTION>Functions for Acquiring Free Memory</CAPTION>
168<TR><TD><B>Functions</B></TD><TD><B>Features</B></TD></TR>
169<TR><TH><A HREF="./ExpHeap/MEMGetTotalFreeSizeForExpHeap.html">MEMGetTotalFreeSizeForExpHeap</A></TH><TD>Gets the total size of all available regions in the expanded heap.</TD></TR>
170<TR><TH><A HREF="./ExpHeap/MEMGetAllocatableSizeForExpHeap.html">MEMGetAllocatableSizeForExpHeap</A></TH><TD>Gets the size of the maximum allocatable memory block. Alignment is fixed to four.</TD></TR>
171<TR><TH><A HREF="./ExpHeap/MEMGetAllocatableSizeForExpHeapEx.html">MEMGetAllocatableSizeForExpHeapEx</A></TH><TD>Gets the size of the maximum allocatable memory block. Alignment can be specified.</TD></TR>
172</TABLE>
173
174<H2>Group IDs</H2>
175<p>
176When the Expanded Heap Manager allocates memory blocks, group IDs from 0 through 255 are stored in the memory block management region. A group ID can be modified as necessary. When a group ID is changed, the change occurs during the next memory block allocation. The group ID is used for the following purposes.
177<p>
178<UL>
179<LI>Collectively deallocate only memory blocks with a specific group ID.
180<LI>Check memory use for each group ID. By managing group IDs per use or user, seeing how memory is used is easier.
181</UL>
182
183<p>
184The following functions set and get group IDs.
185</p>
186
187<TABLE class="api_list" border="1">
188<CAPTION>Functions for Setting and Getting Group IDs</CAPTION>
189<TR><TD><B>Functions</B></TD><TD><B>Features</B></TD></TR>
190<TR><TH><A HREF="./ExpHeap/MEMSetGroupIDForExpHeap.html">MEMSetGroupIDForExpHeap</A></TH><TD>Sets the group ID of the expanded heap.</TD></TR>
191<TR><TH><A HREF="./ExpHeap/MEMGetGroupIDForExpHeap.html">MEMGetGroupIDForExpHeap</A></TH><TD>Gets the group ID of the expanded heap.</TD></TR>
192</TABLE>
193
194<H2>Processes for Memory Blocks</H2>
195<p>
196The Expanded Heap Manager can perform user-specified processes on the allocated memory block. With this functionality, you can perform various processes on the heap that are not already prepared in the Expanded Heap Manager. Below are some examples.
197<p>
198
199<UL>
200<LI>Implement a feature that batch-frees, for temporary use, only memory blocks allocated from the top of the heap region.
201<LI>Get the total capacity of all memory blocks with a specific group ID.
202</UL>
203
204<p>
205Functions that can cause a specified process to be performed are as follows.
206</p>
207
208<TABLE class="api_list" border="1">
209<TR><TD><B>Functions</B></TD><TD><B>Features</B></TD></TR>
210<TR><TH><A HREF="./ExpHeap/MEMVisitAllocatedForExpHeap.html">MEMVisitAllocatedForExpHeap</A></TH><TD>Calls a user-specified function for each allocated memory block.</TD></TR>
211</TABLE>
212
213<H2>Getting Memory Block Information</H2>
214<p>
215The Expanded Heap Manager can get allocated memory block information that indicates memory block size, group ID, and whether the allocated memory blocks were allocated from the lowest or highest address. The functions that get memory block information are shown below.
216</p>
217
218<TABLE class="api_list" border="1">
219<CAPTION>Functions for Getting Memory Block Information</CAPTION>
220<TR><TD><B>Functions</B></TD><TD><B>Features</B></TD></TR>
221<TR><TH><A HREF="./ExpHeap/MEMGetSizeForMBlockExpHeap.html">MEMGetSizeForMBlockExpHeap</A></TH><TD>Gets the size of the memory block.</TD></TR>
222<TR><TH><A HREF="./ExpHeap/MEMGetGroupIDForMBlockExpHeap.html">MEMGetGroupIDForMBlockExpHeap</A></TH><TD>Gets the group ID of the memory block.</TD></TR>
223<TR><TH><A HREF="./ExpHeap/MEMGetAllocDirForMBlockExpHeap.html">MEMGetAllocDirForMBlockExpHeap</A></TH><TD>Gets the allocation direction of the memory block.</TD></TR>
224</TABLE>
225
226<H2>Checking Heaps and Memory Blocks</H2>
227<p>
228The Expanded Heap Manager can check whether the expanded heap and allocated memory blocks from the expanded heap are destroyed. The functions that check expanded heaps and memory blocks are shown below.
229</p>
230
231<TABLE class="api_list" border="1">
232<CAPTION>Functions that Check Extended Heaps and Memory Blocks</CAPTION>
233<TR><TD><B>Functions</B></TD><TD><B>Features</B></TD></TR>
234<TR><TH><A HREF="./ExpHeap/MEMCheckExpHeap.html">MEMCheckExpHeap</A></TH><TD>Checks whether the extended heap is destroyed.</TD></TR>
235<TR><TH><A HREF="./ExpHeap/MEMCheckForMBlockExpHeap.html">MEMCheckForMBlockExpHeap</A></TH><TD>Checks whether a memory block is destroyed.</TD></TR>
236</TABLE>
237
238<H2>Revision History</H2>
239<P>
2402006/03/01 Initial version.<BR>
241</P>
242
243<hr><p>CONFIDENTIAL</p></body>
244</HTML>