]> sigrok.org Git - libsigrokdecode.git/commitdiff
srd: free all decoder instances when unloading decoders
authorBert Vermeulen <redacted>
Tue, 31 Jan 2012 22:48:10 +0000 (23:48 +0100)
committerBert Vermeulen <redacted>
Tue, 31 Jan 2012 22:48:10 +0000 (23:48 +0100)
controller.c
decoder.c
sigrokdecode.h

index 20927cf9b80525a32b5b60f960dc60471cbd6734..2355deea96a8ca651d9f55a34dfefae082a36aad 100644 (file)
@@ -599,6 +599,44 @@ int srd_instance_decode(uint64_t start_samplenum,
        return SRD_OK;
 }
 
+void srd_instance_free(struct srd_decoder_instance *di)
+{
+       GSList *l;
+       struct srd_pd_output *pdo;
+
+       srd_dbg("Freeing instance %s", di->instance_id);
+
+       Py_DecRef(di->py_instance);
+       g_free(di->instance_id);
+       g_free(di->dec_probemap);
+       g_slist_free(di->next_di);
+       for (l = di->pd_output; l; l = l->next) {
+               pdo = l->data;
+               g_free(pdo->proto_id);
+               g_free(pdo);
+       }
+       g_slist_free(di->pd_output);
+
+}
+
+void srd_instance_free_all(GSList *stack)
+{
+       GSList *l;
+       struct srd_decoder_instance *di;
+
+       di = NULL;
+       for (l = stack ? stack : di_list; di == NULL && l != NULL; l = l->next) {
+               di = l->data;
+               if (di->next_di)
+                       srd_instance_free_all(di->next_di);
+               srd_instance_free(di);
+       }
+       if (!stack) {
+               g_slist_free(di_list);
+               di_list = NULL;
+       }
+
+}
 
 int srd_session_start(int num_probes, int unitsize, uint64_t samplerate)
 {
index efb1f9ae4bdd8b2ac827a2aec123455178cf53aa..83735a7ec3442ae30a199393b376db0b5a1d6836 100644 (file)
--- a/decoder.c
+++ b/decoder.c
@@ -304,16 +304,45 @@ char *srd_decoder_doc(struct srd_decoder *dec)
 }
 
 
+static void free_probes(GSList *probelist)
+{
+       GSList *l;
+       struct srd_probe *p;
+
+       if (probelist == NULL)
+               return;
+
+       for (l = probelist; l; l = l->next) {
+               p = l->data;
+               g_free(p->id);
+               g_free(p->name);
+               g_free(p->desc);
+               g_free(p);
+       }
+       g_slist_free(probelist);
+
+}
+
 /**
  * Unload decoder module.
  *
- * @param dec The decoder struct to be unloaded.
+ * @param dec The struct srd_decoder to be unloaded.
  *
  * @return SRD_OK upon success, a (negative) error code otherwise.
  */
 int srd_unload_decoder(struct srd_decoder *dec)
 {
 
+       srd_dbg("unloading decoder %s", dec->name);
+
+       /* Since any instances of this decoder need to be released as well,
+        * but they could be anywhere in the stack, just free the entire
+        * stack. A frontend reloading a decoder thus has to restart all
+        * instances, and rebuild the stack. */
+       srd_instance_free_all(NULL);
+
+       free_probes(dec->probes);
+       free_probes(dec->extra_probes);
        g_free(dec->id);
        g_free(dec->name);
        g_free(dec->longname);
@@ -326,7 +355,9 @@ int srd_unload_decoder(struct srd_decoder *dec)
        if (dec->outputformats != NULL)
                g_slist_free(dec->outputformats);
 
+       /* The module's Decoder class. */
        Py_XDECREF(dec->py_dec);
+       /* The module itself. */
        Py_XDECREF(dec->py_mod);
 
        /* TODO: (g_)free dec itself? */
index 2c9cb40128f7b19eb787b73c37f49e286b0f477f..201aea4c0d9a23f2950b40d91981b6060b76c08c 100644 (file)
@@ -192,6 +192,8 @@ struct srd_decoder_instance *srd_instance_find_by_obj(GSList *stack,
 int srd_instance_start(struct srd_decoder_instance *di, PyObject *args);
 int srd_instance_decode(uint64_t start_samplenum,
                struct srd_decoder_instance *dec, uint8_t *inbuf, uint64_t inbuflen);
+void srd_instance_free(struct srd_decoder_instance *di);
+void srd_instance_free_all(GSList *stack);
 int srd_session_start(int num_probes, int unitsize, uint64_t samplerate);
 int srd_session_feed(uint64_t start_samplenum, uint8_t *inbuf, uint64_t inbuflen);
 int pd_add(struct srd_decoder_instance *di, int output_type, char *output_id);