c++ - 将 operator new 和 operator delete 与自定义内存池/分配器一起使用

标签 c++ memory-management new-operator delete-operator placement-new

我正在研究一个内存池/内存分配器实现,我正在一个庄园中设置它,只有一个特殊的“客户端”对象类型可以从池中提取。客户端可以直接构建到池中,或者它可以使用池进行动态内存调用,或者理论上它可以同时进行。 我希望能够以调用我的池“alloc()”和“free()”函数的方式重载operator newoperator delete,以便获取构建对象所需的内存。

我遇到的主要问题之一是让我的运算符(operator)删除以便能够通过调用我编写的 pool->free() 函数来释放内存。我想出了一个 hack,通过将池传递到构造函数并让析构函数执行释放工作来修复它。这一切都很好而且花花公子,直到有人需要从这个类继承并根据自己的需要重写析构函数,然后忘记进行内存释放。这就是为什么我想将其全部包装在运算符中,以便默认隐藏和继承功能。

我的代码在 GitHub 上:https://github.com/zyvitski/Pool

我的Client类定义如下:

class Client
{
public:
    Client();
    Client(Pool* pool);
    ~Client();

    void* operator new(size_t size,Pool* pool);
    void operator delete(void* memory);

    Pool* m_pPool;
};

实现是:

Client::Client()
{

}
Client::Client(Pool* pool)
{
    m_pPool = pool;
}
Client::~Client()
{
    void* p = (void*)this;
    m_pPool->Free(&p);
    m_pPool=nullptr;
}
void* Client::operator new(size_t size, Pool* pool)
{
    if (pool!=nullptr) {
        //use pool allocator
        MemoryBlock** memory=nullptr;
        memory = pool->Alloc(size);
       return *memory;
    }
    else throw new std::bad_alloc;
}
void Client::operator delete(void* memory)
{
    //should somehow free up the memory back to the pool
    // the proper call will be:
    //pool->free(memory);
    //where memory is the address that the pool returned in operator new

}

这是我目前正在使用的 Main() 示例:

int main(int argc, const char * argv[]){
    Pool* pool = new Pool();
    Client* c = new(pool) Client(pool);
    /*
    I'm using a parameter within operator new to pass the pool in for use and i'm also passing the pool as a constructor parameter so i can free up the memory in the destructor
    */

    delete c;
    delete pool;
    return 0;
}

到目前为止我的代码有效,但我想知道是否有更好的方法来实现这一点? 如果我要问/做的任何事情根本不可能、不好的做法或只是愚蠢,请告诉我。我现在在使用 MacBook Pro,但如果可能的话,我想保持我的代码跨平台。

如果您有任何问题可以帮助我,请告诉我。

当然,在此先感谢任何可以提供帮助的人。

最佳答案

您可以在返回的内存地址之前存储额外的信息

#include <iostream>
#include <type_traits>

class Pool {
public:
    static void* Alloc(std::size_t size) { return data; }
    static void Dealloc(void*) {}
private:
    static char data[1024];
};
char Pool::data[1024];


class Client
{
public:
    void* operator new(size_t size, Pool& pool);
    void operator delete(void* memory);
};


struct MemoryHeader {
    Pool* pool;
};


void* Client::operator new(size_t size, Pool& pool)
{
    auto header = static_cast<MemoryHeader*>(pool.Alloc(sizeof(MemoryHeader) + size));
    std::cout << "    New Header: " << header << '\n';
    header->pool = &pool;
    return header + 1;
}

void Client::operator delete(void* memory)
{
    auto header = static_cast<MemoryHeader*>(memory) - 1;
    std::cout << " Delete Header: " << header << '\n';
    header->pool->Dealloc(header);
}

int main()
{
    Pool pool;
    Client* p = new(pool) Client;
    std::cout << "Client Pointer: " << p << '\n';
    delete p;
    return 0;
}

关于c++ - 将 operator new 和 operator delete 与自定义内存池/分配器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20945439/

相关文章:

c++ - 如何获取 GL 库/头文件?

c++ - C++多字符字符常量

java_为什么打印成员变量给出实际值?

c++ - 动态内存和 "ordinary"内存的区别

c++ - 玩具语言的自定义分配器

c++ - 没有调用正确的构造函数

c++ - 如何使用距离公式用纯色填充我的圆圈?

ios - 圆弧 : "Pointer to non-const type ' id' with no explicit ownership"

c++ - 删除使用 new 创建的 2D 或 3D 指针

c++ - 返回空指针的用户定义的 operator new