#why is my malloc return the wrong currFreeMem?

9 messages · Page 1 of 1 (latest)

limber wren
#
{
Free* pCurrFree = this->poHeap->pFreeHead;
Free* pPrevFree = nullptr;
if (_size == 0 || _size > Mem::TotalSize) {
    return nullptr;
}
   while (pCurrFree != nullptr) {
        if (GET_ALLOC_SIZE(pCurrFree->mData) >= _size) {
            uint32_t blockSize = GET_ALLOC_SIZE(pCurrFree->mData);
            if (blockSize >= _size + sizeof(Used) + sizeof(Free)) 
                Free* pNewFree = reinterpret_cast<Free*>(reinterpret_cast<uint32_t>(pCurrFree) + _size + sizeof(Used));
                SET_FREE(pNewFree->mData);
                SET_ABOVE_USED(pNewFree->mData);
                SET_ALLOC_SIZE(pNewFree->mData, blockSize - _size - sizeof(Used));
                pNewFree->pNext = pCurrFree->pNext;
                pCurrFree->pNext = pNewFree;
            }
            // Mark the current block as used
            SET_USED(pCurrFree->mData);
            if (pPrevFree != nullptr) {
                pPrevFree->pNext = pCurrFree->pNext;
            }
            else {
                this->poHeap->pFreeHead = pCurrFree->pNext;
            }
            if (this->poHeap->pNextFit == pCurrFree) {
                this->poHeap->pNextFit = pCurrFree->pNext;
            }
            Used* pUsedBlock = reinterpret_cast<Used*>(pCurrFree);
            pUsedBlock->pNext = this->poHeap->pUsedHead;
            this->poHeap->pUsedHead = pUsedBlock;
            this->poHeap->currNumUsedBlocks++;
            this->poHeap->currUsedMem += _size;
            if (blockSize == _size) {
                this->poHeap->currNumFreeBlocks--;
                this->poHeap->currFreeMem -= blockSize;
            }
            Trace::out("%d \n", poHeap->currNumFreeBlocks);
             // Update with the actual block size
            return reinterpret_cast<void*>(reinterpret_cast<uint32_t>(pCurrFree) + sizeof(Used));
        }
        pPrevFree = pCurrFree;
        pCurrFree = pCurrFree->pNext;
    }
    return nullptr; // No suitable free block found
}
tardy jungleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

worldly epoch
keen coralBOT
#
Critical error:

Unable to find a codeblock to format!

oak storm
#

!format

tardy jungleBOT
#

void* Mem::malloc(const uint32_t _size) {
  Free* pCurrFree = this->poHeap->pFreeHead;
  Free* pPrevFree = nullptr;
  if (_size == 0 || _size > Mem::TotalSize) {
    return nullptr;
  }
  while (pCurrFree != nullptr) {
    if (GET_ALLOC_SIZE(pCurrFree->mData) >= _size) {
      uint32_t blockSize = GET_ALLOC_SIZE(pCurrFree->mData);
      if (blockSize >= _size + sizeof(Used) + sizeof(Free))
        Free* pNewFree = reinterpret_cast<Free*>(
            reinterpret_cast<uint32_t>(pCurrFree) + _size + sizeof(Used));
      SET_FREE(pNewFree->mData);
      SET_ABOVE_USED(pNewFree->mData);
      SET_ALLOC_SIZE(pNewFree->mData, blockSize - _size - sizeof(Used));
      pNewFree->pNext = pCurrFree->pNext;
      pCurrFree->pNext = pNewFree;
    }
    // Mark the current block as used
    SET_USED(pCurrFree->mData);
    if (pPrevFree != nullptr) {
      pPrevFree->pNext = pCurrFree->pNext;
    } else {
      this->poHeap->pFreeHead = pCurrFree->pNext;
    }
    if (this->poHeap->pNextFit == pCurrFree) {
      this->poHeap->pNextFit = pCurrFree->pNext;
    }
    Used* pUsedBlock = reinterpret_cast<Used*>(pCurrFree);
    pUsedBlock->pNext = this->poHeap->pUsedHead;
    this->poHeap->pUsedHead = pUsedBlock;
    this->poHeap->currNumUsedBlocks++;
    this->poHeap->currUsedMem += _size;
    if (blockSize == _size) {
      this->poHeap->currNumFreeBlocks--;
      this->poHeap->currFreeMem -= blockSize;
    }
    Trace::out("%d \n", poHeap->currNumFreeBlocks);
    // Update with the actual block size
    return reinterpret_cast<void*>(reinterpret_cast<uint32_t>(pCurrFree) +
                                   sizeof(Used));
  }
  pPrevFree = pCurrFree;
  pCurrFree = pCurrFree->pNext;
}
return nullptr;  // No suitabl…
}

dot
oak storm
#

do note that this is almost certainly UB

#

use your debugger to figure out what's going on

#

what does "the wrong currFreeMem" mean?