From: Gerhard Sittig Date: Sat, 13 Oct 2018 06:16:18 +0000 (+0200) Subject: device: introduce common sr_channel_free() support code X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=fe71c7e42ea442d7706c7dab4d98c2c9f1658537;p=libsigrok.git device: introduce common sr_channel_free() support code There was the sr_channel_new() allocation routine, but releasing that allocation was open-coded in call sites. Add the sr_channel_free() routine for code re-use and consistency. --- diff --git a/src/device.c b/src/device.c index 99ccd42f..dbbb2e26 100644 --- a/src/device.c +++ b/src/device.c @@ -73,6 +73,30 @@ SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi, return ch; } +/** + * Release a previously allocated struct sr_channel. + * + * @param[in] ch Pointer to struct sr_channel. + * + * @private + */ +SR_PRIV void sr_channel_free(struct sr_channel *ch) +{ + if (!ch) + return; + g_free(ch->name); + g_free(ch->priv); + g_free(ch); +} + +/** + * Wrapper around @ref sr_channel_free(), suitable for glib iterators. + */ +SR_PRIV void sr_channel_free_cb(void *p) +{ + sr_channel_free(p); +} + /** * Set the name of the specified channel. * @@ -364,9 +388,7 @@ SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi) for (l = sdi->channels; l; l = l->next) { ch = l->data; - g_free(ch->name); - g_free(ch->priv); - g_free(ch); + sr_channel_free(ch); } g_slist_free(sdi->channels); diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index f3e4d335..6f43e8da 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -782,6 +782,8 @@ enum { SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi, int index, int type, gboolean enabled, const char *name); +SR_PRIV void sr_channel_free(struct sr_channel *ch); +SR_PRIV void sr_channel_free_cb(void *p); SR_PRIV struct sr_channel *sr_next_enabled_channel(const struct sr_dev_inst *sdi, struct sr_channel *cur_channel);