找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4718|回复: 1

在ACE_Message_Block中使用Allocator内存池

[复制链接]
发表于 2011-3-4 21:03:39 | 显示全部楼层 |阅读模式
本帖最后由 laocai 于 2011-3-4 21:08 编辑

在ACE_Message_Block的构造函数中可以传入相应的Allocator作为其里面数据的分配器,其中allocator_strategy专用于分配内部ACE_Data_Block里面的buffer,data_block_allocator用于分配内部的ACE_Data_Block,message_block_allocator用于内部构造新的ACE_Message_Block时使用。不过具体使用还得注意一些细节,如果这些细节没有注意很可能会用错,因此本人对在ACE_Message_Block中使用Allocator做了很浅的封装,使用起来就不容易出错了。附件代码中的Allocator是我用ACE写的Slab内存池,用起来效率还很不错。当然把模板参数实例化一下,也可以用其它的符合ACE的Allocator。
因上传附件的大小受限制,在这里贴代码,有兴趣的朋友可以跟我联系取工程文件和相关测试代码:
Message_Block_Oilfield.h:
  1. #ifndef __MESSAGE_BLOCK_OILFIELD_H__
  2. #define __MESSAGE_BLOCK_OILFIELD_H__
  3. # if !defined (ACE_LACKS_PRAGMA_ONCE)
  4. #  pragma once
  5. # endif
  6. #include "Slab_Malloc.h"
  7. #include <ace/Message_Block.h>
  8. #include <ace/String_Base.h>
  9. #include <ace/ace_wchar.h>
  10. #include <string>
  11. // to_str_ptr is idea of c_str_ptr that is from stlsoft
  12. inline const char *to_str_ptr(const char *data, bool &result_want_del)
  13. {
  14.     result_want_del = false;
  15.     return data;
  16. }
  17. inline const char *to_str_ptr(const wchar_t *data, bool &result_want_del)
  18. {
  19.     result_want_del = true;
  20.     return (ACE_Wide_To_Ascii::convert(data));
  21. }
  22. template <typename T>
  23. inline const char *to_str_ptr(std::basic_string<T> const &s, bool &result_want_del)
  24. {
  25.     return (to_str_ptr(s.c_str(), result_want_del));
  26. }
  27. template <typename T>
  28. inline const char *to_str_ptr(ACE_String_Base<T> const &s, bool &result_want_del)
  29. {
  30.     return (to_str_ptr(s.c_str(), result_want_del));
  31. }
  32. /**
  33. * 在这里打个比喻,ACE_Message_Block就像石油,而
  34. * Message_Block_Oilfield就像是油田。我们只管从
  35. * “油田”中获取“石油”使用,不用考虑怎么把
  36. * “石油”归还(释放)给“油田”。ACE_Message_Block
  37. * 使用完毕后还是要释放的,但它可以像正常new出来
  38. * 的那样进行释放。
  39. */
  40. template <typename POOL_ALLOCATOR = Slab_Malloc_Allocator, typename POOL_ALLOCATOR_SINGLETON = Slab_Malloc_Allocator_Singleton>
  41. class Message_Block_Oilfield
  42. {
  43. public:
  44.     ACE_Message_Block *get_message_block(void);
  45.     ACE_Message_Block *get_message_block(size_t len);
  46.     ACE_Message_Block *get_message_block(const char *data, int len);
  47.     ACE_Message_Block *get_message_block(const ACE_UINT8 *data, int len);
  48.     // 这个模板多了两个无用的参数,是因为模板函数不能偏特化,不得已而为之。
  49.     template <typename String_Type>
  50.     ACE_Message_Block *get_message_block(String_Type data, int not_use1, long not_use2);
  51. public:
  52.     ACE_Data_Block *get_data_block(void);
  53.     ACE_Data_Block *get_data_block(size_t size,
  54.                                 ACE_Message_Block::ACE_Message_Type msg_type,
  55.                                 const char *msg_data,
  56.                                 ACE_Allocator *allocator_strategy = 0,
  57.                                 ACE_Lock *locking_strategy = 0);
  58. private:
  59.     ACE_Message_Block *get_message_block(ACE_Data_Block *data_block,
  60.                                         ACE_Message_Block::Message_Flags flags,
  61.                                         ACE_Allocator *message_block_allocator);
  62. private:
  63.     static POOL_ALLOCATOR *allocator_;
  64. };
  65. template<typename POOL_ALLOCATOR, typename POOL_ALLOCATOR_SINGLETON> inline ACE_Data_Block *
  66. Message_Block_Oilfield<POOL_ALLOCATOR, POOL_ALLOCATOR_SINGLETON>::get_data_block(void)
  67. {
  68.     ACE_Data_Block *db;
  69.     ACE_NEW_MALLOC_RETURN(db,
  70.                         static_cast<ACE_Data_Block *>(allocator_->malloc(sizeof(ACE_Data_Block))),
  71.                         ACE_Data_Block(0,
  72.                         ACE_Message_Block::MB_DATA,
  73.                         0,
  74.                         allocator_,
  75.                         0,
  76.                         0,
  77.                         allocator_),
  78.                         0);
  79.     return db;
  80. }
  81. template<typename POOL_ALLOCATOR, typename POOL_ALLOCATOR_SINGLETON> inline ACE_Message_Block *
  82. Message_Block_Oilfield<POOL_ALLOCATOR, POOL_ALLOCATOR_SINGLETON>::get_message_block(size_t len)
  83. {
  84.     ACE_Message_Block *mb;
  85.     mb = this->get_message_block();
  86.     mb->size(len);
  87.     return mb;
  88. }
  89. template<typename POOL_ALLOCATOR, typename POOL_ALLOCATOR_SINGLETON> inline ACE_Message_Block *
  90. Message_Block_Oilfield<POOL_ALLOCATOR, POOL_ALLOCATOR_SINGLETON>::get_message_block(const ACE_UINT8 *data, int len)
  91. {
  92.     return (get_message_block(reinterpret_cast<const char *>(data), len));
  93. }
  94. template<typename POOL_ALLOCATOR, typename POOL_ALLOCATOR_SINGLETON> inline ACE_Message_Block *
  95. Message_Block_Oilfield<POOL_ALLOCATOR, POOL_ALLOCATOR_SINGLETON>::get_message_block(ACE_Data_Block *data_block,
  96.                                      ACE_Message_Block::Message_Flags flags,
  97.                                      ACE_Allocator *message_block_allocator)
  98. {
  99.     ACE_Message_Block *mb;
  100.     ACE_NEW_MALLOC_RETURN(mb,
  101.                         static_cast<ACE_Message_Block *>(allocator_->malloc(sizeof(ACE_Message_Block))),
  102.                         ACE_Message_Block(data_block, flags, message_block_allocator),
  103.                         0);
  104.     return mb;
  105. }
  106. typedef ACE_Singleton<Message_Block_Oilfield<>, ACE_SYNCH_MUTEX> Message_Block_Oilfield_Singleton;
  107. #define MESSAGE_BLOCK_OILFIELD Message_Block_Oilfield_Singleton::instance()
  108. enum Parameter_Not_Use_Type
  109. {
  110.     PARAMETER_NOT_USE_VAL = 0
  111. };
  112. #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
  113. # include "Message_Block_Oilfield.cpp"
  114. #endif
  115. #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
  116. # pragma implementation ("Message_Block_Oilfield.cpp")
  117. #endif
  118. #endif
