当前位置: 首页>>代码示例>>C++>>正文


C++ MemoryBlock::GetAddress方法代码示例

本文整理汇总了C++中MemoryBlock::GetAddress方法的典型用法代码示例。如果您正苦于以下问题:C++ MemoryBlock::GetAddress方法的具体用法?C++ MemoryBlock::GetAddress怎么用?C++ MemoryBlock::GetAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MemoryBlock的用法示例。


在下文中一共展示了MemoryBlock::GetAddress方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: AllocMemory

void* MemoryBlockAllocator::AllocMemory(size_t sizeOfBytes)
{
	if (sizeOfBytes == 0){
		return nullptr;
	}

	void* pResult = nullptr;
	//calculate how much of blocks we need.
	size_t numofRequiredblocks = (size_t)ceil((float)sizeOfBytes / (float)_blockSize);
	//check whether there are enough blocks for us, index of the memory block
	size_t startBit = this->_pBitfield->CheckEnoughSpaces(numofRequiredblocks);
	//if have enough blocks for the memory
	if (startBit != UINT_MAX)
	{
		//wirte the bits in bitfield
		bool success = _pBitfield->WriteBits(startBit, numofRequiredblocks);
		if (!success)
		{
			MessagedAssert(success == true, "Memory alloc failed!");
			TDEBUG_PRINT_FL("Alloc failed! %d bytes of memory is too much to alloc\n", Debugger::VerbosityDebugger::LEVEL1, sizeOfBytes);
			return nullptr;
		}
		//find the memory block which contains the address we want to return to user
		size_t offset = align_get_boundry_offset(sizeof(MemoryBlock), _alignment);
		MemoryBlock* pTtemp = reinterpret_cast<MemoryBlock*>(reinterpret_cast<char*>(_pBlocks)+startBit * offset);
		pResult = pTtemp->GetAddress();
		//record how much of blocks we alloced
		pTtemp->AllocBlock(numofRequiredblocks);
		TDEBUG_PRINT_FL("%d bytes = %d blocks of memory has been alloced\n", Debugger::VerbosityDebugger::LEVEL1, numofRequiredblocks*_blockSize, numofRequiredblocks);
	}
	else
	{
		MessagedAssert(startBit != UINT_MAX, "Memory alloc failed!");
		TDEBUG_PRINT_FL("Alloc failed! %d bytes of memory is too much to alloc\n", Debugger::VerbosityDebugger::LEVEL1, sizeOfBytes);
	}
	return pResult;
}
开发者ID:leonpardlee,项目名称:CDEngine,代码行数:37,代码来源:MemoryAllocator.cpp


注:本文中的MemoryBlock::GetAddress方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。