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

📄 dvb_mem.c

📁 DVB软件,基于CT216软件的开发源程序.
💻 C
📖 第 1 页 / 共 3 页
字号:
#if 0
if (mem_type == 1)
{
	if (gen_point >= 170 && gen_point <= 190)
	{
		MEM_DBG(("!!!!gen_point[%d] u32Number_of_bytes[%d]\n", gen_point, u32Number_of_bytes));
	}	
}
#endif

	if ( u32Number_of_bytes%4 != 0)
	{
		u32Number_of_bytes=(u32Number_of_bytes/4+1)*4;
	}


	au32Start = pau32Start[mem_type];
	au32Length = pau32Length[mem_type];
	au32Used = pau32Used[mem_type];
	pu8BufferPool = pau8BufferPool[mem_type];

	MAX_MEMORY_ALLOCATE = u32aMaxMemoryAllocate[mem_type];
	
	if (CT_OS_WaitOnSemaphore(&semDVB_MEM, CTOS_WAIT)!= EN_CTOS_SUCCESS)
    {
        return NULL;
    }
    
    if (!ab8Initialized [mem_type])
    {
    	CT_OS_FreeSemaphore(&semDVB_MEM);
		return NULL;
	}	
		
	for(alloc_idx=0; alloc_idx<MAX_MEMORY_ALLOCATE; alloc_idx++)
	{// search all free block
		u32AllocGroup = alloc_idx/MAX_NUM_IN_A_GROUP;
		u32AllocBitNo = alloc_idx%MAX_NUM_IN_A_GROUP;
//		if ( ab8Used[alloc_idx]==FALSE && au32Length[alloc_idx]>=u16Number_of_bytes )
		if ((!(au32Used[u32AllocGroup]&au32UsedMask[u32AllocBitNo])) && au32Length[alloc_idx]>=u32Number_of_bytes )
		{// a free block meet the request
			for(free_idx=0; free_idx<MAX_MEMORY_ALLOCATE+1; free_idx++)
			{// search unused record
				if ( au32Length[free_idx]==0 )
				{// a record of table meet.

					u32FreeGroup = free_idx/MAX_NUM_IN_A_GROUP;
					u32FreeBitNo = free_idx%MAX_NUM_IN_A_GROUP;

					au32Length[free_idx] = au32Length[alloc_idx] - u32Number_of_bytes;
					//compute the remain free size, setup to free record
					au32Length[alloc_idx] = u32Number_of_bytes;
					//setup the allocate size to allocated record.
					au32Start[free_idx] =au32Start[alloc_idx]+u32Number_of_bytes;
					//compute the start pointer of new free block.

//					ab8Used[alloc_idx] =TRUE;
					au32Used[u32AllocGroup] |= au32UsedMask[u32AllocBitNo];

					//setup the used block flag

//					ab8Used[free_idx]=FALSE;
					au32Used[u32FreeGroup] &= ~au32UsedMask[u32FreeBitNo];
					//setup the new free block flag
//					return &au8BufferPool[ au32Start[alloc_idx] ];

//MEM_DBG(("mem_type[%d] alloc_idx[%d] size[0x%x] gen_point[%d]\n",
//	mem_type, alloc_idx, (int)u32Number_of_bytes, gen_point));

					if (u16Biggest[mem_type] < alloc_idx)
					{
						u16Biggest[mem_type] = alloc_idx;
					}
	
#ifdef DEBUG_MSG
if (mem_type == gDebugPoolType)
{
	u16 u16idx = 0;
	u32 u32AllocGroup, u32AllocBitNo; 
	
	u16adebug [alloc_idx] = gen_point;
	
	if ((gen_point >= 150) && (gen_point <= 170))
	{
		MEM_DBG(("gen_point[%d] len[%d]\n", (int)gen_point, (int)au32Length[alloc_idx]));
	}		
	for (u16idx = 0; u16idx < u16Biggest[mem_type]; u16idx ++)
	{
		if ((u16adebug [u16idx] == gen_point) && (u16idx != alloc_idx))
		{
			if ((gen_point != 205) && (gen_point != 206) && (gen_point != 222) && (gen_point != 104)
				&& (gen_point != 125) && (gen_point != 126) && (gen_point != 203) && (gen_point != 103)
				&& (gen_point != 221) 
				&& (gen_point != 301) && (gen_point != 524))
			{
				u32AllocGroup = u16idx/MAX_NUM_IN_A_GROUP;
				u32AllocBitNo = u16idx%MAX_NUM_IN_A_GROUP;

				if (au32Used[u32AllocGroup]&au32UsedMask[u32AllocBitNo])
				{
					char strTaskName[9];
					
					strTaskName[8]='\0';
    
    				CT_OS_GetRunningTaskName( strTaskName, 8);
    
					MEM_DBG(("### duplicate [%d] len[%d] strTaskName[%s] ###\n", 
						(int)gen_point, (int)au32Length[u16idx], strTaskName));
				}	
			}	
		}	
	}	
}
#endif /* end #ifdef DEBUG_MSG */
//MEM_DBG(("gen_point[%d] size[0x%x] alloc_idx[0x%x]\n", gen_point, u32Number_of_bytes, alloc_idx));
gen_point = 0;
#ifdef DEBUG_EIT
if ((mem_type == 0) && (alloc_idx < GEN_MAX_MEMORY_ALLOCATE))
{
	u16aEventID[alloc_idx] = u16EventID;
}
u16EventID = 0;
#endif // end ifdef DEBUG_EIT */

#if 0
if (mem_type == 1)
{
	MEM_DBG(("###########mem_type[%d] u32Number_of_bytes[%d] alloc_idx[%d]\n",
		mem_type, u32Number_of_bytes, alloc_idx));
}
#endif

					CT_OS_FreeSemaphore(&semDVB_MEM);
					return (pu8BufferPool+au32Start[alloc_idx]);
					//return start pointer of used block
				}
			}
		}
	}

	MEM_MSG(("{DVB_MEM_Allocate} FAIL type [%d] size[0x%x] gen_point[%ld]\n", 
		mem_type, (int)u32Number_of_bytes, gen_point));

	CT_OS_FreeSemaphore(&semDVB_MEM);
	return NULL;
}
#endif /* end #if (!defined (CT_OS_MEM_ENABLE) || defined (DRAM_FRAME_DIGEST_BUFFER_USED)) */

