Question: I start with : https: / / www . chegg.com / homework - help / questions - and - answers / write - memory -

I start with : https://www.chegg.com/homework-help/questions-and-answers/write-memory-system-cpp-1-write-programs-cross-platform-c-2-containers-stl-allowed-vector--q175770100?share=24e92c6d-bb16-4ee4-978a-4aa5673b32f5
and My code is here :
#include "Mem.h"
#include "Heap.h"
#include "Type.h"
struct SecretPtr { Free *pFree; };
void Mem::initialize(){
this->poHeap->pUsedHead = nullptr;
this->poHeap->pFreeHead = nullptr;
uintptr_t freeBlockAddr = reinterpret_cast(this->poRawMem)+ sizeof(Heap);
this->poHeap->pNextFit = reinterpret_cast(freeBlockAddr);
uint32_t availableMem = Mem::TotalSize - sizeof(Heap)- sizeof(Free);
Free* freeBlock = this->poHeap->pNextFit;
freeBlock->pNext = nullptr;
freeBlock->pPrev = nullptr;
SET_FREE(freeBlock->mData);
SET_ABOVE_USED(freeBlock->mData);
SET_ALLOC_SIZE(freeBlock->mData, availableMem);
this->poHeap->pFreeHead = freeBlock;
this->poHeap->currNumUsedBlocks =0;
this->poHeap->currUsedMem =0;
this->poHeap->currNumFreeBlocks =1;
this->poHeap->currFreeMem = availableMem;
}
void* Mem::malloc(const uint32_t _size){
// check Image
}
void Mem::free(void* const data)
{
if (data == nullptr){
return;
}
Used* usedBlock = reinterpret_cast(data)-1;
Free* freeBlock = reinterpret_cast(usedBlock);
SET_FREE(freeBlock->mData);
SET_ABOVE_USED(freeBlock->mData);
SET_ALLOC_SIZE(freeBlock->mData, usedBlock->mData);
Free* prevFreeBlock = nullptr;
Free* nextFreeBlock = nullptr;
uintptr_t nextBlockAddr = reinterpret_cast(freeBlock)+ GET_ALLOC_SIZE(freeBlock->mData);
uintptr_t h = reinterpret_cast(this->poRawMem)+ Mem::TotalSize;
if (nextBlockAddr h){
Used* nextBlockHeader = reinterpret_cast(nextBlockAddr);
if (!IS_USED(nextBlockHeader->mData)){
nextFreeBlock = reinterpret_cast(nextBlockHeader);
this->poHeap->pNextFit = nextFreeBlock;
}
}
uintptr_t prevBlockAddr = reinterpret_cast(freeBlock)- GET_ALLOC_SIZE(reinterpret_cast(freeBlock -1)->mData);
if (prevBlockAddr >= reinterpret_cast(this->poRawMem)+ sizeof(Heap)){
Free* prevBlockHeader = reinterpret_cast(prevBlockAddr);
if (!IS_USED(prevBlockHeader->mData)){
prevFreeBlock = prevBlockHeader;
}
}
if (this->poHeap->pUsedHead == usedBlock){
this->poHeap->pUsedHead = usedBlock->pNext;
}
else {
usedBlock->pPrev->pNext = usedBlock->pNext;
if (usedBlock->pNext != nullptr){
usedBlock->pNext->pPrev = usedBlock->pPrev;
}
}
this->poHeap->currNumUsedBlocks--;
this->poHeap->currUsedMem -= GET_ALLOC_SIZE(freeBlock->mData);
this->poHeap->currNumFreeBlocks++;
this->poHeap->currFreeMem += GET_ALLOC_SIZE(freeBlock->mData);
if (prevFreeBlock != nullptr){
prevFreeBlock->pNext = freeBlock->pNext;
if (freeBlock->pNext != nullptr){
freeBlock->pNext->pPrev = prevFreeBlock;
}
freeBlock = prevFreeBlock;
}
if (nextFreeBlock != nullptr){
if (freeBlock->pNext != nullptr){
freeBlock->pNext->pPrev = freeBlock;
}
freeBlock->pNext = nextFreeBlock->pNext;
SET_ALLOC_SIZE(freeBlock->mData, GET_ALLOC_SIZE(freeBlock->mData)+ GET_ALLOC_SIZE(nextFreeBlock->mData));
if (this->poHeap->pNextFit == nextFreeBlock){
this->poHeap->pNextFit = freeBlock;
}
}
freeBlock->pNext = this->poHeap->pFreeHead;
if (this->poHeap->pFreeHead != nullptr){
this->poHeap->pFreeHead->pPrev = freeBlock;
}
this->poHeap->pFreeHead = freeBlock;
freeBlock->pPrev = nullptr;
}
//--- End of File ---
No code has been changed except Mem.cpp.
But I cannot pass test code with
blkStart = hdrEnd;
blkEnd = blkStart + GET_ALLOC_SIZE(free->mData);
CHECK_EQUAL(PTR_FIX(blkStart), PTR_FIX(hdrEnd));
CHECK_EQUAL(PTR_FIX(blkEnd),0xc800);
CHECK_EQUAL( GET_ALLOC_SIZE(used->mData),0xc7d4);
Please fix my code and pass this tests code line.
I start with : https: / / www . chegg.com /

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!