复制代码
 楼主| 发表于 2011-3-4 21:05:05 | 显示全部楼层
Message_Block_Oilfield.cpp:
  1. #ifndef __MESSAGE_BLOCK_OILFIELD_CPP__
  2. #define __MESSAGE_BLOCK_OILFIELD_CPP__
  3. #include "Message_Block_Oilfield.h"
  4. #if !defined (ACE_LACKS_PRAGMA_ONCE)
  5. # pragma once
  6. #endif /* ACE_LACKS_PRAGMA_ONCE */
  7. template<typename POOL_ALLOCATOR, typename POOL_ALLOCATOR_SINGLETON>
  8. POOL_ALLOCATOR *Message_Block_Oilfield<POOL_ALLOCATOR, POOL_ALLOCATOR_SINGLETON>::allocator_ = POOL_ALLOCATOR_SINGLETON::instance();
  9. template<typename POOL_ALLOCATOR, typename POOL_ALLOCATOR_SINGLETON> ACE_Message_Block *
  10. Message_Block_Oilfield<POOL_ALLOCATOR, POOL_ALLOCATOR_SINGLETON>::get_message_block(void)
  11. {
  12.     ACE_Message_Block *mb;
  13.     ACE_Data_Block *db;
  14.     db = this->get_data_block();
  15.     mb = this->get_message_block(db, 0, allocator_);
  16.     return mb;
  17. }
  18. template<typename POOL_ALLOCATOR, typename POOL_ALLOCATOR_SINGLETON> ACE_Message_Block *
  19. Message_Block_Oilfield<POOL_ALLOCATOR, POOL_ALLOCATOR_SINGLETON>::get_message_block(const char *data, int len)
  20. {
  21.     ACE_Message_Block *mb;
  22.     ACE_Data_Block *db;
  23.     db = this->get_data_block(len, ACE_Message_Block::MB_DATA, data);
  24.     mb = this->get_message_block(db, 0, allocator_);
  25.     mb->wr_ptr(len);
  26.     return mb;
  27. }
  28. template<typename POOL_ALLOCATOR, typename POOL_ALLOCATOR_SINGLETON>
  29. template<typename String_Type>
  30. ACE_Message_Block *
  31. Message_Block_Oilfield<POOL_ALLOCATOR, POOL_ALLOCATOR_SINGLETON>::get_message_block(String_Type data, int not_use1, long not_use2)
  32. {
  33.     ACE_Message_Block *mb;
  34.     bool result_want_del = false;
  35.     const char *result_data = to_str_ptr(data, result_want_del);
  36.     mb = get_message_block(result_data, ACE_OS::strlen(result_data));
  37.     if (result_want_del)
  38.         delete []result_data;
  39.     return mb;
  40. }
  41. template<typename POOL_ALLOCATOR, typename POOL_ALLOCATOR_SINGLETON> ACE_Data_Block *
  42. Message_Block_Oilfield<POOL_ALLOCATOR, POOL_ALLOCATOR_SINGLETON>::get_data_block(size_t size,
  43.                                    ACE_Message_Block::ACE_Message_Type msg_type,
  44.                                    const char *msg_data,
  45.                                    ACE_Allocator *allocator_strategy,
  46.                                    ACE_Lock *locking_strategy)
  47. {
  48.     ACE_Data_Block *db;
  49.     ACE_NEW_MALLOC_RETURN(db,
  50.         static_cast<ACE_Data_Block *>(allocator_->malloc(sizeof(ACE_Data_Block))),
  51.         ACE_Data_Block(size,
  52.         msg_type,
  53.         0,
  54.         (allocator_strategy != 0) ? allocator_strategy : allocator_,
  55.         locking_strategy,
  56.         0,
  57.         allocator_),
  58.         0);
  59.     ACE_OS::memcpy(db->base(), msg_data, db->size());
  60.     return db;
  61. }
  62. #endif
复制代码
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-15 12:59 , Processed in 0.016637 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表