diff options
author | Beniamino Galvani <bgalvani@redhat.com> | 2015-06-21 20:27:04 +0200 |
---|---|---|
committer | Beniamino Galvani <bgalvani@redhat.com> | 2015-10-02 17:11:40 +0200 |
commit | 2212d76d08f3bc34c683aed1a6736325b841625c (patch) | |
tree | 15d61c2c8ad6f0eb807df35c7ee182dd859ee575 /src/libsystemd-network/lldp-tlv.c | |
parent | 3c2f5a543de9cc3d958920b3bd18dc8657dc201f (diff) |
lldp: add reference counter to struct tlv_packet
Add a reference counter to the tlv_packet structure so that it can be
shared between multiple users and properly free'd when no longer in
use.
Diffstat (limited to 'src/libsystemd-network/lldp-tlv.c')
-rw-r--r-- | src/libsystemd-network/lldp-tlv.c | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/src/libsystemd-network/lldp-tlv.c b/src/libsystemd-network/lldp-tlv.c index 0cea5b10a6..1370d217f4 100644 --- a/src/libsystemd-network/lldp-tlv.c +++ b/src/libsystemd-network/lldp-tlv.c @@ -54,22 +54,41 @@ int tlv_packet_new(tlv_packet **ret) { return -ENOMEM; LIST_HEAD_INIT(m->sections); + m->n_ref = 1; *ret = m; return 0; } -void tlv_packet_free(tlv_packet *m) { +tlv_packet *tlv_packet_ref(tlv_packet *m) { + + if (!m) + return NULL; + + assert(m->n_ref > 0); + m->n_ref++; + + return m; +} + +tlv_packet *tlv_packet_unref(tlv_packet *m) { tlv_section *s, *n; if (!m) - return; + return NULL; + + assert(m->n_ref > 0); + m->n_ref--; + + if (m->n_ref > 0) + return m; LIST_FOREACH_SAFE(section, s, n, m->sections) tlv_section_free(s); free(m); + return NULL; } int tlv_packet_append_bytes(tlv_packet *m, const void *data, size_t data_length) { |