简体   繁体   English

std :: allocate_shared,允许从自定义分配器和单个分配中分配共享指针引用计数

[英]std::allocate_shared, allow shared pointer reference count to be allocated from from custom allocator and in single allocation

Make shared improves performance using single allocation for reference count and object, is it possible to use custom allocator along with std::allocate_shared and still have one allocation, as per test code i have written its not happening: sample 使用引用计数和对象的单一分配使共享提高性能,是否可以使用自定义分配器以及std :: allocate_shared并且仍然有一个分配,根据测试代码我写的没有发生: sample

I am aware of boost::intrusive, but it has additional code to be written and which can go wrong 我知道boost :: intrusive,但它有额外的代码要写,哪些可能出错

with the help of some debugging found out that std::allocate_shared does what is expected, it allows you to make only one allocation for both your object as well as for reference counter. 在一些调试的帮助下发现std :: allocate_shared做了预期的事情,它允许你只为你的对象和引用计数器分配一个。

below is corrected code: allocate shared 下面是更正的代码: 分配共享

Here operator new is called exactly once for both std::make_shared and std::allocate_shared. 对于std :: make_shared和std :: allocate_shared,operator new只调用一次。 Using this object pool can be created and you can have same efficiency expected from boost::intrusive_ptr which needs additional coding. 可以创建使用此对象池,并且您可以从boost :: intrusive_ptr获得相同的效率,这需要额外的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM