Ace source code example-memory management

Source: Internet
Author: User

The ACE architecture contains a rich set of memory management classes. These classes enable you to easily and effectively manage dynamic memory (memory applied from the heap) and shared memory (memory shared between processes ). You can manage the memory in several different ways. You need to decide which solution is most suitable for the application you are developing, and then use the appropriate Ace class to implement this solution.

Ace contains two groups of different classes for memory management.

The first group is the classes based on ace_allocator. This group uses dynamic binding and Policy modes to provide flexibility and scalability. They can only be used for local dynamic memory allocation.

The second class is based on the ace_malloc template class. This group uses the C ++ template and external polymorphism (external polymorphism) to provide flexibility for the memory allocation mechanism. The classes in this group include not only the classes used for local dynamic memory management, but also the classes used to manage shared memory between processes. These shared memory classes use the underlying OS Shared memory interface.

Why use a group of classes instead of another group? This is determined by the trade-off between performance and flexibility. Because the actual distributor object can be changed at runtime, The ace_allocator class is more flexible. This is done through dynamic binding (which requires virtual functions in C ++). Therefore, such flexibility is not costly. The indirect nature of virtual function calls makes this solution a more expensive choice.

On the other hand, the ace_malloc class has better performance. During compilation, the malloc class is configured through the memory distributor it will use. Such a compilation configuration is called "external polymorphism ". The ace_malloc-based Allocator cannot be configured at runtime. Although ace_malloc is more efficient, it is not as flexible as ace_allocator.

The following example has been published in ACE programmers guide.CodeAll from Hughes Network Systems. If you have any questions, you can send an email to Umar syyid <usyyid@hns.com> or communicate with me hxhforwork@hotmail.com

//////////////////////////////////////// //////////////////////////////////////// /////////////////////////////////
/// This example is from the ace programmers guide.
/// Chapter: "Memory Management"
/// For details please see the guide
/// Http://www.cs.wustl.edu /~ Schmidt/ace.html
/// Author: Umar syyid (usyyid@hns.com)
/// And ambreen Ilyas (ambreen@bitsmart.com)
//////////////////////////////////////// //////////////////////////////////////// /////////////////////////////

// Example 1
# Include "ACE/malloc. H"
// A chunk of size 1 K is created
Typedef char memory_block [1024];

 

// Create an ace_cached_allocator which is passed in the type of
// Chunk that it must pre-allocate and assign on the free
// List
Typedef ace_cached_allocator <memory_block, ace_synch_mutex> Allocator;

Class messagemanager {
Public:
// The constructor is passed the number of chunks that the Allocator shoshould pre-allocate // and maintain on its free list.
Messagemanager (INT n_blocks ):
Allocator _ (n_blocks), message_count _ (0 ){}

// Allocate memory for a message using the Allocator
Void allocate_msg (const char * MSG ){
Mesg_array _ [message_count _] =
(Char *) Allocator _. malloc (ace_ OS: strlen (MSG ));
Ace_ OS: strcpy (mesg_array _ [message_count _], MSG );
Message_count _ ++;
}

// 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 ++)
Allocator _. Free (mesg_array _ [I]);
Message_count _ = 0;
}
Void display_all_msg (){
For (INT I = 0; I <message_count _; I ++)
Ace_ OS: printf ("% s/n", mesg_array _ [I]);
}

PRIVATE:
Char * mesg_array _ [20];
Allocator _;
Int message_count _;
};
 

Int main (INT argc, char * argv []) {

If (argc <2 ){
Ace_ OS: printf ("Usage: egxx <number of blocks>/N ");
Exit (1 );
}

// 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
// Cached Allocator controls or manages all the memory for the application.

// Do forever.
While (1 ){
// Allocate the messages somewhere
For (INT I = 0; I <n_blocks; I ++)
Mm. allocate_msg ("Hi There ");
// Show the messages
Mm. display_all_msg ();

For (I = 0; I <n_blocks; I ++)
Mm. free_all_msg ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.