【问题标题】:Memory alignment constraints in boost's managed memoryboost托管内存中的内存对齐约束
【发布时间】:2012-08-18 08:57:47
【问题描述】:

我正在使用 boost::interprocess::managed_external_buffer,当尝试在特定地址创建这样的缓冲区时,我收到以下错误(断言失败):

/homes/mdorier/local/include/boost/interprocess/managed_external_buffer.hpp:67: boost::interprocess::basic_managed_external_buffer::basic_managed_external_buffer(boost::interprocess::create_only_t, 无效*,类型名 boost::interprocess::ipcdetail::basic_managed_memory_impl::size_type) [with CharType = 字符,分配算法 = boost::interprocess::rbtree_best_fit, 0ul>, IndexType = boost::interprocess::iset_index]: 断言`(0 == (((std::size_t)addr) & (AllocationAlgorithm::Alignment - size_type(1u))))' 失败。

managed_external_buffer.hpp 中的第 67 行对应以下函数中的 BOOST_ASSERT 语句:

//!Creates and places the segment manager. This can throw
   basic_managed_external_buffer
      (create_only_t, void *addr, size_type size)
   {
      //Check if alignment is correct
      BOOST_ASSERT((0 == (((std::size_t)addr) & (AllocationAlgorithm::Alignment - size_type(1u)))));
      if(!base_t::create_impl(addr, size)){
         throw interprocess_exception("Could not initialize buffer in basic_managed_external_buffer constructor");
      }
   }

我的代码在带有 GCC 4.6.2 的 Ubuntu i686 上运行良好。上面的错误出现在带有 GCC 4.4.3 的 Ubuntu x86_64 上。

知道为什么在一个平台上我没有收到任何错误,而内存对齐在另一个平台上似乎很重要吗?

如果 managed_external_buffer 想要我对齐地址,我可以接受,但我至少应该知道要提供什么值。我该怎么做?

谢谢

【问题讨论】:

  • 这取决于AllocationAlgorithm::Alignment 是什么。您可以随时中断调试器并查看预期值。或者,它只是几次尝试——8 或 16(最有可能)、32 或 64。对于与 SIMD 相关的数据类型,它可能是 128,但我怀疑 Boost 家伙是否会为此断言。
  • 确实,16 个工作。非常感谢!

标签: c++ memory boost alignment


【解决方案1】:

看起来它正在检查自然点对齐。

所以在 32 位平台上它需要 4 字节对齐,而 64 位平台正在检查 8 字节对齐。

一些架构非常难以容忍数据类型与内存地址的不对齐。

【讨论】:

  • +1。错位是可怕的。令人遗憾的是,x86 架构没有因为没有正确对齐数据而没有给开发人员一巴掌。
猜你喜欢
  • 2021-08-26
  • 2011-04-29
  • 1970-01-01
  • 1970-01-01
  • 2012-11-05
  • 1970-01-01
  • 2019-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多