//// This example is from the ACE Programmers Guide.
////  Chapter:  "Memory Management"
//// For details please see the guide at
//// http://www.cs.wustl.edu/~schmidt/ACE.html
////  AUTHOR: Umar Syyid ([email protected])
//// and Ambreen Ilyas ([email protected])

//Example 1 
#include "ace/Malloc.h"
//A chunk of size 1K is created
typedef char MEMORY_BLOCK[1024];

//Create an ACE_Cached_Allocator which is passed in the type of the
//chunk  that it must pre-allocate and assign on the free
typedef ACE_Cached_Allocator<MEMORY_BLOCK,ACE_SYNCH_MUTEX> Allocator;
class MessageManager{
//The constructor is passed the number of chunks that the allocator should pre-allocate //and maintain on its free list.
MessageManager(int n_blocks):

//Allocate memory for a message using the Allocator
void allocate_msg(const char *msg){

//Free all memory allocated. This will cause the chunks to be returned
//to the allocators internal free list and NOT to the OS.
void free_all_msg(){
 for(int i=0;i<message_count_;i++)
void display_all_msg(){
 for(int i=0;i<message_count_;i++)
 char *mesg_array_[20];
 Allocator allocator_;
 int message_count_;

int main(int argc, char* argv[]){

 ACE_OS::printf("Usage: egXX <Number of blocks>\n");
//Instatiate the Memory Manager class
int n_blocks=ACE_OS::atoi(argv[1]);
MessageManager mm(n_blocks);

//Use the Memory Manager class to assign messages and free them. Run this in your
//debug environment and you will notice that //the amount of memory your program uses
//after Memory Manager has been instantiated remains the same. That means the
//Cached Allocator controls or manages all the memory for the application.

//Do forever.
  //allocate the messages somewhere
 for(int i=0; i<n_blocks;i++)
  mm.allocate_msg("Hi there");
 //show the messages
 for( i=0;i<n_blocks;i++)

 Next Example