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

📄 dvb_mem.c

📁 DVB软件,基于CT216软件的开发源程序.
💻 C
📖 第 1 页 / 共 3 页
字号:
/**************************************************************************

        (C)Copyright Cheertek Inc. 2002-2004,
           D700, all right reserved.

        Product : STB Firmware

****************************************************************************/
#include <stdio.h>
#include <string.h>
#include "malloc.h"
#include "hw_config.h"
#include "ct_config.h"
#include "ct_drammap.h"
#include "ct_os.h"
#include "dvb_type.h"
#include "dvb_mem.h"
#include "ap_defs.h"

/*******************************************************************************************/
/*******************************************************************************************/
#if 0
#define MEM_MSG(p)
#else
#define MEM_MSG(p)			printf p
#endif

#if 1
#define MEM_DBG(p)
#else
#define MEM_DBG(p) 		printf p
#endif

/*******************************************************************************************/       
#ifdef CT216X // use digest frame buffer for teletex & subtitle used 
#define DRAM_FRAME_DIGEST_BUFFER_USED
#define DRAM_FRAME_DIGEST_BUFFER_SIZE				(CT_DRAM_FRAME_DIGEST_ADDR_END - CT_DRAM_FRAME_DIGEST_DEFLICKER_ADDR_ST) 
#endif

#define ADD_BIG_POOL

#define CT_OS_MEM_ENABLE			// use OS memory managerment

//#define DEBUG_MSG /* debug used */
//#define DEBUG_EIT

/* EIT POOL size and pool seting*/
#ifdef DRAM_64MBIT
    #if defined ( CT216S)|| defined (CT216H)
    #define EIT_POOL_SIZE				(300 * 1024)    
    #else
    #define EIT_POOL_SIZE				(768 * 1024)
    #endif
#else
    #define EIT_POOL_SIZE				(1024 * 1024)
#endif

u8 *au8EITPool = NULL;

/* BIG POOL size and pool seting */
#ifdef ADD_BIG_POOL
	#ifdef  DRAM_FRAME_DIGEST_BUFFER_USED
		#define BIG_POOL_SIZE						0
	#else	
		#if defined(ENABLE_SUBTITLE) || defined(TELETEXT_ON_OSD)
			#ifdef DRAM_64MBIT
				#define BIG_POOL_SIZE					(528 * 1024)
			#else
				#define BIG_POOL_SIZE					(528 * 2 * 1024)
			#endif
		#else
			#define BIG_POOL_SIZE						0
		#endif	
	#endif /* end if DRAM_FRAME_DIGEST_BUFFER_USED */	
u8 *au8BIGPool = NULL;
#endif /* end #ifdef ADD_BIG_POOL */

/* GEN POOL size and pool seting */
#ifdef DRAM_64MBIT
#define MIN_GEN_POOL_SIZE				(710 * 1024) //(861 * 1024) 
#else
#define MIN_GEN_POOL_SIZE 				(852 * 1024) //(861 * 2 * 1024)
#endif
u8 *au8GENPool = NULL;

#define GEN_MAX_MEMORY_ALLOCATE 		(1024 * 8)
#define EIT_MAX_MEMORY_ALLOCATE			(1024 * 8)
#ifdef ADD_BIG_POOL
#define BIG_MAX_MEMORY_ALLOCATE			(32)
#endif /* end ifdef ADD_BIG_POOL */
#define MAX_NUM_IN_A_GROUP				32

const u32 au32UsedMask[MAX_NUM_IN_A_GROUP] =
	{1, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7, 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
	1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23, 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30, 1<<31};

#ifndef CT_OS_MEM_ENABLE
static u32 au32GENStart[GEN_MAX_MEMORY_ALLOCATE+1];
static u32 au32GENLength[GEN_MAX_MEMORY_ALLOCATE+1];
static u32 au32GENUsed[(GEN_MAX_MEMORY_ALLOCATE/MAX_NUM_IN_A_GROUP) + 1];
#endif /* #ifndef CT_OS_MEM_ENABLE */


#ifdef ADD_BIG_POOL
#ifndef CT_OS_MEM_ENABLE
static u32 au32BIGStart[BIG_MAX_MEMORY_ALLOCATE+1];
static u32 au32BIGLength[BIG_MAX_MEMORY_ALLOCATE+1];
static u32 au32BIGUsed[(BIG_MAX_MEMORY_ALLOCATE/MAX_NUM_IN_A_GROUP) + 1];
#else
	#if 0 //#ifdef DRAM_FRAME_DIGEST_BUFFER_USED
	static u32 au32BIGStart[BIG_MAX_MEMORY_ALLOCATE+1];
	static u32 au32BIGLength[BIG_MAX_MEMORY_ALLOCATE+1];
	static u32 au32BIGUsed[(BIG_MAX_MEMORY_ALLOCATE/MAX_NUM_IN_A_GROUP) + 1];	
	#endif