#ifdef CT_OS_MEM_ENABLE	
void *os_mem_alloc (EN_MEMPOOL_TYPE mem_type, u32 u32Number_of_bytes)
{
	EN_CTOS_STATUS	enStatus = EN_CTOS_SUCCESS;
	CTOS_MEM_POOL	*pstMemPool = NULL;
	u32				*pu32Data = NULL;
	
	if(b8MemInialized != TRUE)
	{
		return NULL;
	}
	if(mem_type >= MAX_MEMPOOL_TYPE)
	{
		return NULL;
	}
	if ( u32Number_of_bytes%4 != 0)
	{
		u32Number_of_bytes=(u32Number_of_bytes/4+1)*4;
	}

//DVB_MEM_PrintInfo (mem_type);	

	if (CT_OS_WaitOnSemaphore(&semDVB_MEM, CTOS_WAIT)!= EN_CTOS_SUCCESS)
    {
    	MEM_MSG(("{DVB_MEM_Allocate} allocate wait semaphore fail\n"));
        return NULL;
    }
    
    if (!ab8Initialized [mem_type])
    {
    	CT_OS_FreeSemaphore(&semDVB_MEM);
		return NULL;
	}	
		
	pstMemPool = apstMemPool[mem_type];			/* pool structure */
	
	if (!pstMemPool)
	{
		CT_OS_FreeSemaphore(&semDVB_MEM);
		return NULL;	
	}	
			
	enStatus = CT_OS_AllocMem (pstMemPool, (void**)&pu32Data, u32Number_of_bytes);
	if (enStatus != EN_CTOS_SUCCESS)
	{
		CT_OS_FreeSemaphore(&semDVB_MEM);
		return NULL;
	}	
	
#ifdef DEBUG_MSG
if (mem_type == gDebugPoolType)
{
	u16 u16idx = 0;
	
	for (u16idx = 0; (u16idx < u16Biggest[mem_type] + 1) && (u16idx < DEBUG_MAX_MEMORY_ALLOCATE); u16idx++)
	{
		if (u16adebug[u16idx] == 0)
		{
			u16adebug[u16idx] = gen_point;
			u32amemp[u16idx] = (u32)pu32Data;
			break;
		}	 
	}
	
	if (u16idx < DEBUG_MAX_MEMORY_ALLOCATE)
	{
		if (u16Biggest[mem_type] < u16idx)
		{
			u16Biggest[mem_type] = u16idx;
		}
		u32AllocatedNO ++;
	}
	else
	{
		MEM_MSG(("DVB_MEM_ALLOCATE debug fail\n"));
	}		
}
#endif /* end #ifdef DEBUG_MSG */

	CT_OS_FreeSemaphore (&semDVB_MEM);
	return (void*) pu32Data;	
}	
#endif /* end #ifdef CT_OS_MEM_ENABLE */

