简体   繁体   English

如何在C ++中合并内存分配器

[英]How to Incorporate a memory allocator in C++

I am working on a lock free data structure, and I have been examining the code at libcds , but I can not seem to figure out how to incorporate there memory allocator into my class file. 我正在研究无锁数据结构,并且我一直在检查libcds上的代码,但似乎无法弄清楚如何将内存分配器合并到类文件中。

template<class KEY, class VALUE>
class DataStruc{
public:

I have never used custom memory allocators before, and I am having trouble figuring out how to approach incorporating it. 我以前从未使用过自定义内存分配器,并且在弄清楚如何合并它时遇到了麻烦。

You just need to implement operator new() for your class. 您只需要为您的类实现运算符new()即可。 Your implementation should get memory from an instance of the allocator. 您的实现应从分配器的实例获取内存。

In the header file for your class, you'll need to declare the allocation/deallocation operators: 在您的类的头文件中,您需要声明分配/取消分配操作符:

public:
    void * operator new(size_t size);
    void * operator delete(void * obj, size_t size);
    void * operator new[](size_t size);
    void * operator delete[](void * obj, size_t size);

Then you need to implement them. 然后,您需要实现它们。 Somewhere you need to instantiate an allocator; 您需要在某个地方实例化分配器; maybe just make it a static in the file where you implement these operators. 也许只是在实现这些运算符的文件中将其设置为静态。 Then, just looking at the API docs on that web site, you'd do something like the below. 然后,仅查看该网站上的API文档,您将执行以下操作。 I put in the asserts because I can't see how to pass the object size to that allocator; 我放入断言是因为我看不到如何将对象大小传递给该分配器。 that effectively means that YourClass better not serve as a parent to other classes. 这实际上意味着YourClass最好不要充当其他类的父级。

static cds::details::Allocator<YourClass> allocator;

void * YourClass::operator new(size_t size) { 
    assert size == sizeof(YourClass);
    return allocator.New();
}

void * YourClass::operator new[](size_t size) { 
    return allocator.NewArray(size/sizeof(YourClass));
}

void YourClass::operator delete(void * object, size_t size) { 
    return allocator.Delete(object);
}

void YourClass::operator delete[](void * object, size_t size) { 
    return allocator.DeleteArray(object, size/sizeof(YourClass));
}

I'm not too happy with the asserts and divisions, but it's late and I'm tired. 我对断言和分歧不太满意,但是已经晚了,我很累。 Maybe somebody else knows a more elegant way to make these APIs match up a bit better. 也许其他人知道一种更优雅的方法来使这些API更好地匹配。 But this'd work. 但这会起作用。

Read this post on some insight how to implement a custom memory allocation system: http://www.stevestreeting.com/2008/06/28/memory-man/ 阅读有关如何实现自定义内存分配系统的一些见解的文章: http : //www.stevestreeting.com/2008/06/28/memory-man/

His post is in reference to the custom memory allocation system implemented in the open source Ogre3D graphics library. 他的帖子是关于在开源Ogre3D图形库中实现的自定义内存分配系统的。 You can download its source code and see how it is done there at http://www.ogre3d.org/download/source 您可以在http://www.ogre3d.org/download/source下载其源代码并查看其工作方式

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

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