#endif /* end CT_OS_MEM_ENABLE */
#endif /* end ifdef ADD_BIG_POOL */

#ifndef CT_OS_MEM_ENABLE
static u32 au32EITStart[EIT_MAX_MEMORY_ALLOCATE+1];
static u32 au32EITLength[EIT_MAX_MEMORY_ALLOCATE+1];
static u32 au32EITUsed[(EIT_MAX_MEMORY_ALLOCATE/MAX_NUM_IN_A_GROUP) + 1];
#endif /* end #ifndef CT_OS_MEM_ENABLE */

static u8 *pau8BufferPool[MAX_MEMPOOL_TYPE];			/* record buffer pool start address */
static u32 au32BufferSize[MAX_MEMPOOL_TYPE];			/* record buffer pool size */
static u32 *pau32Start[MAX_MEMPOOL_TYPE];				/* chunk start address */
static u32 *pau32Length[MAX_MEMPOOL_TYPE];				/* chunk length */
static u32 *pau32Used[MAX_MEMPOOL_TYPE];				/* chunk used status */
static u32 u32aMaxMemoryAllocate[MAX_MEMPOOL_TYPE];		/* max chunk number */
static bool8 ab8Initialized[MAX_MEMPOOL_TYPE];			/* initialized or not */ 

static u16 u16Biggest[MAX_MEMPOOL_TYPE];
static CTOS_SEMAPHORE 	semDVB_MEM;
static bool8 b8MemInialized = FALSE;

#ifdef CT_OS_MEM_ENABLE
CTOS_MEM_POOL  stEITMemoryPool;
CTOS_MEM_POOL  stBIGMemoryPool;
CTOS_MEM_POOL  stGENMemoryPool;
CTOS_MEM_POOL  *apstMemPool[MAX_MEMPOOL_TYPE]; 			/* memory pool structure */
#endif

u32 gen_point = 0;
#ifdef DEBUG_MSG
#define gDebugPoolType 		GEN_MEMPOOL_TYPE
#define DEBUG_MAX_MEMORY_ALLOCATE GEN_MAX_MEMORY_ALLOCATE
u16 u16adebug [DEBUG_MAX_MEMORY_ALLOCATE];
#ifdef CT_OS_MEM_ENABLE
static u32 u32amemp [DEBUG_MAX_MEMORY_ALLOCATE];
#endif /* end CT_OS_MEM_ENABLE */
u32 u32AllocatedNO = 0;
#endif /* end #ifdef DEBUG_MSG */

#ifdef DEBUG_EIT
u16 u16EventID;
u16 u16aEventID [GEN_MAX_MEMORY_ALLOCATE];
u16 u16Duplicate [9] = {205, 206, 104, 222, 125, 126, 203, 103, 221};

/*
205,206 : DVB_ParserEitLoop event name
104 : DVB_SectionMonitor
222 : newpmtfunc
125,126 eitbksetservice
203 : DVB_ParserEitLoop extended descriptor 
103 : DVB_SectionMonitorEIT
221 : PMT time out
*/
#endif /* end #ifdef DEBUG_EIT */

#if (!defined (CT_OS_MEM_ENABLE) || defined (DRAM_FRAME_DIGEST_BUFFER_USED))
bool8 ctk_mem_pool_create(EN_MEMPOOL_TYPE mem_type, void *pMemroyPool, u32 u32MemorySize)
{
	u32 i = 0;
	u32 *au32Start = NULL;
	u32 *au32Length = NULL;
	u32 *au32Used = NULL;
	u32 MAX_MEMORY_ALLOCATE = 0;

	u32 u32Group = 0;
	
	if (mem_type >= MAX_MEMPOOL_TYPE)
	{
		return FALSE;
	}
	pau8BufferPool[mem_type] = pMemroyPool;
	au32BufferSize[mem_type] = u32MemorySize;
	ab8Initialized [mem_type] = TRUE;
	
	au32Start = pau32Start[mem_type];
	au32Length = pau32Length[mem_type];
	au32Used = pau32Used[mem_type];

	MAX_MEMORY_ALLOCATE = u32aMaxMemoryAllocate[mem_type];

	for ( i=0; i<MAX_MEMORY_ALLOCATE;i++)
	{
		au32Start[i] = 0;
		au32Length[i] = 0;
	}

	u32Group = (MAX_MEMORY_ALLOCATE/MAX_NUM_IN_A_GROUP) + 1;
	for ( i=0; i < u32Group; i++)
	{
		au32Used[i]=0;
	}

	au32Length[0] = u32MemorySize;
	
	return TRUE;
}
#endif /* end #if (!defined (CT_OS_MEM_ENABLE) || defined (DRAM_FRAME_DIGEST_BUFFER_USED)) */

