简体   繁体   English

C ++中固定大小数组的池分配器

[英]Pool allocator for fixed-size arrays in C++

I've done a very simple pool allocator for my embedded MCU projects. 我为嵌入式MCU项目做了一个非常简单的池分配器。 It's a templated class with a following interface (implementation part is skipped, I can post it too if someone is interested): 这是带有以下接口的模板化类(实现部分被跳过,如果有人感兴趣,我也可以发布它):

template <typename T, size_t size>
class SimplePoolAllocator
{
public:
    SimplePoolAllocator();
    T * allocate();
    void deallocate(T *pointer);
...
}

It works perfectly for "simple" things like classes or PODs, for example: 它非常适用于“简单”的事情,例如类或POD,例如:

SimplePoolAllocator<double, 10> poolOfDouble; // returns "double *"
SimplePoolAllocator<WeirdClass, 5> poolOfObjects; // returns "WeirdClass *"

The problem arises if I want to use it for fixed size arrays. 如果我想将其用于固定大小的数组,则会出现问题。 This use is of course for raw data buffers - in my project I have two "transfer" types, one has 16 bytes, the other has 100. 这种用法当然用于原始数据缓冲区-在我的项目中,我有两种“传输”类型,一种具有16个字节,另一种具有100个字节。

So suppose I use sth like that: 因此,假设我使用sth这样的:

SimplePoolAllocator<uint8_t[16], 10> pool1;
SimplePoolAllocator<uint8_t[100], 10> pool2;

Everything seems fine, but the problem is that now allocate() returns sth like that: "uint8_t(*)[16]" and "uint8_t(*)[100]". 一切似乎都很好,但是问题在于现在allocate()返回的是这样的东西:“ uint8_t(*)[16]”和“ uint8_t(*)[100]”。 Ideally I'd like it to return just "uint8_t *" (pointer to beginning) in that case. 理想情况下,在这种情况下,我希望它仅返回“ uint8_t *”(指向开头的指针)。

I know I can do sth like that: 我知道我可以那样做:

uint8_t *p = *pool1.allocate(); // additional asterisk to "drop" the array thing from the type

But this looks... weird... 但这看起来...很奇怪...

So the question is - how can I improve my SimplePoolAllocator's interface (or anything) to support simple allocation of both "plain" objects (like shown above - classes and PODs) and fixed size arrays, but returning just a pointer to first element? 因此,问题是-如何改进SimplePoolAllocator的接口(或其他任何东西),以支持对“普通”对象(如上图所示-类和POD)和固定大小数组的简单分配,而只返回指向第一个元素的指针? Can it be done without using std::array and using it's data() member funcion, or without additional '*' all around the place? 是否可以在不使用std :: array并使用其data()成员函数的情况下完成,或者在各处都没有附加的“ *”来完成此操作? C++11 features are OK with me, if there would be sth that would "convert" the types like this it could save me here: WeirdClass -> WeirdClass *, uint8_t[16] -> uint8_t *. C ++ 11的功能对我来说还可以,如果有某事可以“转换”这样的类型,可以在这里为我节省:WeirdClass-> WeirdClass *,uint8_t [16]-> uint8_t *。 I cannot easily wrap the buffers in classes, because I handle the transfers in interrupt in "raw" form - all I need in there is the pointer to buffer, which is later passed via message queue to task for processing, with "type" (size) as one of elements of the message. 我无法轻易地将缓冲区包装在类中,因为我以“原始”形式处理中断中的传输-我所需要的就是缓冲区的指针,缓冲区的指针随后通过消息队列传递给任务进行处理,使用“类型”(大小)作为邮件的元素之一。 I would like to avoid using virtual functions for this simple task if possible (; 如果可能,我想避免将虚拟函数用于此简单任务(;

Can it be done at all, or maybe I'm asking too much? 可以做到吗,或者我要求太多? Maybe the only solution is to make the interface to the template sth like this: 也许唯一的解决方案是使模板的接口像这样:

template <typename T, size_t array_size, size_t size>

so I'd have: 所以我有:

SimplePoolAllocator<WeirdClass, 1, 10> pool1;
SimplePoolAllocator<uint8_t, 16, 10> pool2;

but this also does not look very good... 但这看起来也不是很好...

Thx in advance for any suggestions! 提前感谢任何建议! Please note that this questions is about a project for microcontroller, so using Boost or sth like that is out of the question. 请注意,此问题与微控制器项目有关,因此无法使用Boost或类似的东西。

You should specialize your class: 您应该对自己的课程进行专长:

template <typename T, size_t size>
class SimplePoolAllocator
{
public:
    SimplePoolAllocator();
    T * allocate();
    void deallocate(T *pointer);
...
};

template <typename T, size_t N, size_t size>
class SimplePoolAllocator<T[N],size> // <-- here
{
public:
    SimplePoolAllocator();

    // you can now use different signatures and different implementations:
    T ** allocate();
    void deallocate(T **pointer);
...
};

This is probably the easiest way to separate those cases and treat them independently. 这可能是分离这些案例并对其进行独立处理的最简单方法。

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

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