⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdd_memapi.c.svn-base

📁 gif to bmp conversion
💻 SVN-BASE
字号:
#define MODULE_NAME						"jdd"
#define FILE_NAME						"jdd_memapi.c"
/***************************************************************************
*
* File Name : jdd_memapi.c
*
* IMPORTANT NOTICE
*
* Please note that any and all title and/or intellectual property rights 
* in and to this Software or any part of this (including without limitation 
* any images, photographs, animations, video, audio, music, text and/or 
* "applets," incorporated into the Software), herein mentioned to as 
* "Software", the accompanying printed materials, and any copies of the 
* Software, are owned by Jataayu Software (P) Ltd., Bangalore ("Jataayu") 
* or Jataayu's suppliers as the case may be. The Software is protected by 
* copyright, including without limitation by applicable copyright laws, 
* international treaty provisions, other intellectual property laws and 
* applicable laws in the country in which the Software is being used. 
* You shall not modify, adapt or translate the Software, without prior 
* express written consent from Jataayu. You shall not reverse engineer, 
* decompile, disassemble or otherwise alter the Software, except and 
* only to the extent that such activity is expressly permitted by 
* applicable law notwithstanding this limitation. Unauthorized reproduction 
* or redistribution of this program or any portion of it may result in severe 
* civil and criminal penalties and will be prosecuted to the maximum extent 
* possible under the law. Jataayu reserves all rights not expressly granted. 
* 
* THIS SOFTWARE IS PROVIDED TO YOU "AS IS" WITHOUT WARRANTY OF ANY KIND 
* AND ANY AND ALL REPRESENTATION 
*
***************************************************************************
*
*
* File Description
* ----------------
*
* Purpose			: This file contains all the memory management API calls
*						for Win32 platform.
*
*
* 
* Created By		: V. Grace Piradhiba
* Created Date		: 10 May 2004
*
* Current Revision	:
*
*
***************************************************************************
*
*
* Revision Details
* ----------------
* 
* 1. Modified By, Modified Date, Purpose
* Modified By		: V. Grace Piradhiba
* Modified Date		: 18 May 2004
* Purpose			: jc_memalloc changed to jdd_memalloc
*					  jc_memfree changed to jdd_memfree
*					  jdd_TimerApi.h also modified to reflect this change.
*
*
* 2. Modified By, Modified Date, Purpose
* 3. Modified By, Modified Date, Purpose
*
*
*
***************************************************************************/


/***************************************************************************
* System Include Files
**************************************************************************/



/***************************************************************************
* User Include Files
**************************************************************************/
# include <ddl.h>

#ifdef JDD_LOG_ENABLED
#define __MODULE_ID__					OTHER_MODULE
#else
#define __MODULE_ID__					0
#endif

# include <jcalnet.h>

#ifndef _ENABLE_MEM_CONSUMPTION
/***************************************************************************
* Type Definitions
**************************************************************************/

#define	MEM_LOG_FILE	TEXT ("C:\\temp\\alloc.log")

/***************************************************************************
* Macros
**************************************************************************/


/***************************************************************************
* Global Variable declarations and definitions
**************************************************************************/
HANDLE ghFile = NULL ;

/***************************************************************************
* Local Function Prototypes
**************************************************************************/
#ifndef MEM_USE_SYSTEMCALL
static void * memalloc (JC_UINT32 items , JC_UINT32 size) ;
static void memfree (void *ptr) ;
#endif

#ifdef MEM_LOG_ENABLED
	static JC_INT32 add_to_alloc_log (const JC_INT8 *fname, JC_INT32 line, JC_UINT32 size, JC_UINT32 len) ;
	static JC_INT32 add_to_free_log (const JC_INT8 *fname, JC_INT32 line, JC_INT32 *pi) ;
#endif


/***************************************************************************
* All Local Function Definitions
**************************************************************************/

JC_RETCODE jdd_MemInit (void)
{
#ifdef MEM_LOG_ENABLED
	init_alloc_log () ;
#endif
	return JC_OK ;
}


JC_RETCODE jdd_MemDeinit (void)
{
#ifdef MEM_LOG_ENABLED
	close_alloc_log () ;
#endif
	return JC_OK ;
}


