创建一个作用域的自定义内存池/分配器? [英] Creating a scoped custom memory pool/allocator?

查看:101
本文介绍了创建一个作用域的自定义内存池/分配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能在C ++中创建一个自定义分配器,其工作原理如下:

Would it be possible in C++ to create a custom allocator that works simply like this:

{
    // Limit memory to 1024 KB
    ScopedMemoryPool memoryPool(1024 * 1024); 

    // From here on all heap allocations ('new', 'malloc', ...) take memory from the pool.
    // If the pool is depleted these calls result in an exception being thrown.

    // Examples:
    std::vector<int> integers(10);
    int a * = new int [10];
}



我在boost库或其他地方找不到类似的东西。

I couldn't find something like this in the boost libraries, or anywhere else.

是否有一个根本性的问题,使这不可能?

Is there a fundamental problem that makes this impossible?

推荐答案

您需要创建一个自定义分配器,您作为模板参数传入向量。这个自定义分配器本质上包装对你的池的访问,并做任何它想要的大小验证。

You would need to create a custom allocator that you pass in as a template param to vector. This custom allocator would essentially wrap the access to your pool and do whatever size validations that it wants.

这篇关于创建一个作用域的自定义内存池/分配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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