#ifdef CT_OS_MEM_ENABLE
bool8 os_mem_pool_create (EN_MEMPOOL_TYPE mem_type, void *pMemroyPool, u32 u32MemorySize)
{
	CT_STATUS		enStatus = EN_CTOS_SUCCESS;
	CTOS_MEM_POOL	*pstMemPool = NULL;
	u8 				au8pool_name[CTOS_MAX_NAME];
	
	if (mem_type >= MAX_MEMPOOL_TYPE)
	{
		return FALSE;
	}
	switch (mem_type)
	{
		case EIT_MEMPOOL_TYPE:
			pstMemPool = &stEITMemoryPool;
			strcpy (au8pool_name, "EITPool");
			break;
		case BIG_MEMPOOL_TYPE:
			pstMemPool = &stBIGMemoryPool;
			strcpy (au8pool_name, "BIGPool");
			break;
		case GEN_MEMPOOL_TYPE:
			pstMemPool = &stGENMemoryPool;
			strcpy (au8pool_name, "GENPool");
			break;
		default:
			return FALSE;
			break;
	}	
		
	enStatus = CT_OS_CreateMemPool (pstMemPool, au8pool_name, pMemroyPool,
									   u32MemorySize);
	if (enStatus != EN_CTOS_SUCCESS)
	{
		MEM_DBG(("{os_mem_pool_create} fail for [%s]\n", au8pool_name));
		return FALSE;
	}										   

	apstMemPool[mem_type] = pstMemPool; 
	pau8BufferPool[mem_type] = pMemroyPool;
	au32BufferSize[mem_type] = u32MemorySize;
	ab8Initialized [mem_type] = TRUE;
	return TRUE;
}
#endif /* end #ifndef CT_OS_MEM_ENABLE */

#ifdef CT_OS_MEM_ENABLE
bool8 os_mem_pool_delete (EN_MEMPOOL_TYPE mem_type)
{
	EN_CTOS_STATUS 		enStatus = EN_CTOS_FAIL;
	CTOS_MEM_POOL		*pstMemPool = NULL;
	
	if(b8MemInialized != TRUE)
	{
		return FALSE;
	}
	if(mem_type >= MAX_MEMPOOL_TYPE)
	{
		return FALSE;
	}
	pstMemPool = apstMemPool[mem_type];			/* pool structure */
	
	if (!pstMemPool)
	{
		return FALSE;	
	}	
	if (ab8Initialized [mem_type])
	{
		enStatus = CT_OS_DeleteMemPool (&stBIGMemoryPool);
			
		if (enStatus == EN_CTOS_SUCCESS)
		{
			ab8Initialized [mem_type] = FALSE;
			apstMemPool[mem_type] = NULL;			/* pool structure */
			return TRUE;
		}	
	}	
	return FALSE;
}
#endif /* end #ifndef CT_OS_MEM_ENABLE */

bool8 memory_pool_create (EN_MEMPOOL_TYPE mem_type, void *pMemroyPool, u32 u32MemorySize)
{
//MEM_DBG(("{memory_pool_create} type[%d] pool_s[0x%x] e[0x%x] size[0x%x]\n",
//	mem_type, (int)pMemroyPool, (int)((int)pMemroyPool + u32MemorySize - 1), (int)u32MemorySize));
	
#ifdef CT_OS_MEM_ENABLE
	if (mem_type == BIG_MEMPOOL_TYPE)
	{
	#if 0 //#ifdef DRAM_FRAME_DIGEST_BUFFER_USED
		return (ctk_mem_pool_create (mem_type, pMemroyPool, u32MemorySize));		
	#else
		return (os_mem_pool_create (mem_type, pMemroyPool, u32MemorySize));
	#endif		
	}
	else
	{
		return (os_mem_pool_create (mem_type, pMemroyPool, u32MemorySize));
	}	
#else
	return (ctk_mem_pool_create (mem_type, pMemroyPool, u32MemorySize));	
#endif /* end #ifdef CT_OS_MEM_ENABLE */
}


