Exemple #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;
}
BOOL CTransientIndexedFile::Allocate(STransientIndexedPointer* psPointer, int iPointerIndex)
{
	SOIndexIndexCacheDescriptor*	psOIndexIndex;
	CMemoryCacheAllocation			cPreAllocated;
	int								i;
	void*							pvData;

	cPreAllocated.Init(psPointer->sIndexedMemory.uiSize);
	if (!mcCache.PreAllocate(&cPreAllocated))
	{
		cPreAllocated.Kill();
		return FALSE;
	}

	for (i = 0; i < cPreAllocated.NumElements(); i++)
	{
		psOIndexIndex = (SOIndexIndexCacheDescriptor*)cPreAllocated.Get(i);
		pvData = RemapSinglePointer(psOIndexIndex, sizeof(SOIndexIndexCacheDescriptor));
		Write(psOIndexIndex->sIndex.iIndex, pvData);
	}

	pvData = mcCache.Allocate(&cPreAllocated);

	if (pvData)
	{
		psOIndexIndex = (SOIndexIndexCacheDescriptor*)RemapSinglePointer(pvData, -(int)(sizeof(SOIndexIndexCacheDescriptor)));
		psOIndexIndex->sIndex.iIndex = iPointerIndex;
		psOIndexIndex->sIndex.oi = psPointer->sIndexedMemory.oi;
		psPointer->pvCache = pvData;
		cPreAllocated.Kill();
		return TRUE;
	}
	else
	{
		cPreAllocated.Kill();
		return FALSE;
	}
}