请求 Boost Pool 体验.它作为预分配的分配器有用吗? [英] Boost Pool experience requested. Is it useful as allocator with preallocation?

查看:19
本文介绍了请求 Boost Pool 体验.它作为预分配的分配器有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在寻找池/分配器机制.Boost Pool 似乎提供了解决方案,但仍有一些东西无法从文档中推断出来.

Recently i have been looking for a pool/allocator mechanism. Boost Pool seems to provide the solution, but there is still things, which it have not been able to deduce from the documentation.

  1. 几个小班(约 30 个字符)
  2. std::map(我想确保它不会自己执行动态分配器)
  3. pugi::xml 中的分配
  4. 标准::字符串

如何控制分配的地址空间(或只是数量)

object_pool 似乎为分配需求提供了一种好方法 1)然而,它想为分配器设置一个固定的大小来使用.默认情况下,它会自动获取内存.如果可能的话,我想给它一个可以在其中播放的地址空间.

How to control of address space for allocation (or just amount)

The object_pool seem the to provide a good way for allocating need 1) However it would like to set a fixed size for the allocator to use. By default it grabs memory be ifself. If possible i would like to give it the address space it can play within.

char * mem_for_class[1024*1024];
boost::object_pool<my_class,mem_for_class> q;

或:

const int max_no_objs=1024;
boost::object_pool<my_class,max_no_objs> q;

虽然 UserAllocator 在 Boost::Pool 中可用;它似乎打败了这一点.恐怕所需的控件会使其效率太低……最好从头开始.

Although the UserAllocator is available in Boost::Pool; it seem to defeat the point. I am afraid the control needed would make it too inefficient... and it would be better to start from scratch.

这个问题和第一个有点相似.将 boost::pool_allocator 提供给 std 类型类(例如映射)时,boost 池是否提供任何限制分配内存的数量/位置的方法

The question is a bit similar to the first. Do boost pool provide any way of limiting how much / where there is allocated memory when giving boost::pool_allocator to a std-type-class (e.g. map)

嵌入式Linux编程.系统必须永远运行.所以我们不能冒任何内存分段的风险.目前我主要是静态分配(堆栈),但也有一些原始的新".我想要一个分配方案,以确保每次程序循环时我都使用相同的内存区域.速度/空间很重要,但安全仍然是重中之重.

Embedded linux programming. The system must keep running for..ever. So we can not risk any memory segmentation. Currently i mostly either static allocation (stack), but also a few raw "new"s. I would like an allocation scheme that ensure i use the same memory area each time the program loops. Speed /space is important, but safety is still top priority.

我希望 StackOverflow 是可以提问的地方.我尝试联系 Boost::Pool Stephen"的作者.没有运气.我没有找到任何特定于 Boost 的论坛.

I hope StackOverflow is the place to ask. I tried contacting the author of Boost::Pool "Stephen" without luck. I have not found any Boost-specific forum.

推荐答案

您始终可以创建与 STL 配合使用的分配器.如果它适用于 STL,它应该适用于 boost,因为您可以将 boost 分配器传递给 STL 容器..

You can always create an allocator that works with STL. If it works with STL, it should work with boost as you are able to pass boost allocators to STL containers..

考虑到上述情况,可以在指定的内存地址分配并且具有您指定的大小限制的分配器可以写成如下:

Considering the above, an allocator that can allocate at a specified memory address AND has a size limitation specified by you can be written as follows:

#include <iostream>
#include <vector>

template<typename T>
class CAllocator
{
    private:
        std::size_t size;
        T* data = nullptr;

    public:
        typedef T* pointer;
        typedef const T* const_pointer;

        typedef T& reference;
        typedef const T& const_reference;

        typedef std::size_t size_type;
        typedef std::ptrdiff_t difference_type;

        typedef T value_type;


        CAllocator() {}
        CAllocator(pointer data_ptr, size_type max_size) noexcept : size(max_size), data(data_ptr) {};

        template<typename U>
        CAllocator(const CAllocator<U>& other) noexcept {};

        CAllocator(const CAllocator &other) : size(other.size), data(other.data) {}

        template<typename U>
        struct rebind {typedef CAllocator<U> other;};

        pointer allocate(size_type n, const void* hint = 0) {return &data[0];}
        void deallocate(void* ptr, size_type n) {}
        size_type max_size() const {return size;}
};

template <typename T, typename U>
inline bool operator == (const CAllocator<T>&, const CAllocator<U>&) {return true;}

template <typename T, typename U>
inline bool operator != (const CAllocator<T>& a, const CAllocator<U>& b) {return !(a == b);}





int main()
{
    const int size = 1024 / 4;
    int ptr[size];
    std::vector<int, CAllocator<int>> vec(CAllocator<int>(&ptr[0], size));

    int ptr2[size];
    std::vector<int, CAllocator<int>> vec2(CAllocator<int>(&ptr2[0], size));

    vec.push_back(10);
    vec.push_back(20);
    vec2.push_back(30);
    vec2.push_back(40);


    for (std::size_t i = 0; i < vec2.size(); ++i)
    {
        int* val = &ptr2[i];
        std::cout<<*val<<"
";
    }

    std::cout<<"

";

    vec2 = vec;

    for (std::size_t i = 0; i < vec2.size(); ++i)
    {
        int* val = &ptr2[i];
        std::cout<<*val<<"
";
    }

    std::cout<<"

";
    vec2.clear();

    vec2.push_back(100);
    vec2.push_back(200);

    for (std::size_t i = 0; i < vec2.size(); ++i)
    {
        int* val = &ptr2[i];
        std::cout<<*val<<"
";
    }
}

这个分配器确保所有内存都分配在指定的地址.无论是在堆栈上还是在堆上,都不能超过您指定的分配数量,并且可以自由分配.

This allocator makes sure that all memory is allocated at a specified address. No more than the amount you specify can be allocated with the freedom to allocate were you want whether it is on the stack or the heap.

您可以创建自己的池或使用 std::unique_ptr 作为单个容器的池.

You may create your own pool or use a std::unique_ptr as the pool for a single container.

对于字符串,您需要 sizeof(_Rep_base) 的偏移量.请参阅:为什么 std::string 分配两次?

For strings, you need an offset of sizeof(_Rep_base). See: Why std::string allocating twice?

http://ideone.com/QWtxWg

定义为:

struct _Rep_base
{
    std::size_t     _M_length;
    std::size_t     _M_capacity;
    _Atomic_word        _M_refcount;
};

所以例子变成:

struct Repbase
{
    std::size_t     length;
    std::size_t     capacity;
    std::int16_t    refcount;
};

int main()
{
    typedef std::basic_string<char, std::char_traits<char>, CAllocator<char>> CAString;

    const int size = 1024;
    char ptr[size] = {0};

    CAString str(CAllocator<char>(&ptr[0], size));
    str = "Hello";

    std::cout<<&ptr[sizeof(Repbase)];
}

这篇关于请求 Boost Pool 体验.它作为预分配的分配器有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