#ifdef MEM_LOG_ENABLED

static JC_INT32 g_allocid = 1;
static JC_INT32 gTotalAlloc = 0;
static FILE *gAllocLog = NULL ;
static FILE *gMemLog = NULL ;
static JC_INT32 add_to_alloc_log (const JC_INT8 *fname, JC_INT32 line, JC_UINT32 size, JC_UINT32 len) ;
static JC_INT32 add_to_free_log (const JC_INT8 *fname, JC_INT32 line, JC_INT32 *pi) ;
static JC_INT32 alloc_failure (const JC_INT8 *fname, JC_INT32 line, JC_UINT32 size) ;
static JC_INT32 free_overrun (const JC_INT8 *fname, JC_INT32 line, JC_UINT32 size) ;

void * jdd_MemDebugAlloc (const JC_INT8 *fname, JC_INT32 line, JC_UINT32 size, JC_UINT32 len)
{
	void *p ;
	JC_INT32 *pi ;

	p = memalloc (1, len * size + 8 + 4) ; //arun 0107. 32 bytes overrun-detect zone.
	if (!p) 
	{
		alloc_failure (fname, line, len*size);
		return p;
	}
	add_to_alloc_log (fname, line, size, len) ;
	pi = (JC_INT32 *) p ;
	*pi = g_allocid ;
	*(pi + 1) = len * size ;
	g_allocid++ ;

	jc_memset ((JC_INT8 *)p + 8 + len*size, '~', 4) ; // arun 0107 fill OD zone with a5
	gTotalAlloc += (len * size) ;
	/*
	if (gAllocLog)
		fprintf (gAllocLog, "A@ %u\n", pi + 2) ;
	*/
	return pi + 2 ;
}


void jdd_MemDebugFree (const JC_INT8 *fname, JC_INT32 line, void *p)
{
	JC_INT32 *pi ;
	JC_INT32 i, size ;

	if (p == NULL)
	{
		return ;
	}
	pi = (JC_INT32 *)p ;
	size = *(pi - 1) ;

	add_to_free_log (fname, line, pi) ;

	for (i = 0; i < 4; i++) 
	{
		if (*((JC_INT8 *) p + size + i) != '~') 
		{
			free_overrun(fname, line, size);
		}
	}
	gTotalAlloc -= (size);
	memfree (pi - 2) ;
}


void* jdd_MemDebugRealloc (const JC_INT8 *fname, JC_INT32 line, void *p, JC_UINT32 uiSize) 
{
	JC_INT32 *pi ;
	JC_INT32 i, iPrevSize ;
	void *pNewPtr ;

	if (p != NULL)
	{
		pi = (JC_INT32 *)p ;
		iPrevSize = *(pi - 1) ;

		add_to_free_log (fname, line, pi) ;

		for (i = 0; i < 4; i++) 
		{
			if (*((JC_INT8 *) p + iPrevSize + i) != '~') 
			{
				free_overrun(fname, line, iPrevSize);
			}
		}
		pNewPtr = jdd_MemDebugAlloc (fname, line, 1, uiSize) ;
		if (pNewPtr != NULL)
		{
			memcpy (pNewPtr, p, iPrevSize) ;
		}
		gTotalAlloc -= (iPrevSize) ;
		memfree (pi - 2) ;
	}
	else
	{
		pNewPtr = jdd_MemDebugAlloc (fname, line, 1, uiSize) ;
	}
	return pNewPtr ;
}


JC_INT32 init_alloc_log () 
{
	if (gAllocLog == NULL)
	{
		gAllocLog = fopen ("c:\\temp\\alloc.log", "w") ;
		if (gAllocLog == NULL)
		{
			return 0 ;
		}
		return 1 ;
	}
	return 0 ;
}

JC_INT32 close_alloc_log ()
{
	if (gAllocLog)
	{
		fclose (gAllocLog) ;
		gAllocLog = NULL ;
	}
	return 0 ;
}

JC_INT32 get_total_alloc ()
{
	return gTotalAlloc ;
}

