Advertisement
_Lunar

list.h

Apr 19th, 2025 (edited)
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.87 KB | Source Code | 0 0
  1. /*
  2.  * Copyright (c) 2004, Swedish Institute of Computer Science.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  * notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  * notice, this list of conditions and the following disclaimer in the
  12.  * documentation and/or other materials provided with the distribution.
  13.  * 3. Neither the name of the Institute nor the names of its contributors
  14.  * may be used to endorse or promote products derived from this software
  15.  * without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  18.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  21.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27.  * SUCH DAMAGE.
  28.  *
  29.  * This file is part of the Contiki operating system.
  30.  *
  31.  * Author: Adam Dunkels <[email protected]>
  32.  *
  33.  */
  34.  
  35. /**
  36.  * \file
  37.  * Linked list manipulation routines.
  38.  * \author Adam Dunkels <[email protected]>
  39.  *
  40.  */
  41.  
  42. /** \addtogroup data
  43.      @{ */
  44. /**
  45.  * \defgroup list Linked list library
  46.  *
  47.  * The linked list library provides a set of functions for
  48.  * manipulating linked lists.
  49.  *
  50.  * A linked list is made up of elements where the first element \b
  51.  * must be a pointer. This pointer is used by the linked list library
  52.  * to form lists of the elements.
  53.  *
  54.  * Lists are declared with the LIST() macro. The declaration specifies
  55.  * the name of the list that later is used with all list functions.
  56.  *
  57.  * Lists can be manipulated by inserting or removing elements from
  58.  * either sides of the list (list_push(), list_add(), list_pop(),
  59.  * list_chop()). A specified element can also be removed from inside a
  60.  * list with list_remove(). The head and tail of a list can be
  61.  * extracted using list_head() and list_tail(), respectively.
  62.  *
  63.  * This library is not safe to be used within an interrupt context.
  64.  * @{
  65.  */
  66.  
  67.  #ifndef LIST_H_
  68.  #define LIST_H_
  69.  
  70.  #include <stdbool.h>
  71.  #include <stddef.h>
  72.  
  73.  #define LIST_CONCAT2(s1, s2) s1##s2
  74.  #define LIST_CONCAT(s1, s2) LIST_CONCAT2(s1, s2)
  75.  
  76.  /**
  77.   * Declare a linked list.
  78.   *
  79.   * This macro declares a linked list with the specified \c type. The
  80.   * type \b must be a structure (\c struct) with its first element
  81.   * being a pointer. This pointer is used by the linked list library to
  82.   * form the linked lists.
  83.   *
  84.   * The list variable is declared as static to make it easy to use in a
  85.   * single C module without unnecessarily exporting the name to other
  86.   * modules.
  87.   *
  88.   * \param name The name of the list.
  89.   */
  90.  #define LIST(name) \
  91.           static void *LIST_CONCAT(name,_list) = NULL; \
  92.           static list_t name = (list_t)&LIST_CONCAT(name,_list)
  93.  
  94.  /**
  95.   * Declare a linked list inside a structure declaraction.
  96.   *
  97.   * This macro declares a linked list with the specified \c type. The
  98.   * type \b must be a structure (\c struct) with its first element
  99.   * being a pointer. This pointer is used by the linked list library to
  100.   * form the linked lists.
  101.   *
  102.   * Internally, the list is defined as two items: the list itself and a
  103.   * pointer to the list. The pointer has the name of the parameter to
  104.   * the macro and the name of the list is a concatenation of the name
  105.   * and the suffix "_list". The pointer must point to the list for the
  106.   * list to work. Thus the list must be initialized before using.
  107.   *
  108.   * The list is initialized with the LIST_STRUCT_INIT() macro.
  109.   *
  110.   * \param name The name of the list.
  111.   */
  112.  #define LIST_STRUCT(name) \
  113.           void *LIST_CONCAT(name,_list); \
  114.           list_t name
  115.  
  116.  /**
  117.   * Initialize a linked list that is part of a structure.
  118.   *
  119.   * This macro sets up the internal pointers in a list that has been
  120.   * defined as part of a struct. This macro must be called before using
  121.   * the list.
  122.   *
  123.   * \param struct_ptr A pointer to the struct
  124.   * \param name The name of the list.
  125.   */
  126.  #define LIST_STRUCT_INIT(struct_ptr, name)                                    \
  127.   do {                                                                       \
  128.            (struct_ptr)->name = &((struct_ptr)->LIST_CONCAT(name,_list));   \
  129.            (struct_ptr)->LIST_CONCAT(name,_list) = NULL;                   \
  130.            list_init((struct_ptr)->name);                                   \
  131.   } while(0)
  132.  
  133.  /**
  134.   * The linked list type.
  135.   */
  136.  typedef void ** list_t;
  137.  
  138.  /**
  139.   * The non-modifiable linked list type.
  140.   */
  141.  typedef void *const *const_list_t;
  142.  
  143.  /**
  144.   * The base structure for list nodes.
  145.   * All structures that will be used with the list library must
  146.   * have this structure as their first element.
  147.   */
  148.  
  149.  /**
  150.   * Initialize a list.
  151.   *
  152.   * This function initalizes a list. The list will be empty after this
  153.   * function has been called.
  154.   *
  155.   * \param list The list to be initialized.
  156.   */
  157.  static inline void
  158.  list_init(list_t list)
  159.  {
  160.   *list = NULL;
  161.  }
  162.  
  163.  /**
  164.   * Get a pointer to the first element of a list.
  165.   *
  166.   * This function returns a pointer to the first element of the
  167.   * list. The element will \b not be removed from the list.
  168.   *
  169.   * \param list The list.
  170.   * \return A pointer to the first element on the list.
  171.   *
  172.   * \sa list_tail()
  173.   */
  174.  static inline void *
  175.  list_head(const_list_t list)
  176.  {
  177.   return *list;
  178.  }
  179.  
  180.  /**
  181.   * Get the tail of a list.
  182.   *
  183.   * This function returns a pointer to the elements following the first
  184.   * element of a list. No elements are removed by this function.
  185.   *
  186.   * \param list The list
  187.   * \return A pointer to the element after the first element on the list.
  188.   *
  189.   * \sa list_head()
  190.   */
  191.  void * list_tail(const_list_t list);
  192.  
  193.  /**
  194.   * Remove the first object on a list.
  195.   *
  196.   * This function removes the first object on the list and returns a
  197.   * pointer to it.
  198.   *
  199.   * \param list The list.
  200.   * \return Pointer to the removed element of list.
  201.   */
  202.  void * list_pop (list_t list);
  203.  
  204.  /**
  205.   * Add an item to the start of the list.
  206.   */
  207.  void   list_push(list_t list, void *item);
  208.  
  209.  /**
  210.   * Remove the last object on the list.
  211.   *
  212.   * This function removes the last object on the list and returns it.
  213.   *
  214.   * \param list The list
  215.   * \return The removed object
  216.   *
  217.   */
  218.  void * list_chop(list_t list);
  219.  
  220.  /**
  221.   * Add an item at the end of a list.
  222.   *
  223.   * This function adds an item to the end of the list.
  224.   *
  225.   * \param list The list.
  226.   * \param item A pointer to the item to be added.
  227.   *
  228.   * \sa list_push()
  229.   *
  230.   */
  231.  void   list_add(list_t list, void *item);
  232.  
  233.  /**
  234.   * Remove a specific element from a list.
  235.   *
  236.   * This function removes a specified element from the list.
  237.   *
  238.   * \param list The list.
  239.   * \param item The item that is to be removed from the list.
  240.   *
  241.   */
  242.  void   list_remove(list_t list, const void *item);
  243.  
  244.  /**
  245.   * Get the length of a list.
  246.   *
  247.   * This function counts the number of elements on a specified list.
  248.   *
  249.   * \param list The list.
  250.   * \return The length of the list.
  251.   */
  252.  int    list_length(const_list_t list);
  253.  
  254.  /**
  255.   * Duplicate a list.
  256.   *
  257.   * This function duplicates a list by copying the list reference, but
  258.   * not the elements.
  259.   *
  260.   * \note This function does \b not copy the elements of the list, but
  261.   * merely duplicates the pointer to the first element of the list.
  262.   *
  263.   * \param dest The destination list.
  264.   * \param src The source list.
  265.   */
  266.  static inline void
  267.  list_copy(list_t dest, const_list_t src)
  268.  {
  269.   *dest = *src;
  270.  }
  271.  
  272.  /**
  273.   * \brief     Insert an item after a specified item on the list
  274.   * \param list The list
  275.   * \param previtem The item after which the new item should be inserted
  276.   * \param newitem   The new item that is to be inserted
  277.   * \author    Adam Dunkels
  278.   *
  279.   * This function inserts an item right after a specified
  280.   * item on the list. This function is useful when using
  281.   * the list module to ordered lists.
  282.   *
  283.   * If previtem is NULL, the new item is placed at the
  284.   * start of the list.
  285.   *
  286.   */
  287.  void   list_insert(list_t list, void *previtem, void *newitem);
  288.  
  289.  /**
  290.   * \brief     Get the next item following this item
  291.   * \param item A list item
  292.   * \returns   A next item on the list
  293.   *
  294.   * This function takes a list item and returns the next
  295.   * item on the list, or NULL if there are no more items on
  296.   * the list. This function is used when iterating through
  297.   * lists.
  298.   */
  299.  static inline void *
  300.  list_item_next(const void *item)
  301.  {
  302.   struct list {
  303.     struct list *next;
  304.   };
  305.   return item == NULL ? NULL : ((struct list *)item)->next;
  306.  }
  307.  
  308.  /**
  309.   * \brief     Check if the list contains an item
  310.   * \param list The list that is checked
  311.   * \param item An item to look for in the list
  312.   * \returns   0 if the list does not contains the item, and 1 otherwise
  313.   *
  314.   * This function searches for an item in the list and returns
  315.   * 0 if the list does not contain the item, and 1 if the item
  316.   * is present in the list.
  317.   */
  318.  bool list_contains(const_list_t list, const void *item);
  319.  
  320.  /* MODIFICATION: Adding headers for our new functions in list.c */
  321.  int list_item_get_value(const void *item);
  322.  void list_item_set_value(void *item, int value);
  323.  
  324.  #endif /* LIST_H_ */
  325.  
  326.  /** @} */
  327.  /** @} */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement