Nut/OS  4.10.3
API Reference
lili.c
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 by egnite GmbH
00003  *
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
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  * 3. Neither the name of the copyright holders nor the names of
00016  *    contributors may be used to endorse or promote products derived
00017  *    from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00026  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00027  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00028  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00029  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00030  * SUCH DAMAGE.
00031  *
00032  * For additional information see http://www.ethernut.de/
00033  */
00034 
00035 /*
00036  * \file gorp/list/lili.c
00037  * \brief Linked list main functions.
00038  *
00039  * \verbatim
00040  * $Id$
00041  * \endverbatim
00042  */
00043 
00044 #include <memdebug.h>
00045 #include <sys/nutdebug.h>
00046 
00047 #include <stdlib.h>
00048 #include <gorp/lili.h>
00049 
00054 
00067 static int LiLiDefaultItemCompare(LILI_ITEMREF ref1, LILI_ITEMREF ref2)
00068 {
00069     return ref1 - ref2;
00070 }
00071 
00072 /*
00073  * \brief Add the first node object to an empty list.
00074  *
00075  * \param list Pointer to a list object, obtained from a previous call
00076  *             to LiLiCreate().
00077  * \param node Pointer to the node object to add.
00078  */
00079 static void LiLiNodeAddFirst(LILI *list, LILI_NODE *node)
00080 {
00081     /* The list must be empty. */
00082     NUTASSERT(list->lst_head == NULL);
00083     NUTASSERT(list->lst_tail == NULL);
00084     /* Insert the first node. */
00085     list->lst_head = node;
00086     list->lst_tail = node;
00087     node->nod_nxt = NULL;
00088     node->nod_prv = NULL;
00089 }
00090 
00107 void LiLiRemoveNode(LILI *list, LILI_NODE *node)
00108 {
00109     /* Sanity checks. */
00110     NUTASSERT(list != NULL);
00111     if (node) {
00112         NUTASSERT(list->lst_head != NULL);
00113         NUTASSERT(list->lst_tail != NULL);
00114 
00115         /* Remove the forward link. */
00116         if (node->nod_nxt) {
00117             node->nod_nxt->nod_prv = node->nod_prv;
00118         } else {
00119             list->lst_tail = node->nod_prv;
00120         }
00121         /* Remove the backward link. */
00122         if (node->nod_prv) {
00123             node->nod_prv->nod_nxt = node->nod_nxt;
00124         } else {
00125             list->lst_head = node->nod_nxt;
00126         }
00127         free(node);
00128     }
00129 }
00130 
00131 /*
00132  * \brief Insert a new node object after a given node.
00133  *
00134  * Note, that list attributes are ignored. Anyway, special applications may 
00135  * use this function instead of LiLiPushItem().
00136  *
00137  * \param list Pointer to a list object, obtained from a previous call
00138  *             to LiLiCreate().
00139  * \param node Pointer to the node after which the a new node will be 
00140  *             inserted. If the list is empty, this must be set to NULL.
00141  * \param ref  The reference of the item to associate with the new node.
00142  *             If a function for creating an item copy has been specified, 
00143  *             it will be called before inserting the new node.
00144  *
00145  * \return Pointer to the new node object or NULL on errors.
00146  */
00147 LILI_NODE *LiLiInsertItemAfterNode(LILI *list, LILI_NODE *node, LILI_ITEMREF ref)
00148 {
00149     LILI_NODE *newnode;
00150 
00151     /* Sanity check. */
00152     NUTASSERT(list != NULL);
00153 
00154     newnode = malloc(sizeof(LILI_NODE));
00155     if (newnode) {
00156         /* Optionally create a duplicate of the item. */
00157         newnode->nod_itm = list->lst_icreate ? list->lst_icreate(ref) : ref;
00158         if (node) {
00159             /* A node is provided. The list must have members. */
00160             NUTASSERT(list->lst_head != NULL);
00161             NUTASSERT(list->lst_tail != NULL);
00162             /* Link the new node to the list. */
00163             newnode->nod_nxt = node->nod_nxt;
00164             newnode->nod_prv = node;
00165             node->nod_nxt = newnode;
00166             if (newnode->nod_nxt) {
00167                 newnode->nod_nxt->nod_prv = newnode;
00168             }
00169             if (node == list->lst_tail) {
00170                 list->lst_tail = newnode;
00171             }
00172         } else {
00173             /* No node provided. Add the first node to an empty list. */
00174             LiLiNodeAddFirst(list, newnode);
00175         }
00176     }
00177     return newnode;
00178 }
00179 
00180 /*
00181  * \brief Insert new data node before a given node.
00182  *
00183  * Note, that list attributes are ignored. Anyway, special applications may 
00184  * use this function instead of LiLiPushItem().
00185  *
00186  * \param list Pointer to a list object, obtained from a previous call
00187  *             to LiLiCreate().
00188  * \param ref  The reference of the item to associate with the new node.
00189  *             If a function for creating an item copy has been specified, 
00190  *             it will be called before inserting the new node.
00191  *
00192  * \return Pointer to the new node object or NULL on errors.
00193  */
00194 LILI_NODE *LiLiInsertItemBeforeNode(LILI *list, LILI_NODE *node, LILI_ITEMREF ref)
00195 {
00196     LILI_NODE *newnode;
00197 
00198     /* Sanity checks. */
00199     NUTASSERT(list != NULL);
00200 
00201     newnode = malloc(sizeof(LILI_NODE));
00202     if (newnode) {
00203         newnode->nod_itm = list->lst_icreate ? list->lst_icreate(ref) : ref;
00204         if (node) {
00205             /* A node is provided. The list must have members. */
00206             NUTASSERT(list->lst_head != NULL);
00207             NUTASSERT(list->lst_tail != NULL);
00208             /* Link the new node to the list. */
00209             newnode->nod_nxt = node;
00210             newnode->nod_prv = node->nod_prv;
00211             if (newnode->nod_prv) {
00212                 newnode->nod_prv->nod_nxt = newnode;
00213             }
00214             node->nod_prv = newnode;
00215             if (node == list->lst_head) {
00216                 list->lst_head = newnode;
00217             }
00218         } else {
00219             /* No node provided. Add the first node to an empty list. */
00220             LiLiNodeAddFirst(list, newnode);
00221         }
00222     }
00223     return newnode;
00224 }
00225 
00247 LILI *LiLiCreate(uint8_t flags, LiLiItemCreateFunc icre, LiLiItemDestroyFunc ides, LiLiItemCompareFunc icmp)
00248 {
00249     LILI *list = calloc(1, sizeof(LILI));
00250 
00251     if (list) {
00252         list->lst_icreate = icre;
00253         list->lst_idestroy = ides;
00254         if (icmp) {
00255             list->lst_icompare = icmp;
00256         } else {
00257             list->lst_icompare = LiLiDefaultItemCompare;
00258         }
00259         list->lst_flags = flags;
00260     }
00261     return list;
00262 }
00263 
00273 void LiLiClean(LILI *list)
00274 {
00275     LILI_NODE *node;
00276 
00277     /* Sanity checks. */
00278     NUTASSERT(list != NULL);
00279 
00280     if (list->lst_head) {
00281         while ((node = list->lst_head) != NULL) {
00282             list->lst_head = node->nod_nxt;
00283             if (list->lst_idestroy) {
00284                 list->lst_idestroy(LiLiNodeItem(node));
00285             }
00286             free(node);
00287         }
00288         list->lst_tail = NULL;
00289     }
00290 }
00291 
00301 void LiLiDestroy(LILI *list)
00302 {
00303     /* Remove all node objects. */
00304     LiLiClean(list);
00305     /* Release the list object. */
00306     free(list);
00307 }
00308