📄 428-432.html
字号:
</FONT>
<P>
<B>Essential Windows CE Application Programming</B>
<FONT SIZE="-1">
<BR>
<I>(Publisher: John Wiley & Sons, Inc.)</I>
<BR>
Author(s): Robert Burdick
<BR>
ISBN: 0471327476
<BR>
Publication Date: 03/01/99
</FONT>
<P>
<form name="Search" method="GET" action="http://search.earthweb.com/search97/search_redir.cgi">
<INPUT TYPE="hidden" NAME="Action" VALUE="Search">
<INPUT TYPE="hidden" NAME="SearchPage" VALUE="http://search.earthweb.com/search97/samples/forms/srchdemo.htm">
<INPUT TYPE="hidden" NAME="Collection" VALUE="ITK">
<INPUT TYPE="hidden" NAME="ResultTemplate" VALUE="itk-simple-intrabook.hts">
<INPUT TYPE="hidden" NAME="ViewTemplate" VALUE="view.hts">
<font face="arial, helvetica" size=2><b>Search this book:</b></font><br>
<INPUT NAME="queryText" size=50 VALUE=""> <input type="submit" name="submitbutton" value="Go!">
<INPUT type=hidden NAME="section_on" VALUE="on">
<INPUT type=hidden NAME="section" VALUE="http://www.itknowledge.com/reference/standard/0471327476/">
</form>
<!-- Empty Reference Subhead -->
<!--ISBN=0471327476//-->
<!--TITLE=Essential Windows CE Application Programming//-->
<!--AUTHOR=Robert Burdick//-->
<!--PUBLISHER=John Wiley & Sons, Inc.//-->
<!--IMPRINT=Wiley Computer Publishing//-->
<!--CHAPTER=16//-->
<!--PAGES=428-432//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="424-428.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="432-435.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><I>flAllocationType</I> specifies the memory allocation type. This can be any combination of the values shown in Table 16.1. Note that Windows CE includes a new allocation type, MEM_AUTO_COMMIT.</P>
<TABLE WIDTH="100%" BORDER RULES="ROWS"><CAPTION ALIGN=LEFT><B>Table 16.1</B> Virtual Memory Allocation Type Flags
<TR>
<TH ALIGN="LEFT" WIDTH="30%">FLAG
<TH ALIGN="LEFT" WIDTH="70%">MEANING
<TR>
<TD>MEM_COMMIT
<TD>Allocates physical memory for the region of pages being allocated.
<TR>
<TD>MEM_RESERVE
<TD>Reserves virtual memory, but does not allocate physical memory.
<TR>
<TD>MEM_TOP_DOWN
<TD>Allocates memory at the highest possible address.
<TR>
<TD VALIGN="TOP">MEM_AUTO_COMMIT
<TD>Reserves virtual memory, which is then automatically committed by Windows CE when accessed.
</TABLE>
<P>When a virtual memory page is allocated with the MEM_AUTO_COMMIT flag, the page is reserved as would be done by specifying MEM_RESERVE. However, the memory is automatically committed by Windows CE the first time the page is accessed.
</P>
<P><I>flProtect</I> specifies the type of access protection to be granted to the allocated virtual memory. The values PAGE_READONLY, granting read-only access, and PAGE_READWRITE, granting read-write access, are most common.</P>
<P>Freeing virtual memory is done by calling the Windows CE API <I>VirtualFree</I>:</P>
<!-- CODE SNIP //-->
<PRE>
VirtualFree(lpAddress, dwSize, dwFreeType);
</PRE>
<!-- END CODE SNIP //-->
<P><I>lpAddress</I> specifies the base address of virtual memory space to be freed. This value would be the return value of a previous <I>VirtualAlloc</I> call. <I>dwSize</I> is the size of the region to be freed.</P>
<P><I>dwFreeType</I> tells the function what to do. A value of MEM_DECOMMIT decommits the committed virtual memory pages. MEM_RELEASE tells <I>VirtualFree</I> to release previously reserved memory.</P>
<P>If <I>VirtualFree</I> succeeds, it returns TRUE. Otherwise, it returns FALSE.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B><B>I<SMALL>NTER-PROCESS</SMALL> V<SMALL>IRTUAL</SMALL> M<SMALL>EMORY</SMALL> A<SMALL>CCESS</SMALL></B>
<P><B>Windows CE does not allow applications to access virtual memory in the address space of other processes. Therefore, the virtual memory APIs <I>VirtualAllocEx</I>, <I>VirtualFreeEx</I>, <I>VirtualProtextEx</I>, and <I>VirtualQueryEx</I> are not supported under Windows CE.</B><HR></FONT>
</BLOCKQUOTE>
</P>
<P><FONT SIZE="+1"><B>Using Heaps</B></FONT></P>
<P>In some sense, allocating virtual memory pages is the most basic form of Windows CE memory allocation. As we have seen, <I>VirtualAlloc</I> never allocates a block of virtual memory smaller than the page size defined for the particular processor powering the device.</P>
<P>There are obviously times when an application needs to dynamically allocate much less memory than this. For example, an application might need to allocate memory to hold the contents of a file to be read from the Windows CE file system. After determining the size of the file, the application would allocate that number of bytes and read the file. The memory would be freed when the application is done using the file.</P>
<P>Applications typically use heaps for such dynamic memory allocation needs. Every Windows CE application has a default heap, created by the operating system when the application is launched. Applications can also create additional heaps that are separate from the default heap as needed.</P>
<P>An application’s default heap contains 384 memory pages reserved by Windows CE. These memory pages are committed by Windows CE only as needed, that is, as memory is allocated on the default heap.</P>
<P><FONT SIZE="+1"><B><I>Allocating Memory on the Default Heap</I></B></FONT></P>
<P>An application allocates memory on its default heap by calling the function <I>LocalAlloc</I>. Memory is freed from the default heap with <I>LocalFree</I>.</P>
<P><I>LocalAlloc</I> is defined as follows:</P>
<!-- CODE SNIP //-->
<PRE>
LocalAlloc(uFlags, uBytes);
</PRE>
<!-- END CODE SNIP //-->
<P><I>uBytes</I> indicates the number of bytes to be allocated on the default heap. <I>uFlags</I> contains one or more flags indicating how the memory is to be allocated. The allowable flag values are specified in Table 16.2.</P>
<TABLE WIDTH="100%" BORDER RULES="ROWS"><CAPTION ALIGN=LEFT><B>Table 16.2</B> LocalAlloc Memory Allocation Flags
<TR>
<TH WIDTH="25%" ALIGN="LEFT">FLAG
<TH WIDTH="75%" ALIGN="LEFT">MEANING
<TR>
<TD>LMEM_FIXED
<TD>Allocates fixed memory.
<TR>
<TD>LMEM_ZEROINIT
<TD>Initializes each byte of the allocated memory block to zero.
<TR>
<TD VALIGN="TOP">LPTR
<TD>Combines the LMEM_FIXED and LMEM_ZEROINIT flags, i.e., equals LMEM_FIXED | LMEM_ZEROINIT.
</TABLE>
<P><I>LocalAlloc</I> returns a handle to the allocated memory if the function is successful. Otherwise it returns NULL.</P>
<P>Applications typically specify the LPTR flag to allocate fixed heap memory, which is initialized to zero. The value returned by <I>LocalAlloc</I> in this case is a pointer to the allocated memory block. An application can therefore simply cast the return value to the proper pointer type and reference the allocated memory through that pointer.</P>
<P>For example, let’s say that we want to allocate enough default heap space to hold a 256-character Unicode string, including NULL terminator. The string is then filled with the contents of an edit control specified by <I>hwndEdit</I>. Our application would include the following code:</P>
<!-- CODE SNIP //-->
<PRE>
TCHAR* pszString; //Pointer to the string memory
int nCount = 257; //String length, plus NULL terminator
pszString = (TCHAR*)LocalAlloc(LPTR, nCount*sizeof(TCHAR));
if (pszString)
{
GetWindowText(hwndEdit, pszString, nCount);
}
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B><B>W<SMALL>INDOWS</SMALL> CE O<SMALL>NLY</SMALL> S<SMALL>UPPORTS</SMALL> F<SMALL>IXED</SMALL> M<SMALL>EMORY</SMALL> H<SMALL>EAPS</SMALL></B>
<P><B>Unlike Windows NT, Windows CE only allows fixed heap memory allocation. The LMEM_MOVEABLE flag is therefore not supported by the <I>LocalAlloc</I> function.</B><HR></FONT>
</BLOCKQUOTE>
</P>
<P>To prevent memory leaks from accumulating in your applications, dynamically allocated memory must be freed when it is no longer needed. Default heap memory is freed with the <I>LocalFree</I> API:</P>
<!-- CODE SNIP //-->
<PRE>
LocalFree(hMem);
</PRE>
<!-- END CODE SNIP //-->
<P><I>hMem</I> is the pointer to the block of heap memory to be freed. If successful, LocalFree returns NULL. Otherwise it returns back the <I>hMem</I> pointer.</P>
<P><FONT SIZE="+1"><B><I>Creating and Using Additional Application Heaps</I></B></FONT></P>
<P>Applications can create their own heaps in addition to the default heap created by Windows CE. Using such heaps may be advantageous in situations where an application needs to temporarily allocate lots of small pieces of memory.
</P>
<P>The classic example is a word processing application. A heap can be created to store the contents of each open document. As the documents are closed, the corresponding heaps are freed.</P>
<P>Using such custom heaps is similar to working with the application’s default heap. The primary difference is that the application must create and destroy such heaps.</P>
<P>An application creates a new heap by calling <I>HeapCreate</I>:</P>
<!-- CODE SNIP //-->
<PRE>
HeapCreate(flOptions, dwInitialSize, dwMaximumSize);
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="424-428.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="432-435.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<!-- all of the reference materials (books) have the footer and subfoot reveresed -->
<!-- reference_subfoot = footer -->
<!-- reference_footer = subfoot -->
<!-- BEGIN SUB FOOTER -->
<br><br>
</TD>
</TR>
</TABLE>
<table width="640" border=0 cellpadding=0 cellspacing=0>
<tr>
<td align="left" width=135><img src="/images/white.gif" width=100 height="1" alt="" border="0"></td>
<!-- END SUB FOOTER -->
<!-- all of the books have the footer and subfoot reveresed -->
<!-- reference_subfoot = footer -->
<!-- reference_footer = subfoot -->
<!-- FOOTER -->
<td width="515" align="left" bgcolor="#FFFFFF">
<font face="arial, helvetica" size="1"><b><a href="/products.html"><font color="#006666">Products</font></a> | <a href="/contactus.html"><font color="#006666">Contact Us</font></a> | <a href="/aboutus.html"><font color="#006666">About Us</font></a> | <a href="http://www.earthweb.com/corporate/privacy.html" target="_blank"><font color="#006666">Privacy</font></a> | <a href="http://www.itmarketer.com/" target="_blank"><font color="#006666">Ad Info</font></a> | <a href="/"><font color="#006666">Home</font></a></b>
<br><br>
Use of this site is subject to certain <a href="/agreement.html">Terms & Conditions</a>, <a href="/copyright.html">Copyright © 1996-1999 EarthWeb Inc.</a><br>
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.</font><p>
</td>
</tr>
</table>
</BODY>
</HTML>
<!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -