ACE源碼樣本 — Memory Management

來源:互聯網
上載者:User

ACE構架含有一組非常豐富的記憶體管理類。這些類使得你能夠很容易和有效地管理動態記憶體(從堆中申請的記憶體)和共用記憶體(在進程間共用的記憶體)。你可以使用若干不同的方案來管理記憶體。你需要決定何種方案最適合你正在開發的應用,然後採用恰當的ACE類來實現此方案。

  ACE含有兩組不同的類用於記憶體管理。

  第一組是那些基於ACE_Allocator的類。這組類使用動態綁定和策略模式來提供靈活性和可擴充性。它們只能用於局部的動態記憶體分配。

  第二組類基於ACE_Malloc模板類。這組類使用C++模板和外部多態性 (External Polymorphism)來為記憶體配置機制提供靈活性。在這組類中的類不僅包括了用於局部動態記憶體管理的類,也包括了管理進程間共用記憶體的類。這些共用記憶體類使用底層OS(OS)共用記憶體介面。

  為什麼使用一組類而不是另外一組呢?這是由在效能和靈活性之間所作的權衡決定的。因為實際的分配器對象可以在運行時改變,ACE_Allocator類更為靈活。這是通過動態綁定(這在C++裡需要使用虛函數)來完成的,因此,這樣的靈活性並非不需要代價。虛函數調用帶來的間接性使得這一方案成了更為昂貴的選擇。

  另一方面,ACE_Malloc類有著更好的效能。在編譯時間,malloc類通過它將要使用的記憶體 Clerk進行配置。這樣的編譯時間配置被稱為“外部多態性”。基於ACE_Malloc的分配器不能在運行時進行配置。儘管ACE_Malloc效率更高,它不像ACE_Allocator那樣靈活。

以下樣本是在《 ACE programmers guide》中已經發布過的. 這些代碼都是出自Hughes Network Systems. 如有疑問可以發郵件給 Umar Syyid <usyyid@hns.com> ,或者與我交流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
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
//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 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_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 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 the
//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();
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.