mtree_dup(9)
πΈ mtree_dup(9) πΈ
okay bestie, welcome to the Kernel API Manual β today's episode is mtree_dup, a function so extra it literally clones a whole tree in one go. no cap. π
β¨ NAME β¨
mtree_dup β duplicate an entire maple tree π (yes, the whole thing, every last node)
π«Ά SYNOPSIS π«Ά
int mtree_dup (struct maple_tree *mt , struct maple_tree *new , gfp_t
gfp );
literally the whole flex is right there β¬οΈ
π ARGUMENTS π
mtβ the source maple tree π the one you're copying from, the original, the main character.newβ the new maple tree β¨ the fresh one, the blank canvas, the destination.gfpβ theGFP_FLAGSto use for allocations π i.e. how the kernel should go about grabbing memory for this whole operation.
π₯ DESCRIPTION π₯
so here's the tea β β this function duplicates a maple tree in Depth-First Search (DFS) pre-order traversal. That's the vibe check: it dives all the way down first, and it does the parent before the kids. π§
It uses memcpy to copy nodes in the source tree, and it allocates brand new child nodes in non-leaf nodes. The new node is exactly the same as the source node β like, identical twin energy β except for all the addresses stored in it. those get swapped out, obviously, because it's a whole new tree. π
the way this is faster than traversing all elements in the source tree and inserting them one by one into the new treeβ¦ iconic. it just ate. π₯
β οΈ but bestie, listen β you (the user) need to make sure the attributes of the source tree and the new tree are the same, and the new tree needs to be an empty tree. otherwise you get -EINVAL thrown right back at you. π no exceptions, fr fr.
β RETURN β
0β success, we slayed π-ENOMEMβ memory could not be allocated π (the allocator said no)-EINVALβ the attributes of the two trees are different, or the new tree is not an empty tree π«
π¦ SEE ALSO π¦
all the friends in the kernel file ./lib/maple_tree.c β go say hi, they're all linked up:
mas_prealloc_calc(9)mas_wr_preallocate(9)mas_insert(9)mas_alloc_cyclic(9)mas_walk(9)mte_dead_walk(9)mt_free_walk(9)mas_store(9)mas_store_gfp(9)mas_store_prealloc(9)mas_preallocate(9)mas_next(9)mas_next_range(9)mt_next(9)mas_prev(9)mas_prev_range(9)mt_prev(9)mas_pause(9)mas_find_setup(9)mas_find(9)mas_find_range(9)mas_find_rev_setup(9)mas_find_rev(9)mas_find_range_rev(9)mas_erase(9)mas_nomem(9)mas_nomem_nofail(9)mtree_load(9)mtree_store_range(9)mtree_store(9)mtree_insert_range(9)mtree_insert(9)mtree_alloc_cyclic(9)mtree_erase(9)__mt_dup(9)__mt_destroy(9)mtree_destroy(9)mt_find(9)mt_find_after(9)
and that's the whole page, bestie π«Ά this came out of ./lib, dated October 2026, and it's filed under mtree_dup(9) β go forth and duplicate responsibly β¨π
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms