00001 00002 // 00003 // Copyright (c) 2000-2003 Intel Corporation 00004 // All rights reserved. 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // * Redistributions of source code must retain the above copyright notice, 00010 // this list of conditions and the following disclaimer. 00011 // * Redistributions in binary form must reproduce the above copyright notice, 00012 // this list of conditions and the following disclaimer in the documentation 00013 // and/or other materials provided with the distribution. 00014 // * Neither name of Intel Corporation nor the names of its contributors 00015 // may be used to endorse or promote products derived from this software 00016 // without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR 00022 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00025 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00026 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00027 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00031 00032 #ifndef LINKED_LIST_H 00033 #define LINKED_LIST_H 00034 00035 #include "FreeList.h" 00036 00037 #ifdef __cplusplus 00038 extern "C" { 00039 #endif 00040 00041 #define EOUTOFMEM (-7 & 1<<29) 00042 00043 #define FREELISTSIZE 100 00044 #define LIST_SUCCESS 1 00045 #define LIST_FAIL 0 00046 00047 /**************************************************************************** 00048 * Name: free_routine 00049 * 00050 * Description: 00051 * Function for freeing list items 00052 *****************************************************************************/ 00053 typedef void (*free_function)(void *arg); 00054 00055 /**************************************************************************** 00056 * Name: cmp_routine 00057 * 00058 * Description: 00059 * Function for comparing list items 00060 * Returns 1 if itemA==itemB 00061 *****************************************************************************/ 00062 typedef int (*cmp_routine)(void *itemA,void *itemB); 00063 00064 /**************************************************************************** 00065 * Name: ListNode 00066 * 00067 * Description: 00068 * linked list node. stores generic item and pointers to next and prev. 00069 * Internal Use Only. 00070 *****************************************************************************/ 00071 typedef struct LISTNODE 00072 { 00073 struct LISTNODE *prev; //previous node 00074 struct LISTNODE *next; //next node 00075 void *item; //item 00076 } ListNode; 00077 00078 /**************************************************************************** 00079 * Name: LinkedList 00080 * 00081 * Description: 00082 * linked list (no protection). Internal Use Only. 00083 * Because this is for internal use, parameters are NOT checked for 00084 * validity. 00085 * The first item of the list is stored at node: head->next 00086 * The last item of the list is stored at node: tail->prev 00087 * If head->next=tail, then list is empty. 00088 * To iterate through the list: 00089 * 00090 * LinkedList g; 00091 * ListNode *temp = NULL; 00092 * for (temp = ListHead(g);temp!=NULL;temp = ListNext(g,temp)) 00093 * { 00094 * } 00095 * 00096 *****************************************************************************/ 00097 typedef struct LINKEDLIST 00098 { 00099 ListNode head; //head, first item is stored at: head->next 00100 ListNode tail; //tail, last item is stored at: tail->prev 00101 long size; //size of list 00102 FreeList freeNodeList; //free list to use 00103 free_function free_func; //free function to use 00104 cmp_routine cmp_func; //compare function to use 00105 } LinkedList; 00106 00107 /**************************************************************************** 00108 * Function: ListInit 00109 * 00110 * Description: 00111 * Initializes LinkedList. Must be called first. 00112 * And only once for List. 00113 * Parameters: 00114 * list - must be valid, non null, pointer to a linked list. 00115 * cmp_func - function used to compare items. (May be NULL) 00116 * free_func - function used to free items. (May be NULL) 00117 * Returns: 00118 * 0 on success, EOUTOFMEM on failure. 00119 *****************************************************************************/ 00120 int ListInit(LinkedList *list,cmp_routine cmp_func, free_function free_func); 00121 00122 /**************************************************************************** 00123 * Function: ListAddHead 00124 * 00125 * Description: 00126 * Adds a node to the head of the list. 00127 * Node gets immediately after list.head. 00128 * Parameters: 00129 * LinkedList *list - must be valid, non null, pointer to a linked list. 00130 * void * item - item to be added 00131 * Returns: 00132 * The pointer to the ListNode on success, NULL on failure. 00133 * Precondition: 00134 * The list has been initialized. 00135 *****************************************************************************/ 00136 ListNode *ListAddHead(LinkedList *list, void *item); 00137 00138 /**************************************************************************** 00139 * Function: ListAddTail 00140 * 00141 * Description: 00142 * Adds a node to the tail of the list. 00143 * Node gets added immediately before list.tail. 00144 * Parameters: 00145 * LinkedList *list - must be valid, non null, pointer to a linked list. 00146 * void * item - item to be added 00147 * Returns: 00148 * The pointer to the ListNode on success, NULL on failure. 00149 * Precondition: 00150 * The list has been initialized. 00151 *****************************************************************************/ 00152 ListNode *ListAddTail(LinkedList *list, void *item); 00153 00154 /**************************************************************************** 00155 * Function: ListAddAfter 00156 * 00157 * Description: 00158 * Adds a node after the specified node. 00159 * Node gets added immediately after bnode. 00160 * Parameters: 00161 * LinkedList *list - must be valid, non null, pointer to a linked list. 00162 * void * item - item to be added 00163 * ListNode * bnode - node to add after 00164 * Returns: 00165 * The pointer to the ListNode on success, NULL on failure. 00166 * Precondition: 00167 * The list has been initialized. 00168 *****************************************************************************/ 00169 ListNode *ListAddAfter(LinkedList *list, void *item, ListNode *bnode); 00170 00171 00172 /**************************************************************************** 00173 * Function: ListAddBefore 00174 * 00175 * Description: 00176 * Adds a node before the specified node. 00177 * Node gets added immediately before anode. 00178 * Parameters: 00179 * LinkedList *list - must be valid, non null, pointer to a linked list. 00180 * ListNode * anode - node to add the in front of. 00181 * void * item - item to be added 00182 * Returns: 00183 * The pointer to the ListNode on success, NULL on failure. 00184 * Precondition: 00185 * The list has been initialized. 00186 *****************************************************************************/ 00187 ListNode *ListAddBefore(LinkedList *list,void *item, ListNode *anode); 00188 00189 00190 /**************************************************************************** 00191 * Function: ListDelNode 00192 * 00193 * Description: 00194 * Removes a node from the list 00195 * The memory for the node is freed. 00196 * Parameters: 00197 * LinkedList *list - must be valid, non null, pointer to a linked list. 00198 * ListNode *dnode - done to delete. 00199 * freeItem - if !0 then item is freed using free function. 00200 * if 0 (or free function is NULL) then item is not freed 00201 * Returns: 00202 * The pointer to the item stored in the node or NULL if the item is freed. 00203 * Precondition: 00204 * The list has been initialized. 00205 *****************************************************************************/ 00206 void *ListDelNode(LinkedList *list,ListNode *dnode, int freeItem); 00207 00208 /**************************************************************************** 00209 * Function: ListDestroy 00210 * 00211 * Description: 00212 * Removes all memory associated with list nodes. 00213 * Does not free LinkedList *list. 00214 * 00215 * Parameters: 00216 * LinkedList *list - must be valid, non null, pointer to a linked list. 00217 * freeItem - if !0 then items are freed using the free_function. 00218 * if 0 (or free function is NULL) then items are not freed. 00219 * Returns: 00220 * 0 on success. Always returns 0. 00221 * Precondition: 00222 * The list has been initialized. 00223 *****************************************************************************/ 00224 int ListDestroy(LinkedList *list, int freeItem); 00225 00226 00227 /**************************************************************************** 00228 * Function: ListHead 00229 * 00230 * Description: 00231 * Returns the head of the list. 00232 * 00233 * Parameters: 00234 * LinkedList *list - must be valid, non null, pointer to a linked list. 00235 * 00236 * Returns: 00237 * The head of the list. NULL if list is empty. 00238 * Precondition: 00239 * The list has been initialized. 00240 *****************************************************************************/ 00241 ListNode* ListHead(LinkedList *list); 00242 00243 /**************************************************************************** 00244 * Function: ListTail 00245 * 00246 * Description: 00247 * Returns the tail of the list. 00248 * 00249 * Parameters: 00250 * LinkedList *list - must be valid, non null, pointer to a linked list. 00251 * 00252 * Returns: 00253 * The tail of the list. NULL if list is empty. 00254 * Precondition: 00255 * The list has been initialized. 00256 *****************************************************************************/ 00257 ListNode* ListTail(LinkedList *list); 00258 00259 /**************************************************************************** 00260 * Function: ListNext 00261 * 00262 * Description: 00263 * Returns the next item in the list. 00264 * 00265 * Parameters: 00266 * LinkedList *list - must be valid, non null, pointer to a linked list. 00267 * 00268 * Returns: 00269 * The next item in the list. NULL if there are no more items in list. 00270 * Precondition: 00271 * The list has been initialized. 00272 *****************************************************************************/ 00273 ListNode* ListNext(LinkedList *list, ListNode * node); 00274 00275 /**************************************************************************** 00276 * Function: ListPrev 00277 * 00278 * Description: 00279 * Returns the previous item in the list. 00280 * 00281 * Parameters: 00282 * LinkedList *list - must be valid, non null, pointer to a linked list. 00283 * 00284 * Returns: 00285 * The previous item in the list. NULL if there are no more items in list. 00286 * Precondition: 00287 * The list has been initialized. 00288 *****************************************************************************/ 00289 ListNode* ListPrev(LinkedList *list, ListNode * node); 00290 00291 /**************************************************************************** 00292 * Function: ListFind 00293 * 00294 * Description: 00295 * Finds the specified item in the list. 00296 * Uses the compare function specified in ListInit. If compare function 00297 * is NULL then compares items as pointers. 00298 * Parameters: 00299 * LinkedList *list - must be valid, non null, pointer to a linked list. 00300 * ListNode *start - the node to start from, NULL if to start from 00301 * beginning. 00302 * void * item - the item to search for. 00303 * Returns: 00304 * The node containing the item. NULL if no node contains the item. 00305 * Precondition: 00306 * The list has been initialized. 00307 *****************************************************************************/ 00308 ListNode* ListFind(LinkedList *list, ListNode *start, void * item); 00309 00310 /**************************************************************************** 00311 * Function: ListSize 00312 * 00313 * Description: 00314 * Returns the size of the list. 00315 * Parameters: 00316 * LinkedList *list - must be valid, non null, pointer to a linked list. 00317 00318 * Returns: 00319 * The number of items in the list. 00320 * Precondition: 00321 * The list has been initialized. 00322 *****************************************************************************/ 00323 int ListSize(LinkedList* list); 00324 00325 00326 #ifdef __cplusplus 00327 } 00328 #endif 00329 00330 #endif //LINKED_LIST_H
1.6.1