mtree_dup(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

🌸 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 β€” the GFP_FLAGS to 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:


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