"Fossies" - the Fresh Open Source Software Archive

Member "usbutils-015/list.h" (22 Feb 2021, 2492 Bytes) of package /linux/misc/usbutils-015.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "list.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 013_vs_014.

    1 // SPDX-License-Identifier: GPL-2.0-only
    2 #ifndef _LINUX_LIST_H
    3 #define _LINUX_LIST_H
    4 
    5 /*
    6  * Simple doubly linked list implementation.
    7  *
    8  * Some of the internal functions ("__xxx") are useful when
    9  * manipulating whole lists rather than single entries, as
   10  * sometimes we already know the next/prev entries and we can
   11  * generate better code by using them directly rather than
   12  * using the generic single-entry routines.
   13  *
   14  * Copyright (c) 2004 Thomas Sailer <sailer@ife.ee.ethz.ch>
   15  *
   16  * Based on list.h from the Linux kernel source tree.
   17  */
   18 
   19 struct list_head {
   20     struct list_head *next, *prev;
   21 };
   22 
   23 #define LIST_HEAD_INIT(name) { &(name), &(name) }
   24 
   25 #define LIST_HEAD(name) \
   26     struct list_head name = LIST_HEAD_INIT(name)
   27 
   28 #define INIT_LIST_HEAD(ptr) do { \
   29     (ptr)->next = (ptr); (ptr)->prev = (ptr); \
   30 } while (0)
   31 
   32 /*
   33  * Insert a new entry between two known consecutive entries.
   34  *
   35  * This is only for internal list manipulation where we know
   36  * the prev/next entries already!
   37  */
   38 static inline void __list_add(struct list_head *new,
   39                   struct list_head *prev,
   40                   struct list_head *next)
   41 {
   42     next->prev = new;
   43     new->next = next;
   44     new->prev = prev;
   45     prev->next = new;
   46 }
   47 
   48 /*
   49  * Insert a new entry after the specified head..
   50  */
   51 static inline void list_add(struct list_head *new, struct list_head *head)
   52 {
   53     __list_add(new, head, head->next);
   54 }
   55 
   56 /*
   57  * Insert a new entry before the specified head..
   58  */
   59 static inline void list_add_tail(struct list_head *new, struct list_head *head)
   60 {
   61     __list_add(new, head->prev, head);
   62 }
   63 
   64 /*
   65  * Delete a list entry by making the prev/next entries
   66  * point to each other.
   67  *
   68  * This is only for internal list manipulation where we know
   69  * the prev/next entries already!
   70  */
   71 static inline void __list_del(struct list_head *prev, struct list_head *next)
   72 {
   73     next->prev = prev;
   74     prev->next = next;
   75 }
   76 
   77 static inline void list_del(struct list_head *entry)
   78 {
   79     __list_del(entry->prev, entry->next);
   80 }
   81 
   82 static inline int list_empty(struct list_head *head)
   83 {
   84     return head->next == head;
   85 }
   86 
   87 /*
   88  * Splice in "list" into "head"
   89  */
   90 static inline void list_splice(struct list_head *list,
   91                    struct list_head *head)
   92 {
   93     struct list_head *first = list->next;
   94 
   95     if (first != list) {
   96         struct list_head *last = list->prev;
   97         struct list_head *at = head->next;
   98 
   99         first->prev = head;
  100         head->next = first;
  101 
  102         last->next = at;
  103         at->prev = last;
  104     }
  105 }
  106 
  107 #define list_entry(ptr, type, member) \
  108     ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
  109 
  110 #endif