Skip to content

Commit 1389f79

Browse files
committed
Refine the comments for List API
This circular doubly-linked list mimics the Linux kernel's list.h API. The Linux kernel's list API is a battle-tested, elegant design for managing linked lists, widely admired for its simplicity and efficiency. However, the documentation should be improved with additional examples and refined to clarify distinct behaviors. Change-Id: If1badbc7f2f6f497be476e971de0c16927be154c
1 parent 15b4c4f commit 1389f79

File tree

2 files changed

+56
-47
lines changed

2 files changed

+56
-47
lines changed

list.h

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,19 @@ extern "C" {
2424
#endif
2525

2626
/**
27-
* struct list_head - Head and node of a doubly-linked list
28-
* @prev: pointer to the previous node in the list
29-
* @next: pointer to the next node in the list
27+
* struct list_head - Node structure for a circular doubly-linked list
28+
* @next: Pointer to the next node in the list.
29+
* @prev: Pointer to the previous node in the list.
3030
*
31-
* The simple doubly-linked list consists of a head and nodes attached to
32-
* this head. Both node and head share the same struct type. The list_*
33-
* functions and macros can be used to access and modify this data structure.
31+
* Defines both the head and nodes of a circular doubly-linked list. The head's
32+
* @next points to the first node and @prev to the last node; in an empty list,
33+
* both point to the head itself. All nodes, including the head, share this
34+
* structure type.
3435
*
35-
* The @prev pointer of the list head points to the last list node of the
36-
* list and @next points to the first list node of the list. For an empty list,
37-
* both member variables point to the head.
38-
*
39-
* The list nodes are usually embedded in a container structure which holds the
40-
* actual data. Such container structure is called entry. The helper list_entry
41-
* can be used to calculate the structure address from the address of the node.
36+
* Nodes are typically embedded within a container structure holding actual
37+
* data, accessible via the list_entry() helper, which computes the container's
38+
* address from a node pointer. The list_* functions and macros provide an API
39+
* for manipulating this data structure efficiently.
4240
*/
4341
struct list_head {
4442
struct list_head *prev;
@@ -67,25 +65,23 @@ struct list_head {
6765
#endif
6866

6967
/**
70-
* LIST_HEAD - Declare list head and initialize it
68+
* LIST_HEAD - Define and initialize a circular list head
7169
* @head: name of the new list
7270
*/
7371
#define LIST_HEAD(head) struct list_head head = {&(head), &(head)}
7472

7573
/**
7674
* INIT_LIST_HEAD() - Initialize empty list head
77-
* @head: pointer to list head
78-
*
79-
* This can also be used to initialize a unlinked list node.
75+
* @head: Pointer to the list_head structure to initialize.
8076
*
81-
* A node is usually linked inside a list, will be added to a list in
82-
* the near future or the entry containing the node will be free'd soon.
77+
* It sets both @next and @prev to point to the structure itself. The
78+
* initialization applies to either a list head or an unlinked node that is
79+
* not yet part of a list.
8380
*
84-
* But an unlinked node may be given to a function which uses list_del(_init)
85-
* before it ends up in a previously mentioned state. The list_del(_init) on an
86-
* initialized node is well defined and safe. But the result of a
87-
* list_del(_init) on an uninitialized node is undefined (unrelated memory is
88-
* modified, crashes, ...).
81+
* Unlinked nodes may be passed to functions using 'list_del()' or
82+
* 'list_del_init()', which are safe only on initialized nodes. Applying these
83+
* operations to an uninitialized node results in undefined behavior, such as
84+
* memory corruption or crashes.
8985
*/
9086
static inline void INIT_LIST_HEAD(struct list_head *head)
9187
{
@@ -94,9 +90,13 @@ static inline void INIT_LIST_HEAD(struct list_head *head)
9490
}
9591

9692
/**
97-
* list_add() - Add a list node to the beginning of the list
98-
* @node: pointer to the new node
99-
* @head: pointer to the head of the list
93+
* list_add - Insert a node at the beginning of a circular list
94+
* @node: Pointer to the list_head structure to add.
95+
* @head: Pointer to the list_head structure representing the list head.
96+
*
97+
* Adds the specified @node immediately after @head in a circular doubly-linked
98+
* list, effectively placing it at the beginning. The existing first node, if
99+
* any, shifts to follow @node, and the list's circular structure is maintained.
100100
*/
101101
static inline void list_add(struct list_head *node, struct list_head *head)
102102
{
@@ -124,20 +124,22 @@ static inline void list_add_tail(struct list_head *node, struct list_head *head)
124124
}
125125

126126
/**
127-
* list_del() - Remove a list node from the list
128-
* @node: pointer to the node
127+
* list_del - Remove a node from a circular doubly-linked list
128+
* @node: Pointer to the list_head structure to remove.
129129
*
130-
* The node is only removed from the list. Neither the memory of the removed
131-
* node nor the memory of the entry containing the node is free'd. The node
132-
* has to be handled like an uninitialized node. Accessing the next or prev
133-
* pointer of the node is not safe.
130+
* Removes @node from its list by updating the adjacent nodes’ pointers to
131+
* bypass it. The node’s memory and its containing structure, if any, are not
132+
* freed. After removal, @node is left unlinked and should be treated as
133+
* uninitialized; accessing its @next or @prev pointers is unsafe and may cause
134+
* undefined behavior.
134135
*
135-
* Unlinked, initialized nodes are also uninitialized after list_del.
136+
* Even previously initialized but unlinked nodes become uninitialized after
137+
* this operation. To reintegrate @node into a list, it must be reinitialized
138+
* (e.g., via INIT_LIST_HEAD).
136139
*
137-
* LIST_POISONING can be enabled during build-time to provoke an invalid memory
138-
* access when the memory behind the next/prev pointer is used after a list_del.
139-
* This only works on systems which prohibit access to the predefined memory
140-
* addresses.
140+
* If LIST_POISONING is enabled at build time, @next and @prev are set to
141+
* invalid addresses to trigger memory access faults on misuse. This feature is
142+
* effective only on systems that restrict access to these specific addresses.
141143
*/
142144
static inline void list_del(struct list_head *node)
143145
{
@@ -154,11 +156,13 @@ static inline void list_del(struct list_head *node)
154156
}
155157

156158
/**
157-
* list_del_init() - Remove a list node from the list and reinitialize it
158-
* @node: pointer to the node
159+
* list_del_init - Remove a node and reinitialize it as unlinked
160+
* @node: Pointer to the list_head structure to remove and reinitialize.
159161
*
160-
* The removed node will not end up in an uninitialized state like when using
161-
* list_del. Instead the node is initialized again to the unlinked state.
162+
* Removes @node from its circular doubly-linked list using list_del() and then
163+
* reinitializes it as an unlinked node via INIT_LIST_HEAD(). Unlike list_del(),
164+
* which leaves the node uninitialized, this ensures @node is safely reset to an
165+
* empty, standalone state with @next and @prev pointing to itself.
162166
*/
163167
static inline void list_del_init(struct list_head *node)
164168
{
@@ -167,10 +171,14 @@ static inline void list_del_init(struct list_head *node)
167171
}
168172

169173
/**
170-
* list_empty() - Check if list head has no nodes attached
171-
* @head: pointer to the head of the list
174+
* list_empty - Test if a circular list has no nodes
175+
* @head: Pointer to the list_head structure representing the list head.
176+
*
177+
* Checks whether the circular doubly-linked list headed by @head is empty.
178+
* A list is empty if @head’s @next points to itself, indicating no nodes are
179+
* attached.
172180
*
173-
* Return: 0 - list is not empty !0 - list is empty
181+
* Returns: 0 if the list has nodes, non-zero if the list is empty.
174182
*/
175183
static inline int list_empty(const struct list_head *head)
176184
{
@@ -181,7 +189,8 @@ static inline int list_empty(const struct list_head *head)
181189
* list_is_singular() - Check if list head has exactly one node attached
182190
* @head: pointer to the head of the list
183191
*
184-
* Return: 0 - list is not singular !0 -list has exactly one entry
192+
* Returns: 0 if the list is not singular, non-zero if the list has exactly one
193+
* entry.
185194
*/
186195
static inline int list_is_singular(const struct list_head *head)
187196
{

scripts/checksums

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
db6784ff3917888db4d1dceaa0570d99ed40e762 queue.h
2-
53de5d72c52d63b706c7060c6020077f5ac59663 list.h
2+
a99303fdca46c2121c61f7b96346de55226d0086 list.h
33
3bb0192cee08d165fd597a9f6fbb404533e28fcf scripts/check-commitlog.sh

0 commit comments

Comments
 (0)