Use g_try_malloc/g_free/g_strdup consistently.
authorUwe Hermann <uwe@hermann-uwe.de>
Sat, 11 Feb 2012 19:06:46 +0000 (20:06 +0100)
committerUwe Hermann <uwe@hermann-uwe.de>
Sat, 11 Feb 2012 21:34:16 +0000 (22:34 +0100)
Avoid plain malloc()/free() in sr/srd, especially in the API calls.
Also avoid g_malloc*() in favor of g_try_malloc*().

Use g_strdup() instead of strdup() so that we can use g_free()
consistently everywhere.

Exceptions: Stuff that is allocated via other libs (not using glib),
should also be properly free'd using the respective free-ing function
(instead of g_free()). Examples: Stuff allocated by libusb, libftdi, etc.

Also, use sr_err() instead of sr_warn() for actual errors. sr_warn() is
meant for non-fatal/uncritical warnings.

parsers.c
sigrok-cli.c

index 2dbbd930095be243ef5fc71d44af31857ad58b35..fdf8562f365f73a04a4099f361eadceade29e191 100644 (file)
--- a/parsers.c
+++ b/parsers.c
@@ -33,7 +33,9 @@ char **parse_probestring(int max_probes, const char *probestring)
 
        error = FALSE;
        range = NULL;
-       probelist = g_malloc0(max_probes * sizeof(char *));
+       if (!(probelist = g_try_malloc0(max_probes * sizeof(char *)))) {
+               /* TODO: Handle errors. */
+       }
        tokens = g_strsplit(probestring, ",", max_probes);
 
        for (i = 0; tokens[i]; i++) {
index d51c893ebbc10fe58e6f28aebc06da4fe35a1ff7..86ac278250d6425d2be440f3b2adc315f1e0539c 100644 (file)
@@ -234,17 +234,17 @@ static void show_device_detail(void)
                                if (!(s = sr_samplerate_string(samplerates->low)))
                                        continue;
                                printf(" (%s", s);
-                               free(s);
+                               g_free(s);
                                /* high */
                                if (!(s = sr_samplerate_string(samplerates->high)))
                                        continue;
                                printf(" - %s", s);
-                               free(s);
+                               g_free(s);
                                /* step */
                                if (!(s = sr_samplerate_string(samplerates->step)))
                                        continue;
                                printf(" in steps of %s)\n", s);
-                               free(s);
+                               g_free(s);
                        } else {
                                printf(" - supported samplerates:\n");
                                for (i = 0; samplerates->list[i]; i++) {
@@ -313,7 +313,7 @@ static void datafeed_in(struct sr_device *device, struct sr_datafeed_packet *pac
        case SR_DF_HEADER:
                g_message("cli: Received SR_DF_HEADER");
                /* Initialize the output module. */
-               if (!(o = malloc(sizeof(struct sr_output)))) {
+               if (!(o = g_try_malloc(sizeof(struct sr_output)))) {
                        printf("Output module malloc failed.\n");
                        exit(1);
                }
@@ -370,7 +370,7 @@ static void datafeed_in(struct sr_device *device, struct sr_datafeed_packet *pac
                        if (output_len) {
                                if (outfile)
                                        fwrite(output_buf, 1, output_len, outfile);
-                               free(output_buf);
+                               g_free(output_buf);
                                output_len = 0;
                        }
                }
@@ -383,7 +383,7 @@ static void datafeed_in(struct sr_device *device, struct sr_datafeed_packet *pac
                sr_session_halt();
                if (outfile && outfile != stdout)
                        fclose(outfile);
-               free(o);
+               g_free(o);
                o = NULL;
                break;
        case SR_DF_TRIGGER:
@@ -447,7 +447,7 @@ static void datafeed_in(struct sr_device *device, struct sr_datafeed_packet *pac
                        o->format->data(o, filter_out, filter_out_len, &output_buf, &output_len);
                if (output_len) {
                        fwrite(output_buf, 1, output_len, outfile);
-                       free(output_buf);
+                       g_free(output_buf);
                }
        }
 
@@ -646,7 +646,7 @@ static void load_input_file_format(void)
        }
 
        /* Initialize the input module. */
-       if (!(in = malloc(sizeof(struct sr_input)))) {
+       if (!(in = g_try_malloc(sizeof(struct sr_input)))) {
                printf("Failed to allocate input module.\n");
                exit(1);
        }