source: mainline/kernel/generic/include/adt/list.h@ 516adce

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

use [u]int{8|16|32|64}_t type definitions as detected by the autotool
replace direct usage of arch/types.h with typedefs.h

  • Property mode set to 100644
File size: 5.6 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
[7ac426e]38#include <typedefs.h>
[f761f1eb]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 */
[80bcaed]50#define LIST_INITIALIZE(name) \
51 link_t name = { .prev = &name, .next = &name }
[7dd2561]52
[40a468a]53/** Initialize doubly-linked circular list link
54 *
55 * Initialize doubly-linked list link.
56 *
57 * @param link Pointer to link_t structure to be initialized.
58 */
[c0a91d1]59static inline void link_initialize(link_t *link)
60{
61 link->prev = NULL;
62 link->next = NULL;
[f761f1eb]63}
64
[40a468a]65/** Initialize doubly-linked circular list
66 *
67 * Initialize doubly-linked circular list.
68 *
69 * @param head Pointer to link_t structure representing head of the list.
70 */
[c0a91d1]71static inline void list_initialize(link_t *head)
72{
73 head->prev = head;
74 head->next = head;
[f761f1eb]75}
76
[40a468a]77/** Add item to the beginning of doubly-linked circular list
78 *
79 * Add item to the beginning of doubly-linked circular list.
80 *
81 * @param link Pointer to link_t structure to be added.
82 * @param head Pointer to link_t structure representing head of the list.
83 */
[c0a91d1]84static inline void list_prepend(link_t *link, link_t *head)
85{
86 link->next = head->next;
87 link->prev = head;
88 head->next->prev = link;
89 head->next = link;
[f761f1eb]90}
91
[40a468a]92/** Add item to the end of doubly-linked circular list
93 *
94 * Add item to the end of doubly-linked circular list.
95 *
96 * @param link Pointer to link_t structure to be added.
97 * @param head Pointer to link_t structure representing head of the list.
98 */
[c0a91d1]99static inline void list_append(link_t *link, link_t *head)
100{
101 link->prev = head->prev;
102 link->next = head;
103 head->prev->next = link;
104 head->prev = link;
[f761f1eb]105}
106
[40a468a]107/** Remove item from doubly-linked circular list
108 *
109 * Remove item from doubly-linked circular list.
110 *
[80bcaed]111 * @param link Pointer to link_t structure to be removed from the list it is
112 * contained in.
[40a468a]113 */
[c0a91d1]114static inline void list_remove(link_t *link)
115{
116 link->next->prev = link->prev;
117 link->prev->next = link->next;
118 link_initialize(link);
[f761f1eb]119}
120
[40a468a]121/** Query emptiness of doubly-linked circular list
122 *
123 * Query emptiness of doubly-linked circular list.
124 *
125 * @param head Pointer to link_t structure representing head of the list.
126 */
127static inline bool list_empty(link_t *head)
128{
129 return head->next == head ? true : false;
130}
131
132
133/** Split or concatenate headless doubly-linked circular list
134 *
135 * Split or concatenate headless doubly-linked circular list.
136 *
137 * Note that the algorithm works both directions:
138 * concatenates splitted lists and splits concatenated lists.
139 *
[80bcaed]140 * @param part1 Pointer to link_t structure leading the first (half of the
141 * headless) list.
142 * @param part2 Pointer to link_t structure leading the second (half of the
143 * headless) list.
[40a468a]144 */
145static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
146{
147 link_t *hlp;
148
149 part1->prev->next = part2;
150 part2->prev->next = part1;
151 hlp = part1->prev;
152 part1->prev = part2->prev;
153 part2->prev = hlp;
154}
155
156
157/** Split headless doubly-linked circular list
158 *
159 * Split headless doubly-linked circular list.
160 *
[80bcaed]161 * @param part1 Pointer to link_t structure leading the first half of the
162 * headless list.
163 * @param part2 Pointer to link_t structure leading the second half of the
164 * headless list.
[40a468a]165 */
166static inline void headless_list_split(link_t *part1, link_t *part2)
167{
168 headless_list_split_or_concat(part1, part2);
169}
170
171/** Concatenate two headless doubly-linked circular lists
172 *
173 * Concatenate two headless doubly-linked circular lists.
174 *
175 * @param part1 Pointer to link_t structure leading the first headless list.
[80bcaed]176 * @param part2 Pointer to link_t structure leading the second headless list.
[40a468a]177 */
178static inline void headless_list_concat(link_t *part1, link_t *part2)
179{
180 headless_list_split_or_concat(part1, part2);
181}
[f761f1eb]182
[ea7890e7]183#define list_get_instance(link, type, member) \
[80bcaed]184 ((type *)(((uint8_t *)(link)) - ((uint8_t *)&(((type *)NULL)->member))))
[f761f1eb]185
[45671f48]186extern bool list_member(const link_t *link, const link_t *head);
[f761f1eb]187extern void list_concat(link_t *head1, link_t *head2);
188
189#endif
[b45c443]190
[06e1e95]191/** @}
[b45c443]192 */
Note: See TracBrowser for help on using the repository browser.