network buffers packets reference counting - linino/zephyr GitHub Wiki
Since this is a difficult topic, I'll write down here what I find out.
-
net_buf_ref(buf): increments reference count of buf, leaves any fragments untouched
-
net_buf_unref(buf): decrements reference count of buf. If > 0, returns leaving untouched any fragments. If 0 (or < 0, theoretically), gets next fragment and unrefs it. Goes on until there are no more fragments or a fragment with ref > 0 (post decrement) is found.
-
net_pkt_ref(pkt): increments packet's reference count and returns.
-
net_pkt_unref(pkt): decrements packet's reference count and returns if the resulting value is greater_than_0 (pre-decrement value greater than 1). Otherwise calls net_pkt_frag_unref(pkt->frags) and then frees the packet.
-
net_pkt_frag_ref(frag): equivalent to calling net_buf_ref(frag)
-
net_pkt_frag_unref(frag): equivalent to calling net_buf_unref(frag)