]> git.ipfire.org Git - thirdparty/glibc.git/blame - include/list.h
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / include / list.h
CommitLineData
04277e02 1/* Copyright (C) 2002-2019 Free Software Foundation, Inc.
76a50749
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
76a50749
UD
18
19#ifndef _LIST_H
500b3a49 20#define _LIST_H 1
7a775e6b 21
500b3a49 22/* Internal: doubly linked lists. */
76a50749 23
76a50749
UD
24/* The definitions of this file are adopted from those which can be
25 found in the Linux kernel headers to enable people familiar with
26 the latter find their way in these sources as well. */
27
500b3a49
ZW
28#include <list_t.h>
29#include <atomic.h>
76a50749
UD
30
31/* Define a variable with the head and tail of the list. */
500b3a49 32#define LIST_HEAD(name) \
76a50749
UD
33 list_t name = { &(name), &(name) }
34
35/* Initialize a new list head. */
500b3a49 36#define INIT_LIST_HEAD(ptr) \
76a50749
UD
37 (ptr)->next = (ptr)->prev = (ptr)
38
39
40/* Add new element at the head of the list. */
41static inline void
42list_add (list_t *newp, list_t *head)
43{
76a50749
UD
44 newp->next = head->next;
45 newp->prev = head;
f25c7b08 46 head->next->prev = newp;
7a775e6b 47 atomic_write_barrier ();
76a50749
UD
48 head->next = newp;
49}
50
51
76a50749
UD
52/* Remove element from list. */
53static inline void
54list_del (list_t *elem)
55{
56 elem->next->prev = elem->prev;
57 elem->prev->next = elem->next;
58}
59
60
61/* Join two lists. */
62static inline void
63list_splice (list_t *add, list_t *head)
64{
65 /* Do nothing if the list which gets added is empty. */
66 if (add != add->next)
67 {
68 add->next->prev = head;
69 add->prev->next = head->next;
70 head->next->prev = add->prev;
71 head->next = add->next;
72 }
73}
74
75
76/* Get typed element from list at a given position. */
500b3a49 77#define list_entry(ptr, type, member) \
76a50749
UD
78 ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))
79
80
81
82/* Iterate forward over the elements of the list. */
500b3a49 83#define list_for_each(pos, head) \
76a50749
UD
84 for (pos = (head)->next; pos != (head); pos = pos->next)
85
86
87/* Iterate forward over the elements of the list. */
500b3a49 88#define list_for_each_prev(pos, head) \
76a50749
UD
89 for (pos = (head)->prev; pos != (head); pos = pos->prev)
90
91
92/* Iterate backwards over the elements list. The list elements can be
93 removed from the list while doing this. */
500b3a49 94#define list_for_each_prev_safe(pos, p, head) \
76a50749
UD
95 for (pos = (head)->prev, p = pos->prev; \
96 pos != (head); \
97 pos = p, p = pos->prev)
98
99#endif /* list.h */