使用operator new和operator delete与自定义内存池/分配器

Ale*_*cki 4 c++ memory-management placement-new new-operator delete-operator

我正在开发内存池/内存分配器实现,我在一个庄园中设置它,只有一个特殊的"客户端"对象类型可以从池中绘制.客户端可以直接构建到池上,也可以使用池进行动态内存调用,理论上它可以同时执行.我希望能够以一种调用我的池"alloc()"和"free()"函数的方式重载operator newoperator delete,以便获得构造对象所需的内存.

我遇到的一个主要问题是通过调用我编写的pool-> free()函数让我的operator删除能够释放内存.我想出了一个通过将池传递给构造函数并让析构函数执行解除分配工作来修复它的hack.这是好的和花花公子,直到有人需要从这个类继承并覆盖析构函数以满足他们自己的需要,然后忘记执行内存释放.这就是为什么我想把它全部包装在运算符中,所以默认情况下功能隐藏起来并继承.

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

我对客户的类定义如下:

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

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

    Pool* m_pPool;
};
Run Code Online (Sandbox Code Playgroud)

实施是:

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

}
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的示例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;
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我的代码有效,但我想知道是否有更好的方法来实现这一目标?如果我要求/做的事情根本不可能,不好的做法或者只是简单地愚蠢,请告诉我.我现在在MacBook Pro上,但我想尽可能保持我的代码跨平台.

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

当然,提前感谢任何可以提供帮助的人.

小智 5

您可能会在返回的内存地址之前存储其他信息

#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;
}
Run Code Online (Sandbox Code Playgroud)

  • @AlexZywicki 对齐存储没用(这里不需要 C++11 的东西,我也在学习) (2认同)