parent(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

✨ parent(9) ✨

okay so bestie, welcome to a lil corner of the Kernel API Manual that is genuinely so cute once you get it πŸŽ€ this is parent(9), and no it's not about your mom β€” it's about heap math. slay.


πŸ’– NAME πŸ’–

parent β€” ok so given the offset of the child, find the offset of the parent. that's it. that's the whole vibe. πŸ¦‹


🌸 SYNOPSIS 🌸

size_t parent (size_t i , unsigned int lsbit , size_t size );

πŸŽ€ ARGUMENTS πŸŽ€

  • i β€” the offset of the heap element whose parent is sought. must be non-zero, no cap. like literally don't pass zero, it's not that kind of party πŸ™ˆ
  • lsbit β€” a precomputed 1-bit mask, equal to "size & -size". yes bestie, precompute it, it's giving prepared ✨
  • size β€” size of each element. the unit. the whole point. πŸ’…

πŸ”₯ DESCRIPTION πŸ”₯

so in terms of array indexes, the parent of element j = i/size is simply (j-1)/2. easy, iconic, no notes. πŸ‘‘

but β€” and here's the plot twist β€” when you're working in byte offsets, we can't use implicit truncation of integer divides. nope. not allowed. the math just doesn't math. 😭

fortunately (say it with me: fortunately πŸ’œ), we only need one bit of the quotient, not the full divide. size has a least significant bit. that bit will be clear if i is an even multiple of size, and set if it's an odd multiple. one bit carrying the whole team fr fr.

logically, we're doing "if (i & lsbit) i -= size;" β€” but since the branch is unpredictable, it's done with a bit of clever branch-free code instead. not me writing branch-free code πŸ’…πŸ”₯ main character optimization energy.

lowkey the whole trick is: unpredictable branches are a vibe check the CPU fails, so we skip the branch entirely. no branch, no problem 🧚

🌈 SEE ALSO 🌈

go read those too, they're all in the same friend group 🫢


⭐ that's the whole page bestie, ily, go find some parents πŸ’–

source: ./lib Β· dated: October 2026 πŸŽ€βœ¨

πŸ“¦ where this page comes from