C 栈实现队列节点的管理

时间:2023-03-09 17:41:07
C 栈实现队列节点的管理

栈预先存储节点,队列的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 = ;
pStack->m_nTop = nSize-;
pStack->m_nInterator = ;
pStack->m_nSize = nSize;
} /**
* @brief pop stack
* @param pStack : stack to pop
* @return NULL
*/
void* pop_stack(Stack_t * pStack )
{
MS_U32 i = ;
void *pResult = NULL; ASSERT( pStack != NULL );
if( pStack->m_nInterator > )
{
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 = ; 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 = ;
MS_U32 err = ; create_stack(gs_pFileBufferSpace, IS_MAX_FILE_NUM, &stack_file); for( i=; 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, , sizeof(FileList_t) );
} return pFileList;
} MS_U32 com_is_free_file( FileList_t *buf )
{
memset(buf,,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 ;
} if( NULL != pFileList )
{
while( pFileList->next != NULL )
{
pPreFileList = pFileList;
pFileList= pFileList->next;
} if(pFileList == buf)
{
buf = NULL;
pPreFileList->next = NULL;
}
}
return ;
} 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,,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 = ;
FileState_t state = FILE_OK;
len = strlen((char*)pFile->m_pchFileName);
if( == pFile->m_pchFileName[len-]//x
&& == pFile->m_pchFileName[len-]//m
&& == pFile->m_pchFileName[len-])//l
{
pFile->m_FileType = FILE_XML;
}
else if( == pFile->m_pchFileName[len-]//b
&& == pFile->m_pchFileName[len-]//m
&& == pFile->m_pchFileName[len-])//p
{
pFile->m_FileType = FILE_BMP;
}
else if( == pFile->m_pchFileName[len-]//m
&& == pFile->m_pchFileName[len-]//p
&& == pFile->m_pchFileName[len-])//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) // #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 = ,
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 +];
MS_U8 m_nLastSection;
MS_U32 m_nFileAddr;
MS_U32 m_nFileLength;
MS_U32 m_nSubFileAddr[];
MS_U32 m_nSubFileLength[]; 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