ACE Source Example - Memory Management

xiaoxiao2021-03-06  157

The ACE architecture contains a very rich memory management class. These classes make you easily and effectively manage dynamic memory (memory from the stack) and shared memory (memory shared between processes). You can use a number of different scenarios to manage memory. You need to decide what program is best for you to develop, and then use the appropriate ACE class to implement this program. ACE contains two groups of different classes for memory management. The first group is those classifications 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 group class is based on the ACE_Malloc template class. This group uses C templates and external polymorphisms to provide flexibility for memory allocation mechanisms. The class in this set of classes includes not only classes for local dynamic memory management, but also classes with shared memory between management processes. These shared memory classes use the underlying OS (OS) shared memory interface. Why use a group of categories rather than another group? This is determined by the trade-off between performance and flexibility. Because the actual dispenser object can change at runtime, the ACE_allocator class is more flexible. This is done by dynamic binding (which requires virtual functions in C ), so this flexibility is not required. The indirect of virtual function calls makes this solution to become more expensive. On the other hand, the ACE_Malloc class has better performance. At compile time, the Malloc class configures the memory splitter to be used. This compile time configuration is called "external polymorphism". ACE_Malloc-based distributors cannot be configured at runtime. Although ACE_malloc is more efficient, it is not flexible as ACE_Allocator. The following example has been released in "ACE Programmers Guide". These code are from hughes network systems. If you have any questions, you can send an email to umar syyid , or communicate with me hxhforwork@hotmail.com :)

/ 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 (usyyid @ hns. COM) and Ambreen ilyas (Ambreen@bitsmart.com) /

// Example 1 #include "ace / malloc.h" // a chunk of size 1k is created typef 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 // list typedef ACE_Cached_Allocator Allocator; class MessageManager {public: // The constructor is passed the number of chunks that the allocator should 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_ASG_ASGE_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

INT Main (int Argc, char * argv []) {

IF (Argc <2) {ACE_OS :: Printf ("Usage: Egxx / 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 The // Cached Allocator Controls or Manages All The Memory for the Application.

转载请注明原文地址:https://www.9cbs.com/read-128083.html

New Post(0)