source: mainline/kernel/generic/include/adt/list.h@ 0c1a5d8a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0c1a5d8a was b3f8fb7, checked in by Martin Decky <martin@…>, 18 years ago

huge type system cleanup
remove cyclical type dependencies across multiple header files
many minor coding style fixes

  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[f761f1eb]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[06e1e95]29/** @addtogroup genericadt
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[06e1e95]35#ifndef KERN_LIST_H_
36#define KERN_LIST_H_
[f761f1eb]37
38#include <arch/types.h>
39
[7dd2561]40/** Doubly linked list head and link type. */
[b3f8fb7]41typedef struct link {
42 struct link *prev; /**< Pointer to the previous item in the list. */
43 struct link *next; /**< Pointer to the next item in the list. */
44} link_t;
[f761f1eb]45
[7dd2561]46/** Declare and initialize statically allocated list.
47 *
48 * @param name Name of the new statically allocated list.
49 */
50#define LIST_INITIALIZE(name) link_t name = { .prev = &name, .next = &name }
51
[40a468a]52/** Initialize doubly-linked circular list link
53 *
54 * Initialize doubly-linked list link.
55 *
56 * @param link Pointer to link_t structure to be initialized.
57 */
[c0a91d1]58static inline void link_initialize(link_t *link)
59{
60 link->prev = NULL;
61 link->next = NULL;
[f761f1eb]62}
63
[40a468a]64/** Initialize doubly-linked circular list
65 *
66 * Initialize doubly-linked circular list.
67 *
68 * @param head Pointer to link_t structure representing head of the list.
69 */
[c0a91d1]70static inline void list_initialize(link_t *head)
71{
72 head->prev = head;
73 head->next = head;
[f761f1eb]74}
75
[40a468a]76/** Add item to the beginning of doubly-linked circular list
77 *
78 * Add item to the beginning of doubly-linked circular list.
79 *
80 * @param link Pointer to link_t structure to be added.
81 * @param head Pointer to link_t structure representing head of the list.
82 */
[c0a91d1]83static inline void list_prepend(link_t *link, link_t *head)
84{
85 link->next = head->next;
86 link->prev = head;
87 head->next->prev = link;
88 head->next = link;
[f761f1eb]89}
90
[40a468a]91/** Add item to the end of doubly-linked circular list
92 *
93 * Add item to the end of doubly-linked circular list.
94 *
95 * @param link Pointer to link_t structure to be added.
96 * @param head Pointer to link_t structure representing head of the list.
97 */
[c0a91d1]98static inline void list_append(link_t *link, link_t *head)
99{
100 link->prev = head->prev;
101 link->next = head;
102 head->prev->next = link;
103 head->prev = link;
[f761f1eb]104}
105
[40a468a]106/** Remove item from doubly-linked circular list
107 *
108 * Remove item from doubly-linked circular list.
109 *
110 * @param link Pointer to link_t structure to be removed from the list it is contained in.
111 */
[c0a91d1]112static inline void list_remove(link_t *link)
113{
114 link->next->prev = link->prev;
115 link->prev->next = link->next;
116 link_initialize(link);
[f761f1eb]117}
118
[40a468a]119/** Query emptiness of doubly-linked circular list
120 *
121 * Query emptiness of doubly-linked circular list.
122 *
123 * @param head Pointer to link_t structure representing head of the list.
124 */
125static inline bool list_empty(link_t *head)
126{
127 return head->next == head ? true : false;
128}
129
130
131/** Split or concatenate headless doubly-linked circular list
132 *
133 * Split or concatenate headless doubly-linked circular list.
134 *
135 * Note that the algorithm works both directions:
136 * concatenates splitted lists and splits concatenated lists.
137 *
138 * @param part1 Pointer to link_t structure leading the first (half of the headless) list.
139 * @param part2 Pointer to link_t structure leading the second (half of the headless) list.
140 */
141static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
142{
143 link_t *hlp;
144
145 part1->prev->next = part2;
146 part2->prev->next = part1;
147 hlp = part1->prev;
148 part1->prev = part2->prev;
149 part2->prev = hlp;
150}
151
152
153/** Split headless doubly-linked circular list
154 *
155 * Split headless doubly-linked circular list.
156 *
157 * @param part1 Pointer to link_t structure leading the first half of the headless list.
158 * @param part2 Pointer to link_t structure leading the second half of the headless list.
159 */
160static inline void headless_list_split(link_t *part1, link_t *part2)
161{
162 headless_list_split_or_concat(part1, part2);
163}
164
165/** Concatenate two headless doubly-linked circular lists
166 *
167 * Concatenate two headless doubly-linked circular lists.
168 *
169 * @param part1 Pointer to link_t structure leading the first headless list.
170 * @param part2 Pointer to link_t structure leading the second headless list.
171 */
172static inline void headless_list_concat(link_t *part1, link_t *part2)
173{
174 headless_list_split_or_concat(part1, part2);
175}
[f761f1eb]176
[7f1c620]177#define list_get_instance(link,type,member) (type *)(((uint8_t*)(link))-((uint8_t*)&(((type *)NULL)->member)))
[f761f1eb]178
[45671f48]179extern bool list_member(const link_t *link, const link_t *head);
[f761f1eb]180extern void list_concat(link_t *head1, link_t *head2);
181
182#endif
[b45c443]183
[06e1e95]184/** @}
[b45c443]185 */
Note: See TracBrowser for help on using the repository browser.