Path: blob/main/sys/contrib/ncsw/inc/etc/list_ext.h
48375 views
/* Copyright (c) 2008-2012 Freescale Semiconductor, Inc1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions are met:5* * Redistributions of source code must retain the above copyright6* notice, this list of conditions and the following disclaimer.7* * Redistributions in binary form must reproduce the above copyright8* notice, this list of conditions and the following disclaimer in the9* documentation and/or other materials provided with the distribution.10* * Neither the name of Freescale Semiconductor nor the11* names of its contributors may be used to endorse or promote products12* derived from this software without specific prior written permission.13*14*15* ALTERNATIVELY, this software may be distributed under the terms of the16* GNU General Public License ("GPL") as published by the Free Software17* Foundation, either version 2 of that License or (at your option) any18* later version.19*20* THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY21* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23* DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY24* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES25* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;26* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND27* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT28* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233/**************************************************************************//**3435@File list_ext.h3637@Description External prototypes for list.c38*//***************************************************************************/3940#ifndef __LIST_EXT_H41#define __LIST_EXT_H424344#include "std_ext.h"454647/**************************************************************************//**48@Group etc_id Utility Library Application Programming Interface4950@Description External routines.5152@{53*//***************************************************************************/5455/**************************************************************************//**56@Group list_id List5758@Description List module functions,definitions and enums.5960@{61*//***************************************************************************/6263/**************************************************************************//**64@Description List structure.65*//***************************************************************************/66typedef struct List67{68struct List *p_Next; /**< A pointer to the next list object */69struct List *p_Prev; /**< A pointer to the previous list object */70} t_List;717273/**************************************************************************//**74@Function NCSW_LIST_FIRST/NCSW_LIST_LAST/NCSW_LIST_NEXT/NCSW_LIST_PREV7576@Description Macro to get first/last/next/previous entry in a list.7778@Param[in] p_List - A pointer to a list.79*//***************************************************************************/80#define NCSW_LIST_FIRST(p_List) (p_List)->p_Next81#define NCSW_LIST_LAST(p_List) (p_List)->p_Prev82#define NCSW_LIST_NEXT NCSW_LIST_FIRST83#define NCSW_LIST_PREV NCSW_LIST_LAST848586/**************************************************************************//**87@Function NCSW_LIST_INIT8889@Description Macro for initialization of a list struct.9091@Param[in] lst - The t_List object to initialize.92*//***************************************************************************/93#define NCSW_LIST_INIT(lst) {&(lst), &(lst)}949596/**************************************************************************//**97@Function NCSW_LIST9899@Description Macro to declare of a list.100101@Param[in] listName - The list object name.102*//***************************************************************************/103#define NCSW_LIST(listName) t_List listName = NCSW_LIST_INIT(listName)104105106/**************************************************************************//**107@Function INIT_LIST108109@Description Macro to initialize a list pointer.110111@Param[in] p_List - The list pointer.112*//***************************************************************************/113#define INIT_LIST(p_List) NCSW_LIST_FIRST(p_List) = NCSW_LIST_LAST(p_List) = (p_List)114115116/**************************************************************************//**117@Function NCSW_LIST_OBJECT118119@Description Macro to get the struct (object) for this entry.120121@Param[in] type - The type of the struct (object) this list is embedded in.122@Param[in] member - The name of the t_List object within the struct.123124@Return The structure pointer for this entry.125*//***************************************************************************/126#define MEMBER_OFFSET(type, member) (PTR_TO_UINT(&((type *)0)->member))127#define NCSW_LIST_OBJECT(p_List, type, member) \128((type *)((char *)(p_List)-MEMBER_OFFSET(type, member)))129130131/**************************************************************************//**132@Function NCSW_LIST_FOR_EACH133134@Description Macro to iterate over a list.135136@Param[in] p_Pos - A pointer to a list to use as a loop counter.137@Param[in] p_Head - A pointer to the head for your list pointer.138139@Cautions You can't delete items with this routine.140For deletion use NCSW_LIST_FOR_EACH_SAFE().141*//***************************************************************************/142#define NCSW_LIST_FOR_EACH(p_Pos, p_Head) \143for (p_Pos = NCSW_LIST_FIRST(p_Head); p_Pos != (p_Head); p_Pos = NCSW_LIST_NEXT(p_Pos))144145146/**************************************************************************//**147@Function NCSW_LIST_FOR_EACH_SAFE148149@Description Macro to iterate over a list safe against removal of list entry.150151@Param[in] p_Pos - A pointer to a list to use as a loop counter.152@Param[in] p_Tmp - Another pointer to a list to use as temporary storage.153@Param[in] p_Head - A pointer to the head for your list pointer.154*//***************************************************************************/155#define NCSW_LIST_FOR_EACH_SAFE(p_Pos, p_Tmp, p_Head) \156for (p_Pos = NCSW_LIST_FIRST(p_Head), p_Tmp = NCSW_LIST_FIRST(p_Pos); \157p_Pos != (p_Head); \158p_Pos = p_Tmp, p_Tmp = NCSW_LIST_NEXT(p_Pos))159160161/**************************************************************************//**162@Function NCSW_LIST_FOR_EACH_OBJECT_SAFE163164@Description Macro to iterate over list of given type safely.165166@Param[in] p_Pos - A pointer to a list to use as a loop counter.167@Param[in] p_Tmp - Another pointer to a list to use as temporary storage.168@Param[in] type - The type of the struct this is embedded in.169@Param[in] p_Head - A pointer to the head for your list pointer.170@Param[in] member - The name of the list_struct within the struct.171172@Cautions You can't delete items with this routine.173For deletion use NCSW_LIST_FOR_EACH_SAFE().174*//***************************************************************************/175#define NCSW_LIST_FOR_EACH_OBJECT_SAFE(p_Pos, p_Tmp, p_Head, type, member) \176for (p_Pos = NCSW_LIST_OBJECT(NCSW_LIST_FIRST(p_Head), type, member), \177p_Tmp = NCSW_LIST_OBJECT(NCSW_LIST_FIRST(&p_Pos->member), type, member); \178&p_Pos->member != (p_Head); \179p_Pos = p_Tmp, \180p_Tmp = NCSW_LIST_OBJECT(NCSW_LIST_FIRST(&p_Pos->member), type, member))181182/**************************************************************************//**183@Function NCSW_LIST_FOR_EACH_OBJECT184185@Description Macro to iterate over list of given type.186187@Param[in] p_Pos - A pointer to a list to use as a loop counter.188@Param[in] type - The type of the struct this is embedded in.189@Param[in] p_Head - A pointer to the head for your list pointer.190@Param[in] member - The name of the list_struct within the struct.191192@Cautions You can't delete items with this routine.193For deletion use NCSW_LIST_FOR_EACH_SAFE().194*//***************************************************************************/195#define NCSW_LIST_FOR_EACH_OBJECT(p_Pos, type, p_Head, member) \196for (p_Pos = NCSW_LIST_OBJECT(NCSW_LIST_FIRST(p_Head), type, member); \197&p_Pos->member != (p_Head); \198p_Pos = NCSW_LIST_OBJECT(NCSW_LIST_FIRST(&(p_Pos->member)), type, member))199200201/**************************************************************************//**202@Function NCSW_LIST_Add203204@Description Add a new entry to a list.205206Insert a new entry after the specified head.207This is good for implementing stacks.208209@Param[in] p_New - A pointer to a new list entry to be added.210@Param[in] p_Head - A pointer to a list head to add it after.211212@Return none.213*//***************************************************************************/214static __inline__ void NCSW_LIST_Add(t_List *p_New, t_List *p_Head)215{216NCSW_LIST_PREV(NCSW_LIST_NEXT(p_Head)) = p_New;217NCSW_LIST_NEXT(p_New) = NCSW_LIST_NEXT(p_Head);218NCSW_LIST_PREV(p_New) = p_Head;219NCSW_LIST_NEXT(p_Head) = p_New;220}221222223/**************************************************************************//**224@Function NCSW_LIST_AddToTail225226@Description Add a new entry to a list.227228Insert a new entry before the specified head.229This is useful for implementing queues.230231@Param[in] p_New - A pointer to a new list entry to be added.232@Param[in] p_Head - A pointer to a list head to add it before.233234@Return none.235*//***************************************************************************/236static __inline__ void NCSW_LIST_AddToTail(t_List *p_New, t_List *p_Head)237{238NCSW_LIST_NEXT(NCSW_LIST_PREV(p_Head)) = p_New;239NCSW_LIST_PREV(p_New) = NCSW_LIST_PREV(p_Head);240NCSW_LIST_NEXT(p_New) = p_Head;241NCSW_LIST_PREV(p_Head) = p_New;242}243244245/**************************************************************************//**246@Function NCSW_LIST_Del247248@Description Deletes entry from a list.249250@Param[in] p_Entry - A pointer to the element to delete from the list.251252@Return none.253254@Cautions NCSW_LIST_IsEmpty() on entry does not return true after this,255the entry is in an undefined state.256*//***************************************************************************/257static __inline__ void NCSW_LIST_Del(t_List *p_Entry)258{259NCSW_LIST_PREV(NCSW_LIST_NEXT(p_Entry)) = NCSW_LIST_PREV(p_Entry);260NCSW_LIST_NEXT(NCSW_LIST_PREV(p_Entry)) = NCSW_LIST_NEXT(p_Entry);261}262263264/**************************************************************************//**265@Function NCSW_LIST_DelAndInit266267@Description Deletes entry from list and reinitialize it.268269@Param[in] p_Entry - A pointer to the element to delete from the list.270271@Return none.272*//***************************************************************************/273static __inline__ void NCSW_LIST_DelAndInit(t_List *p_Entry)274{275NCSW_LIST_Del(p_Entry);276INIT_LIST(p_Entry);277}278279280/**************************************************************************//**281@Function NCSW_LIST_Move282283@Description Delete from one list and add as another's head.284285@Param[in] p_Entry - A pointer to the list entry to move.286@Param[in] p_Head - A pointer to the list head that will precede our entry.287288@Return none.289*//***************************************************************************/290static __inline__ void NCSW_LIST_Move(t_List *p_Entry, t_List *p_Head)291{292NCSW_LIST_Del(p_Entry);293NCSW_LIST_Add(p_Entry, p_Head);294}295296297/**************************************************************************//**298@Function NCSW_LIST_MoveToTail299300@Description Delete from one list and add as another's tail.301302@Param[in] p_Entry - A pointer to the entry to move.303@Param[in] p_Head - A pointer to the list head that will follow our entry.304305@Return none.306*//***************************************************************************/307static __inline__ void NCSW_LIST_MoveToTail(t_List *p_Entry, t_List *p_Head)308{309NCSW_LIST_Del(p_Entry);310NCSW_LIST_AddToTail(p_Entry, p_Head);311}312313314/**************************************************************************//**315@Function NCSW_LIST_IsEmpty316317@Description Tests whether a list is empty.318319@Param[in] p_List - A pointer to the list to test.320321@Return 1 if the list is empty, 0 otherwise.322*//***************************************************************************/323static __inline__ int NCSW_LIST_IsEmpty(t_List *p_List)324{325return (NCSW_LIST_FIRST(p_List) == p_List);326}327328329/**************************************************************************//**330@Function NCSW_LIST_Append331332@Description Join two lists.333334@Param[in] p_NewList - A pointer to the new list to add.335@Param[in] p_Head - A pointer to the place to add it in the first list.336337@Return none.338*//***************************************************************************/339void NCSW_LIST_Append(t_List *p_NewList, t_List *p_Head);340341342/**************************************************************************//**343@Function NCSW_LIST_NumOfObjs344345@Description Counts number of objects in the list346347@Param[in] p_List - A pointer to the list which objects are to be counted.348349@Return Number of objects in the list.350*//***************************************************************************/351int NCSW_LIST_NumOfObjs(t_List *p_List);352353/** @} */ /* end of list_id group */354/** @} */ /* end of etc_id group */355356357#endif /* __LIST_EXT_H */358359360