lib/assoc_array.c
Source file repositories/reference/linux-study-clean/lib/assoc_array.c
File Facts
- System
- Linux kernel
- Corpus path
lib/assoc_array.c- Extension
.c- Size
- 52883 bytes
- Lines
- 1724
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: implementation source
- Status
- source implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- 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
linux/rcupdate.hlinux/slab.hlinux/err.hlinux/assoc_array_priv.h
Detected Declarations
struct assoc_array_walk_resultstruct assoc_array_delete_collapse_contextenum assoc_array_walk_statusfunction Copyrightfunction assoc_array_iteratefunction assoc_array_walkfunction assoc_array_destroy_subtreefunction assoc_array_destroyfunction assoc_array_insert_in_empty_treefunction assoc_array_insert_into_terminal_nodefunction assoc_array_insert_mid_shortcutfunction assoc_array_apply_editfunction assoc_array_insert_set_objectfunction assoc_array_delete_collapse_iteratorfunction assoc_array_apply_editfunction assoc_array_apply_editfunction assoc_array_rcu_cleanupfunction assoc_array_apply_editfunction assoc_array_cancel_editfunction count
Annotated Snippet
struct assoc_array_walk_result {
struct {
struct assoc_array_node *node; /* Node in which leaf might be found */
int level;
int slot;
} terminal_node;
struct {
struct assoc_array_shortcut *shortcut;
int level;
int sc_level;
unsigned long sc_segments;
unsigned long dissimilarity;
} wrong_shortcut;
};
/*
* Navigate through the internal tree looking for the closest node to the key.
*/
static enum assoc_array_walk_status
assoc_array_walk(const struct assoc_array *array,
const struct assoc_array_ops *ops,
const void *index_key,
struct assoc_array_walk_result *result)
{
struct assoc_array_shortcut *shortcut;
struct assoc_array_node *node;
struct assoc_array_ptr *cursor, *ptr;
unsigned long sc_segments, dissimilarity;
unsigned long segments;
int level, sc_level, next_sc_level;
int slot;
pr_devel("-->%s()\n", __func__);
cursor = READ_ONCE(array->root); /* Address dependency. */
if (!cursor)
return assoc_array_walk_tree_empty;
level = 0;
/* Use segments from the key for the new leaf to navigate through the
* internal tree, skipping through nodes and shortcuts that are on
* route to the destination. Eventually we'll come to a slot that is
* either empty or contains a leaf at which point we've found a node in
* which the leaf we're looking for might be found or into which it
* should be inserted.
*/
jumped:
segments = ops->get_key_chunk(index_key, level);
pr_devel("segments[%d]: %lx\n", level, segments);
if (assoc_array_ptr_is_shortcut(cursor))
goto follow_shortcut;
consider_node:
node = assoc_array_ptr_to_node(cursor);
slot = segments >> (level & ASSOC_ARRAY_KEY_CHUNK_MASK);
slot &= ASSOC_ARRAY_FAN_MASK;
ptr = READ_ONCE(node->slots[slot]); /* Address dependency. */
pr_devel("consider slot %x [ix=%d type=%lu]\n",
slot, level, (unsigned long)ptr & 3);
if (!assoc_array_ptr_is_meta(ptr)) {
/* The node doesn't have a node/shortcut pointer in the slot
* corresponding to the index key that we have to follow.
*/
result->terminal_node.node = node;
result->terminal_node.level = level;
result->terminal_node.slot = slot;
pr_devel("<--%s() = terminal_node\n", __func__);
return assoc_array_walk_found_terminal_node;
}
if (assoc_array_ptr_is_node(ptr)) {
/* There is a pointer to a node in the slot corresponding to
* this index key segment, so we need to follow it.
*/
cursor = ptr;
level += ASSOC_ARRAY_LEVEL_STEP;
if ((level & ASSOC_ARRAY_KEY_CHUNK_MASK) != 0)
goto consider_node;
goto jumped;
}
/* There is a shortcut in the slot corresponding to the index key
* segment. We follow the shortcut if its partial index key matches
* this leaf's. Otherwise we need to split the shortcut.
*/
cursor = ptr;
Annotation
- Immediate include surface: `linux/rcupdate.h`, `linux/slab.h`, `linux/err.h`, `linux/assoc_array_priv.h`.
- Detected declarations: `struct assoc_array_walk_result`, `struct assoc_array_delete_collapse_context`, `enum assoc_array_walk_status`, `function Copyright`, `function assoc_array_iterate`, `function assoc_array_walk`, `function assoc_array_destroy_subtree`, `function assoc_array_destroy`, `function assoc_array_insert_in_empty_tree`, `function assoc_array_insert_into_terminal_node`.
- Atlas domain: Kernel Services / lib.
- Implementation status: source implementation candidate.
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.