• Jason A. Donenfeld's avatar
    wireguard: allowedips: free empty intermediate nodes when removing single node · bf7b042d
    Jason A. Donenfeld authored
    When removing single nodes, it's possible that that node's parent is an
    empty intermediate node, in which case, it too should be removed.
    Otherwise the trie fills up and never is fully emptied, leading to
    gradual memory leaks over time for tries that are modified often. There
    was originally code to do this, but was removed during refactoring in
    2016 and never reworked. Now that we have proper parent pointers from
    the previous commits, we can implement this properly.
    
    In order to reduce branching and expensive comparisons, we want to keep
    the double pointer for parent assignment (which lets us easily chain up
    to the root), but we still need to actually get the parent's base
    address. So encode the bit number into the last two bits of the pointer,
    and pack and unpack it as needed. This is a little bit clumsy but is the
    fastest and less memory wasteful of the compromises. Note that we align
    the root struct here to a minimum of 4, b...
    bf7b042d
allowedips.c 19.3 KB