简体   繁体   English

自定义内存分配器设计

[英]Custom memory allocator design

I have memory allocator which allocates memory for an object and calls its constructor with any given arguments, see below. 我有一个内存分配器,它为一个对象分配内存,并使用任何给定的参数调用其构造函数,请参见下文。

    // 0 args to constructor
    template <class T>
    inline T* AllocateObject() { return new (InternalAllocate(sizeof(T), alignof(T))) T(); }

    // 1 args to constructor
    template <class T, typename arg0>
    inline T* AllocateObject(const arg0& a0) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0); }

    template <class T, typename arg0>
    inline T* AllocateObject(arg0& a0) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0); }

    // 2 args to constructor
    template <class T, typename arg0, typename arg1>
    inline T* AllocateObject(arg0& a0, arg1& a1) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0,a1); }

    template <class T, typename arg0, typename arg1>
    inline T* AllocateObject(const arg0& a0, arg1& a1) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0,a1); }

    template <class T, typename arg0, typename arg1>
    inline T* AllocateObject(arg0& a0, const arg1& a1) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0,a1); }

    template <class T, typename arg0, typename arg1>
    inline T* AllocateObject(const arg0& a0, const arg1& a1) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0,a1); }

    //.........

As you can see the number of calls grows with quite rapidly with the number of arguments. 如您所见,调用的数量随着参数的数量而迅速增加。 I have to alternate with 'const' and 'non-const' for each argument to make sure it plays fine with any argument I pass. 对于每个参数,我都必须交替使用“ const”和“ non-const”,以确保它与我传递的任何参数都能正常工作。 (specifically, to be able to pass by references aswell as pass by value) (具体来说,既可以按引用传递也可以按值传递)

Is there any better way to perform the same task than to repeat this scheme? 有没有比重复此方案更好的方法来执行相同的任务? Basically I am looking at something like 8-10 arguments max and its just not very feasible I feel. 基本上我正在看的东西最多8-10个参数,我觉得这不太可行。

Thanks 谢谢

You can use a variadic template. 您可以使用可变参数模板。

template <class T, class... Args>
inline T* AllocateObject(Args&&... args) {
    return new (InternalAllocate(sizeof(T), alignof(T)))
               T(std::forward<Args>(args)...);
}

The std::forward call will preserve any references and const ness. std::forward调用将保留所有引用和const


Note that this requires C++11. 请注意,这需要C ++ 11。 Most recent compilers already support variadic templates (I am unsure about Microsoft's, though). 最新的编译器已经支持可变参数模板(尽管我不确定Microsoft的模板)。

Not a template solution, but a variable arguments #define could help you out of this problem. 不是模板解决方案,而是可变参数#define可以帮助您解决此问题。
The exact format depends on your compiler, but in MSVC it would look like this: 确切的格式取决于您的编译器,但是在MSVC中,它看起来像这样:

#define ALLOCATE_OBJECT(TYPE, ...) \
    ( new( InternalAllocate(sizeof(TYPE), alignof(TYPE)) ) TYPE(__VA_ARGS__) )

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

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