www.pudn.com > Particlefilter_code.zip > MemAlloc.cpp


/*******************************************************************************\ 
 
Description: 
 
	Header  file of Memory allocation routiness  
 
history: 
	XinFan Mar. 16, 2002 
\*******************************************************************************/ 
 
//#include "StdAfx.h" 
#include  
#include "MemAlloc.h" 
 
 
// 
//Allocate memeory and arrange pointers 
// 
bool AllocMem( BYTE** &pLoc , long lWidth , long lHeight ) 
{ 
	if ((pLoc = (BYTE **)malloc(lHeight * sizeof(BYTE *))) != NULL) 
	{ 
		if ((*pLoc = (BYTE *)malloc(lWidth * lHeight * sizeof(BYTE))) != NULL) 
		{ 
			for(long j = 0; j < lHeight; j++) 
			{ 
				*(pLoc + j) = (BYTE *)(*pLoc + lWidth * j); 
			} 
			return true; 
		} 
	} 
	exit(0); 
	return false; 
} 
 
// 
//Free memory 
// 
//Note: Should be called as pMem = FreeMem(pMem) 
// 
BYTE** FreeMem(BYTE **pMem) 
{ 
	if (pMem == NULL) return NULL; 
	free(*pMem); 
	*pMem = NULL; 
	free(pMem); 
	return NULL; 
} 
 
bool AllocMem( long** &pLoc , long lWidth , long lHeight ) 
{ 
	if ((pLoc = (long **)malloc(lHeight * sizeof(long *))) != NULL) 
	{ 
		if ((*pLoc = (long *)malloc(lWidth * lHeight * sizeof(long))) != NULL) 
		{ 
			for(long j = 0; j < lHeight; j++) 
			{ 
				*(pLoc + j) = (long *)(*pLoc + lWidth * j); 
			} 
			return true; 
		} 
	} 
	exit(0); 
 
	return false; 
} 
 
// 
//Free memory 
// 
//Note: Should be called as pMem = FreeMem(pMem) 
// 
long** FreeMem(long **pMem) 
{ 
	if (pMem == NULL) return NULL; 
	free(*pMem); 
	*pMem = NULL; 
	free(pMem); 
	return NULL; 
} 
 
// 
//Allocate memeory and arrange pointers 
// 
bool AllocMem( double** &pLoc , long lWidth , long lHeight ) 
{ 
	if ((pLoc = (double **)malloc(lHeight * sizeof(double *))) != NULL) 
	{ 
		if ((*pLoc = (double *)malloc(lWidth * lHeight * sizeof(double))) != NULL) 
		{ 
			for(long j = 0; j < lHeight; j++) 
			{ 
				*(pLoc + j) = (double *)(*pLoc + lWidth * j); 
			} 
			return true; 
		} 
	} 
	exit(0); 
 
	return false; 
} 
 
double** FreeMem(double **pMem) 
{ 
	if (pMem == NULL) return NULL; 
	free(*pMem); 
	*pMem = NULL; 
	free(pMem); 
	return NULL; 
} 
// 
//Allocate memeory and arrange pointers 
// 
bool AllocMem( float** &pLoc , long lWidth , long lHeight ) 
{ 
	if ((pLoc = (float **)malloc(lHeight * sizeof(float *))) != NULL) 
	{ 
		if ((*pLoc = (float *)malloc(lWidth * lHeight * sizeof(float))) != NULL) 
		{ 
			for(long j = 0; j < lHeight; j++) 
			{ 
				*(pLoc + j) = (float *)(*pLoc + lWidth * j); 
			} 
			return true; 
		} 
	} 
	exit(0); 
 
	return false; 
} 
 
float** FreeMem(float **pMem) 
{ 
	if (pMem == NULL) return NULL; 
	free(*pMem); 
	*pMem = NULL; 
	free(pMem); 
	return NULL; 
} 
 
bool AllocMem( float*** &pLoc , long lLayer, long lWidth , long lHeight ) 
{ 
	pLoc = (float ***)malloc(lLayer * sizeof(float**)); 
	if (pLoc != NULL) 
	{ 
		float **sample_i; 
		for (int i = 0; i < lLayer; i++) 
		{ 
			sample_i = NULL; 
			if (AllocMem(sample_i, lWidth, lHeight)) 
			{ 
				//memset(*sample_i, 0, nStateDim * nSamplesNum *sizeof(float)); 
				*(pLoc + i) = sample_i; 
			} 
			else 
			{ 
				exit(0); 
				return false; 
			} 
		} 
		return true; 
	} 
	exit(0); 
	return false; 
} 
float *** FreeMem(float ***pMem, long lLayer) 
{ 
	for (int i = 0; i < lLayer; i++) 
		*(pMem + i) = FreeMem(*(pMem + i)); 
	free(pMem); 
	return NULL; 
}