www.pudn.com > Gimcrack-v0.0051-Source.zip > mem.cpp


#include "mem.h" 
 
 
///////////////////////////////////////////////////////////////////////////////// 
 
bool GcMem::Init(uint size, bool vidMem) 
{ 
	// Save the size of the pool 
	poolSize = size; 
	usedSize = 0; 
	g_Debug->Memory(poolSize); 
 
	if(vidMem) 
	{ 
		// Allocate memory on the graphic card 
		memPool = (byte*)wglAllocateMemoryNV(poolSize, 0.0f, 0.0f, 1.0f); 
 
		if(memPool) 
		{ 
			// Activate the use of the NV_VERTEX_ARRAY_RANGE 
			glVertexArrayRangeNV(poolSize, memPool); 
			glEnableClientState(GL_VERTEX_ARRAY_RANGE_NV); 
 
			// Using video memory 
			videoMem = true; 
 
			g_Debug->Log("Using video ram\n"); 
 
			return true; 
		} 
	} 
 
	/* Video memory can either not be used or is wanted to be used */ 
 
	// Allocate memory of the system ram 
	memPool = (byte*)malloc(poolSize); 
 
	// Using system memory 
	videoMem = false; 
 
	g_Debug->Log("Using system ram\n"); 
 
	return true; 
} 
 
///////////////////////////////////////////////////////////////////////////////// 
 
void *GcMem::Allocate(uint size) 
{ 
	// Check to see that there's enought mem left 
	if((poolSize - usedSize) >= size) 
	{ 
		void *address = (void*) ((uint)memPool + usedSize); 
 
		usedSize += size; 
 
		return address; 
	} 
	else 
	{ 
		// Not enought memory left in the pool 
		return NULL; 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////////// 
 
void GcMem::ShutDown() 
{ 
	// Free the memory pool 
	if(videoMem) { 
		wglFreeMemoryNV(memPool); 
		memPool = NULL; 
	} 
	else { 
		free(memPool); 
		memPool = NULL; 
	} 
} 
 
/////////////////////////////////////////////////////////////////////////////////