贝贝花花包包店,精品555皮具,钱夹,皮夹

字体: | 推荐给好友 上一篇 | 下一篇

ACE程序员教程-第3章 ACE的内存管理

发布: 2008-6-13 14:30 | 作者: Umar Syyid | 来源: 转载 | 查看: 253次

 

3-2

#include "ace/Shared_Memory_MM.h"

#include "ace/Malloc.h"

#include "ace/Malloc_T.h"

#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 Malloc_Allocator;

 

static void server (void)

{

//Create the memory allocator passing it the shared memory

//pool that you want to use

Malloc_Allocator shm_allocator(poolname);

 

//Create a message, allocate memory for it and bind it with

//a name so that the client can the find it in the memory

//pool

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));

 

//How about a second message

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));

 

//Set the Server to go to sleep for a while so that the client has

//a chance to do its stuff

ACE_DEBUG((LM_DEBUG,

"Server done writing.. going to sleep zzz..\n\n\n"));

ACE_OS::sleep(2);

 

//Get rid of all resources allocated by the server. In other

//words get rid of the shared memory pool that had been

//previously allocated

shm_allocator.remove();

}

 

static void client(void)

{

//Create a memory allocator. Be sure that the client passes

// in the "right" name here so that both the client and the

//server use the same memory pool. We wouldn’t want them to

 

// BOTH create different underlying pools.

Malloc_Allocator shm_allocator(poolname);

 

//Get that first message. Notice that the find is looking up the

//memory based on the "name" that was bound to it by the server.

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);

 

//Lets get that second message now.

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::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);

}

 

int main (int, char *[])

{

switch (ACE_OS::fork ())

{

case -1:

ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "fork"), 1);

 

case 0:

// Make sure the server starts up first.

ACE_OS::sleep (1);

client ();

break;

 

default:

server ();

break;

}

 

return 0;

}

 

3.2.3 通过分配器接口使用Malloc

  大多数ACE中的容器类都可以接受分配器对象作为参数,以用于容器内的内存管理。因为某些内存分配方案只能用于ACE_Malloc类集,ACE含有一个适配器模板类ACE_Allocator_Adapter,它将ACE_Malloc类适配到ACE_Allocator接口。也就是说,在实例化这个模板之后创建的新类可用于替换任何ACE_Allocator。例如:

 

typedef ACE_Allocator_Adapter> Allocator;

 

这个新创建的Allocator类可用在任何需要分配器接口的地方,但它使用的却是采用ACE_Shared_Memory_PoolACE_Malloc的底层功能。这样该适配器就将Malloc类“适配”到了分配器(Allocator)类。

  这样的适配允许我们使用与ACE_Malloc类集相关联的功能,同时具有ACE_Allocator的动态绑定灵活性。但重要的是要记住,这样的灵活性是以牺牲部分性能为代价的。

 

44/4<1234
 

评分:0

我来说两句

seccode