void DVB_MEM_Init (void)
{
	EN_CTOS_STATUS 		enStatus = EN_CTOS_FAIL;
	EN_MEMPOOL_TYPE 	mem_type = 0;
	u32 				u32GENNumberofBytes = 0;
	u32 				*pu32Data = NULL;

	u32 				u32available = 0;
	u8 					u8idx = 0;
	bool8				b8Result = FALSE;
	
#ifdef CTOS_NUCLEUS
	extern unsigned int HEAP_SIZE;
#endif

#ifdef CTOS_ECOS
	unsigned int HEAP_SIZE;
	HEAP_SIZE = CT_OS_GetHeapSize();
#endif
    
	enStatus = CT_OS_CreateSemaphore(&semDVB_MEM, "DVB_MEM", 1, EN_CTOS_SUSPEND_FIFO);
    if( enStatus == EN_CTOS_SUCCESS)
    {
        // MEM_DBG(("semDVB_MEM Created\n"));
    }
    else
    {
        // MEM_DBG(("semDVB_MEM Create Fail %d\n", enStatus));
    }

	for (u8idx = 0; u8idx < MAX_MEMPOOL_TYPE; u8idx ++)
	{
		pau8BufferPool[u8idx] = NULL;
		au32BufferSize[u8idx] = 0;				/* record buffer pool size */
		pau32Start[u8idx] = NULL;				/* chunk start address */
		pau32Length[u8idx] = NULL;				/* chunk length */
		pau32Used[u8idx] = NULL;				/* chunk used status */
		u32aMaxMemoryAllocate[u8idx] = 0;		/* max chunk number */
		ab8Initialized[u8idx] = FALSE;			/* not initialized */

#ifdef CT_OS_MEM_ENABLE		
		apstMemPool[u8idx] = NULL;			/* pool structure */
#endif /* end #ifdef CT_OS_MEM_ENABLE */		
	}	

	memset (u16Biggest, 0x00, sizeof (u16Biggest));

	if (HEAP_SIZE <= 0x50)
	{
		MEM_DBG((">>>>>>>>>  {DVB_MEM_Init} HEAP_SIZE is too small 0x%x]\n", HEAP_SIZE));
		return;
	}
		
	u32available = HEAP_SIZE-0x50;
	
	pu32Data = (u32*) malloc(u32available);

	if (NULL == pu32Data)
	{
		MEM_MSG((">>>>>>>>>  {DVB_MEM_Init} fail[0x%lx, 0x%x]\n", u32available, HEAP_SIZE));
		return;
	}

	au8EITPool = (u8*) pu32Data;

#ifdef ADD_BIG_POOL
	#ifdef DRAM_FRAME_DIGEST_BUFFER_USED
		au8BIGPool = (u8*) (CT_DRAM_FRAME_DIGEST_DEFLICKER_ADDR_ST);
	#else
		au8BIGPool = (u8*) (((u32)pu32Data) + EIT_POOL_SIZE);
	#endif	
	
	au8GENPool = (u8*) (((u32)pu32Data) + EIT_POOL_SIZE + BIG_POOL_SIZE);
	if (u32available <= (EIT_POOL_SIZE  + BIG_POOL_SIZE))
	{
		MEM_MSG(("DVB_MEM_Init fail\n"));
		return;
	}	
	u32GENNumberofBytes = u32available - (EIT_POOL_SIZE  + BIG_POOL_SIZE) ;
#else
	au8GENPool = (u8*) (((u32)pu32Data) + EIT_POOL_SIZE);
	u32GENNumberofBytes = u32available - EIT_POOL_SIZE;	
#endif /* end #ifdef ADD_BIG_POOL */	


#if 1
	/* to check the left Gen Pool size is enought or not */	
	if (u32GENNumberofBytes < MIN_GEN_POOL_SIZE)
	{
		MEM_MSG(("### DVB_MEM_Init Gen pool is too small fail\n"));
		return;
	}
#endif			
//	MEM_DBG(("u32available[0x%x] EIT_POOL_SIZE[0x%x] u32GENNumberofBytes[0x%x]\n",
//		u32available, EIT_POOL_SIZE, u32GENNumberofBytes));

	/* EIT memory pool */
	mem_type = EIT_MEMPOOL_TYPE;
#ifndef CT_OS_MEM_ENABLE	
	pau32Start[mem_type] = au32EITStart;
	pau32Length[mem_type] = au32EITLength;
	pau32Used[mem_type] = au32EITUsed;
	u32aMaxMemoryAllocate[mem_type] = EIT_MAX_MEMORY_ALLOCATE;
	/* init EIT memory pool */
#endif /* end #ifndef CT_OS_MEM_ENABLE */
	b8Result = memory_pool_create (mem_type, au8EITPool, EIT_POOL_SIZE);
	
#ifdef ADD_BIG_POOL
	/* BIG memory pool */
	mem_type = BIG_MEMPOOL_TYPE;
#ifndef CT_OS_MEM_ENABLE	
	pau32Start[mem_type] = au32BIGStart;
	pau32Length[mem_type] = au32BIGLength;
	pau32Used[mem_type] = au32BIGUsed;
	u32aMaxMemoryAllocate[mem_type] = BIG_MAX_MEMORY_ALLOCATE;
#endif /* end if #ifndef CT_OS_MEM_ENABLE */	
	/* init BIG memory pool */
	#ifdef DRAM_FRAME_DIGEST_BUFFER_USED
		//pau32Start[mem_type] = au32BIGStart;
		//pau32Length[mem_type] = au32BIGLength;
		//pau32Used[mem_type] = au32BIGUsed;
		//u32aMaxMemoryAllocate[mem_type] = BIG_MAX_MEMORY_ALLOCATE;
		b8Result = memory_pool_create (mem_type, au8BIGPool, DRAM_FRAME_DIGEST_BUFFER_SIZE);
	#else
		b8Result = memory_pool_create (mem_type, au8BIGPool, BIG_POOL_SIZE);
	#endif /* end if DRAM_FRAME_DIGEST_BUFFER_USED */
#endif

	/* GEN memory pool */
	mem_type = GEN_MEMPOOL_TYPE;
#ifndef CT_OS_MEM_ENABLE	
	pau32Start[mem_type] = au32GENStart;
	pau32Length[mem_type] = au32GENLength;
	pau32Used[mem_type] = au32GENUsed;
	u32aMaxMemoryAllocate[mem_type] = GEN_MAX_MEMORY_ALLOCATE;
#endif /* end #ifndef CT_OS_MEM_ENABLE */	
	/* init GEN memory pool */
	b8Result = memory_pool_create (mem_type, au8GENPool, u32GENNumberofBytes);
	
//MEM_DBG((">>[0x%x]-[0x%x] [0x%x]-[0x%x] [0x%x]-[0x%x]\n",
//	(int)au8EITPool, EIT_POOL_SIZE, (int)au8BIGPool, BIG_POOL_SIZE, (int)au8GENPool, (int)u32GENNumberofBytes));

	b8MemInialized = TRUE;
	return;
}

