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程式員教程》內容。
http://www.acejoy.com/space/html/50/n-50.html
這裡給出兩個執行個體的代碼,雖然書上有代碼,有些不是太全,少些標頭檔之類。
Allocator執行個體代碼:
#include "ace/Synch_Traits.h"
#include "ace/Thread_Mutex.h"
#include "ace/malloc.h"
#include "ace/Malloc_T.h"
#include "ace/log_msg.h"
#include "ace/os.h"
#ifdef _DEBUG
#pragma comment(lib,"aced.lib")
#else
#pragma comment(lib,"ace.lib")
#endif
typedef char MEMORY_BLOCK[1024];
typedef ACE_Cached_Allocator <MEMORY_BLOCK,ACE_SYNCH_MUTEX> Allocator;
class MessageManager
{
public:
MessageManager(int n_blocks):allocator_(n_blocks),message_count_(0)
{
mesg_array = new char *[n_blocks];
}
void allocate_msg(const char *msg)
{
mesg_array[message_count_] = (char *)allocator_.malloc(ACE_OS::strlen(msg)+1);
ACE_OS::strcpy(mesg_array[message_count_],msg);
message_count_++;
}
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]);
}
}
protected:
private:
char **mesg_array;
Allocator allocator_;
int message_count_;
};
int main(int argc,char *argv[])
{
if(argc<2)
{
ACE_DEBUG((LM_DEBUG,"usage:%s <number of blocks>/n",argv[0]));
exit(1);
}
int n_blocks = ACE_OS::atoi(argv[1]);
MessageManager mm(n_blocks);
while (1)
{
ACE_DEBUG((LM_DEBUG,"/n/n/nAllocating Messages/n"));
char message[1024];
for(int i=0;i<n_blocks;i++)
{
memset(message,0,sizeof(message));
ACE_OS::sprintf(message,"Message %d:Hi There",i);
mm.allocate_msg(message);
}
ACE_DEBUG((LM_DEBUG,"Displaying the messages/n"));
ACE_OS::sleep(2);
mm.display_all_msg();
ACE_DEBUG((LM_DEBUG,"Releasing message/n"));
ACE_OS::sleep(2);
mm.free_all_msg();
}
return 0;
}
Malloc執行個體代碼:
#include "ace/synch_traits.h"
#include "ace/thread_mutex.h"
#include "ace/Null_Mutex.h"
//#include "ace/shared_memory_mm.h"
#include <ace/mmap_Memory_Pool.h>
#include "ace/malloc.h"
#include "ace/malloc_T.h"
#include "ace/os.h"
#pragma comment(lib,"aced.lib")
#define DATA_SIZE 100
#define MESSAGE1 "Hiya over there client process"
#define MESSAGE2 "Did you hear me the first time?"
LPCTSTR poolname = "My_Pool";
//typedef ACE_Malloc<ACE_Shared_Memory_Pool,ACE_Null_Mutex> Malloc_Allocator; //支援UNIX環境,在NT下無法編譯通過。如果想在NT下編譯成功使用下面的記憶體池類型
typedef ACE_Malloc <ACE_MMAP_MEMORY_POOL, ACE_Null_Mutex> Malloc_Allocator;
//伺服器處理序
static void server(void)
{
Malloc_Allocator shm_allocator(poolname);
char *Message1 = (char *)shm_allocator.malloc(strlen(MESSAGE1));
ACE_OS::strcpy(Message1,MESSAGE1);
shm_allocator.bind("FirstMessage",Message1);
ACE_DEBUG((LM_DEBUG,"<<%s/n",Message1));
char *Message2 = (char *)shm_allocator.malloc(strlen(MESSAGE2));
ACE_OS::strcpy(Message2,MESSAGE2);
shm_allocator.bind("SecondMessage",Message2);
ACE_DEBUG((LM_DEBUG,"<<%s/n",Message2));
ACE_DEBUG((LM_DEBUG,"Server done writing ..going to sleep zzz../n/n/n"));
ACE_OS::sleep(5);
shm_allocator.remove();
}
//用戶端進程
static void client(void)
{
Malloc_Allocator shm_allocator(poolname);
void *Message1;
if(shm_allocator.find("FirstMessage",Message1)==-1)
{
ACE_ERROR((LM_ERROR,"client: problem cant find data that server has sent/n"));
ACE_OS::exit(1);
}
ACE_OS::printf(">>%s/n",(char*)Message1);
ACE_OS::fflush(stdout);
void *Message2;
if(shm_allocator.find("SecondMessage",Message2)==-1)
{
ACE_ERROR((LM_ERROR,"client: problem cant find data that server has sent/n"));
ACE_OS::sleep(2);
ACE_OS::exit(1);
}
ACE_OS::printf(">>%s/n",(char*)Message2);
ACE_OS::fflush(stdout);
ACE_DEBUG((LM_DEBUG,"client done reading! BYE NOW/n"));
ACE_OS::fflush(stdout);
ACE_OS::sleep(2);
}
int main(int argc,char *argv[])
{
//NT環境無法運行,fork調用失敗(fork需要支援UNIX環境)。
switch(ACE_OS::fork())
{
case -1:
ACE_ERROR_RETURN((LM_ERROR,"%p/n","fork"),1);
break;
case 0:
ACE_OS::sleep(1);
client();
break;
default:
server();
break;
}
//NT 環境下測試
//main函數放server()函數產生一個版本server.exe,main函數放client()函數產生另一個版本client.exe
//先運行server.exe,接著運行client.exe,就可以看到共用記憶體資料的效果。(注意:設定server.exe等待時間比client.exe長)
return 0;
}