Documentation/core-api/list.rst
Source file repositories/reference/linux-study-clean/Documentation/core-api/list.rst
File Facts
- System
- Linux kernel
- Corpus path
Documentation/core-api/list.rst- Extension
.rst- Size
- 27016 bytes
- Lines
- 786
- Domain
- Support Tooling And Documentation
- Bucket
- Documentation
- Inferred role
- Support Tooling And Documentation: documentation
- Status
- atlas-only
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
struct clownstruct clown_carfunction circus_initfunction list_addfunction circus_fill_carfunction circus_get_max_shoe_sizefunction list_for_eachfunction list_entryfunction list_for_each_entryfunction list_entryfunction circus_fill_carfunction circus_eject_insufficient_clownsfunction list_for_each_entry_safefunction managementfunction circus_retire_clownsfunction list_movefunction circus_clowns_exit_carfunction list_bulk_move_tailfunction circus_clowns_swapfunction list_splice
Annotated Snippet
struct clown {
unsigned long long shoe_size;
const char *name;
struct list_head node; /* the aforementioned member */
};
This may be an unfamiliar approach to some, as the classical explanation of a
linked list is a list node data structure with pointers to the previous and next
list node, as well the payload data. Linux chooses this approach because it
allows for generic list modification code regardless of what data structure is
contained within the list. Since the struct list_head member is not a pointer
but part of the data structure proper, the container_of() pattern can be used by
the list implementation to access the payload data regardless of its type, while
staying oblivious to what said type actually is.
Declaring and initializing a list
---------------------------------
A doubly-linked list can then be declared as just another struct list_head,
and initialized with the LIST_HEAD_INIT() macro during initial assignment, or
with the INIT_LIST_HEAD() function later:
.. code-block:: c
struct clown_car {
int tyre_pressure[4];
struct list_head clowns; /* Looks like a node! */
};
/* ... Somewhere later in our driver ... */
static int circus_init(struct circus_priv *circus)
{
struct clown_car other_car = {
.tyre_pressure = {10, 12, 11, 9},
.clowns = LIST_HEAD_INIT(other_car.clowns)
};
INIT_LIST_HEAD(&circus->car.clowns);
return 0;
}
A further point of confusion to some may be that the list itself doesn't really
have its own type. The concept of the entire linked list and a
struct list_head member that points to other entries in the list are one and
the same.
Adding nodes to the list
------------------------
Adding a node to the linked list is done through the list_add() macro.
We'll return to our clown car example to illustrate how nodes get added to the
list:
.. code-block:: c
static int circus_fill_car(struct circus_priv *circus)
{
struct clown_car *car = &circus->car;
struct clown *grock;
struct clown *dimitri;
/* State 1 */
grock = kzalloc_obj(*grock);
if (!grock)
return -ENOMEM;
grock->name = "Grock";
Annotation
- Detected declarations: `struct clown`, `struct clown_car`, `function circus_init`, `function list_add`, `function circus_fill_car`, `function circus_get_max_shoe_size`, `function list_for_each`, `function list_entry`, `function list_for_each_entry`, `function list_entry`.
- Atlas domain: Support Tooling And Documentation / Documentation.
- Implementation status: atlas-only.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.