<HTML> <!-- ex01.html,v 1.2 2000/06/04 22:02:08 brunsch Exp --> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <META NAME="Author" CONTENT="Ambreen Ilyas"> <META NAME="GENERATOR" CONTENT="Mozilla/4.05 [en] (X11; I; SunOS 5.5.1 sun4u) [Netscape]"> <TITLE>Example 1</TITLE> </HEAD> <BODY> <FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT> <BR><FONT COLOR="#CC0000">//// This example is from the ACE Programmers Guide.</FONT> <BR><FONT COLOR="#CC0000">//// Chapter: "Memory Management"</FONT> <BR><FONT COLOR="#CC0000">//// For details please see the guide at</FONT> <BR><FONT COLOR="#CC0000">//// http://www.cs.wustl.edu/~schmidt/ACE.html</FONT> <BR><FONT COLOR="#CC0000">//// AUTHOR: Umar Syyid (usyyid@hns.com)</FONT> <BR><FONT COLOR="#CC0000">//// and Ambreen Ilyas (ambreen@bitsmart.com)</FONT> <BR><FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT> <P><FONT COLOR="#CC0000">//Example 1 </FONT> <BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Malloc.h"</FONT> <BR><FONT COLOR="#FF0000">//A chunk of size 1K is created</FONT> <BR><FONT COLOR="#000099">typedef</FONT> <FONT COLOR="#993300">char</FONT><FONT COLOR="#666600"> MEMORY_BLOCK[1024];</FONT> <BR> <BR> <P><FONT COLOR="#FF0000">//Create an ACE_Cached_Allocator which is passed in the type of the</FONT> <BR><FONT COLOR="#FF0000">//chunk that it must pre-allocate and assign on the free</FONT> <BR><FONT COLOR="#FF0000">//list</FONT> <BR><FONT COLOR="#000000">typedef ACE_Cached_Allocator<MEMORY_BLOCK,ACE_SYNCH_MUTEX> Allocator;</FONT> <BR> <BR>class MessageManager{ <BR>public: <BR><FONT COLOR="#FF0000">//The constructor is passed the number of chunks that the allocator should pre-allocate //and maintain on its free list.</FONT> <BR>MessageManager(int n_blocks): <BR> allocator_(n_blocks),message_count_(0){} <P><FONT COLOR="#FF0000">//Allocate memory for a message using the Allocator</FONT> <BR>void allocate_msg(const char *msg){ <BR> mesg_array_[message_count_]= <BR> (char*)allocator_.malloc(ACE_OS::strlen(msg)); <BR> ACE_OS::strcpy(mesg_array_[message_count_],msg); <BR> message_count_++; <BR> } <P><FONT COLOR="#FF0000">//Free all memory allocated. This will cause the chunks to be returned</FONT> <BR><FONT COLOR="#FF0000">//to the allocators internal free list and NOT to the OS.</FONT> <BR>void free_all_msg(){ <BR> for(int i=0;i<message_count_;i++) <BR> allocator_.free(mesg_array_[i]); <BR> message_count_=0; <BR> } <BR>void display_all_msg(){ <BR> for(int i=0;i<message_count_;i++) <BR> ACE_OS::printf("%s\n",mesg_array_[i]); <BR> } <BR> <BR>private: <BR> char *mesg_array_[20]; <BR> Allocator allocator_; <BR> int message_count_; <BR>}; <BR> <P>int main(int argc, char* argv[]){ <P>if(argc<2){ <BR> ACE_OS::printf("Usage: egXX <Number of blocks>\n"); <BR> exit(1); <BR> } <BR> <BR><FONT COLOR="#FF0000">//Instatiate the Memory Manager class</FONT> <BR>int n_blocks=ACE_OS::atoi(argv[1]); <BR>MessageManager mm(n_blocks); <BR> <P><FONT COLOR="#FF0000">//Use the Memory Manager class to assign messages and free them.</FONT> <FONT COLOR="#FF0000">Run this in your</FONT> <BR><FONT COLOR="#FF0000">//debug environment and you will notice that //the</FONT> <FONT COLOR="#FF0000">amount of memory your program uses</FONT> <BR><FONT COLOR="#FF0000">//after Memory Manager has been</FONT> <FONT COLOR="#FF0000">instantiated remains the same. That means the</FONT> <BR><FONT COLOR="#FF0000">//Cached Allocator</FONT> <FONT COLOR="#FF0000">controls or manages all the memory for the application.</FONT> <P><FONT COLOR="#FF0000">//Do forever.</FONT> <BR>while(1){ <BR> <FONT COLOR="#FF0000">//allocate the messages somewhere</FONT> <BR> for(int i=0; i<n_blocks;i++) <BR> mm.allocate_msg("Hi there"); <BR> <FONT COLOR="#FF0000">//show the messages</FONT> <BR> mm.display_all_msg(); <BR> <BR> for( i=0;i<n_blocks;i++) <BR> mm.free_all_msg(); <BR> } <BR>} <P> <A HREF="ex02.html">Next Example</A> </BODY> </HTML>