blob: 9d1129b185d53c330f918eb769e234077e6ae83a [file] [log] [blame]
José Fonsecab4783622007-11-23 17:22:54 +00001/**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28/**
29 * \file
30 * List macros heavily inspired by the Linux kernel
31 * list handling. No list looping yet.
32 *
33 * Is not threadsafe, so common operations need to
34 * be protected using an external mutex.
35 */
36
José Fonsecab9da3792008-02-19 15:07:53 +090037#ifndef _U_DOUBLE_LIST_H_
38#define _U_DOUBLE_LIST_H_
José Fonsecab4783622007-11-23 17:22:54 +000039
40
41#include <stddef.h>
Jerome Glisseebe304f2011-03-28 17:45:31 -040042#include "pipe/p_compiler.h"
José Fonsecab4783622007-11-23 17:22:54 +000043
44
45struct list_head
46{
47 struct list_head *prev;
48 struct list_head *next;
49};
50
Jerome Glisseebe304f2011-03-28 17:45:31 -040051static INLINE void list_inithead(struct list_head *item)
52{
53 item->prev = item;
54 item->next = item;
55}
José Fonsecab4783622007-11-23 17:22:54 +000056
Jerome Glisseebe304f2011-03-28 17:45:31 -040057static INLINE void list_add(struct list_head *item, struct list_head *list)
58{
59 item->prev = list;
60 item->next = list->next;
61 list->next->prev = item;
62 list->next = item;
63}
José Fonsecab4783622007-11-23 17:22:54 +000064
Jerome Glisseebe304f2011-03-28 17:45:31 -040065static INLINE void list_addtail(struct list_head *item, struct list_head *list)
66{
67 item->next = list;
68 item->prev = list->prev;
69 list->prev->next = item;
70 list->prev = item;
71}
José Fonsecab4783622007-11-23 17:22:54 +000072
Jerome Glisseebe304f2011-03-28 17:45:31 -040073static INLINE void list_replace(struct list_head *from, struct list_head *to)
74{
75 to->prev = from->prev;
76 to->next = from->next;
77 from->next->prev = to;
78 from->prev->next = to;
79}
José Fonsecab4783622007-11-23 17:22:54 +000080
Jerome Glisseebe304f2011-03-28 17:45:31 -040081static INLINE void list_del(struct list_head *item)
82{
83 item->prev->next = item->next;
84 item->next->prev = item->prev;
85}
José Fonsecabc56e872008-04-10 22:57:21 +090086
Jerome Glisseebe304f2011-03-28 17:45:31 -040087static INLINE void list_delinit(struct list_head *item)
88{
89 item->prev->next = item->next;
90 item->next->prev = item->prev;
91 item->next = item;
92 item->prev = item;
93}
José Fonsecab4783622007-11-23 17:22:54 +000094
Jerome Glisseebe304f2011-03-28 17:45:31 -040095#define LIST_INITHEAD(__item) list_inithead(__item)
96#define LIST_ADD(__item, __list) list_add(__item, __list)
97#define LIST_ADDTAIL(__item, __list) list_addtail(__item, __list)
98#define LIST_REPLACE(__from, __to) list_replace(__from, __to)
99#define LIST_DEL(__item) list_del(__item)
100#define LIST_DELINIT(__item) list_delinit(__item)
José Fonsecab4783622007-11-23 17:22:54 +0000101
102#define LIST_ENTRY(__type, __item, __field) \
103 ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
104
José Fonseca3708aae2009-03-23 12:05:07 +0000105#define LIST_IS_EMPTY(__list) \
106 ((__list)->next == (__list))
107
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000108/**
109 * Cast from a pointer to a member of a struct back to the containing struct.
110 *
111 * 'sample' MUST be initialized, or else the result is undefined!
112 */
Dave Airlie8556b772010-07-02 15:27:17 +1000113#ifndef container_of
114#define container_of(ptr, sample, member) \
115 (void *)((char *)(ptr) \
116 - ((char *)&(sample)->member - (char *)(sample)))
117#endif
José Fonsecab4783622007-11-23 17:22:54 +0000118
Dave Airlie8556b772010-07-02 15:27:17 +1000119#define LIST_FOR_EACH_ENTRY(pos, head, member) \
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000120 for (pos = NULL, pos = container_of((head)->next, pos, member); \
Dave Airlie8556b772010-07-02 15:27:17 +1000121 &pos->member != (head); \
122 pos = container_of(pos->member.next, pos, member))
123
124#define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member) \
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000125 for (pos = NULL, pos = container_of((head)->next, pos, member), \
Dave Airlie8556b772010-07-02 15:27:17 +1000126 storage = container_of(pos->member.next, pos, member); \
127 &pos->member != (head); \
128 pos = storage, storage = container_of(storage->member.next, storage, member))
Jerome Glisseebe304f2011-03-28 17:45:31 -0400129
130#define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member) \
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000131 for (pos = NULL, pos = container_of((head)->prev, pos, member), \
Jerome Glisseebe304f2011-03-28 17:45:31 -0400132 storage = container_of(pos->member.prev, pos, member); \
133 &pos->member != (head); \
134 pos = storage, storage = container_of(storage->member.prev, storage, member))
135
136#define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member) \
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000137 for (pos = NULL, pos = container_of((start), pos, member); \
Jerome Glisseebe304f2011-03-28 17:45:31 -0400138 &pos->member != (head); \
139 pos = container_of(pos->member.next, pos, member))
140
141#define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member) \
Dylan Noblesmithccff7492012-04-01 18:21:47 +0000142 for (pos = NULL, pos = container_of((start), pos, member); \
Jerome Glisseebe304f2011-03-28 17:45:31 -0400143 &pos->member != (head); \
144 pos = container_of(pos->member.prev, pos, member))
145
José Fonsecab9da3792008-02-19 15:07:53 +0900146#endif /*_U_DOUBLE_LIST_H_*/