📄 list_ctrl.c
字号:
#include <stdio.h>
#include "ct_type.h"
#include "list_ctrl.h"
#include "dvb_sys.h"
#define LIST_MSG(p) printf p
#if 1
#define LIST_DBG(p)
#else
#define LIST_DBG(p) printf p
#endif
/*******************************************************************************************/
ST_LIST_ITEM* LIST_AllocItem(u32 u32ObjectSize)
{
ST_LIST_ITEM* pstListItem = NULL;
pstListItem = DVB_MemoryAllocate(sizeof(ST_LIST_ITEM));
if (pstListItem == NULL)
{
return NULL;
}
pstListItem->pstObject = DVB_MemoryAllocate(u32ObjectSize);
if (pstListItem->pstObject == NULL)
{
DVB_MemoryFree(pstListItem);
pstListItem = NULL;
return NULL;
}
memset(pstListItem->pstObject, 0x00, u32ObjectSize);
pstListItem->pstPrevItem = NULL;
pstListItem->pstNextItem = NULL;
return pstListItem;
}
/*******************************************************************************************/
bool8 LIST_FreeItem(ST_LIST_ITEM* pstListItem)
{
if (pstListItem==NULL)
{
return FALSE;
}
DVB_MemoryFree(pstListItem->pstObject);
DVB_MemoryFree(pstListItem);
return TRUE;
}
/*******************************************************************************************/
bool8 LIST_AddTailItem(ST_LIST_ITEM* pstListTail, ST_LIST_ITEM* pstListItem)
{
if ((pstListTail == NULL)||
(pstListItem == NULL))
{
return FALSE;
}
pstListTail->pstNextItem = (void*)pstListItem;
pstListItem->pstPrevItem = (void*)pstListTail;
return TRUE;
}
/*******************************************************************************************/
bool8 LIST_DeleteItem(ST_LIST_ITEM* pstListItem)
{
ST_LIST_ITEM* pstPrevItem = NULL;
ST_LIST_ITEM* pstNextItem = NULL;
if (pstListItem==NULL)
{
return FALSE;
}
pstPrevItem = (ST_LIST_ITEM*)pstListItem->pstPrevItem;
pstNextItem = (ST_LIST_ITEM*)pstListItem->pstNextItem;
if (pstPrevItem != NULL)
{
pstPrevItem->pstNextItem = (void*)pstListItem->pstNextItem;
}
if (pstNextItem != NULL)
{
pstNextItem->pstPrevItem = (void*)pstListItem->pstPrevItem;
}
return TRUE;
}
/*******************************************************************************************/
ST_LIST_ITEM* LIST_GetTailItem(ST_LIST_ITEM* pstListStart)
{
ST_LIST_ITEM* pstListItem = NULL;
if (pstListStart == NULL)
{
return NULL;
}
pstListItem = (ST_LIST_ITEM*)pstListStart;
while(pstListItem->pstNextItem!=NULL)
{
pstListItem = (ST_LIST_ITEM*)pstListItem->pstNextItem;
}
if (pstListItem==pstListStart)
{
// Not Item In List
return NULL;
}
return pstListItem;
}
/*******************************************************************************************/
ST_LIST_ITEM* LIST_GetNthItem(ST_LIST_ITEM* pstListStart, u32 u32ItemIndex)
{
ST_LIST_ITEM* pstListItem = NULL;
u32 u32ItemNum = 0;
if (pstListStart == NULL)
{
return NULL;
}
pstListItem = (ST_LIST_ITEM*)pstListStart;
while(pstListItem->pstNextItem!=NULL)
{
pstListItem = (ST_LIST_ITEM*)pstListItem->pstNextItem;
if (u32ItemNum==u32ItemIndex)
{
break;
}
u32ItemNum++;
}
if (pstListItem==pstListStart)
{
// Not Item In List
return NULL;
}
return pstListItem;
}
/*******************************************************************************************/
u32 LIST_GetItemNum(ST_LIST_ITEM* pstListStart)
{
u32 u32ItemNum = 0;
ST_LIST_ITEM* pstListItem = NULL;
if (pstListStart == NULL)
{
return u32ItemNum;
}
pstListItem = (ST_LIST_ITEM*)pstListStart;
u32ItemNum = 0;
while(pstListItem->pstNextItem!=NULL)
{
u32ItemNum++;
pstListItem = (ST_LIST_ITEM*)pstListItem->pstNextItem;
}
return u32ItemNum;
}
/*******************************************************************************************/
bool8 LIST_DestoryList(ST_LIST_ITEM* pstListStart)
{
ST_LIST_ITEM* pstListItem = NULL;
ST_LIST_ITEM* pstNextItem = NULL;
if (pstListStart==NULL)
{
return FALSE;
}
pstListItem = (ST_LIST_ITEM*)pstListStart->pstNextItem;
while(pstListItem!=NULL)
{
pstNextItem = (ST_LIST_ITEM*)pstListItem->pstNextItem;
if (LIST_FreeItem(pstListItem)!=TRUE)
{
break;
}
pstListItem = pstNextItem;
}
pstListStart->pstNextItem = NULL;
return TRUE;
}
/*******************************************************************************************/
bool8 LIST_DumpList(ST_LIST_ITEM* pstListStart)
{
u32 u32Count = 0;
ST_LIST_ITEM* pstListItem = NULL;
if (pstListStart==NULL)
{
return FALSE;
}
pstListItem = (ST_LIST_ITEM*)pstListStart->pstNextItem;
LIST_MSG(("pstListStart = %08lX\n", (u32) pstListStart));
while(pstListItem!=NULL)
{
u32Count++;
LIST_MSG(("%05ld. %08lX, (p)%08lX, (n)%08lX, (o)%08lX\n",
u32Count,
(u32)pstListItem,
(u32)pstListItem->pstPrevItem,
(u32)pstListItem->pstNextItem,
(u32)pstListItem->pstObject));
pstListItem = (ST_LIST_ITEM*)pstListItem->pstNextItem;
}
return TRUE;
}
/*******************************************************************************************/
ST_LIST_ITEM stTestListStart;
ST_LIST_ITEM* apstTestListItem[20];
extern void DVB_MEM_PoolInfo(void);
void LIST_Test(u32 u32Mode, u32 u32Param1, u32 u32Param2)
{
u32 u32Count = 0;
u32 u32TestObjectSize = 32;
ST_LIST_ITEM* pstListTail = NULL;
switch(u32Mode)
{
default:
case 0:
for (u32Count = 0; u32Count <20; u32Count++)
{
LIST_MSG(("%05ld. %08lX\n", u32Count, (u32)apstTestListItem[u32Count]));
}
LIST_DumpList(&stTestListStart);
break;
case 1:
// Init Test List
stTestListStart.pstPrevItem = NULL;
stTestListStart.pstNextItem = NULL;
stTestListStart.pstObject = NULL;
DVB_MEM_PoolInfo();
LIST_MSG(("List Item Num = %ld\n", LIST_GetItemNum(&stTestListStart)));
LIST_MSG(("List Last Item = %08lX\n", (u32)LIST_GetTailItem(&stTestListStart)));
pstListTail = &stTestListStart;
for (u32Count = 0; u32Count <20; u32Count++)
{
apstTestListItem[u32Count] = LIST_AllocItem(u32TestObjectSize);
if (apstTestListItem[u32Count]==NULL)
{
LIST_MSG(("LIST_AllocItem Fail (%05ld)\n", u32Count));
break;
}
if (LIST_AddTailItem(pstListTail, apstTestListItem[u32Count])!=TRUE)
{
LIST_MSG(("LIST_AddTailItem Fail (%05ld)\n", u32Count));
}
pstListTail = apstTestListItem[u32Count];
LIST_DumpList(&stTestListStart);
LIST_MSG(("List Item Num = %ld\n", LIST_GetItemNum(&stTestListStart)));
LIST_MSG(("List Last Item = %08lX\n", (u32)LIST_GetTailItem(&stTestListStart)));
DVB_MEM_PoolInfo();
}
DVB_MEM_PoolInfo();
break;
case 2:
u32Count = u32Param1%20;
DVB_MEM_PoolInfo();
if (LIST_DeleteItem(apstTestListItem[u32Count])==TRUE)
{
LIST_MSG(("LIST_DeleteItem Success (%05ld)\n", u32Count));
LIST_DumpList(&stTestListStart);
if (LIST_FreeItem(apstTestListItem[u32Count])==TRUE)
{
apstTestListItem[u32Count] = NULL;
LIST_MSG(("LIST_FreeItem Success (%05ld)\n", u32Count));
}
else
{
LIST_MSG(("LIST_FreeItem Fail (%05ld)\n", u32Count));
}
}
else
{
LIST_MSG(("LIST_DeleteItem Fail (%05ld)\n", u32Count));
}
LIST_MSG(("List Item Num = %ld\n", LIST_GetItemNum(&stTestListStart)));
LIST_MSG(("List Last Item = %08lX\n", (u32)LIST_GetTailItem(&stTestListStart)));
DVB_MEM_PoolInfo();
break;
case 3:
DVB_MEM_PoolInfo();
LIST_DumpList(&stTestListStart);
LIST_MSG(("List Item Num = %ld\n", LIST_GetItemNum(&stTestListStart)));
LIST_MSG(("List Last Item = %08lX\n", (u32)LIST_GetTailItem(&stTestListStart)));
LIST_DestoryList(&stTestListStart);
for (u32Count = 0; u32Count <20; u32Count++)
{
apstTestListItem[u32Count] = NULL;
}
LIST_DumpList(&stTestListStart);
LIST_MSG(("List Item Num = %ld\n", LIST_GetItemNum(&stTestListStart)));
LIST_MSG(("List Last Item = %08lX\n", (u32)LIST_GetTailItem(&stTestListStart)));
DVB_MEM_PoolInfo();
break;
case 9:
LIST_MSG(("ST_LIST_ITEM size = %ld\n", (u32)sizeof(ST_LIST_ITEM)));
break;
}
}
/*******************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -