[Krafton Jungle] PintOS 2.0.0
크래프톤 정글 PintOS
 
Loading...
Searching...
No Matches
list.h
Go to the documentation of this file.
1#ifndef __LIB_KERNEL_LIST_H
2#define __LIB_KERNEL_LIST_H
3
4/* Doubly linked list.
5 *
6 * This implementation of a doubly linked list does not require
7 * use of dynamically allocated memory. Instead, each structure
8 * that is a potential list element must embed a struct list_elem
9 * member. All of the list functions operate on these `struct
10 * list_elem's. The list_entry macro allows conversion from a
11 * struct list_elem back to a structure object that contains it.
12
13 * For example, suppose there is a needed for a list of `struct
14 * foo'. `struct foo' should contain a `struct list_elem'
15 * member, like so:
16
17 * struct foo {
18 * struct list_elem elem;
19 * int bar;
20 * ...other members...
21 * };
22
23 * Then a list of `struct foo' can be be declared and initialized
24 * like so:
25
26 * struct list foo_list;
27
28 * list_init (&foo_list);
29
30 * Iteration is a typical situation where it is necessary to
31 * convert from a struct list_elem back to its enclosing
32 * structure. Here's an example using foo_list:
33
34 * struct list_elem *e;
35
36 * for (e = list_begin (&foo_list); e != list_end (&foo_list);
37 * e = list_next (e)) {
38 * struct foo *f = list_entry (e, struct foo, elem);
39 * ...do something with f...
40 * }
41
42 * You can find real examples of list usage throughout the
43 * source; for example, malloc.c, palloc.c, and thread.c in the
44 * threads directory all use lists.
45
46 * The interface for this list is inspired by the list<> template
47 * in the C++ STL. If you're familiar with list<>, you should
48 * find this easy to use. However, it should be emphasized that
49 * these lists do *no* type checking and can't do much other
50 * correctness checking. If you screw up, it will bite you.
51
52 * Glossary of list terms:
53
54 * - "front": The first element in a list. Undefined in an
55 * empty list. Returned by list_front().
56
57 * - "back": The last element in a list. Undefined in an empty
58 * list. Returned by list_back().
59
60 * - "tail": The element figuratively just after the last
61 * element of a list. Well defined even in an empty list.
62 * Returned by list_end(). Used as the end sentinel for an
63 * iteration from front to back.
64
65 * - "beginning": In a non-empty list, the front. In an empty
66 * list, the tail. Returned by list_begin(). Used as the
67 * starting point for an iteration from front to back.
68
69 * - "head": The element figuratively just before the first
70 * element of a list. Well defined even in an empty list.
71 * Returned by list_rend(). Used as the end sentinel for an
72 * iteration from back to front.
73
74 * - "reverse beginning": In a non-empty list, the back. In an
75 * empty list, the head. Returned by list_rbegin(). Used as
76 * the starting point for an iteration from back to front.
77 *
78 * - "interior element": An element that is not the head or
79 * tail, that is, a real list element. An empty list does
80 * not have any interior elements.*/
81
82#include <stdbool.h>
83#include <stddef.h>
84#include <stdint.h>
85
86/* List element. */
87struct list_elem {
88 struct list_elem *prev; /* Previous list element. */
89 struct list_elem *next; /* Next list element. */
90};
91
92/* List. */
93struct list {
94 struct list_elem head; /* List head. */
95 struct list_elem tail; /* List tail. */
96};
97
98/* Converts pointer to list element LIST_ELEM into a pointer to
99 the structure that LIST_ELEM is embedded inside. Supply the
100 name of the outer structure STRUCT and the member name MEMBER
101 of the list element. See the big comment at the top of the
102 file for an example. */
103#define list_entry(LIST_ELEM, STRUCT, MEMBER) \
104 ((STRUCT *) ((uint8_t *) &(LIST_ELEM)->next \
105 - offsetof (STRUCT, MEMBER.next)))
106
107void list_init (struct list *);
108
109/* List traversal. */
110struct list_elem *list_begin (struct list *);
111struct list_elem *list_next (struct list_elem *);
112struct list_elem *list_end (struct list *);
113
114struct list_elem *list_rbegin (struct list *);
115struct list_elem *list_prev (struct list_elem *);
116struct list_elem *list_rend (struct list *);
117
118struct list_elem *list_head (struct list *);
119struct list_elem *list_tail (struct list *);
120
121/* List insertion. */
122void list_insert (struct list_elem *, struct list_elem *);
123void list_splice (struct list_elem *before,
124 struct list_elem *first, struct list_elem *last);
125void list_push_front (struct list *, struct list_elem *);
126void list_push_back (struct list *, struct list_elem *);
127
128/* List removal. */
129struct list_elem *list_remove (struct list_elem *);
130struct list_elem *list_pop_front (struct list *);
131struct list_elem *list_pop_back (struct list *);
132
133/* List elements. */
134struct list_elem *list_front (struct list *);
135struct list_elem *list_back (struct list *);
136
137/* List properties. */
138size_t list_size (struct list *);
139bool list_empty (struct list *);
140
141/* Miscellaneous. */
142void list_reverse (struct list *);
143
144/* Compares the value of two list elements A and B, given
145 auxiliary data AUX. Returns true if A is less than B, or
146 false if A is greater than or equal to B. */
147typedef bool list_less_func (const struct list_elem *a,
148 const struct list_elem *b,
149 void *aux);
150
151/* Operations on lists with ordered elements. */
152void list_sort (struct list *,
153 list_less_func *, void *aux);
154void list_insert_ordered (struct list *, struct list_elem *,
155 list_less_func *, void *aux);
156void list_unique (struct list *, struct list *duplicates,
157 list_less_func *, void *aux);
158
159/* Max and min. */
160struct list_elem *list_max (struct list *, list_less_func *, void *aux);
161struct list_elem *list_min (struct list *, list_less_func *, void *aux);
162
163#endif /* lib/kernel/list.h */
struct list_elem * list_pop_front(struct list *)
Definition: list.c:251
bool list_less_func(const struct list_elem *a, const struct list_elem *b, void *aux)
Definition: list.h:147
struct list_elem * list_min(struct list *, list_less_func *, void *aux)
Definition: list.c:479
void list_sort(struct list *, list_less_func *, void *aux)
Definition: list.c:381
void list_init(struct list *)
Definition: list.c:58
struct list_elem * list_begin(struct list *)
Definition: list.c:68
void list_insert_ordered(struct list *, struct list_elem *, list_less_func *, void *aux)
Definition: list.c:419
struct list_elem * list_head(struct list *)
Definition: list.c:141
void list_push_back(struct list *, struct list_elem *)
Definition: list.c:202
struct list_elem * list_prev(struct list_elem *)
Definition: list.c:105
struct list_elem * list_rbegin(struct list *)
Definition: list.c:96
struct list_elem * list_next(struct list_elem *)
Definition: list.c:77
struct list_elem * list_tail(struct list *)
Definition: list.c:148
void list_push_front(struct list *, struct list_elem *)
Definition: list.c:195
void list_splice(struct list_elem *before, struct list_elem *first, struct list_elem *last)
Definition: list.c:171
struct list_elem * list_rend(struct list *)
Definition: list.c:124
void list_insert(struct list_elem *, struct list_elem *)
Definition: list.c:157
struct list_elem * list_remove(struct list_elem *)
Definition: list.c:241
bool list_empty(struct list *)
Definition: list.c:296
void list_unique(struct list *, struct list *duplicates, list_less_func *, void *aux)
Definition: list.c:438
struct list_elem * list_max(struct list *, list_less_func *, void *aux)
Definition: list.c:462
size_t list_size(struct list *)
Definition: list.c:285
struct list_elem * list_front(struct list *)
Definition: list.c:269
struct list_elem * list_end(struct list *)
Definition: list.c:88
struct list_elem * list_back(struct list *)
Definition: list.c:277
void list_reverse(struct list *)
Definition: list.c:310
struct list_elem * list_pop_back(struct list *)
Definition: list.c:260
Definition: list.h:87
struct list_elem * next
Definition: list.h:89
struct list_elem * prev
Definition: list.h:88
Definition: list.h:93
struct list_elem tail
Definition: list.h:95
struct list_elem head
Definition: list.h:94