• Thomas Haller's avatar
    device/trivial: add comment about lifetime of "kind" in tc_commit() · 6d9030ac
    Thomas Haller authored
    In general, all fields of public NMPlatform* structs must be
    plain/simple. Meaning: copying the struct must be possible without
    caring about cloning/duplicating memory.
    In other words, if there are fields which lifetime is limited,
    then these fields cannot be inside the public part NMPlatform*.
    
    That is why
    
      - "NMPlatformLink.kind", "NMPlatformQdisc.kind", "NMPlatformTfilter.kind"
        are set by platform code to an interned string (g_intern_string())
        that has a static lifetime.
    
      - the "ingress_qos_map" field is inside the ref-counted struct NMPObjectLnkVlan
        and not NMPlatformLnkVlan. This field requires managing the lifetime
        of the array and NMPlatformLnkVlan cannot provide that.
    
    See also for example NMPClass.cmd_obj_copy() which can deep-copy an object.
    But this is only suitable for fields in NMPObject*. The purpose of this
    rule is that you always can safely copy a NMPlatform* struct without
    worrying about the ownership and lifetime of the fields (the field's
    lifetime is unlimited).
    
    This rule and managing of resource lifetime is the main reason for the
    NMPlatform*/NMPObject* split. NMPlatform* structs simply have no mechanism
    for copying/releasing fields, that is why the NMPObject* counterpart exists
    (which is ref-counted and has a copy and destructor function).
    
    This is violated in tc_commit() for the "kind" strings. The lifetime
    of these strings is tied to the setting instance.
    
    We cannot intern the strings (because these are arbitrary strings
    and interned strings are leaked indefinitely). We also cannot g_strdup()
    the strings, because NMPlatform* is not supposed to own strings.
    
    So, just add comments that warn about this ugliness.
    
    The more correct solution would be to move the "kind" fields inside
    NMPObjectQdisc and NMPObjectTfilter, but that is a lot of extra effort.
    
    (cherry picked from commit f2ae994b)
    6d9030ac
nm-platform.c 262 KB