]> sigrok.org Git - libsigrok.git/commitdiff
device: introduce common sr_channel_free() support code
authorGerhard Sittig <redacted>
Sat, 13 Oct 2018 06:16:18 +0000 (08:16 +0200)
committerUwe Hermann <redacted>
Sat, 13 Oct 2018 13:06:43 +0000 (15:06 +0200)
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.

src/device.c
src/libsigrok-internal.h

index 99ccd42f1d9be3b78693a7f626c79e4d2b5010cd..dbbb2e26ac122e446f4c4c4efcf66e16260a5a2a 100644 (file)
@@ -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);
 
index f3e4d335ce2248199b9f2cdda016a3c9021fda5a..6f43e8da78ac63d04c3de550dcd4d9e2b347994e 100644 (file)
@@ -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);