]> git.ipfire.org Git - thirdparty/glibc.git/blame - include/list.h
Further harden glibc malloc metadata against 1-byte overflows.
[thirdparty/glibc.git] / include / list.h
CommitLineData
bfff8b1b 1/* Copyright (C) 2002-2017 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
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
76a50749
UD
18
19#ifndef _LIST_H
7a775e6b
AS
20
21#ifndef __need_list_t
22# define _LIST_H 1
23#endif
76a50749 24
76a50749
UD
25/* The definitions of this file are adopted from those which can be
26 found in the Linux kernel headers to enable people familiar with
27 the latter find their way in these sources as well. */
28
29
7a775e6b
AS
30#if defined __need_list_t || defined _LIST_H
31# ifndef __list_t_defined
32# define __list_t_defined
76a50749
UD
33/* Basic type for the double-link list. */
34typedef struct list_head
35{
36 struct list_head *next;
37 struct list_head *prev;
38} list_t;
7a775e6b
AS
39# endif
40# undef __need_list_t
41#endif
42
43#ifdef _LIST_H
76a50749 44
7a775e6b 45# include <atomic.h>
76a50749
UD
46
47/* Define a variable with the head and tail of the list. */
7a775e6b 48# define LIST_HEAD(name) \
76a50749
UD
49 list_t name = { &(name), &(name) }
50
51/* Initialize a new list head. */
7a775e6b 52# define INIT_LIST_HEAD(ptr) \
76a50749
UD
53 (ptr)->next = (ptr)->prev = (ptr)
54
55
56/* Add new element at the head of the list. */
57static inline void
58list_add (list_t *newp, list_t *head)
59{
76a50749
UD
60 newp->next = head->next;
61 newp->prev = head;
f25c7b08 62 head->next->prev = newp;
7a775e6b 63 atomic_write_barrier ();
76a50749
UD
64 head->next = newp;
65}
66
67
76a50749
UD
68/* Remove element from list. */
69static inline void
70list_del (list_t *elem)
71{
72 elem->next->prev = elem->prev;
73 elem->prev->next = elem->next;
74}
75
76
77/* Join two lists. */
78static inline void
79list_splice (list_t *add, list_t *head)
80{
81 /* Do nothing if the list which gets added is empty. */
82 if (add != add->next)
83 {
84 add->next->prev = head;
85 add->prev->next = head->next;
86 head->next->prev = add->prev;
87 head->next = add->next;
88 }
89}
90
91
92/* Get typed element from list at a given position. */
7a775e6b 93# define list_entry(ptr, type, member) \
76a50749
UD
94 ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))
95
96
97
98/* Iterate forward over the elements of the list. */
7a775e6b 99# define list_for_each(pos, head) \
76a50749
UD
100 for (pos = (head)->next; pos != (head); pos = pos->next)
101
102
103/* Iterate forward over the elements of the list. */
7a775e6b 104# define list_for_each_prev(pos, head) \
76a50749
UD
105 for (pos = (head)->prev; pos != (head); pos = pos->prev)
106
107
108/* Iterate backwards over the elements list. The list elements can be
109 removed from the list while doing this. */
7a775e6b 110# define list_for_each_prev_safe(pos, p, head) \
76a50749
UD
111 for (pos = (head)->prev, p = pos->prev; \
112 pos != (head); \
113 pos = p, p = pos->prev)
114
7a775e6b
AS
115#endif /* _LIST_H */
116
76a50749 117#endif /* list.h */