定制内存管理器

时间:2010-07-31 12:26:37

标签: c++

我正在尝试实现一个自定义内存管理器,我想知道是否有更好的方法来实现这个函数,因为当我被问及无效指针算术时,有些人认为如果我在C ++中有一个void *,事情是非常错误的。

// allocates a page of memory.
void ObjectAllocator::allocatePage()
{    
    //if(OAStats_.PagesInUse_ >= Config_.MaxPages_)
        //throw exception

    void* buffer = ::operator new(OAStats_.PageSize_); // allocate memory, no constructor call.

    // =============== Setup the PageList_ ===============
    GenericObject* pNewNode = ::new(buffer) GenericObject();    // Construct GenericObject for the pagelist.
    pNewNode->Next = PageList_->Next;                            // pNewNode points to wherever PageList_ pointed to.
    PageList_->Next = pNewNode;                                    // PageList_ points to pNewNode
    pNewNode = NULL;                                            // dont need this handle anymore
    buffer = static_cast<char*>(buffer) + sizeof(GenericObject);    // move pointer to point after the generic object.

    // =============== Setup the FreeList_ ===============
    for(int i=0;i<Config_.ObjectsPerPage_;++i)
    {
        static GenericObject* pPreviousNode = NULL;            // static variable to hold the previous node
        pNewNode = ::new(buffer) GenericObject();            // Construct GenericObject for the freelist.
        pNewNode->Next = pPreviousNode;
        pPreviousNode = pNewNode;
        buffer = static_cast<char*>(buffer) + OAStats_.ObjectSize_;    // move pointer by ObjectSize.
        ++OAStats_.FreeObjects_;
    }
    FreeList_->Next = pNewNode;

    ++OAStats_.PagesInUse_;
    ++OAStats_.Allocations_;
}

3 个答案:

答案 0 :(得分:10)

如果你需要一块内存用于存储字符串(8位ANSI),那么将指向该缓冲区的指针声明为char并对其进行操作是有意义的。

在你的情况下,你需要一块“blob”的内存块,它没有固有的类型,所以你正确地选择了void *来代表那个blob。

现在您需要按某个对象的大小增加该指针。由于显而易见的原因,您无法对void指针执行算术运算,那么您该怎么办?投吧。没有羞耻。

答案 1 :(得分:6)

在C ++中,在原始字节上,使用char *,并且不要想你自己。这是正确的事(tm)。特别是如果你把它包装在一个更高级别的结构中,就像你一样。

答案 2 :(得分:2)

虚空*没有任何内在错误。然而,我们经常看到的是来自C的人,当他们应该做其他事情时,过度使用void *。如果你正在管理原始内存blob,那么void *是完全合适的。但是,很少有任何其他理由去做。

相关问题