#ifdef _MEM_CHECK_

JC_INT32 add_buflen_allocid_to_log (const JC_INT8 *fname, JC_INT8 *pcUrl, JC_UINT32 uiBufLen)//Added on 13th Jan by Sriram
{
	gMemLog = fopen ("c:\\temp\\mem.log", "a") ;
	if (NULL == gMemLog)
	{
		return 0 ;
	}
	fprintf (gMemLog, "BM@ %s %s BufLen: %d InitId: %d ", fname, pcUrl, uiBufLen, g_allocid) ;
	fclose (gMemLog) ;
	gMemLog = NULL ;
	return 1 ;
}

JC_INT32 add_to_mem_log (const JC_INT8 *fname, JC_INT32 line, JC_INT8 *funcName, JC_INT32 initialMemVal, JC_INT32 finalMemVal, JC_UINT32 uiTotalControls)
{
	gMemLog = fopen ("c:\\temp\\mem.log", "a") ;
	if (gMemLog)
		fprintf (gMemLog, "M@ %s:%d  Function:%s Init:%d Final:%d Total:%d Finalid:%d TotalControls:%d \n\n", fname, line, funcName, initialMemVal, finalMemVal, (finalMemVal - initialMemVal), g_allocid, uiTotalControls) ;
	fclose (gMemLog) ;
	gMemLog = NULL ;
	return 0 ;
}

#endif

static JC_INT32 add_to_alloc_log (const JC_INT8 *fname, JC_INT32 line, JC_UINT32 size, JC_UINT32 len)
{
	if (gAllocLog)
		fprintf (gAllocLog, "A@ %s:%d, id:%d, size:%d TA[%05ld]\n", fname, line, g_allocid, len*size, gTotalAlloc);
	return 0 ;
}


static JC_INT32 add_to_free_log (const JC_INT8 *fname, JC_INT32 line, JC_INT32 *pi)
{
	if (gAllocLog)
		fprintf (gAllocLog, "F@ %s:%d, id:%d, size:%d FTA[%ld]\n", fname, line, *(pi-2), *(pi-1), gTotalAlloc) ;
	//fprintf (gAllocLog, "F@ %s:%d, id:%d, size:%d %p FTA[%ld]\n", fname, line, *(pi-2), *(pi-1), pi-2, gTotalAlloc);
	return 0 ;
}


static JC_INT32 alloc_failure (const JC_INT8 *fname, JC_INT32 line, JC_UINT32 size)
{
	if (gAllocLog)
		fprintf (gAllocLog, "MAF in %s:%d[%d]\n", fname, line, size);
	return 0 ;
}


static JC_INT32 free_overrun (const JC_INT8 *fname, JC_INT32 line, JC_UINT32 size)
{
	if (gAllocLog)
		fprintf (gAllocLog, "OD in %s:%d[%d]\n", fname, line, size);
	return 0 ;
}

#else
#ifndef MEM_USE_SYSTEMCALL

void *jdd_MemAlloc (JC_UINT32 uiItemSize, JC_UINT32 uiItems)
{
	if ((0 == uiItemSize) || (0 == uiItems))
	{
		return NULL ;
	}
	return memalloc (uiItemSize, uiItems) ;
}


void jdd_MemFree (void *pFreeMemAddress)
{
	if (NULL == pFreeMemAddress)
	{
		return ;
	}
	memfree (pFreeMemAddress) ;
	pFreeMemAddress = NULL ;
}

void * jdd_MemRealloc (void *pPtr, JC_UINT32 uiSize) 
{
	if (uiSize == 0)
	{
		return pPtr ;
	}
	return realloc (pPtr, uiSize) ;
}
#endif
#endif /** MEM_LOG_ENABLED **/

#ifndef MEM_USE_SYSTEMCALL

static void * memalloc (JC_UINT32 items , JC_UINT32 size)
{
	void *pMem = NULL ;

	pMem = calloc (items, size) ;

	return pMem ;
}


static void memfree (void *ptr)
{
	free (ptr) ;
}

#endif

#endif
/***************************************************************************
* All Global Function Definitions
**************************************************************************/


/* END OF FILE */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -