qiuri2008

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

栈预先存储节点,队列的malloc/free均有栈模拟,从而保证不频繁的开辟/是否节点内存。

#include "com_is_buf.h"
#include "com_is_filter.h"
#include "assert.h"

FileList_t*        pgFileList = NULL;
FileList_t        FileBuffer[IS_MAX_FILE_NUM];
static MS_U32*    gs_pFileBufferSpace[IS_MAX_FILE_NUM];
Stack_t            stack_file;

#define ASSERT(x)  assert(x)

void create_stack(MS_U32 **ppStackSpace, MS_U32 nSize,
                  Stack_t* pStack)
{
    ASSERT(ppStackSpace != NULL && pStack != NULL);

    pStack->m_pBuf = (void**)ppStackSpace;

        pStack->m_nBottom    = 0;
        pStack->m_nTop       = nSize-1;
        pStack->m_nInterator = 0;
        pStack->m_nSize      = nSize;
}

 /**
 * @brief  pop stack
 * @param  pStack : stack to pop
 * @return  NULL
 */
void* pop_stack(Stack_t * pStack )
{
    MS_U32  i       = 0;
    void *pResult = NULL;

    ASSERT( pStack != NULL );
    if( pStack->m_nInterator > 0 )
    {
        i = (--pStack->m_nInterator) + pStack->m_nBottom;

        pResult = (void*)(pStack->m_pBuf[i]); 

        return pResult;
    }

    // no more stack to alloc
    return NULL;
}

 /**
 * @brief  push data into stack
 * @param  pStack : stack to push
 * @param  pBuf : the pointer of data
 * @return  NULL
 */
void push_stack(Stack_t * pStack, void* pBuf )
{
    MS_U32 i   = 0;

    ASSERT(pStack != NULL && pBuf != NULL);
    
    if( pStack->m_nInterator < pStack->m_nSize )
    {
            i = (pStack->m_nInterator++)+pStack->m_nBottom;

            pStack->m_pBuf[i] = pBuf;
    }
}

MS_U32 com_stack_file_initialize( void )
{
    MS_U32 i = 0;
    MS_U32 err = 0;

    create_stack(gs_pFileBufferSpace, IS_MAX_FILE_NUM, &stack_file);

    for( i=0; i<IS_MAX_FILE_NUM; i++ )
    {
        push_stack( &stack_file, (void*)&FileBuffer[i] );
    }
    return err;
}

FileList_t* com_is_malloc_file(void)
{
    FileList_t* pFileList = NULL;    
    pFileList = (FileList_t*)pop_stack( &stack_file );
    if( pFileList!=NULL )
    {
        memset( pFileList, 0, sizeof(FileList_t) );
    }

    return pFileList;
}


MS_U32 com_is_free_file( FileList_t *buf )
{
    memset(buf,0,sizeof(FileList_t));
    push_stack( &stack_file, (void*) buf);

    FileList_t  *pFileList = pgFileList;
    FileList_t  *pPreFileList = pgFileList;

    if(pgFileList == buf)
    {
        pgFileList = NULL;
        buf = NULL;
        return 0;
    }

    if( NULL != pFileList )
    {
        while( pFileList->next != NULL )
        {
            pPreFileList = pFileList;
            pFileList= pFileList->next;
        }

        if(pFileList == buf)
        {
            buf = NULL;
            pPreFileList->next = NULL;
        }
    }
    return 0;
}

void com_is_add_file( FileList_t** pFileOut)                   
{
    FileList_t  *pFileList = pgFileList;
    FileList_t *pFileNew = NULL;

    pFileNew = com_is_malloc_file();
    if(pFileNew == NULL)
    {
        return ;
    }
    pFileList = pgFileList;
    if( NULL != pFileList )
    {
        while( pFileList->next != NULL )
        {
            pFileList= pFileList->next;
        }

        pFileList->next = pFileNew;
        pFileNew->next = NULL;
    }
    else
    {
        pFileNew->next = NULL;
        pgFileList = pFileNew;
    }    
    *pFileOut = pFileNew;
    return ;

}

