tinythread

tiny threading library for linux
git clone git://git.2f30.org/tinythread
Log | Files | Refs | README | LICENSE

list.h (3004B)


      1 /* This header file was taken from the Linux Kernel and it is under
      2  * GPLv2.
      3  *
      4  * This program is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU General Public License
      6  * as published by the Free Software Foundation; either version 2
      7  * of the License, or (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  * GNU General Public License for more details.
     13 
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program; if not, write to the Free Software
     16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     17  */
     18 
     19 #ifndef _LIST_H
     20 #define _LIST_H
     21 
     22 #define offsetof(TYPE, MEMBER) ((size_t)__builtin_offsetof(TYPE, MEMBER))
     23 #define container_of(ptr, type, member) ({ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) ); })
     24 
     25 /**
     26  * list_entry - get the struct for this entry
     27  * @ptr: the &struct list_head pointer.
     28  * @type:   the type of the struct this is embedded in.
     29  * @member: the name of the list_struct within the struct.
     30  */
     31 #define list_entry(ptr, type, member) \
     32 	container_of(ptr, type, member)
     33 
     34 /**
     35  * list_for_each  -  iterate over a list
     36  * @pos: the &struct list_head to use as a loop cursor.
     37  * @head:   the head for your list.
     38  */
     39 #define list_for_each(pos, head) \
     40 	for (pos = (head)->next; pos != (head); \
     41 	     pos = pos->next)
     42 /**
     43  * list_for_each_safe - iterate over a list safe against removal of list entry
     44  * @pos: the &struct list_head to use as a loop cursor.
     45  * @n:      another &struct list_head to use as temporary storage
     46  * @head:   the head for your list.
     47  */
     48 #define list_for_each_safe(pos, n, head) \
     49 	for (pos = (head)->next, n = pos->next; pos != (head); \
     50 	     pos = n, n = pos->next)
     51 
     52 #define LIST_HEAD(name) \
     53 	struct list_head name = LIST_HEAD_INIT(name)
     54 
     55 #define LIST_HEAD_INIT(name) { &(name), &(name) } /* assign both next and prev to point to &name */
     56 
     57 struct list_head {
     58 	struct list_head *next;
     59 	struct list_head *prev;
     60 };
     61 
     62 static inline void
     63 INIT_LIST_HEAD(struct list_head *list)
     64 {
     65 	list->next = list;
     66 	list->prev = list;
     67 }
     68 
     69 static inline void
     70 list_add(struct list_head *new, struct list_head *head)
     71 {
     72 	head->next->prev = new;
     73 	new->next = head->next;
     74 	new->prev = head;
     75 	head->next = new;
     76 }
     77 
     78 static inline void
     79 list_add_tail(struct list_head *new, struct list_head *head)
     80 {
     81 	head->prev->next = new;
     82 	new->next = head;
     83 	new->prev = head->prev;
     84 	head->prev = new;
     85 }
     86 
     87 static inline void
     88 list_del(struct list_head *entry)
     89 {
     90 	entry->prev->next = entry->next;
     91 	entry->next->prev = entry->prev;
     92 	entry->next = NULL;
     93 	entry->prev = NULL;
     94 }
     95 
     96 /**
     97  * list_empty - tests whether a list is empty
     98  * @head: the list to test.
     99  */
    100 static inline int list_empty(const struct list_head *head)
    101 {
    102 	return head->next == head;
    103 }
    104 
    105 #endif