void *DVB_MEM_Allocate (EN_MEMPOOL_TYPE mem_type, u32 u32Number_of_bytes)
{
#ifdef CT_OS_MEM_ENABLE
	if (mem_type == BIG_MEMPOOL_TYPE)
	{
	#if 0 //#ifdef DRAM_FRAME_DIGEST_BUFFER_USED
		return (ctk_mem_alloc (mem_type, u32Number_of_bytes));		
	#else
		return (os_mem_alloc (mem_type, u32Number_of_bytes));
	#endif		
	}
	else
	{
		return (os_mem_alloc (mem_type, u32Number_of_bytes));
	}	
#else
	return (ctk_mem_alloc (mem_type, u32Number_of_bytes));	
#endif /* end #ifdef CT_OS_MEM_ENABLE */

}

#if (!defined (CT_OS_MEM_ENABLE) || defined (DRAM_FRAME_DIGEST_BUFFER_USED)) 
static bool8 CombineFree(EN_MEMPOOL_TYPE mem_type, u32 alloc_idx, u32 *pu8Idx)
{
	u32 free_idx = 0;
	u32 *au32Start = NULL;
	u32 *au32Length = NULL;
	u32 *au32Used = NULL;
	u32 MAX_MEMORY_ALLOCATE = 0;

	u32 u32FreeGroup = 0, u32FreeBitNo = 0;

	if (mem_type >= MAX_MEMPOOL_TYPE)
	{
		return FALSE;
	}
	au32Start = pau32Start[mem_type];
	au32Length = pau32Length[mem_type];
	au32Used = pau32Used[mem_type];

	MAX_MEMORY_ALLOCATE = u32aMaxMemoryAllocate[mem_type];

	for ( free_idx=0; free_idx<MAX_MEMORY_ALLOCATE+1;free_idx++)
	{// try to combine two free block in one.
		u32FreeGroup = free_idx/MAX_NUM_IN_A_GROUP;
		u32FreeBitNo = free_idx%MAX_NUM_IN_A_GROUP;

//		if ( ab8Used[free_idx]==TRUE || au32Length[free_idx]==0)
		if ((au32Used[u32FreeGroup]&au32UsedMask[u32FreeBitNo]) || au32Length[free_idx]==0)
		{
			continue;
		}	
		if ( (au32Start[alloc_idx]+au32Length[alloc_idx]) ==au32Start[free_idx] )
		{//[alloc block] + [free block] = [new free block] + [null]
//			MEM_DBG(("combine %d %d to %d.\n",alloc_idx, free_idx, alloc_idx));
//			dump_table();
			au32Length[alloc_idx]+=au32Length[free_idx];
			au32Length[free_idx]=0;
			*pu8Idx=alloc_idx;
			return TRUE;
		}
		if ( (au32Start[free_idx]+au32Length[free_idx]) ==au32Start[alloc_idx] )
		{//[free block] + [alloc block] = [new free block] + [null]
//			MEM_DBG(("combine %d %d to %d.\n",alloc_idx, free_idx, free_idx));
//			dump_table();
			au32Length[free_idx]+=au32Length[alloc_idx];
			au32Length[alloc_idx]=0;
			*pu8Idx=free_idx;
			return TRUE;
		}
	}

	return FALSE;
}

void ctk_mem_free (EN_MEMPOOL_TYPE mem_type, void *pFree)
{
	u32 alloc_idx;
	u32 alloc_idx1;
	u32 pos1, pos2;
	bool8 b8Ret;
	u32 *au32Start = NULL;
	u32 *au32Length = NULL;
	u32 *au32Used = NULL;
	u8 *pu8BufferPool = NULL;
	u32 MAX_MEMORY_ALLOCATE = 0;
	u32 u32AllocGroup = 0, u32AllocBitNo = 0;
	
	EN_CTOS_STATUS	enStatus = EN_CTOS_SUCCESS;
	
	if (b8MemInialized != TRUE)
	{
		return;
	}
	if (mem_type >= MAX_MEMPOOL_TYPE)
	{
		MEM_DBG(("{DVB_MEM_Free} return wrong mem_type\n"));
		return;
	}
	
//MEM_DBG(("free mem_type[%d]\n", mem_type));
	enStatus = CT_OS_WaitOnSemaphore(&semDVB_MEM, CTOS_WAIT);

	if (enStatus != EN_CTOS_SUCCESS)
    {
		MEM_DBG(("{DVB_MEM_Free} return because Wait Semaphore wrong status[%d]\n", enStatus));
        return;
    }

	au32Start = pau32Start[mem_type];
	au32Length = pau32Length[mem_type];
	au32Used = pau32Used[mem_type];
	pu8BufferPool = pau8BufferPool[mem_type];

	MAX_MEMORY_ALLOCATE = u32aMaxMemoryAllocate[mem_type];

//MEM_DBG(("{DVB_MEM_Free} mem_type[0x%x] pFree[0x%x]\n", mem_type, pFree));
	for(alloc_idx=0; alloc_idx<MAX_MEMORY_ALLOCATE; alloc_idx++)
	{// search all record of table
		u32AllocGroup = alloc_idx/MAX_NUM_IN_A_GROUP;
		u32AllocBitNo = alloc_idx%MAX_NUM_IN_A_GROUP;
//		if ( ab8Used[alloc_idx]==TRUE )
		if (au32Used[u32AllocGroup]&au32UsedMask[u32AllocBitNo])
		{//the block is used
			pos1=au32Start[alloc_idx];
			pos2=au32Start[alloc_idx]+au32Length[alloc_idx];
//			if ( pFree>= &au8BufferPool[ pos1 ] && pFree < &au8BufferPool[ pos2 ] )
			if ( (u32)pFree>= ((u32)pu8BufferPool+pos1) && (u32)pFree < ((u32)pu8BufferPool+pos2) )
			{//the pointer is in this block
//				MEM_DBG(("free %d\n", alloc_idx));
//				au32Used[alloc_idx]=FALSE;
#ifdef DEBUG_EIT
if (mem_type == 0)
{
u16aEventID[alloc_idx] = 0;
}
#endif /* end #ifdef DEBUG_EIT */
				au32Used[u32AllocGroup] &= ~au32UsedMask[u32AllocBitNo];
//MEM_DBG(("{DVB_MEM_Free} pFree[0x%x] alloc_idx[0x%x] used[0x%x]\n", pFree, alloc_idx, au32Used[u32AllocGroup]));
				b8Ret=CombineFree(mem_type, alloc_idx, &alloc_idx1);
				//set this block is unused
				if ( b8Ret==TRUE)
				{//combine two free block
					alloc_idx=alloc_idx1;
					CombineFree(mem_type, alloc_idx, &alloc_idx1);
				}
				CT_OS_FreeSemaphore(&semDVB_MEM);
				return;
			}
		}
	}

//MEM_DBG(("mem_type[%d] free[0x%x] pu8BufferPool[0x%x] end[0x%x]\n", 
//	mem_type, (int)pFree, (int)pu8BufferPool, (int)((int)pu8BufferPool + (int)au32BufferSize[mem_type])));
//	MEM_DBG(("can not find the meet pool\n"));
	CT_OS_FreeSemaphore(&semDVB_MEM);
	return;
}
#endif /* end #if (!defined (CT_OS_MEM_ENABLE) || defined (DRAM_FRAME_DIGEST_BUFFER_USED)) */

#ifdef CT_OS_MEM_ENABLE	
void os_mem_free (EN_MEMPOOL_TYPE mem_type, void *pFree)
{
	EN_CTOS_STATUS	enStatus = EN_CTOS_SUCCESS;
	CTOS_MEM_POOL	*pstMemPool = NULL;
	
	if (b8MemInialized != TRUE)
	{
		return;
	}
	if (mem_type >= MAX_MEMPOOL_TYPE)
	{
		MEM_DBG(("{DVB_MEM_Free} return wrong mem_type\n"));
		return;
	}

	if (CT_OS_WaitOnSemaphore(&semDVB_MEM, CTOS_WAIT) != EN_CTOS_SUCCESS)
	{
		MEM_MSG(("{DVB_MEM_Free} free semaphore fail\n")); 
		return;
	}	
	
	if (!ab8Initialized [mem_type])
	{
		CT_OS_FreeSemaphore (&semDVB_MEM);
		return;
	}	
		
	pstMemPool = apstMemPool[mem_type];			/* pool structure */
	
	if (!pstMemPool)
	{
		CT_OS_FreeSemaphore (&semDVB_MEM);
		return;
	}	
		
	/* free memory */ 
	enStatus = CT_OS_FreeMem (pFree);

#ifdef DEBUG_MSG
if (mem_type == gDebugPoolType)
{
	u16 u16idx = 0;
	
	for (u16idx = 0; (u16idx < u16Biggest[mem_type]) && (u16idx < DEBUG_MAX_MEMORY_ALLOCATE); u16idx ++)
	{
		if ((u32*)u32amemp[u16idx] == (u32*)pFree)
		{
			u32amemp[u16idx] = 0;
			u16adebug[u16idx] = 0;
			break;
		}	 
	}
	
	if (u16idx < DEBUG_MAX_MEMORY_ALLOCATE)
	{
		u32AllocatedNO --;
	}
	else
	{
		MEM_MSG(("DVB_MEM_FREE debug fail\n"));
	}		
}
#endif /* end #ifdef DEBUG_MSG */
	
	if (enStatus != EN_CTOS_SUCCESS)
	{
		MEM_MSG(("DVB_MEM_Free fail, mem_type = %ld, pFree = 0x%08lX\n", (u32)mem_type, (u32)pFree));
		CT_OS_FreeSemaphore(&semDVB_MEM);
		return;
	}	
	
	CT_OS_FreeSemaphore (&semDVB_MEM);
	return;
}	
#endif

void DVB_MEM_Free (EN_MEMPOOL_TYPE mem_type, void *pFree)
{
#ifdef CT_OS_MEM_ENABLE
	if (mem_type == BIG_MEMPOOL_TYPE)
	{
	#if 0 //#ifdef DRAM_FRAME_DIGEST_BUFFER_USED
		return (ctk_mem_free (mem_type, pFree));		
	#else
		return (os_mem_free (mem_type, pFree));
	#endif		
	}
	else
	{
		return (os_mem_free (mem_type, pFree));
	}	
#else
	return (ctk_mem_free (mem_type, pFree));	
#endif /* end #ifdef CT_OS_MEM_ENABLE */
}

#if (!defined (CT_OS_MEM_ENABLE) || defined (DRAM_FRAME_DIGEST_BUFFER_USED))
void ctk_mem_printinfo (EN_MEMPOOL_TYPE mem_type)
{
	u16 i;	
	u32 u32UsedMem = 0;
	u32	u32UsedCount = 0;
	u32 u32NoUsedCount = 0;

	u32 *au32Start = NULL;
	u32 *au32Length = NULL;
	u32 *au32Used = NULL;
	u8 *pu8BufferPool = NULL;
	u32 u32BufferSize = 0;
	u32 MAX_MEMORY_ALLOCATE = 0;
	u32 u32MaxFreeSize = 0, u32MaxUsedSize = 0;

	u32 u32AllocGroup = 0, u32AllocBitNo = 0;
	u8 u8Name[10];
	
	if (mem_type >= MAX_MEMPOOL_TYPE)
	{
		return;
	}	
	
	au32Start = pau32Start[mem_type];
	au32Length = pau32Length[mem_type];
	au32Used = pau32Used[mem_type];
	pu8BufferPool = pau8BufferPool[mem_type];
	u32BufferSize =  au32BufferSize[mem_type];

⌨️ 快捷键说明

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