Example #1
0
void* CNamedIndexes::AllocateInCache(size_t iSize)
{
	CMemoryCacheAllocation		cPreAllocated;
	void*						pvData;
	int							i;
	SMemoryCacheDescriptor*		psMemoryDesc;
	int							iCacheDescriptorSize;
	CNamedIndexesBlock*			pcNamedIndexes;
	void*						pvCacheMem;
	CIndexedFile*				pcFile;
	BOOL						bResult;
	CNamedIndexesBlocks*		pcBlocks;

	cPreAllocated.Init(iSize);
	if (!mcCache.PreAllocate(&cPreAllocated))
	{
		cPreAllocated.Kill();
		return NULL;
	}

	for (i = 0; i < cPreAllocated.NumElements(); i++)
	{
		psMemoryDesc = cPreAllocated.Get(i);
		iCacheDescriptorSize = psMemoryDesc->iDataSize;
		pcBlocks = GetBlockForCacheDescriptorSize(iCacheDescriptorSize);
		if (!pcBlocks)
		{
			return NULL;
		}

		pvCacheMem = RemapSinglePointer(psMemoryDesc, sizeof(SMemoryCacheDescriptor));
		pcNamedIndexes = pcBlocks->GetBlock(pvCacheMem);
		if (!pcNamedIndexes)
		{
			return NULL;
		}

		pcFile = GetOrCreateFile(pcBlocks->GetDataSize(), pcBlocks->GetFileNumber());
		if (!pcFile)
		{
			return NULL;
		}
		pcBlocks->SetFileNumber(pcFile->miFileNumber);

		bResult = pcNamedIndexes->Uncache(pcFile);
		if (!bResult)
		{
			cPreAllocated.Kill();
			return NULL;
		}
	}

	pvData = mcCache.Allocate(&cPreAllocated);

	cPreAllocated.Kill();

	return pvData;
}
void CNamedIndexesOptimiser::AssignBlockNumbers(void)
{
	int						i;
	CNamedIndexesBlocks*	pcBlocks;
	int						iFileNumber;
	
	for (i = 0; i < mpacBlocks->NumElements(); i++)
	{
		pcBlocks = mpacBlocks->Get(i);
		iFileNumber = mpcFiles->GetUniqueFileNumber(pcBlocks->GetDataSize());
		pcBlocks->SetFileNumber(iFileNumber);
	}
}