list_ext.h revision 296177
1296177Sjhibbits/* Copyright (c) 2008-2011 Freescale Semiconductor, Inc. 2296177Sjhibbits * All rights reserved. 3296177Sjhibbits * 4296177Sjhibbits * Redistribution and use in source and binary forms, with or without 5296177Sjhibbits * modification, are permitted provided that the following conditions are met: 6296177Sjhibbits * * Redistributions of source code must retain the above copyright 7296177Sjhibbits * notice, this list of conditions and the following disclaimer. 8296177Sjhibbits * * Redistributions in binary form must reproduce the above copyright 9296177Sjhibbits * notice, this list of conditions and the following disclaimer in the 10296177Sjhibbits * documentation and/or other materials provided with the distribution. 11296177Sjhibbits * * Neither the name of Freescale Semiconductor nor the 12296177Sjhibbits * names of its contributors may be used to endorse or promote products 13296177Sjhibbits * derived from this software without specific prior written permission. 14296177Sjhibbits * 15296177Sjhibbits * 16296177Sjhibbits * ALTERNATIVELY, this software may be distributed under the terms of the 17296177Sjhibbits * GNU General Public License ("GPL") as published by the Free Software 18296177Sjhibbits * Foundation, either version 2 of that License or (at your option) any 19296177Sjhibbits * later version. 20296177Sjhibbits * 21296177Sjhibbits * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY 22296177Sjhibbits * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23296177Sjhibbits * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24296177Sjhibbits * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY 25296177Sjhibbits * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26296177Sjhibbits * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27296177Sjhibbits * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 28296177Sjhibbits * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29296177Sjhibbits * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30296177Sjhibbits * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31296177Sjhibbits */ 32296177Sjhibbits 33296177Sjhibbits/**************************************************************************//** 34296177Sjhibbits 35296177Sjhibbits @File list_ext.h 36296177Sjhibbits 37296177Sjhibbits @Description External prototypes for list.c 38296177Sjhibbits*//***************************************************************************/ 39296177Sjhibbits 40296177Sjhibbits#ifndef __LIST_EXT_H 41296177Sjhibbits#define __LIST_EXT_H 42296177Sjhibbits 43296177Sjhibbits 44296177Sjhibbits#include "std_ext.h" 45296177Sjhibbits 46296177Sjhibbits 47296177Sjhibbits/**************************************************************************//** 48296177Sjhibbits @Group etc_id Utility Library Application Programming Interface 49296177Sjhibbits 50296177Sjhibbits @Description External routines. 51296177Sjhibbits 52296177Sjhibbits @{ 53296177Sjhibbits*//***************************************************************************/ 54296177Sjhibbits 55296177Sjhibbits/**************************************************************************//** 56296177Sjhibbits @Group list_id List 57296177Sjhibbits 58296177Sjhibbits @Description List module functions,definitions and enums. 59296177Sjhibbits 60296177Sjhibbits @{ 61296177Sjhibbits*//***************************************************************************/ 62296177Sjhibbits 63296177Sjhibbits/**************************************************************************//** 64296177Sjhibbits @Description List structure. 65296177Sjhibbits*//***************************************************************************/ 66296177Sjhibbitstypedef struct List 67296177Sjhibbits{ 68296177Sjhibbits struct List *p_Next; /**< A pointer to the next list object */ 69296177Sjhibbits struct List *p_Prev; /**< A pointer to the previous list object */ 70296177Sjhibbits} t_List; 71296177Sjhibbits 72296177Sjhibbits 73296177Sjhibbits/**************************************************************************//** 74296177Sjhibbits @Function NCSW_LIST_FIRST/LIST_LAST/NCSW_LIST_NEXT/NCSW_LIST_PREV 75296177Sjhibbits 76296177Sjhibbits @Description Macro to get first/last/next/previous entry in a list. 77296177Sjhibbits 78296177Sjhibbits @Param[in] p_List - A pointer to a list. 79296177Sjhibbits*//***************************************************************************/ 80296177Sjhibbits#define NCSW_LIST_FIRST(p_List) (p_List)->p_Next 81296177Sjhibbits#define LIST_LAST(p_List) (p_List)->p_Prev 82296177Sjhibbits#define NCSW_LIST_NEXT NCSW_LIST_FIRST 83296177Sjhibbits#define NCSW_LIST_PREV LIST_LAST 84296177Sjhibbits 85296177Sjhibbits 86296177Sjhibbits/**************************************************************************//** 87296177Sjhibbits @Function NCSW_LIST_INIT 88296177Sjhibbits 89296177Sjhibbits @Description Macro for initialization of a list struct. 90296177Sjhibbits 91296177Sjhibbits @Param[in] lst - The t_List object to initialize. 92296177Sjhibbits*//***************************************************************************/ 93296177Sjhibbits#define NCSW_LIST_INIT(lst) {&(lst), &(lst)} 94296177Sjhibbits 95296177Sjhibbits 96296177Sjhibbits/**************************************************************************//** 97296177Sjhibbits @Function LIST 98296177Sjhibbits 99296177Sjhibbits @Description Macro to declare of a list. 100296177Sjhibbits 101296177Sjhibbits @Param[in] listName - The list object name. 102296177Sjhibbits*//***************************************************************************/ 103296177Sjhibbits#define LIST(listName) t_List listName = NCSW_LIST_INIT(listName) 104296177Sjhibbits 105296177Sjhibbits 106296177Sjhibbits/**************************************************************************//** 107296177Sjhibbits @Function INIT_LIST 108296177Sjhibbits 109296177Sjhibbits @Description Macro to initialize a list pointer. 110296177Sjhibbits 111296177Sjhibbits @Param[in] p_List - The list pointer. 112296177Sjhibbits*//***************************************************************************/ 113296177Sjhibbits#define INIT_LIST(p_List) NCSW_LIST_FIRST(p_List) = LIST_LAST(p_List) = (p_List) 114296177Sjhibbits 115296177Sjhibbits 116296177Sjhibbits/**************************************************************************//** 117296177Sjhibbits @Function LIST_OBJECT 118296177Sjhibbits 119296177Sjhibbits @Description Macro to get the struct (object) for this entry. 120296177Sjhibbits 121296177Sjhibbits @Param[in] type - The type of the struct (object) this list is embedded in. 122296177Sjhibbits @Param[in] member - The name of the t_List object within the struct. 123296177Sjhibbits 124296177Sjhibbits @Return The structure pointer for this entry. 125296177Sjhibbits*//***************************************************************************/ 126296177Sjhibbits#define MEMBER_OFFSET(type, member) (PTR_TO_UINT(&((type *)0)->member)) 127296177Sjhibbits#define LIST_OBJECT(p_List, type, member) \ 128296177Sjhibbits ((type *)((char *)(p_List)-MEMBER_OFFSET(type, member))) 129296177Sjhibbits 130296177Sjhibbits 131296177Sjhibbits/**************************************************************************//** 132296177Sjhibbits @Function LIST_FOR_EACH 133296177Sjhibbits 134296177Sjhibbits @Description Macro to iterate over a list. 135296177Sjhibbits 136296177Sjhibbits @Param[in] p_Pos - A pointer to a list to use as a loop counter. 137296177Sjhibbits @Param[in] p_Head - A pointer to the head for your list pointer. 138296177Sjhibbits 139296177Sjhibbits @Cautions You can't delete items with this routine. 140296177Sjhibbits For deletion use LIST_FOR_EACH_SAFE(). 141296177Sjhibbits*//***************************************************************************/ 142296177Sjhibbits#define LIST_FOR_EACH(p_Pos, p_Head) \ 143296177Sjhibbits for (p_Pos = NCSW_LIST_FIRST(p_Head); p_Pos != (p_Head); p_Pos = NCSW_LIST_NEXT(p_Pos)) 144296177Sjhibbits 145296177Sjhibbits 146296177Sjhibbits/**************************************************************************//** 147296177Sjhibbits @Function LIST_FOR_EACH_SAFE 148296177Sjhibbits 149296177Sjhibbits @Description Macro to iterate over a list safe against removal of list entry. 150296177Sjhibbits 151296177Sjhibbits @Param[in] p_Pos - A pointer to a list to use as a loop counter. 152296177Sjhibbits @Param[in] p_Tmp - Another pointer to a list to use as temporary storage. 153296177Sjhibbits @Param[in] p_Head - A pointer to the head for your list pointer. 154296177Sjhibbits*//***************************************************************************/ 155296177Sjhibbits#define LIST_FOR_EACH_SAFE(p_Pos, p_Tmp, p_Head) \ 156296177Sjhibbits for (p_Pos = NCSW_LIST_FIRST(p_Head), p_Tmp = NCSW_LIST_FIRST(p_Pos); \ 157296177Sjhibbits p_Pos != (p_Head); \ 158296177Sjhibbits p_Pos = p_Tmp, p_Tmp = NCSW_LIST_NEXT(p_Pos)) 159296177Sjhibbits 160296177Sjhibbits 161296177Sjhibbits/**************************************************************************//** 162296177Sjhibbits @Function LIST_FOR_EACH_OBJECT_SAFE 163296177Sjhibbits 164296177Sjhibbits @Description Macro to iterate over list of given type safely. 165296177Sjhibbits 166296177Sjhibbits @Param[in] p_Pos - A pointer to a list to use as a loop counter. 167296177Sjhibbits @Param[in] p_Tmp - Another pointer to a list to use as temporary storage. 168296177Sjhibbits @Param[in] type - The type of the struct this is embedded in. 169296177Sjhibbits @Param[in] p_Head - A pointer to the head for your list pointer. 170296177Sjhibbits @Param[in] member - The name of the list_struct within the struct. 171296177Sjhibbits 172296177Sjhibbits @Cautions You can't delete items with this routine. 173296177Sjhibbits For deletion use LIST_FOR_EACH_SAFE(). 174296177Sjhibbits*//***************************************************************************/ 175296177Sjhibbits#define LIST_FOR_EACH_OBJECT_SAFE(p_Pos, p_Tmp, p_Head, type, member) \ 176296177Sjhibbits for (p_Pos = LIST_OBJECT(NCSW_LIST_FIRST(p_Head), type, member), \ 177296177Sjhibbits p_Tmp = LIST_OBJECT(NCSW_LIST_FIRST(&p_Pos->member), type, member); \ 178296177Sjhibbits &p_Pos->member != (p_Head); \ 179296177Sjhibbits p_Pos = p_Tmp, \ 180296177Sjhibbits p_Tmp = LIST_OBJECT(NCSW_LIST_FIRST(&p_Pos->member), type, member)) 181296177Sjhibbits 182296177Sjhibbits/**************************************************************************//** 183296177Sjhibbits @Function LIST_FOR_EACH_OBJECT 184296177Sjhibbits 185296177Sjhibbits @Description Macro to iterate over list of given type. 186296177Sjhibbits 187296177Sjhibbits @Param[in] p_Pos - A pointer to a list to use as a loop counter. 188296177Sjhibbits @Param[in] type - The type of the struct this is embedded in. 189296177Sjhibbits @Param[in] p_Head - A pointer to the head for your list pointer. 190296177Sjhibbits @Param[in] member - The name of the list_struct within the struct. 191296177Sjhibbits 192296177Sjhibbits @Cautions You can't delete items with this routine. 193296177Sjhibbits For deletion use LIST_FOR_EACH_SAFE(). 194296177Sjhibbits*//***************************************************************************/ 195296177Sjhibbits#define LIST_FOR_EACH_OBJECT(p_Pos, type, p_Head, member) \ 196296177Sjhibbits for (p_Pos = LIST_OBJECT(NCSW_LIST_FIRST(p_Head), type, member); \ 197296177Sjhibbits &p_Pos->member != (p_Head); \ 198296177Sjhibbits p_Pos = LIST_OBJECT(NCSW_LIST_FIRST(&(p_Pos->member)), type, member)) 199296177Sjhibbits 200296177Sjhibbits 201296177Sjhibbits/**************************************************************************//** 202296177Sjhibbits @Function LIST_Add 203296177Sjhibbits 204296177Sjhibbits @Description Add a new entry to a list. 205296177Sjhibbits 206296177Sjhibbits Insert a new entry after the specified head. 207296177Sjhibbits This is good for implementing stacks. 208296177Sjhibbits 209296177Sjhibbits @Param[in] p_New - A pointer to a new list entry to be added. 210296177Sjhibbits @Param[in] p_Head - A pointer to a list head to add it after. 211296177Sjhibbits 212296177Sjhibbits @Return none. 213296177Sjhibbits*//***************************************************************************/ 214296177Sjhibbitsstatic __inline__ void LIST_Add(t_List *p_New, t_List *p_Head) 215296177Sjhibbits{ 216296177Sjhibbits NCSW_LIST_PREV(NCSW_LIST_NEXT(p_Head)) = p_New; 217296177Sjhibbits NCSW_LIST_NEXT(p_New) = NCSW_LIST_NEXT(p_Head); 218296177Sjhibbits NCSW_LIST_PREV(p_New) = p_Head; 219296177Sjhibbits NCSW_LIST_NEXT(p_Head) = p_New; 220296177Sjhibbits} 221296177Sjhibbits 222296177Sjhibbits 223296177Sjhibbits/**************************************************************************//** 224296177Sjhibbits @Function LIST_AddToTail 225296177Sjhibbits 226296177Sjhibbits @Description Add a new entry to a list. 227296177Sjhibbits 228296177Sjhibbits Insert a new entry before the specified head. 229296177Sjhibbits This is useful for implementing queues. 230296177Sjhibbits 231296177Sjhibbits @Param[in] p_New - A pointer to a new list entry to be added. 232296177Sjhibbits @Param[in] p_Head - A pointer to a list head to add it after. 233296177Sjhibbits 234296177Sjhibbits @Return none. 235296177Sjhibbits*//***************************************************************************/ 236296177Sjhibbitsstatic __inline__ void LIST_AddToTail(t_List *p_New, t_List *p_Head) 237296177Sjhibbits{ 238296177Sjhibbits NCSW_LIST_NEXT(NCSW_LIST_PREV(p_Head)) = p_New; 239296177Sjhibbits NCSW_LIST_PREV(p_New) = NCSW_LIST_PREV(p_Head); 240296177Sjhibbits NCSW_LIST_NEXT(p_New) = p_Head; 241296177Sjhibbits NCSW_LIST_PREV(p_Head) = p_New; 242296177Sjhibbits} 243296177Sjhibbits 244296177Sjhibbits 245296177Sjhibbits/**************************************************************************//** 246296177Sjhibbits @Function LIST_Del 247296177Sjhibbits 248296177Sjhibbits @Description Deletes entry from a list. 249296177Sjhibbits 250296177Sjhibbits @Param[in] p_Entry - A pointer to the element to delete from the list. 251296177Sjhibbits 252296177Sjhibbits @Return none. 253296177Sjhibbits 254296177Sjhibbits @Cautions LIST_IsEmpty() on entry does not return true after this, 255296177Sjhibbits the entry is in an undefined state. 256296177Sjhibbits*//***************************************************************************/ 257296177Sjhibbitsstatic __inline__ void LIST_Del(t_List *p_Entry) 258296177Sjhibbits{ 259296177Sjhibbits NCSW_LIST_PREV(NCSW_LIST_NEXT(p_Entry)) = NCSW_LIST_PREV(p_Entry); 260296177Sjhibbits NCSW_LIST_NEXT(NCSW_LIST_PREV(p_Entry)) = NCSW_LIST_NEXT(p_Entry); 261296177Sjhibbits} 262296177Sjhibbits 263296177Sjhibbits 264296177Sjhibbits/**************************************************************************//** 265296177Sjhibbits @Function LIST_DelAndInit 266296177Sjhibbits 267296177Sjhibbits @Description Deletes entry from list and reinitialize it. 268296177Sjhibbits 269296177Sjhibbits @Param[in] p_Entry - A pointer to the element to delete from the list. 270296177Sjhibbits 271296177Sjhibbits @Return none. 272296177Sjhibbits*//***************************************************************************/ 273296177Sjhibbitsstatic __inline__ void LIST_DelAndInit(t_List *p_Entry) 274296177Sjhibbits{ 275296177Sjhibbits LIST_Del(p_Entry); 276296177Sjhibbits INIT_LIST(p_Entry); 277296177Sjhibbits} 278296177Sjhibbits 279296177Sjhibbits 280296177Sjhibbits/**************************************************************************//** 281296177Sjhibbits @Function LIST_Move 282296177Sjhibbits 283296177Sjhibbits @Description Delete from one list and add as another's head. 284296177Sjhibbits 285296177Sjhibbits @Param[in] p_Entry - A pointer to the list entry to move. 286296177Sjhibbits @Param[in] p_Head - A pointer to the list head that will precede our entry. 287296177Sjhibbits 288296177Sjhibbits @Return none. 289296177Sjhibbits*//***************************************************************************/ 290296177Sjhibbitsstatic __inline__ void LIST_Move(t_List *p_Entry, t_List *p_Head) 291296177Sjhibbits{ 292296177Sjhibbits LIST_Del(p_Entry); 293296177Sjhibbits LIST_Add(p_Entry, p_Head); 294296177Sjhibbits} 295296177Sjhibbits 296296177Sjhibbits 297296177Sjhibbits/**************************************************************************//** 298296177Sjhibbits @Function LIST_MoveToTail 299296177Sjhibbits 300296177Sjhibbits @Description Delete from one list and add as another's tail. 301296177Sjhibbits 302296177Sjhibbits @Param[in] p_Entry - A pointer to the entry to move. 303296177Sjhibbits @Param[in] p_Head - A pointer to the list head that will follow our entry. 304296177Sjhibbits 305296177Sjhibbits @Return none. 306296177Sjhibbits*//***************************************************************************/ 307296177Sjhibbitsstatic __inline__ void LIST_MoveToTail(t_List *p_Entry, t_List *p_Head) 308296177Sjhibbits{ 309296177Sjhibbits LIST_Del(p_Entry); 310296177Sjhibbits LIST_AddToTail(p_Entry, p_Head); 311296177Sjhibbits} 312296177Sjhibbits 313296177Sjhibbits 314296177Sjhibbits/**************************************************************************//** 315296177Sjhibbits @Function LIST_IsEmpty 316296177Sjhibbits 317296177Sjhibbits @Description Tests whether a list is empty. 318296177Sjhibbits 319296177Sjhibbits @Param[in] p_List - A pointer to the list to test. 320296177Sjhibbits 321296177Sjhibbits @Return 1 if the list is empty, 0 otherwise. 322296177Sjhibbits*//***************************************************************************/ 323296177Sjhibbitsstatic __inline__ int LIST_IsEmpty(t_List *p_List) 324296177Sjhibbits{ 325296177Sjhibbits return (NCSW_LIST_FIRST(p_List) == p_List); 326296177Sjhibbits} 327296177Sjhibbits 328296177Sjhibbits 329296177Sjhibbits/**************************************************************************//** 330296177Sjhibbits @Function LIST_Append 331296177Sjhibbits 332296177Sjhibbits @Description Join two lists. 333296177Sjhibbits 334296177Sjhibbits @Param[in] p_NewList - A pointer to the new list to add. 335296177Sjhibbits @Param[in] p_Head - A pointer to the place to add it in the first list. 336296177Sjhibbits 337296177Sjhibbits @Return none. 338296177Sjhibbits*//***************************************************************************/ 339296177Sjhibbitsvoid LIST_Append(t_List *p_NewList, t_List *p_Head); 340296177Sjhibbits 341296177Sjhibbits 342296177Sjhibbits/**************************************************************************//** 343296177Sjhibbits @Function LIST_NumOfObjs 344296177Sjhibbits 345296177Sjhibbits @Description Counts number of objects in the list 346296177Sjhibbits 347296177Sjhibbits @Param[in] p_List - A pointer to the list which objects are to be counted. 348296177Sjhibbits 349296177Sjhibbits @Return Number of objects in the list. 350296177Sjhibbits*//***************************************************************************/ 351296177Sjhibbitsint LIST_NumOfObjs(t_List *p_List); 352296177Sjhibbits 353296177Sjhibbits/** @} */ /* end of list_id group */ 354296177Sjhibbits/** @} */ /* end of etc_id group */ 355296177Sjhibbits 356296177Sjhibbits 357296177Sjhibbits#endif /* __LIST_EXT_H */ 358