]> sigrok.org Git - libsigrok.git/blobdiff - hardware/demo/demo.c
Fix two small warnings.
[libsigrok.git] / hardware / demo / demo.c
index fb6dd7f23df6dae6ead4406b426d617048ee095b..0ebfd271701e99d146752c6d741a9a2973ef2a1f 100644 (file)
@@ -23,6 +23,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <sigrok.h>
+#include <sigrok-internal.h>
 #ifdef _WIN32
 #include <io.h>
 #include <fcntl.h>
@@ -340,9 +341,11 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        struct sr_datafeed_header *header;
        struct databag *mydata;
 
-       mydata = malloc(sizeof(struct databag));
-       if (!mydata)
+       /* TODO: 'mydata' is never g_free()'d? */
+       if (!(mydata = g_try_malloc(sizeof(struct databag)))) {
+               sr_err("demo: %s: mydata malloc failed", __func__);
                return SR_ERR_MALLOC;
+       }
 
        mydata->sample_generator = default_genmode;
        mydata->session_device_id = session_device_id;
@@ -376,10 +379,15 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        if (!my_thread)
                return SR_ERR;
 
-       packet = malloc(sizeof(struct sr_datafeed_packet));
-       header = malloc(sizeof(struct sr_datafeed_header));
-       if (!packet || !header)
+       if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
+               sr_err("demo: %s: packet malloc failed", __func__);
                return SR_ERR_MALLOC;
+       }
+
+       if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
+               sr_err("demo: %s: header malloc failed", __func__);
+               return SR_ERR_MALLOC;
+       }
 
        packet->type = SR_DF_HEADER;
        packet->length = sizeof(struct sr_datafeed_header);
@@ -391,8 +399,8 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        header->num_logic_probes = NUM_PROBES;
        header->num_analog_probes = 0;
        sr_session_bus(session_device_id, packet);
-       free(header);
-       free(packet);
+       g_free(header);
+       g_free(packet);
 
        return SR_OK;
 }
@@ -408,17 +416,17 @@ static void hw_stop_acquisition(int device_index, gpointer session_device_id)
 }
 
 struct sr_device_plugin demo_plugin_info = {
-       "demo",
-       "Demo driver and pattern generator",
-       1,
-       hw_init,
-       hw_cleanup,
-       hw_opendev,
-       hw_closedev,
-       hw_get_device_info,
-       hw_get_status,
-       hw_get_capabilities,
-       hw_set_configuration,
-       hw_start_acquisition,
-       hw_stop_acquisition,
+       .name = "demo",
+       .longname = "Demo driver and pattern generator",
+       .api_version = 1,
+       .init = hw_init,
+       .cleanup = hw_cleanup,
+       .open = hw_opendev,
+       .close = hw_closedev,
+       .get_device_info = hw_get_device_info,
+       .get_status = hw_get_status,
+       .get_capabilities = hw_get_capabilities,
+       .set_configuration = hw_set_configuration,
+       .start_acquisition = hw_start_acquisition,
+       .stop_acquisition = hw_stop_acquisition,
 };