void MemoryBlockAllocator::FreeMemory(void* pAddress) { if (!pAddress) { MessagedAssert(pAddress != NULL, "You cannot free empty memory."); TDEBUG_PRINT_FL("Hey, don't free the empty address.\n", Debugger::VerbosityDebugger::LEVEL1); return; } if (!this->Contains(pAddress)) { MessagedAssert(this->Contains(pAddress), "The address is not contained by this allocator."); TDEBUG_PRINT_FL("Hey, this memory is not owned by me.\n", Debugger::VerbosityDebugger::LEVEL1); return; } //get the memory block which contains the pAddress. size_t memoryOffset = (size_t)pAddress - (size_t)this->_pMemoryAddress; size_t startBit = memoryOffset / this->_blockSize;// index of the memory block size_t align_offset = align_get_boundry_offset(sizeof(MemoryBlock), _alignment); MemoryBlock* pBlock = reinterpret_cast<MemoryBlock*>(reinterpret_cast<char*>(_pBlocks) + startBit * align_offset); //read how much of blocks we alloced size_t numOfSequence = pBlock->GetNumOfAllocBlocks(); if (numOfSequence != UINT_MAX) { //clear the bitfield this->_pBitfield->EraseBits(startBit, numOfSequence); //free the memory blocks, just set memory 0 for (unsigned int i = 0; i < numOfSequence; i++) { pBlock->FreeBlock(); pBlock = align_get_next<MemoryBlock>(pBlock, _alignment); } TDEBUG_PRINT_FL("%d bytes = %d blocks of memory has been freed\n", Debugger::VerbosityDebugger::LEVEL1, numOfSequence*_blockSize, numOfSequence); } else { MessagedAssert(numOfSequence != UINT_MAX, "Memory free failed!"); TDEBUG_PRINT_FL("Free failed!\n", Debugger::VerbosityDebugger::LEVEL1); } }