void DVB_MEM_GetInfo (EN_MEMPOOL_TYPE mem_type, u32 *au32Start, u32 *au32End)
{
	*au32Start = *au32End = 0;
	
	if (b8MemInialized != TRUE)
	{
		return;
	}
	if (mem_type >= MAX_MEMPOOL_TYPE)
	{
		return;
	}
	if (pau8BufferPool[mem_type] == NULL || au32BufferSize[mem_type] == 0)
	{
		return;
	}	
		
	*au32Start = (u32) pau8BufferPool[mem_type];
	*au32End = (u32) (*au32Start + au32BufferSize[mem_type] - 1);

	return;
}

#if (!defined (CT_OS_MEM_ENABLE) || defined (DRAM_FRAME_DIGEST_BUFFER_USED))
void *ctk_mem_alloc (EN_MEMPOOL_TYPE mem_type, u32 u32Number_of_bytes)
{
	u32 alloc_idx = 0, free_idx = 0;
	u32 *au32Start = NULL;
	u32 *au32Length = NULL;
	u32 *au32Used = NULL;
	u8 *pu8BufferPool = NULL;
	u32 MAX_MEMORY_ALLOCATE = 0;
	u32 u32AllocGroup = 0, u32AllocBitNo = 0;
	u32 u32FreeGroup = 0, u32FreeBitNo = 0;
	
	if (b8MemInialized != TRUE)
	{
		return NULL;
	}
	if (mem_type >= MAX_MEMPOOL_TYPE)
	{
		return NULL;
	}	
#if 0
if (mem_type == 0)		
{
	return NULL;
}	
#endif
#if 0
if (mem_type == 1)
{
	MEM_DBG(("@@@@@@@@mem_type[%d] u32Number_of_bytes[%d]\n", mem_type, u32Number_of_bytes));
}
#endif

⌨️ 快捷键说明

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