void com_is_destroy_filelist(void )
{
    FileList_t *pFile = pgFileList;
    FileList_t *pFile_temp = NULL;

    for(pFile = pgFileList;pFile != NULL; )
    {
        //com_is_free_file(pFile);

        pFile_temp = pFile->next;
        memset(pFile,0,sizeof(FileList_t));
        push_stack( &stack_file, (void*) pFile);
        pFile = pFile_temp;
    }
    pgFileList = NULL;
}

FileState_t com_is_update_file_type(FileList_t *pFile)
{
    MS_U16 len = 0;
    FileState_t state = FILE_OK;
    len = strlen((char*)pFile->m_pchFileName);
    if(120 == pFile->m_pchFileName[len-3]//x
        &&109 == pFile->m_pchFileName[len-2]//m
        &&108 == pFile->m_pchFileName[len-1])//l
    {
        pFile->m_FileType = FILE_XML;
    }
    else if(98 == pFile->m_pchFileName[len-3]//b
        &&109 == pFile->m_pchFileName[len-2]//m
        &&112 == pFile->m_pchFileName[len-1])//p
    {
        pFile->m_FileType = FILE_BMP;
    }
    else if(109 == pFile->m_pchFileName[len-3]//m
        &&112 == pFile->m_pchFileName[len-2]//p
        &&103 == pFile->m_pchFileName[len-1])//g
    {
        pFile->m_FileType = FILE_MPG;
    }
    else
    {
        pFile->m_FileType = FILE_ERR;
        state = FILE_ERROR;
    }
    return state;
}

FileList_t* com_is_get_file_list(void)
{
    return pgFileList;
}

 

#ifndef __COM_IS_BUF_H__
#define __COM_IS_BUF_H__
#include "MsCommon.h"


#define IS_MAX_FILE_NAME            (50)
#define IS_MAX_SECTION_NUM            (700)    // TODO: 2.5MB->640 Section
#define IS_MAX_SECTION_LEN            (4100) //4096


#define ADDR_ALIGN(data)    ((((data) & 0x3) == 0)? (data) : ((data) + 0x4 - ((data) & 0x3)))

typedef struct Stack_s
{
    MS_U32     m_nBottom;
    MS_U32     m_nTop;
    MS_S32     m_nInterator;
    MS_U32     m_nSize;
    void**  m_pBuf;
}Stack_t;

typedef enum FileType_e
{
    FILE_ALL,
    FILE_BMP,
    FILE_XML,
    FILE_MPG,
    FILE_ERR,
}FileType_t;

typedef enum FileState_e
{
    FILE_NOT_EXIST = 1,
    FILE_EXIST,
    FILE_ERROR,
    FILE_OK
}FileState_t;

typedef struct FileList_s
{
    FileType_t m_FileType;
    MS_U16 m_FileId;
    MS_U8 m_version;
    MS_U8 m_chFileNameLen;
    MS_S8 m_pchFileName[IS_MAX_FILE_NAME +1];
    MS_U8 m_nLastSection;
    MS_U32 m_nFileAddr;
    MS_U32 m_nFileLength;
    MS_U32 m_nSubFileAddr[256];
    MS_U32 m_nSubFileLength[256];
    
    struct FileList_s *next;
}FileList_t;


MS_U32 com_stack_file_initialize( void );
MS_U32 com_stack_file_free(void);
FileList_t* com_is_malloc_file(void);
MS_U32 com_is_free_file( FileList_t *buf );
void com_is_add_file( FileList_t** pFileOut);
void com_is_destroy_filelist(void );
FileState_t com_is_update_file_type(FileList_t *pFile);

FileList_t* com_is_get_file_list(void);

#endif

 

posted on 2019-01-28 09:40  江召伟  阅读(240)  评论(0编辑  收藏  举报