找出最大尺寸的几个对象放置新 [英] Finding out the largest size of several objects for placement new

查看:111
本文介绍了找出最大尺寸的几个对象放置新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在一个小型的ARM嵌入式系统,内存相当有限,没有MMU。我有几个对象,我需要动态分配不同的函数,所有的继承自同一个超类,但执行不同的功能,可能是不同的大小。我没有足够的内存可以在启动时实例化它们,并保留它们。

So I'm working on a small ARM embedded system that has pretty limited memory, and no MMU. I have several objects that I need to dynamically allocate for different functions that all inherit from the same superclass, but perform different functions and are likely different sizes. I don't have enough memory available to instantiate them all at startup and leave them in place.

例如,我可能有对象定义类似: / p>

So as an example, I might have objects defined sort of like:

class Waveform1 : public OutputStream
class Waveform2 : public OutputStream
class Waveform3 : public OutputStream

和子类将有不同的大小,因为一些可能有更少的方法和私有变量,

and the subclasses are going to have different sizes as some may have fewer methods and private variables than others, but likely none will be substantially larger than any of the others.

我想做的是在系统启动时分配一个缓冲区用于placement new,该缓冲区足够大以容纳最大的定义对象。然后,我应该能够实例化和销毁对象没有任何问题,因为它总是足够大,以容纳最大的对象所需。我希望这个可以自动处理,因为新的对象可以添加到列表中作为系统设计进展。

What I'd like to do is allocate a buffer to use with placement new at system startup that is large enough to contain the largest of the defined objects. Then I should be able to instantiate and destroy objects there without a problem, as it's always large enough to hold the largest of the objects required. I'd like this to be taken care of automatically, as new objects may be added to the list as system design progresses.

有一个规范的方法来完成这一点它可能看起来像我知道我在做什么?

Is there a canonical way to accomplish this so it might sort of seem like I know what I'm doing?

推荐答案

过去我做的是使用一个char数组和我需要存储在其中的所有类型的联合。好处是缓冲区将正确对齐。

What I've done in the past is use a union of a char array and all of the types I need to store in it. The benefit is that the buffer will be aligned properly.

像:

class MyClass {
    public:
        union {
            char buffer[1];
            ClassA a;
            ClassB b;
        };
    MyClass() {}
    ~MyClass() {}
};

注意,你可以省略 char buffer [1] code>并将新的权利放置到联合的类成员上,例如 new(& a)ClassA 。还要注意,如果ClassA或ClassB是除了POD之外的任何东西,那么这只能在C ++ 11中使用。

Note that you can just leave out the char buffer[1] and placement new right onto the class member of the union, like new (&a) ClassA. Also note that if ClassA or ClassB are anything except PODs then this only works in C++11.

现在,事实上, em>做的是不同于我记住的。可能是因为我不得不支持C ++ 03。在我的实际代码中我这样做:

Now, it turns out that what I actually did do is different from what I remember doing. Probably because I had to support C++03. In my actual code I did this:

char DECLARE_ALIGN(8) buffer[ sizeof(int*) * 8 ];

DECLARE_ALIGN 是:

#  define DECLARE_ALIGN(x)  __attribute__((aligned(x)))

# define DECLARE_ALIGN(x) __declspec( align(x) )

然后在代码中,在实际分配对象的函数中,已经将缓冲区作为指针传递,并且sizeof(缓冲区)作为buffer_len:

And then later in the code, in the function that was actually allocating the object and after I had passed buffer as a pointer, and sizeof(buffer) as buffer_len:

assert( buffer==0 || buffer_len >= sizeof(BTreeNodeWriter_X<X>) );
assert( buffer==0 || buffer_len >= sizeof(BTreePackedNodeWriter_X<X>) );

这篇关于找出最大尺寸的几个对象放置新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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