]> sigrok.org Git - libsigrok.git/commitdiff
sr: fx2lafw: Allocate header and packet struct on the stack
authorLars-Peter Clausen <redacted>
Sun, 24 Jun 2012 10:04:35 +0000 (12:04 +0200)
committerUwe Hermann <redacted>
Wed, 27 Jun 2012 22:40:51 +0000 (00:40 +0200)
The header and packet struct are only used in the scope of this function and
they are freed at the end of it. Also these structs are rather small, so they
can safely be allocated on the stack. By doing so memory leaks on the error
paths are avoided.

Signed-off-by: Lars-Peter Clausen <redacted>
hardware/fx2lafw/fx2lafw.c

index 2b8f1c34e25aafa3d8e483ee9b268d8dc841aa6a..a1a131e5fb18834837c684e325464f463b9bb9df 100644 (file)
@@ -830,8 +830,8 @@ static void receive_transfer(struct libusb_transfer *transfer)
 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
 {
        struct sr_dev_inst *sdi;
-       struct sr_datafeed_packet *packet;
-       struct sr_datafeed_header *header;
+       struct sr_datafeed_packet packet;
+       struct sr_datafeed_header header;
        struct sr_datafeed_meta_logic meta;
        struct context *ctx;
        struct libusb_transfer *transfer;
@@ -845,16 +845,6 @@ static int hw_dev_acquisition_start(int dev_index, void *cb_data)
        ctx->session_dev_id = cb_data;
        ctx->num_samples = 0;
 
-       if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
-               sr_err("fx2lafw: %s: packet malloc failed.", __func__);
-               return SR_ERR_MALLOC;
-       }
-
-       if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
-               sr_err("fx2lafw: %s: header malloc failed.", __func__);
-               return SR_ERR_MALLOC;
-       }
-
        /* Start with 2K transfer, subsequently increased to 4K. */
        size = 2048;
        for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
@@ -883,21 +873,18 @@ static int hw_dev_acquisition_start(int dev_index, void *cb_data)
                              40, receive_data, NULL);
        free(lupfd); /* NOT g_free()! */
 
-       packet->type = SR_DF_HEADER;
-       packet->payload = header;
-       header->feed_version = 1;
-       gettimeofday(&header->starttime, NULL);
-       sr_session_send(cb_data, packet);
+       packet.type = SR_DF_HEADER;
+       packet.payload = &header;
+       header.feed_version = 1;
+       gettimeofday(&header.starttime, NULL);
+       sr_session_send(cb_data, &packet);
 
        /* Send metadata about the SR_DF_LOGIC packets to come. */
-       packet->type = SR_DF_META_LOGIC;
-       packet->payload = &meta;
+       packet.type = SR_DF_META_LOGIC;
+       packet.payload = &meta;
        meta.samplerate = ctx->cur_samplerate;
        meta.num_probes = ctx->sample_wide ? 16 : 8;
-       sr_session_send(cb_data, packet);
-
-       g_free(header);
-       g_free(packet);
+       sr_session_send(cb_data, &packet);
 
        if ((ret = command_start_acquisition (ctx->usb->devhdl,
                ctx->cur_samplerate, ctx->sample_wide)) != SR_OK) {