1

メモリ マネージャーで配列を正常に割り当てたいと考えています。ヒープでデータを正常にセットアップするのに苦労しています。配列の要素をインスタンス化し、その配列に渡されるポインターを設定する方法がわかりません。どんな助けでも大歓迎です。 =)

基本的に要約すると、通常のヒープの代わりに独自のヒープ ブロックを使用して、独自の new[#] 関数を記述したいと考えています。動的配列に必要なものについて考えたくありません。oO

// Parameter 1: Pointer that you want to pointer to the Array.
// Parameter 2: Amount of Array Elements requested.
// Return: true if Allocation was successful, false if it failed.
template <typename T>
bool AllocateArray(T*& data, unsigned int count)
{
    if((m_Heap.m_Pool == nullptr) || count <= 0)
        return false;

    unsigned int allocSize = sizeof(T)*count;
    // If we have an array, pad an extra 16 bytes so that it will start the data on a 16 byte boundary and have room to store
    // the number of items allocated within this pad space, and the size of the original data type so in a delete call we can move
    // the pointer by the appropriate size and call a destructor(potentially a base class destructor) on each element in the array
    allocSize += 16;

    unsigned int* mem = (unsigned int*)(m_Heap.Allocate(allocSize));
    if(!mem)
    {
        return false;
    }

    mem[2] = count;
    mem[3] = sizeof(T);

    T* iter = (T*)(&(mem[4]));
    data = iter;
    iter++;

    for(unsigned int i = 0; i < count; ++i,++iter)
    {
        // I have tried a bunch of stuff, not sure what to do.  :(
    }

    return true;
}

ヒープ割り当て機能:

void* Heap::Allocate(unsigned int allocSize)
{
Header* HeadPtr = FindBlock(allocSize);
Footer* FootPtr = (Footer*)HeadPtr;
FootPtr = (Footer*)((char*)FootPtr + (HeadPtr->size + sizeof(Header)));

// Right Split Free Memory if there is enough to make another block.
if((HeadPtr->size - allocSize) >= MINBLOCKSIZE)
{           
    // Create the Header for the Allocated Block and Update it's Footer
    Header* NewHead = (Header*)FootPtr;
    NewHead = (Header*)((char*)NewHead - (allocSize + sizeof(Header)));
    NewHead->size = allocSize;
    NewHead->next = NewHead;
    NewHead->prev = NewHead;
    FootPtr->size = NewHead->size;

    // Create the Footer for the remaining Free Block and update it's size
    Footer* NewFoot = (Footer*)NewHead;
    NewFoot = (Footer*)((char*)NewFoot - sizeof(Footer));
    HeadPtr->size -= (allocSize + HEADANDFOOTSIZE);
    NewFoot->size = HeadPtr->size;

    // Turn new Header and Old Footer High Bits On
    (NewHead->size |= (1 << 31)); 
    (FootPtr->size |= (1 << 31));

    // Return actual allocated memory's location
    void* MemAddress = NewHead;
    MemAddress = ((char*)MemAddress + sizeof(Header));

    m_PoolSizeTotal = HeadPtr->size;
    return MemAddress;
}
else
{
    // Updating descriptors
    HeadPtr->prev->next = HeadPtr->next;
    HeadPtr->next->prev = HeadPtr->prev;
    HeadPtr->next = NULL;
    HeadPtr->prev = NULL;

    // Turning Header and Footer High Bits On
    (HeadPtr->size |= (1 << 31)); 
    (FootPtr->size |= (1 << 31));

    // Return actual allocated memory's location
    void* MemAddress = HeadPtr;
    MemAddress = ((char*)MemAddress + sizeof(Header));

    m_PoolSizeTotal = HeadPtr->size;
    return MemAddress;
}
}

メイン.cpp

int* TestArray;

MemoryManager::GetInstance()->CreateHeap(1);  // Allocates 1MB

MemoryManager::GetInstance()->AllocateArray(TestArray, 3);

MemoryManager::GetInstance()->DeallocateArray(TestArray);

MemoryManager::GetInstance()->DestroyHeap();
4

1 に答える 1