kb_dc_pool.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:7k
- //-----------------------------------------------------------------------------
- // Project include files:
- //-----------------------------------------------------------------------------
- #include "kb_dc_pub.h"
- #ifdef STATIC_POOL_ALLOC
- SECTION_POOL_DC section_buffer_status[SECTION_POOL_NUM];
- #else
- SECTION_POOL_DC *section_buffer_status;
- static int SECTION_POOL_NUM = 0;
- #endif
- unsigned char *section_buffer;
- unsigned short section_buffer_index_w=0;
- unsigned short section_buffer_index_r=0;
- unsigned short section_last_read_index=0;
- extern unsigned long GetXMLMovieMaxSize(void);
- void Section_Pool_Init(void)
- {
- UINT16 i;
- #ifndef STATIC_POOL_ALLOC
- SECTION_POOL_NUM = DEFAULT_POOL_NUM;
- section_buffer_status = (SECTION_POOL_DC *)KBDcMalloc((SECTION_POOL_NUM)*sizeof(SECTION_POOL_DC));
- if(section_buffer_status==NULL)
- {
- DCDebug1("section_buffer_status malloc errorn");
- return;
- }
- #endif
- section_buffer = (unsigned char *)KBDcMalloc((SECTION_POOL_NUM+1)*SECTION_LEN*sizeof(unsigned char));
- if(section_buffer==NULL)
- {
- DCDebug1("section_buffer malloc errorn");
- return;
- }
-
- for(i = 0; i < SECTION_POOL_NUM; i++)
- {
- KBDcMemset(§ion_buffer[i*SECTION_LEN], 0, SECTION_LEN);
- section_buffer_status[i].used = 0;
- section_buffer_status[i].data_ok = 0;
- }
- section_buffer_index_w = 0;
- section_buffer_index_r = 0;
- section_last_read_index = 0;
- //DCDebug4("section_buffer=0x%x,*section_buffer=%dn",section_buffer,*section_buffer);
-
- DCDebug3("n-----Section_Pool_Init done-----n");
- }
- #ifndef STATIC_POOL_ALLOC
- void Section_Pool_Rebuild(void)
- {
- UINT16 i;
- int s_size;
- DCDebug3("n -----***--Section_Pool_Rebuild--***-----");
-
- //----free buffer----
- if(section_buffer!=NULL)
- {
- KBDcFree(section_buffer);
- section_buffer = NULL;
- }
- if(section_buffer_status!=NULL)
- {
- KBDcFree(section_buffer_status);
- section_buffer_status = NULL;
- }
- //---reallocate buffer---
- SECTION_POOL_NUM = GetXMLMovieMaxSize()/(SECTION_LEN);
-
- //缓冲池不能太小,以避免产生内存碎片
- if(SECTION_POOL_NUM<MIN_POOL_NUM)
- SECTION_POOL_NUM = MIN_POOL_NUM;
- //对于大文件,缓冲池采用折半方式,节省空间
- else if((SECTION_POOL_NUM>MIN_POOL_NUM)&&(SECTION_POOL_NUM<MAX_POOL_NUM))
- SECTION_POOL_NUM = (SECTION_POOL_NUM*2)/3;
- else
- SECTION_POOL_NUM = (SECTION_POOL_NUM)/2;
- SECTION_POOL_NUM = 30;//test only
- DCDebug4("n [Section_Pool_Rebuild]--SECTION_POOL_NUM=%d", SECTION_POOL_NUM);
- section_buffer = (unsigned char *)KBDcMalloc((SECTION_POOL_NUM)*SECTION_LEN);
- if(section_buffer==NULL)
- {
- DCDebug1("section_buffer malloc errorn");
- return;
- }
-
- section_buffer_status = (SECTION_POOL_DC *)KBDcMalloc((SECTION_POOL_NUM)*sizeof(SECTION_POOL_DC));
- if(section_buffer_status==NULL)
- {
- DCDebug1("section_buffer_status malloc errorn");
- return;
- }
- //----memset buffer----
- s_size = sizeof(SECTION_POOL_DC);
- for(i = 0; i < SECTION_POOL_NUM; i++)
- {
- KBDcMemset(§ion_buffer[i*SECTION_LEN], 0, SECTION_LEN);
- KBDcMemset(§ion_buffer_status[i], 0, s_size);
- }
- section_buffer_index_w = 0;
- section_buffer_index_r = 0;
- section_last_read_index = 0;
- //DCDebug4("section_buffer=0x%x,*section_buffer=%dn",section_buffer,*section_buffer);
-
- DCDebug4("n-----Section_Pool_Rebuild done-----n");
- }
- #endif
- void Section_Pool_Reset(void)
- {
- unsigned short i;
- for(i = 0; i < SECTION_POOL_NUM; i++)
- {
- KBDcMemset(§ion_buffer[i*SECTION_LEN], 0, SECTION_LEN);
- section_buffer_status[i].used = 0;
- section_buffer_status[i].data_ok = 0;
- }
- section_buffer_index_w = 0;
- section_buffer_index_r = 0;
- section_last_read_index = 0;
-
- DCDebug3("nReset_Pool_Init donen");
- }
- void DCGetSectionCallback(unsigned long nChannelID, unsigned char *pData, unsigned long nDataLength)
- {
- int i;
- int sec_len;
-
- //sec_len=(((pData[1] << 8) | pData[2]) & 0x0FFF) +3;
- //DCDebug4("rn------DCfilterCallback:%x %x %x %x %x %x %x %x %x sec_len=%d", pData[0], pData[1],pData[2],pData[3],pData[4],pData[5],pData[6],pData[7],pData[8], sec_len);
-
- for(i=section_buffer_index_w;i<SECTION_POOL_NUM;i++)
- {
- if(section_buffer_status[i].used == 0 && section_buffer_status[i].data_ok == 0)
- break;
- }
-
- if(i == SECTION_POOL_NUM)
- {
- for(i=0;i<section_buffer_index_w;i++)
- {
- if(section_buffer_status[i].used == 0 && section_buffer_status[i].data_ok == 0)
- break;
- }
- if(i==section_buffer_index_r)
- {
- DCDebug1("rn====[DSMCC]===: Section arrived too fast!rn");
- while(1);
- }
- }
- //DCDebug4("ni=%d, w=%d, r=%d",i,section_buffer_index_w, section_buffer_index_r);
- KBDcMemcpy(§ion_buffer[i*SECTION_LEN], pData, nDataLength);
-
- //DCDebug4("section_buffer=%x %x %x %xnn",section_buffer[i*SECTION_LEN],section_buffer[i*SECTION_LEN+1],section_buffer[i*SECTION_LEN+2],section_buffer[i*SECTION_LEN+3]);
-
- section_buffer_status[i].used = 1;
- section_buffer_status[i].data_ok = 1;
- section_buffer_index_w = i;
-
- }
- int DCSectionValid(void)
- {
- //DCDebug4(" w=%d, r=%dnr", section_buffer_index_w,section_buffer_index_r);
- if(section_buffer_index_w==section_buffer_index_r)
- return 0;
- else
- return 1;
- }
- void FreeSectionBuffer(void)
- {
- section_buffer_status[section_last_read_index].used = 0;
- }
- void Section_Pool_Free(void)
- {
- KBDcFree(section_buffer);
- section_buffer = NULL;
- }
- unsigned char* Filter_GetSection(int *section_size)
- {
- int i;
- unsigned char * ptr;
- *section_size=0;
- //DCDebug4("nrsection_buffer_index_w[%d],section_buffer_index_r[%d]nr",section_buffer_index_w,section_buffer_index_r));
- i = section_buffer_index_r;
- if(!section_buffer_status[i].data_ok)
- {
- DCDebug1("!!!!!!!!!!call GetSection,but no datarn");
- while(1);
- }
-
- //DCDebug4("n Filter_GetSection: i=%d, w=%d, r=%dn",i,section_buffer_index_w,section_buffer_index_r);
- ptr = §ion_buffer[i*SECTION_LEN];
- *section_size=(((ptr[1] << 8) | ptr[2]) & 0x0FFF) +3;
-
- section_buffer_status[i].data_ok = 0;
- section_buffer_status[i].used = 0;
- section_last_read_index = i;
- section_buffer_index_r++;
- if(section_buffer_index_r == SECTION_POOL_NUM)
- section_buffer_index_r = 0;
-
- return ptr;
- }
- int KB_DC_GetSection( unsigned char* pbBuf, unsigned short* pwLen )
- {
- int len;
- unsigned char* pbSect;
- unsigned long timeout;
- *pwLen = 0;
- timeout=0;
- while(1)
- {
- if(DCSectionValid()==1)
- {
- DCDebug4("n find one!");
- break;
- }
- timeout++;
- //延时1 分半,做超时处理
- DCDebug4("Wait for sectionnr");
- if(timeout>=2*30000000)
- //if(timeout>=12*30000000)
- {
- DCDebug1("Get section timeout.--stop!!!nr");
- return -3;
- }
- if(KB_DC_FilterStatGet()!=FILTER_RUNNING)
- {
- DCDebug1("FilterStat==FILTER_COLSED, exit now!!nr");
- return -4;
- }
- }
- pbSect = Filter_GetSection(&len);
- if( pbSect == NULL )
- {
- DCDebug1("[LOADER]:Section ptr NULL.rn");
- return -1;
- }
-
- if( len > SECTION_LEN )
- {
- DCDebug1("[LOADER]:Section len error[=%d].rn", len);
- FreeSectionBuffer();
- return -2;
- }
- //DCDebug4("n now sec is %d", pbSect[6]);
- KBDcMemcpy( pbBuf, pbSect, len );
- FreeSectionBuffer();
- *pwLen = len;
-
- return 0;
- }