From: Uwe Hermann Date: Sat, 11 Feb 2012 19:06:46 +0000 (+0100) Subject: Use g_try_malloc/g_free/g_strdup consistently. X-Git-Tag: libsigrok-0.1.0~137 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=133a37bfba1a7e1423716b2b872d3bb82a2e64d9 Use g_try_malloc/g_free/g_strdup consistently. 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. --- diff --git a/hardware/alsa/alsa.c b/hardware/alsa/alsa.c index 3fa72a8c..4c930e4f 100644 --- a/hardware/alsa/alsa.c +++ b/hardware/alsa/alsa.c @@ -104,22 +104,22 @@ static int hw_opendev(int device_index) err = snd_pcm_open(&alsa->capture_handle, AUDIO_DEV, SND_PCM_STREAM_CAPTURE, 0); if (err < 0) { - sr_warn("cannot open audio device %s (%s)", AUDIO_DEV, - snd_strerror(err)); + sr_err("cannot open audio device %s (%s)", AUDIO_DEV, + snd_strerror(err)); return SR_ERR; } err = snd_pcm_hw_params_malloc(&alsa->hw_params); if (err < 0) { - sr_warn("cannot allocate hardware parameter structure (%s)", - snd_strerror(err)); + sr_err("cannot allocate hardware parameter structure (%s)", + snd_strerror(err)); return SR_ERR; } err = snd_pcm_hw_params_any(alsa->capture_handle, alsa->hw_params); if (err < 0) { - sr_warn("cannot initialize hardware parameter structure (%s)", - snd_strerror(err)); + sr_err("cannot initialize hardware parameter structure (%s)", + snd_strerror(err)); return SR_ERR; } @@ -157,7 +157,7 @@ static void hw_cleanup(void) if (!(sdi = sr_get_device_instance(device_instances, 0))) return; - free(sdi->priv); + g_free(sdi->priv); sr_device_instance_free(sdi); } @@ -248,7 +248,7 @@ static int receive_data(int fd, int revents, void *user_data) count = snd_pcm_readi(alsa->capture_handle, inb, MIN(4096/4, alsa->limit_samples)); if (count < 1) { - sr_warn("Failed to read samples"); + sr_err("Failed to read samples"); return FALSE; } @@ -303,7 +303,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) err = snd_pcm_hw_params_set_access(alsa->capture_handle, alsa->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); if (err < 0) { - sr_warn("cannot set access type (%s)", snd_strerror(err)); + sr_err("cannot set access type (%s)", snd_strerror(err)); return SR_ERR; } @@ -311,52 +311,52 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) err = snd_pcm_hw_params_set_format(alsa->capture_handle, alsa->hw_params, SND_PCM_FORMAT_S16_LE); if (err < 0) { - sr_warn("cannot set sample format (%s)", snd_strerror(err)); + sr_err("cannot set sample format (%s)", snd_strerror(err)); return SR_ERR; } err = snd_pcm_hw_params_set_rate_near(alsa->capture_handle, alsa->hw_params, (unsigned int *) &alsa->cur_rate, 0); if (err < 0) { - sr_warn("cannot set sample rate (%s)", snd_strerror(err)); + sr_err("cannot set sample rate (%s)", snd_strerror(err)); return SR_ERR; } err = snd_pcm_hw_params_set_channels(alsa->capture_handle, alsa->hw_params, NUM_PROBES); if (err < 0) { - sr_warn("cannot set channel count (%s)", snd_strerror(err)); + sr_err("cannot set channel count (%s)", snd_strerror(err)); return SR_ERR; } err = snd_pcm_hw_params(alsa->capture_handle, alsa->hw_params); if (err < 0) { - sr_warn("cannot set parameters (%s)", snd_strerror(err)); + sr_err("cannot set parameters (%s)", snd_strerror(err)); return SR_ERR; } err = snd_pcm_prepare(alsa->capture_handle); if (err < 0) { - sr_warn("cannot prepare audio interface for use (%s)", - snd_strerror(err)); + sr_err("cannot prepare audio interface for use (%s)", + snd_strerror(err)); return SR_ERR; } count = snd_pcm_poll_descriptors_count(alsa->capture_handle); if (count < 1) { - sr_warn("Unable to obtain poll descriptors count"); + sr_err("Unable to obtain poll descriptors count"); return SR_ERR; } if (!(ufds = g_try_malloc(count * sizeof(struct pollfd)))) { - sr_warn("alsa: %s: ufds malloc failed", __func__); + sr_err("alsa: %s: ufds malloc failed", __func__); return SR_ERR_MALLOC; } err = snd_pcm_poll_descriptors(alsa->capture_handle, ufds, count); if (err < 0) { - sr_warn("Unable to obtain poll descriptors (%s)", - snd_strerror(err)); + sr_err("Unable to obtain poll descriptors (%s)", + snd_strerror(err)); g_free(ufds); return SR_ERR; } diff --git a/hardware/asix-sigma/asix-sigma.c b/hardware/asix-sigma/asix-sigma.c index 2340044d..f05bc4dc 100644 --- a/hardware/asix-sigma/asix-sigma.c +++ b/hardware/asix-sigma/asix-sigma.c @@ -126,8 +126,8 @@ static int sigma_read(void *buf, size_t size, struct sigma *sigma) ret = ftdi_read_data(&sigma->ftdic, (unsigned char *)buf, size); if (ret < 0) { - sr_warn("ftdi_read_data failed: %s", - ftdi_get_error_string(&sigma->ftdic)); + sr_err("ftdi_read_data failed: %s", + ftdi_get_error_string(&sigma->ftdic)); } return ret; @@ -139,10 +139,10 @@ static int sigma_write(void *buf, size_t size, struct sigma *sigma) ret = ftdi_write_data(&sigma->ftdic, (unsigned char *)buf, size); if (ret < 0) { - sr_warn("ftdi_write_data failed: %s", - ftdi_get_error_string(&sigma->ftdic)); + sr_err("ftdi_write_data failed: %s", + ftdi_get_error_string(&sigma->ftdic)); } else if ((size_t) ret != size) { - sr_warn("ftdi_write_data did not complete write\n"); + sr_err("ftdi_write_data did not complete write\n"); } return ret; @@ -190,7 +190,7 @@ static uint8_t sigma_get_register(uint8_t reg, struct sigma *sigma) uint8_t value; if (1 != sigma_read_register(reg, &value, 1, sigma)) { - sr_warn("sigma_get_register: 1 byte expected"); + sr_err("sigma_get_register: 1 byte expected"); return 0; } @@ -337,12 +337,12 @@ static int bin2bitbang(const char *filename, f = g_fopen(filename, "rb"); if (!f) { - sr_warn("g_fopen(\"%s\", \"rb\")", filename); + sr_err("g_fopen(\"%s\", \"rb\")", filename); return SR_ERR; } if (-1 == fseek(f, 0, SEEK_END)) { - sr_warn("fseek on %s failed", filename); + sr_err("fseek on %s failed", filename); fclose(f); return SR_ERR; } @@ -376,7 +376,7 @@ static int bin2bitbang(const char *filename, if (ret < 0) { g_free(compressed_buf); g_free(firmware); - sr_warn("Could not unpack Sigma firmware. (Error %d)\n", ret); + sr_err("Could not unpack Sigma firmware. (Error %d)\n", ret); return SR_ERR; } @@ -404,9 +404,9 @@ static int bin2bitbang(const char *filename, if (offset != *buf_size) { g_free(*buf); - sr_warn("Error reading firmware %s " - "offset=%ld, file_size=%ld, buf_size=%zd\n", - filename, offset, file_size, *buf_size); + sr_err("Error reading firmware %s " + "offset=%ld, file_size=%ld, buf_size=%zd\n", + filename, offset, file_size, *buf_size); return SR_ERR; } @@ -474,21 +474,21 @@ static int upload_firmware(int firmware_idx, struct sigma *sigma) /* Make sure it's an ASIX SIGMA. */ if ((ret = ftdi_usb_open_desc(&sigma->ftdic, USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) { - sr_warn("ftdi_usb_open failed: %s", - ftdi_get_error_string(&sigma->ftdic)); + sr_err("ftdi_usb_open failed: %s", + ftdi_get_error_string(&sigma->ftdic)); return 0; } if ((ret = ftdi_set_bitmode(&sigma->ftdic, 0xdf, BITMODE_BITBANG)) < 0) { - sr_warn("ftdi_set_bitmode failed: %s", - ftdi_get_error_string(&sigma->ftdic)); + sr_err("ftdi_set_bitmode failed: %s", + ftdi_get_error_string(&sigma->ftdic)); return 0; } /* Four times the speed of sigmalogan - Works well. */ if ((ret = ftdi_set_baudrate(&sigma->ftdic, 750000)) < 0) { - sr_warn("ftdi_set_baudrate failed: %s", - ftdi_get_error_string(&sigma->ftdic)); + sr_err("ftdi_set_baudrate failed: %s", + ftdi_get_error_string(&sigma->ftdic)); return 0; } @@ -515,8 +515,8 @@ static int upload_firmware(int firmware_idx, struct sigma *sigma) firmware_files[firmware_idx]); if ((ret = bin2bitbang(firmware_path, &buf, &buf_size)) != SR_OK) { - sr_warn("An error occured while reading the firmware: %s", - firmware_path); + sr_err("An error occured while reading the firmware: %s", + firmware_path); return ret; } @@ -526,8 +526,8 @@ static int upload_firmware(int firmware_idx, struct sigma *sigma) g_free(buf); if ((ret = ftdi_set_bitmode(&sigma->ftdic, 0x00, BITMODE_RESET)) < 0) { - sr_warn("ftdi_set_bitmode failed: %s", - ftdi_get_error_string(&sigma->ftdic)); + sr_err("ftdi_set_bitmode failed: %s", + ftdi_get_error_string(&sigma->ftdic)); return SR_ERR; } @@ -544,7 +544,7 @@ static int upload_firmware(int firmware_idx, struct sigma *sigma) ret = sigma_read(result, 3, sigma); if (ret != 3 || result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa) { - sr_warn("Configuration failed. Invalid reply received."); + sr_err("Configuration failed. Invalid reply received."); return SR_ERR; } @@ -568,8 +568,8 @@ static int hw_opendev(int device_index) if ((ret = ftdi_usb_open_desc(&sigma->ftdic, USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) { - sr_warn("ftdi_usb_open failed: %s", - ftdi_get_error_string(&sigma->ftdic)); + sr_err("ftdi_usb_open failed: %s", + ftdi_get_error_string(&sigma->ftdic)); return 0; } @@ -643,8 +643,8 @@ static int configure_probes(struct sr_device_instance *sdi, GSList *probes) if (sigma->cur_samplerate >= SR_MHZ(100)) { /* Fast trigger support. */ if (trigger_set) { - sr_warn("ASIX SIGMA only supports a single " - "pin trigger in 100 and 200MHz mode."); + sr_err("ASIX SIGMA only supports a single " + "pin trigger in 100 and 200MHz mode."); return SR_ERR; } if (probe->trigger[0] == 'f') @@ -652,9 +652,9 @@ static int configure_probes(struct sr_device_instance *sdi, GSList *probes) else if (probe->trigger[0] == 'r') sigma->trigger.risingmask |= probebit; else { - sr_warn("ASIX SIGMA only supports " - "rising/falling trigger in 100 " - "and 200MHz mode."); + sr_err("ASIX SIGMA only supports " + "rising/falling trigger in 100 " + "and 200MHz mode."); return SR_ERR; } @@ -684,8 +684,8 @@ static int configure_probes(struct sr_device_instance *sdi, GSList *probes) * does not permit ORed triggers. */ if (trigger_set > 1) { - sr_warn("ASIX SIGMA only supports 1 rising/" - "falling triggers."); + sr_err("ASIX SIGMA only supports 1 rising/" + "falling triggers."); return SR_ERR; } } diff --git a/hardware/chronovu-la8/chronovu-la8.c b/hardware/chronovu-la8/chronovu-la8.c index 8b3a7f84..b0609f7e 100644 --- a/hardware/chronovu-la8/chronovu-la8.c +++ b/hardware/chronovu-la8/chronovu-la8.c @@ -170,8 +170,8 @@ static int is_valid_samplerate(uint64_t samplerate) return 1; } - sr_warn("la8: %s: invalid samplerate (%" PRIu64 "Hz)", - __func__, samplerate); + sr_err("la8: %s: invalid samplerate (%" PRIu64 "Hz)", + __func__, samplerate); return 0; } @@ -237,12 +237,12 @@ static int la8_write(struct la8 *la8, uint8_t *buf, int size) bytes_written = ftdi_write_data(la8->ftdic, buf, size); if (bytes_written < 0) { - sr_warn("la8: %s: ftdi_write_data: (%d) %s", __func__, - bytes_written, ftdi_get_error_string(la8->ftdic)); + sr_err("la8: %s: ftdi_write_data: (%d) %s", __func__, + bytes_written, ftdi_get_error_string(la8->ftdic)); (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */ } else if (bytes_written != size) { - sr_warn("la8: %s: bytes to write: %d, bytes written: %d", - __func__, size, bytes_written); + sr_err("la8: %s: bytes to write: %d, bytes written: %d", + __func__, size, bytes_written); (void) la8_close_usb_reset_sequencer(la8); /* Ignore errors. */ } @@ -284,11 +284,11 @@ static int la8_read(struct la8 *la8, uint8_t *buf, int size) bytes_read = ftdi_read_data(la8->ftdic, buf, size); if (bytes_read < 0) { - sr_warn("la8: %s: ftdi_read_data: (%d) %s", __func__, - bytes_read, ftdi_get_error_string(la8->ftdic)); + sr_err("la8: %s: ftdi_read_data: (%d) %s", __func__, + bytes_read, ftdi_get_error_string(la8->ftdic)); } else if (bytes_read != size) { - // sr_warn("la8: %s: bytes to read: %d, bytes read: %d", - // __func__, size, bytes_read); + // sr_err("la8: %s: bytes to read: %d, bytes read: %d", + // __func__, size, bytes_read); } return bytes_read; @@ -309,8 +309,8 @@ static int la8_close(struct la8 *la8) } if ((ret = ftdi_usb_close(la8->ftdic)) < 0) { - sr_warn("la8: %s: ftdi_usb_close: (%d) %s", - __func__, ret, ftdi_get_error_string(la8->ftdic)); + sr_err("la8: %s: ftdi_usb_close: (%d) %s", + __func__, ret, ftdi_get_error_string(la8->ftdic)); } return ret; @@ -351,14 +351,14 @@ static int la8_close_usb_reset_sequencer(struct la8 *la8) /* Log errors, but ignore them (i.e., don't abort). */ if ((ret = ftdi_usb_purge_buffers(la8->ftdic)) < 0) - sr_warn("la8: %s: ftdi_usb_purge_buffers: (%d) %s", + sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s", __func__, ret, ftdi_get_error_string(la8->ftdic)); if ((ret = ftdi_usb_reset(la8->ftdic)) < 0) - sr_warn("la8: %s: ftdi_usb_reset: (%d) %s", __func__, - ret, ftdi_get_error_string(la8->ftdic)); + sr_err("la8: %s: ftdi_usb_reset: (%d) %s", __func__, + ret, ftdi_get_error_string(la8->ftdic)); if ((ret = ftdi_usb_close(la8->ftdic)) < 0) - sr_warn("la8: %s: ftdi_usb_close: (%d) %s", __func__, - ret, ftdi_get_error_string(la8->ftdic)); + sr_err("la8: %s: ftdi_usb_close: (%d) %s", __func__, + ret, ftdi_get_error_string(la8->ftdic)); } else { sr_spew("la8: %s: usb_dev was NULL, nothing to do", __func__); } @@ -689,7 +689,7 @@ static void hw_cleanup(void) /* Properly close all devices. */ for (l = device_instances; l; l = l->next) { if ((sdi = l->data) == NULL) { - sr_warn("la8: %s: sdi was NULL, continuing", __func__); + sr_err("la8: %s: sdi was NULL, continuing", __func__); continue; } #if 0 @@ -698,10 +698,10 @@ static void hw_cleanup(void) * TODO: Document who is supposed to free this, and when. */ if (sdi->priv != NULL) - free(sdi->priv); + g_free(sdi->priv); else - sr_warn("la8: %s: sdi->priv was NULL, nothing " - "to do", __func__); + sr_err("la8: %s: sdi->priv was NULL, nothing " + "to do", __func__); #endif sr_device_instance_free(sdi); /* Returns void. */ } @@ -762,7 +762,7 @@ static int hw_get_status(int device_index) struct sr_device_instance *sdi; if (!(sdi = sr_get_device_instance(device_instances, device_index))) { - sr_warn("la8: %s: sdi was NULL, device not found", __func__); + sr_err("la8: %s: sdi was NULL, device not found", __func__); return SR_ST_NOT_FOUND; } @@ -870,7 +870,7 @@ static int la8_read_block(struct la8 *la8) /* Check if block read was successful or a timeout occured. */ if (bytes_read != BS) { - sr_warn("la8: %s: trigger timed out", __func__); + sr_err("la8: %s: trigger timed out", __func__); (void) la8_reset(la8); /* Ignore errors. */ return SR_ERR; } diff --git a/hardware/common/ezusb.c b/hardware/common/ezusb.c index c2849741..56ad5da5 100644 --- a/hardware/common/ezusb.c +++ b/hardware/common/ezusb.c @@ -40,7 +40,7 @@ SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear) err = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR, 0xa0, 0xe600, 0x0000, buf, 1, 100); if (err < 0) - sr_warn("Unable to send control request: %d", err); + sr_err("Unable to send control request: %d", err); return err; } @@ -54,8 +54,8 @@ SR_PRIV int ezusb_install_firmware(libusb_device_handle *hdl, sr_info("Uploading firmware at %s", filename); if ((fw = g_fopen(filename, "rb")) == NULL) { - sr_warn("Unable to open firmware file %s for reading: %s", - filename, strerror(errno)); + sr_err("Unable to open firmware file %s for reading: %s", + filename, strerror(errno)); return SR_ERR; } @@ -69,7 +69,7 @@ SR_PRIV int ezusb_install_firmware(libusb_device_handle *hdl, LIBUSB_ENDPOINT_OUT, 0xa0, offset, 0x0000, buf, chunksize, 100); if (err < 0) { - sr_warn("Unable to send firmware to device: %d", err); + sr_err("Unable to send firmware to device: %d", err); result = SR_ERR; break; } @@ -92,7 +92,7 @@ SR_PRIV int ezusb_upload_firmware(libusb_device *dev, int configuration, libusb_get_bus_number(dev), libusb_get_device_address(dev)); if ((err = libusb_open(dev, &hdl)) < 0) { - sr_warn("failed to open device: %d", err); + sr_err("failed to open device: %d", err); return SR_ERR; } @@ -100,14 +100,14 @@ SR_PRIV int ezusb_upload_firmware(libusb_device *dev, int configuration, #if !defined(_WIN32) && !defined(__APPLE__) if (libusb_kernel_driver_active(hdl, 0)) { if ((err = libusb_detach_kernel_driver(hdl, 0)) < 0) { - sr_warn("failed to detach kernel driver: %d", err); + sr_err("failed to detach kernel driver: %d", err); return SR_ERR; } } #endif if ((err = libusb_set_configuration(hdl, configuration)) < 0) { - sr_warn("Unable to set configuration: %d", err); + sr_err("Unable to set configuration: %d", err); return SR_ERR; } diff --git a/hardware/common/serial.c b/hardware/common/serial.c index 4866230c..332cb587 100644 --- a/hardware/common/serial.c +++ b/hardware/common/serial.c @@ -57,7 +57,7 @@ SR_PRIV GSList *list_serial_ports(void) #ifdef _WIN32 /* TODO */ ports = NULL; - ports = g_slist_append(ports, strdup("COM1")); + ports = g_slist_append(ports, g_strdup("COM1")); #else glob_t g; unsigned int i, j; @@ -67,7 +67,7 @@ SR_PRIV GSList *list_serial_ports(void) if (glob(serial_port_glob[i], 0, NULL, &g)) continue; for (j = 0; j < g.gl_pathc; j++) - ports = g_slist_append(ports, strdup(g.gl_pathv[j])); + ports = g_slist_append(ports, g_strdup(g.gl_pathv[j])); globfree(&g); } #endif diff --git a/hardware/link-mso19/link-mso19.c b/hardware/link-mso19/link-mso19.c index 138ffe97..7e8e797f 100644 --- a/hardware/link-mso19/link-mso19.c +++ b/hardware/link-mso19/link-mso19.c @@ -416,7 +416,7 @@ static int hw_init(const char *deviceinfo) */ udev = udev_new(); if (!udev) { - sr_warn("Failed to initialize udev."); + sr_err("Failed to initialize udev."); goto ret; } enumerate = udev_enumerate_new(udev); @@ -436,8 +436,8 @@ static int hw_init(const char *deviceinfo) parent = udev_device_get_parent_with_subsystem_devtype( dev, "usb", "usb_device"); if (!parent) { - sr_warn("Unable to find parent usb device for %s", - sysname); + sr_err("Unable to find parent usb device for %s", + sysname); continue; } @@ -455,7 +455,7 @@ static int hw_init(const char *deviceinfo) s = strcspn(iProduct, " "); if (s > sizeof(product) || strlen(iProduct) - s > sizeof(manufacturer)) { - sr_warn("Could not parse iProduct: %s", iProduct); + sr_err("Could not parse iProduct: %s", iProduct); continue; } strncpy(product, iProduct, s); @@ -468,7 +468,7 @@ static int hw_init(const char *deviceinfo) } if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) { - sr_warn("Invalid iSerial: %s", iSerial); + sr_err("Invalid iSerial: %s", iSerial); goto err_free_mso; } sprintf(hwrev, "r%d", mso->hwrev); @@ -489,8 +489,8 @@ static int hw_init(const char *deviceinfo) sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING, manufacturer, product, hwrev); if (!sdi) { - sr_warn("Unable to create device instance for %s", - sysname); + sr_err("Unable to create device instance for %s", + sysname); goto err_free_mso; } @@ -508,7 +508,7 @@ static int hw_init(const char *deviceinfo) err_device_instance_free: sr_device_instance_free(sdi); err_free_mso: - free(mso); + g_free(mso); } udev_enumerate_unref(enumerate); @@ -530,7 +530,7 @@ static void hw_cleanup(void) serial_close(sdi->serial->fd); if (sdi->priv != NULL) { - free(sdi->priv); + g_free(sdi->priv); sdi->priv = NULL; } sr_device_instance_free(sdi); diff --git a/hardware/openbench-logic-sniffer/ols.c b/hardware/openbench-logic-sniffer/ols.c index 3bc087ca..7dcd8b82 100644 --- a/hardware/openbench-logic-sniffer/ols.c +++ b/hardware/openbench-logic-sniffer/ols.c @@ -359,7 +359,7 @@ static int hw_init(const char *deviceinfo) final_devcnt = 0; if (deviceinfo) - ports = g_slist_append(NULL, strdup(deviceinfo)); + ports = g_slist_append(NULL, g_strdup(deviceinfo)); else /* No specific device given, so scan all serial ports. */ ports = list_serial_ports(); @@ -415,10 +415,10 @@ static int hw_init(const char *deviceinfo) send_shortcommand(fd, CMD_ID); fds[devcnt].fd = fd; fds[devcnt].events = G_IO_IN; - device_names[devcnt] = strdup(l->data); + device_names[devcnt] = g_strdup(l->data); devcnt++; } - free(l->data); + g_free(l->data); } /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */ @@ -465,8 +465,8 @@ static int hw_init(const char *deviceinfo) serial_restore_params(fds[i].fd, serial_params[i]); serial_close(fds[i].fd); } - free(serial_params[i]); - free(device_names[i]); + g_free(serial_params[i]); + g_free(device_names[i]); } g_free(serial_params); @@ -614,11 +614,11 @@ static int set_configuration_samplerate(struct sr_device_instance *sdi, * from the requested. */ ols->cur_samplerate = CLOCK_RATE / (ols->cur_samplerate_divider + 1); - if(ols->flag_reg & FLAG_DEMUX) + if (ols->flag_reg & FLAG_DEMUX) ols->cur_samplerate *= 2; - if(ols->cur_samplerate != samplerate) - sr_warn("ols: can't match samplerate %" PRIu64 ", using %" PRIu64, - samplerate, ols->cur_samplerate); + if (ols->cur_samplerate != samplerate) + sr_err("ols: can't match samplerate %" PRIu64 ", using %" + PRIu64, samplerate, ols->cur_samplerate); return SR_OK; } @@ -650,7 +650,7 @@ static int hw_set_configuration(int device_index, int capability, void *value) if (*tmp_u64 < MIN_NUM_SAMPLES) return SR_ERR; if (*tmp_u64 > ols->max_samples) - sr_warn("ols: sample limit exceeds hw max"); + sr_err("ols: sample limit exceeds hw max"); ols->limit_samples = *tmp_u64; sr_info("ols: sample limit %" PRIu64, ols->limit_samples); ret = SR_OK; diff --git a/hardware/saleae-logic/saleae-logic.c b/hardware/saleae-logic/saleae-logic.c index ab303306..e0956b38 100644 --- a/hardware/saleae-logic/saleae-logic.c +++ b/hardware/saleae-logic/saleae-logic.c @@ -185,7 +185,7 @@ static int sl_open_device(int device_index) libusb_get_device_list(usb_context, &devlist); for (i = 0; devlist[i]; i++) { if ((err = libusb_get_device_descriptor(devlist[i], &des))) { - sr_warn("failed to get device descriptor: %d", err); + sr_err("failed to get device descriptor: %d", err); continue; } @@ -222,7 +222,7 @@ static int sl_open_device(int device_index) sdi->index, fx2->usb->bus, fx2->usb->address, USB_INTERFACE); } else { - sr_warn("failed to open device: %d", err); + sr_err("failed to open device: %d", err); } /* if we made it here, we handled the device one way or another */ @@ -331,7 +331,7 @@ static int hw_init(const char *deviceinfo) (void)deviceinfo; if (libusb_init(&usb_context) != 0) { - sr_warn("Failed to initialize USB."); + sr_err("Failed to initialize USB."); return 0; } @@ -342,7 +342,7 @@ static int hw_init(const char *deviceinfo) fx2_prof = NULL; err = libusb_get_device_descriptor(devlist[i], &des); if (err != 0) { - sr_warn("failed to get device descriptor: %d", err); + sr_err("failed to get device descriptor: %d", err); continue; } @@ -379,7 +379,7 @@ static int hw_init(const char *deviceinfo) /* Remember when the firmware on this device was updated */ g_get_current_time(&fx2->fw_updated); else - sr_warn("firmware upload failed for device %d", devcnt); + sr_err("firmware upload failed for device %d", devcnt); fx2->usb = sr_usb_device_instance_new (libusb_get_bus_number(devlist[i]), 0xff, NULL); } @@ -424,14 +424,14 @@ static int hw_opendev(int device_index) } if (err != SR_OK) { - sr_warn("unable to open device"); + sr_err("unable to open device"); return SR_ERR; } fx2 = sdi->priv; err = libusb_claim_interface(fx2->usb->devhdl, USB_INTERFACE); if (err != 0) { - sr_warn("Unable to claim interface: %d", err); + sr_err("Unable to claim interface: %d", err); return SR_ERR; } @@ -603,7 +603,7 @@ static int set_configuration_samplerate(struct sr_device_instance *sdi, ret = libusb_bulk_transfer(fx2->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT, buf, 2, &result, 500); if (ret != 0) { - sr_warn("failed to set samplerate: %d", ret); + sr_err("failed to set samplerate: %d", ret); return SR_ERR; } fx2->cur_samplerate = samplerate; @@ -689,15 +689,14 @@ static void receive_transfer(struct libusb_transfer *transfer) /* Fire off a new request. */ if (!(new_buf = g_try_malloc(4096))) { sr_err("saleae: %s: new_buf malloc failed", __func__); - // return SR_ERR_MALLOC; - return; /* FIXME */ + return; /* TODO: SR_ERR_MALLOC */ } transfer->buffer = new_buf; transfer->length = 4096; if (libusb_submit_transfer(transfer) != 0) { /* TODO: Stop session? */ - sr_warn("eek"); + sr_err("eek"); } if (cur_buflen == 0) { @@ -841,7 +840,7 @@ static int hw_start_acquisition(int device_index, gpointer session_data) for (i = 0; lupfd[i]; i++) sr_source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data, NULL); - free(lupfd); + free(lupfd); /* NOT g_free()! */ packet->type = SR_DF_HEADER; packet->payload = header; diff --git a/hardware/zeroplus-logic-cube/zeroplus.c b/hardware/zeroplus-logic-cube/zeroplus.c index 4abd7f78..6a6ed0b7 100644 --- a/hardware/zeroplus-logic-cube/zeroplus.c +++ b/hardware/zeroplus-logic-cube/zeroplus.c @@ -197,7 +197,7 @@ static int opendev4(struct sr_device_instance **sdi, libusb_device *dev, } if ((err = libusb_get_device_descriptor(dev, des))) { - sr_warn("failed to get device descriptor: %d", err); + sr_err("failed to get device descriptor: %d", err); return -1; } @@ -219,7 +219,7 @@ static int opendev4(struct sr_device_instance **sdi, libusb_device *dev, } if (zp->num_channels == 0) { - sr_warn("Unknown ZeroPlus device %04X", des->idProduct); + sr_err("Unknown ZeroPlus device %04X", des->idProduct); return -2; } @@ -230,7 +230,7 @@ static int opendev4(struct sr_device_instance **sdi, libusb_device *dev, (*sdi)->index, zp->usb->bus, zp->usb->address, USB_INTERFACE); } else { - sr_warn("failed to open device: %d", err); + sr_err("failed to open device: %d", err); *sdi = NULL; } } @@ -363,7 +363,7 @@ static int hw_init(const char *deviceinfo) // memset(zp->trigger_buffer, 0, NUM_TRIGGER_STAGES); if (libusb_init(&usb_context) != 0) { - sr_warn("Failed to initialize USB."); + sr_err("Failed to initialize USB."); return 0; } @@ -374,7 +374,7 @@ static int hw_init(const char *deviceinfo) for (i = 0; devlist[i]; i++) { err = libusb_get_device_descriptor(devlist[i], &des); if (err != 0) { - sr_warn("failed to get device descriptor: %d", err); + sr_err("failed to get device descriptor: %d", err); continue; } @@ -416,7 +416,7 @@ static int hw_opendev(int device_index) int err; if (!(sdi = zp_open_device(device_index))) { - sr_warn("unable to open device"); + sr_err("unable to open device"); return SR_ERR; } @@ -429,14 +429,14 @@ static int hw_opendev(int device_index) err = libusb_set_configuration(zp->usb->devhdl, USB_CONFIGURATION); if (err < 0) { - sr_warn("zp: Unable to set USB configuration %d: %d", - USB_CONFIGURATION, err); + sr_err("zp: Unable to set USB configuration %d: %d", + USB_CONFIGURATION, err); return SR_ERR; } err = libusb_claim_interface(zp->usb->devhdl, USB_INTERFACE); if (err != 0) { - sr_warn("Unable to claim interface: %d", err); + sr_err("Unable to claim interface: %d", err); return SR_ERR; } diff --git a/hwplugin.c b/hwplugin.c index 4e13ed1f..b582d97f 100644 --- a/hwplugin.c +++ b/hwplugin.c @@ -152,8 +152,10 @@ SR_API struct sr_device_instance *sr_device_instance_new(int index, int status, { struct sr_device_instance *sdi; - if (!(sdi = g_malloc(sizeof(struct sr_device_instance)))) + if (!(sdi = g_try_malloc(sizeof(struct sr_device_instance)))) { + sr_err("hwplugin: %s: sdi malloc failed", __func__); return NULL; + } sdi->index = index; sdi->status = status; @@ -198,8 +200,10 @@ SR_PRIV struct sr_usb_device_instance *sr_usb_device_instance_new(uint8_t bus, { struct sr_usb_device_instance *udi; - if (!(udi = malloc(sizeof(struct sr_usb_device_instance)))) + if (!(udi = g_try_malloc(sizeof(struct sr_usb_device_instance)))) { + sr_err("hwplugin: %s: udi malloc failed", __func__); return NULL; + } udi->bus = bus; udi->address = address; @@ -223,10 +227,12 @@ SR_PRIV struct sr_serial_device_instance *sr_serial_device_instance_new( { struct sr_serial_device_instance *serial; - if (!(serial = malloc(sizeof(struct sr_serial_device_instance)))) + if (!(serial = g_try_malloc(sizeof(struct sr_serial_device_instance)))) { + sr_err("hwplugin: %s: serial malloc failed", __func__); return NULL; + } - serial->port = strdup(port); + serial->port = g_strdup(port); serial->fd = fd; return serial; @@ -235,7 +241,7 @@ SR_PRIV struct sr_serial_device_instance *sr_serial_device_instance_new( SR_PRIV void sr_serial_device_instance_free( struct sr_serial_device_instance *serial) { - free(serial->port); + g_free(serial->port); } SR_API int sr_find_hwcap(int *capabilities, int hwcap) diff --git a/input/chronovu_la8.c b/input/chronovu_la8.c index a2aab335..e1b6ad10 100644 --- a/input/chronovu_la8.c +++ b/input/chronovu_la8.c @@ -49,21 +49,21 @@ static uint64_t divcount_to_samplerate(uint8_t divcount) static int format_match(const char *filename) { if (!filename) { - sr_warn("la8input: %s: filename was NULL", __func__); + sr_err("la8input: %s: filename was NULL", __func__); // return SR_ERR; /* FIXME */ return FALSE; } if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { - sr_warn("la8input: %s: input file '%s' does not exist", - __func__, filename); + sr_err("la8input: %s: input file '%s' does not exist", + __func__, filename); // return SR_ERR; /* FIXME */ return FALSE; } if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) { - sr_warn("la8input: %s: input file '%s' not a regular file", - __func__, filename); + sr_err("la8input: %s: input file '%s' not a regular file", + __func__, filename); // return SR_ERR; /* FIXME */ return FALSE; } @@ -83,7 +83,7 @@ static int init(struct sr_input *in) if (in->param && in->param[0]) { num_probes = strtoul(in->param, NULL, 10); if (num_probes < 1) { - sr_warn("la8input: %s: strtoul failed", __func__); + sr_err("la8input: %s: strtoul failed", __func__); return SR_ERR; } } else { @@ -113,7 +113,7 @@ static int loadfile(struct sr_input *in, const char *filename) /* TODO: Use glib functions! GIOChannel, g_fopen, etc. */ if ((fd = open(filename, O_RDONLY)) == -1) { - sr_warn("la8input: %s: file open failed", __func__); + sr_err("la8input: %s: file open failed", __func__); return SR_ERR; } diff --git a/output/analog.c b/output/analog.c index 58c37dba..c59399a2 100644 --- a/output/analog.c +++ b/output/analog.c @@ -97,8 +97,10 @@ static int init(struct sr_output *o, int default_spl, enum outputmode mode) int num_probes; char *samplerate_s; - if (!(ctx = calloc(1, sizeof(struct context)))) + if (!(ctx = g_try_malloc0(sizeof(struct context)))) { + sr_err("analog out: %s: ctx malloc failed", __func__); return SR_ERR_MALLOC; + } o->internal = ctx; ctx->num_enabled_probes = 0; @@ -125,8 +127,9 @@ static int init(struct sr_output *o, int default_spl, enum outputmode mode) } else ctx->samples_per_line = default_spl; - if (!(ctx->header = malloc(512))) { - free(ctx); + if (!(ctx->header = g_try_malloc(512))) { + g_free(ctx); + sr_err("analog out: %s: ctx->header malloc failed", __func__); return SR_ERR_MALLOC; } @@ -136,26 +139,29 @@ static int init(struct sr_output *o, int default_spl, enum outputmode mode) samplerate = *((uint64_t *) o->device->plugin->get_device_info( o->device->plugin_index, SR_DI_CUR_SAMPLERATE)); if (!(samplerate_s = sr_samplerate_string(samplerate))) { - free(ctx->header); - free(ctx); + g_free(ctx->header); + g_free(ctx); return SR_ERR; } snprintf(ctx->header + strlen(ctx->header), 511 - strlen(ctx->header), "Acquisition with %d/%d probes at %s\n", ctx->num_enabled_probes, num_probes, samplerate_s); - free(samplerate_s); + g_free(samplerate_s); } ctx->linebuf_len = ctx->samples_per_line * 2 + 4; - if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) { - free(ctx->header); - free(ctx); + if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) { + g_free(ctx->header); + g_free(ctx); + sr_err("analog out: %s: ctx->linebuf malloc failed", __func__); return SR_ERR_MALLOC; } - if (!(ctx->linevalues = calloc(1, num_probes))) { - free(ctx->header); - free(ctx); + if (!(ctx->linevalues = g_try_malloc0(num_probes))) { + g_free(ctx->header); + g_free(ctx); + sr_err("analog out: %s: ctx->linevalues malloc failed", + __func__); return SR_ERR_MALLOC; } @@ -179,12 +185,15 @@ static int event(struct sr_output *o, int event_type, char **data_out, case SR_DF_END: outsize = ctx->num_enabled_probes * (ctx->samples_per_line + 20) + 512; - if (!(outbuf = calloc(1, outsize))) + if (!(outbuf = g_try_malloc0(outsize))) { + sr_err("analog out: %s: outbuf malloc failed", + __func__); return SR_ERR_MALLOC; + } flush_linebufs(ctx, outbuf); *data_out = outbuf; *length_out = strlen(outbuf); - free(o->internal); + g_free(o->internal); o->internal = NULL; break; default: @@ -220,14 +229,16 @@ static int data_bits(struct sr_output *o, const char *data_in, outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line) * (ctx->num_enabled_probes * max_linelen); - if (!(outbuf = calloc(1, outsize + 1))) + if (!(outbuf = g_try_malloc0(outsize + 1))) { + sr_err("analog out: %s: outbuf malloc failed", __func__); return SR_ERR_MALLOC; + } outbuf[0] = '\0'; if (ctx->header) { /* The header is still here, this must be the first packet. */ strncpy(outbuf, ctx->header, outsize); - free(ctx->header); + g_free(ctx->header); ctx->header = NULL; /* Ensure first transition. */ @@ -302,14 +313,16 @@ static int data_hex(struct sr_output *o, const char *data_in, outsize = length_in / ctx->unitsize * ctx->num_enabled_probes / ctx->samples_per_line * max_linelen + 512; - if (!(outbuf = calloc(1, outsize + 1))) + if (!(outbuf = g_try_malloc0(outsize + 1))) { + sr_err("analog out: %s: outbuf malloc failed", __func__); return SR_ERR_MALLOC; + } outbuf[0] = '\0'; if (ctx->header) { /* The header is still here, this must be the first packet. */ strncpy(outbuf, ctx->header, outsize); - free(ctx->header); + g_free(ctx->header); ctx->header = NULL; } @@ -371,14 +384,16 @@ static int data_ascii(struct sr_output *o, const char *data_in, outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line) * (ctx->num_enabled_probes * max_linelen); - if (!(outbuf = calloc(1, outsize + 1))) + if (!(outbuf = g_try_malloc0(outsize + 1))) { + sr_err("analog out: %s: outbuf malloc failed", __func__); return SR_ERR_MALLOC; + } outbuf[0] = '\0'; if (ctx->header) { /* The header is still here, this must be the first packet. */ strncpy(outbuf, ctx->header, outsize); - free(ctx->header); + g_free(ctx->header); ctx->header = NULL; } diff --git a/output/binary.c b/output/binary.c index f0a3c35a..bbf49c70 100644 --- a/output/binary.c +++ b/output/binary.c @@ -33,22 +33,22 @@ static int data(struct sr_output *o, const char *data_in, uint64_t length_in, (void)o; if (!data_in) { - sr_warn("binary output: %s: data_in was NULL", __func__); - return SR_ERR; + sr_err("binary out: %s: data_in was NULL", __func__); + return SR_ERR_ARG; } if (!length_out) { - sr_warn("binary output: %s: length_out was NULL", __func__); - return SR_ERR; + sr_err("binary out: %s: length_out was NULL", __func__); + return SR_ERR_ARG; } if (length_in == 0) { - sr_warn("binary output: %s: length_in was 0", __func__); - return SR_ERR; + sr_err("binary out: %s: length_in was 0", __func__); + return SR_ERR_ARG; } - if (!(outbuf = calloc(1, length_in))) { - sr_warn("binary output: %s: outbuf calloc failed", __func__); + if (!(outbuf = g_try_malloc0(length_in))) { + sr_err("binary out: %s: outbuf malloc failed", __func__); return SR_ERR_MALLOC; } diff --git a/output/chronovu_la8.c b/output/chronovu_la8.c index 6ef99daf..993d29fc 100644 --- a/output/chronovu_la8.c +++ b/output/chronovu_la8.c @@ -102,8 +102,8 @@ static int init(struct sr_output *o) return SR_ERR_ARG; } - if (!(ctx = calloc(1, sizeof(struct context)))) { - sr_warn("la8 out: %s: ctx calloc failed", __func__); + if (!(ctx = g_try_malloc0(sizeof(struct context)))) { + sr_warn("la8 out: %s: ctx malloc failed", __func__); return SR_ERR_MALLOC; } @@ -163,7 +163,7 @@ static int event(struct sr_output *o, int event_type, char **data_out, break; case SR_DF_END: sr_dbg("la8 out: %s: SR_DF_END event", __func__); - if (!(outbuf = malloc(4 + 1))) { + if (!(outbuf = g_try_malloc(4 + 1))) { sr_warn("la8 out: %s: outbuf malloc failed", __func__); return SR_ERR_MALLOC; } @@ -183,7 +183,7 @@ static int event(struct sr_output *o, int event_type, char **data_out, *data_out = outbuf; *length_out = 4 + 1; - free(o->internal); + g_free(o->internal); o->internal = NULL; break; default: @@ -218,8 +218,8 @@ static int data(struct sr_output *o, const char *data_in, uint64_t length_in, return SR_ERR_ARG; } - if (!(outbuf = calloc(1, length_in))) { - sr_warn("la8 out: %s: outbuf calloc failed", __func__); + if (!(outbuf = g_try_malloc0(length_in))) { + sr_warn("la8 out: %s: outbuf malloc failed", __func__); return SR_ERR_MALLOC; } diff --git a/output/gnuplot.c b/output/gnuplot.c index fffafdfc..cfb5d7ec 100644 --- a/output/gnuplot.c +++ b/output/gnuplot.c @@ -62,30 +62,28 @@ static int init(struct sr_output *o) time_t t; if (!o) { - sr_warn("gnuplot out: %s: o was NULL", __func__); + sr_err("gnuplot out: %s: o was NULL", __func__); return SR_ERR_ARG; } if (!o->device) { - sr_warn("gnuplot out: %s: o->device was NULL", __func__); + sr_err("gnuplot out: %s: o->device was NULL", __func__); return SR_ERR_ARG; } if (!o->device->plugin) { - sr_warn("gnuplot out: %s: o->device->plugin was NULL", - __func__); + sr_err("gnuplot out: %s: o->device->plugin was NULL", __func__); return SR_ERR_ARG; } - if (!(ctx = calloc(1, sizeof(struct context)))) { - sr_warn("gnuplot out: %s: ctx calloc failed", __func__); + if (!(ctx = g_try_malloc0(sizeof(struct context)))) { + sr_err("gnuplot out: %s: ctx malloc failed", __func__); return SR_ERR_MALLOC; } - if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) { - sr_warn("gnuplot out: %s: ctx->header calloc failed", - __func__); - free(ctx); + if (!(ctx->header = g_try_malloc0(MAX_HEADER_LEN + 1))) { + sr_err("gnuplot out: %s: ctx->header malloc failed", __func__); + g_free(ctx); return SR_ERR_MALLOC; } @@ -106,15 +104,15 @@ static int init(struct sr_output *o) samplerate = *((uint64_t *) o->device->plugin->get_device_info( o->device->plugin_index, SR_DI_CUR_SAMPLERATE)); if (!(frequency_s = sr_samplerate_string(samplerate))) { - sr_warn("gnuplot out: %s: sr_samplerate_string failed", - __func__); - free(ctx->header); - free(ctx); + sr_err("gnuplot out: %s: sr_samplerate_string failed", + __func__); + g_free(ctx->header); + g_free(ctx); return SR_ERR; } snprintf(comment, 127, gnuplot_header_comment, ctx->num_enabled_probes, num_probes, frequency_s); - free(frequency_s); + g_free(frequency_s); } /* Columns / channels */ @@ -125,9 +123,9 @@ static int init(struct sr_output *o) } if (!(frequency_s = sr_period_string(samplerate))) { - sr_warn("gnuplot out: %s: sr_period_string failed", __func__); - free(ctx->header); - free(ctx); + sr_err("gnuplot out: %s: sr_period_string failed", __func__); + g_free(ctx->header); + g_free(ctx); return SR_ERR; } @@ -135,12 +133,12 @@ static int init(struct sr_output *o) b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header, PACKAGE_STRING, ctime(&t), comment, frequency_s, (char *)&wbuf); - free(frequency_s); + g_free(frequency_s); if (b < 0) { - sr_warn("gnuplot out: %s: sprintf failed", __func__); - free(ctx->header); - free(ctx); + sr_err("gnuplot out: %s: sprintf failed", __func__); + g_free(ctx->header); + g_free(ctx); return SR_ERR; } @@ -153,17 +151,17 @@ static int event(struct sr_output *o, int event_type, char **data_out, struct context *ctx; if (!o) { - sr_warn("gnuplot out: %s: o was NULL", __func__); + sr_err("gnuplot out: %s: o was NULL", __func__); return SR_ERR_ARG; } if (!data_out) { - sr_warn("gnuplot out: %s: data_out was NULL", __func__); + sr_err("gnuplot out: %s: data_out was NULL", __func__); return SR_ERR_ARG; } if (!length_out) { - sr_warn("gnuplot out: %s: length_out was NULL", __func__); + sr_err("gnuplot out: %s: length_out was NULL", __func__); return SR_ERR_ARG; } @@ -174,12 +172,12 @@ static int event(struct sr_output *o, int event_type, char **data_out, /* TODO: Can a trigger mark be in a gnuplot data file? */ break; case SR_DF_END: - free(o->internal); + g_free(o->internal); o->internal = NULL; break; default: - sr_warn("gnuplot out: %s: unsupported event type: %d", - __func__, event_type); + sr_err("gnuplot out: %s: unsupported event type: %d", + __func__, event_type); break; } @@ -199,27 +197,27 @@ static int data(struct sr_output *o, const char *data_in, uint64_t length_in, char *outbuf, *c; if (!o) { - sr_warn("gnuplot out: %s: o was NULL", __func__); + sr_err("gnuplot out: %s: o was NULL", __func__); return SR_ERR_ARG; } if (!o->internal) { - sr_warn("gnuplot out: %s: o->internal was NULL", __func__); + sr_err("gnuplot out: %s: o->internal was NULL", __func__); return SR_ERR_ARG; } if (!data_in) { - sr_warn("gnuplot out: %s: data_in was NULL", __func__); + sr_err("gnuplot out: %s: data_in was NULL", __func__); return SR_ERR_ARG; } if (!data_out) { - sr_warn("gnuplot out: %s: data_out was NULL", __func__); + sr_err("gnuplot out: %s: data_out was NULL", __func__); return SR_ERR_ARG; } if (!length_out) { - sr_warn("gnuplot out: %s: length_out was NULL", __func__); + sr_err("gnuplot out: %s: length_out was NULL", __func__); return SR_ERR_ARG; } @@ -229,8 +227,8 @@ static int data(struct sr_output *o, const char *data_in, uint64_t length_in, if (ctx->header) outsize += strlen(ctx->header); - if (!(outbuf = calloc(1, outsize))) { - sr_warn("gnuplot out: %s: outbuf calloc failed", __func__); + if (!(outbuf = g_try_malloc0(outsize))) { + sr_err("gnuplot out: %s: outbuf malloc failed", __func__); return SR_ERR_MALLOC; } @@ -238,7 +236,7 @@ static int data(struct sr_output *o, const char *data_in, uint64_t length_in, if (ctx->header) { /* The header is still here, this must be the first packet. */ strncpy(outbuf, ctx->header, outsize); - free(ctx->header); + g_free(ctx->header); ctx->header = NULL; } @@ -300,11 +298,14 @@ static int analog_init(struct sr_output *o) char wbuf[1000], comment[128]; time_t t; - if (!(ctx = calloc(1, sizeof(struct context)))) + if (!(ctx = g_try_malloc0(sizeof(struct context)))) { + sr_err("gnuplot out: %s: ctx malloc failed", __func__); return SR_ERR_MALLOC; + } - if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) { - free(ctx); + if (!(ctx->header = g_try_malloc0(MAX_HEADER_LEN + 1))) { + g_free(ctx); + sr_err("gnuplot out: %s: ctx->header malloc failed", __func__); return SR_ERR_MALLOC; } @@ -327,13 +328,13 @@ static int analog_init(struct sr_output *o) samplerate = *((uint64_t *) o->device->plugin->get_device_info( o->device->plugin_index, SR_DI_CUR_SAMPLERATE)); if (!(frequency_s = sr_samplerate_string(samplerate))) { - free(ctx->header); - free(ctx); + g_free(ctx->header); + g_free(ctx); return SR_ERR; } snprintf(comment, 127, gnuplot_header_comment, ctx->num_enabled_probes, num_probes, frequency_s); - free(frequency_s); + g_free(frequency_s); } /* Columns / channels */ @@ -344,19 +345,19 @@ static int analog_init(struct sr_output *o) } if (!(frequency_s = sr_period_string(samplerate))) { - free(ctx->header); - free(ctx); + g_free(ctx->header); + g_free(ctx); return SR_ERR; } t = time(NULL); b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header, PACKAGE_STRING, ctime(&t), comment, frequency_s, (char *)&wbuf); - free(frequency_s); + g_free(frequency_s); if (b < 0) { - free(ctx->header); - free(ctx); + g_free(ctx->header); + g_free(ctx); return SR_ERR; } @@ -380,14 +381,16 @@ static int analog_data(struct sr_output *o, char *data_in, uint64_t length_in, if (ctx->header) outsize += strlen(ctx->header); - if (!(outbuf = calloc(1, outsize))) + if (!(outbuf = g_try_malloc0(outsize))) { + sr_err("gnuplot out: %s: outbuf malloc failed", __func__); return SR_ERR_MALLOC; + } outbuf[0] = '\0'; if (ctx->header) { /* The header is still here, this must be the first packet. */ strncpy(outbuf, ctx->header, outsize); - free(ctx->header); + g_free(ctx->header); ctx->header = NULL; } diff --git a/output/ols.c b/output/ols.c index 04403456..dff54a42 100644 --- a/output/ols.c +++ b/output/ols.c @@ -85,7 +85,7 @@ static int event(struct sr_output *o, int event_type, char **data_out, if (ctx && event_type == SR_DF_END) { g_string_free(ctx->header, TRUE); - free(o->internal); + g_free(o->internal); o->internal = NULL; } diff --git a/output/text/ascii.c b/output/text/ascii.c index d97243e6..923cb93d 100644 --- a/output/text/ascii.c +++ b/output/text/ascii.c @@ -50,14 +50,16 @@ SR_PRIV int data_ascii(struct sr_output *o, const char *data_in, outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line) * (ctx->num_enabled_probes * max_linelen); - if (!(outbuf = calloc(1, outsize + 1))) + if (!(outbuf = g_try_malloc0(outsize + 1))) { + sr_err("ascii out: %s: outbuf malloc failed", __func__); return SR_ERR_MALLOC; + } outbuf[0] = '\0'; if (ctx->header) { /* The header is still here, this must be the first packet. */ strncpy(outbuf, ctx->header, outsize); - free(ctx->header); + g_free(ctx->header); ctx->header = NULL; } diff --git a/output/text/bits.c b/output/text/bits.c index bb90e1c9..d66b7823 100644 --- a/output/text/bits.c +++ b/output/text/bits.c @@ -49,14 +49,16 @@ SR_PRIV int data_bits(struct sr_output *o, const char *data_in, outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line) * (ctx->num_enabled_probes * max_linelen); - if (!(outbuf = calloc(1, outsize + 1))) + if (!(outbuf = g_try_malloc0(outsize + 1))) { + sr_err("bits out: %s: outbuf malloc failed", __func__); return SR_ERR_MALLOC; + } outbuf[0] = '\0'; if (ctx->header) { /* The header is still here, this must be the first packet. */ strncpy(outbuf, ctx->header, outsize); - free(ctx->header); + g_free(ctx->header); ctx->header = NULL; /* Ensure first transition. */ diff --git a/output/text/hex.c b/output/text/hex.c index 964cb9cc..cfe3bfb9 100644 --- a/output/text/hex.c +++ b/output/text/hex.c @@ -22,6 +22,7 @@ #include #include #include "sigrok.h" +#include "sigrok-internal.h" #include "text.h" SR_PRIV int init_hex(struct sr_output *o) @@ -44,14 +45,16 @@ SR_PRIV int data_hex(struct sr_output *o, const char *data_in, outsize = length_in / ctx->unitsize * ctx->num_enabled_probes / ctx->samples_per_line * max_linelen + 512; - if (!(outbuf = calloc(1, outsize + 1))) + if (!(outbuf = g_try_malloc0(outsize + 1))) { + sr_err("hex out: %s: outbuf malloc failed", __func__); return SR_ERR_MALLOC; + } outbuf[0] = '\0'; if (ctx->header) { /* The header is still here, this must be the first packet. */ strncpy(outbuf, ctx->header, outsize); - free(ctx->header); + g_free(ctx->header); ctx->header = NULL; } diff --git a/output/text/text.c b/output/text/text.c index 29740656..10ce0767 100644 --- a/output/text/text.c +++ b/output/text/text.c @@ -24,6 +24,7 @@ #include #include "config.h" #include "sigrok.h" +#include "sigrok-internal.h" #include "text.h" SR_PRIV void flush_linebufs(struct context *ctx, char *outbuf) @@ -72,8 +73,10 @@ SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode) int num_probes; char *samplerate_s; - if (!(ctx = calloc(1, sizeof(struct context)))) + if (!(ctx = g_try_malloc0(sizeof(struct context)))) { + sr_err("text out: %s: ctx malloc failed", __func__); return SR_ERR_MALLOC; + } o->internal = ctx; ctx->num_enabled_probes = 0; @@ -99,8 +102,9 @@ SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode) } else ctx->samples_per_line = default_spl; - if (!(ctx->header = malloc(512))) { - free(ctx); + if (!(ctx->header = g_try_malloc0(512))) { + g_free(ctx); + sr_err("text out: %s: ctx->header malloc failed", __func__); return SR_ERR_MALLOC; } @@ -110,26 +114,28 @@ SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode) samplerate = *((uint64_t *) o->device->plugin->get_device_info( o->device->plugin_index, SR_DI_CUR_SAMPLERATE)); if (!(samplerate_s = sr_samplerate_string(samplerate))) { - free(ctx->header); - free(ctx); + g_free(ctx->header); + g_free(ctx); return SR_ERR; } snprintf(ctx->header + strlen(ctx->header), 511 - strlen(ctx->header), "Acquisition with %d/%d probes at %s\n", ctx->num_enabled_probes, num_probes, samplerate_s); - free(samplerate_s); + g_free(samplerate_s); } ctx->linebuf_len = ctx->samples_per_line * 2 + 4; - if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) { - free(ctx->header); - free(ctx); + if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) { + g_free(ctx->header); + g_free(ctx); + sr_err("text out: %s: ctx->linebuf malloc failed", __func__); return SR_ERR_MALLOC; } - if (!(ctx->linevalues = calloc(1, num_probes))) { - free(ctx->header); - free(ctx); + if (!(ctx->linevalues = g_try_malloc0(num_probes))) { + g_free(ctx->header); + g_free(ctx); + sr_err("text out: %s: ctx->linevalues malloc failed", __func__); return SR_ERR_MALLOC; } @@ -153,12 +159,14 @@ SR_PRIV int event(struct sr_output *o, int event_type, char **data_out, case SR_DF_END: outsize = ctx->num_enabled_probes * (ctx->samples_per_line + 20) + 512; - if (!(outbuf = calloc(1, outsize))) + if (!(outbuf = g_try_malloc0(outsize))) { + sr_err("text out: %s: outbuf malloc failed", __func__); return SR_ERR_MALLOC; + } flush_linebufs(ctx, outbuf); *data_out = outbuf; *length_out = strlen(outbuf); - free(o->internal); + g_free(o->internal); o->internal = NULL; break; default: diff --git a/output/vcd.c b/output/vcd.c index a0f70e37..c5a6bb20 100644 --- a/output/vcd.c +++ b/output/vcd.c @@ -49,8 +49,10 @@ static int init(struct sr_output *o) char *samplerate_s, *frequency_s, *timestamp; time_t t; - if (!(ctx = calloc(1, sizeof(struct context)))) + if (!(ctx = g_try_malloc0(sizeof(struct context)))) { + sr_err("vcd out: %s: ctx malloc failed", __func__); return SR_ERR_MALLOC; + } o->internal = ctx; ctx->num_enabled_probes = 0; @@ -62,7 +64,7 @@ static int init(struct sr_output *o) ctx->probelist[ctx->num_enabled_probes++] = probe->name; } if (ctx->num_enabled_probes > 94) { - sr_warn("VCD only supports 94 probes."); + sr_err("VCD only supports 94 probes."); return SR_ERR; } @@ -73,10 +75,10 @@ static int init(struct sr_output *o) /* timestamp */ t = time(NULL); - timestamp = strdup(ctime(&t)); + timestamp = g_strdup(ctime(&t)); timestamp[strlen(timestamp)-1] = 0; g_string_printf(ctx->header, "$date %s $end\n", timestamp); - free(timestamp); + g_free(timestamp); /* generator */ g_string_append_printf(ctx->header, "$version %s %s $end\n", @@ -87,12 +89,12 @@ static int init(struct sr_output *o) o->device->plugin_index, SR_DI_CUR_SAMPLERATE)); if (!((samplerate_s = sr_samplerate_string(ctx->samplerate)))) { g_string_free(ctx->header, TRUE); - free(ctx); + g_free(ctx); return SR_ERR; } g_string_append_printf(ctx->header, vcd_header_comment, ctx->num_enabled_probes, num_probes, samplerate_s); - free(samplerate_s); + g_free(samplerate_s); } /* timescale */ @@ -105,11 +107,11 @@ static int init(struct sr_output *o) ctx->period = SR_KHZ(1); if (!(frequency_s = sr_period_string(ctx->period))) { g_string_free(ctx->header, TRUE); - free(ctx); + g_free(ctx); return SR_ERR; } g_string_append_printf(ctx->header, "$timescale %s $end\n", frequency_s); - free(frequency_s); + g_free(frequency_s); /* scope */ g_string_append_printf(ctx->header, "$scope module %s $end\n", PACKAGE); @@ -123,9 +125,10 @@ static int init(struct sr_output *o) g_string_append(ctx->header, "$upscope $end\n" "$enddefinitions $end\n$dumpvars\n"); - if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) { + if (!(ctx->prevbits = g_try_malloc0(sizeof(int) * num_probes))) { g_string_free(ctx->header, TRUE); - free(ctx); + g_free(ctx); + sr_err("vcd out: %s: ctx->prevbits malloc failed", __func__); return SR_ERR_MALLOC; } @@ -141,10 +144,10 @@ static int event(struct sr_output *o, int event_type, char **data_out, ctx = o->internal; switch (event_type) { case SR_DF_END: - outbuf = strdup("$dumpoff\n$end\n"); + outbuf = g_strdup("$dumpoff\n$end\n"); *data_out = outbuf; *length_out = strlen(outbuf); - free(o->internal); + g_free(o->internal); o->internal = NULL; break; default: diff --git a/session.c b/session.c index 683ca908..ed8cadc5 100644 --- a/session.c +++ b/session.c @@ -57,7 +57,7 @@ static int source_timeout = -1; */ SR_API struct sr_session *sr_session_new(void) { - if (!(session = calloc(1, sizeof(struct sr_session)))) { + if (!(session = g_try_malloc0(sizeof(struct sr_session)))) { sr_err("session: %s: session malloc failed", __func__); return NULL; /* TODO: SR_ERR_MALLOC? */ } @@ -75,7 +75,7 @@ SR_API struct sr_session *sr_session_new(void) SR_API int sr_session_destroy(void) { if (!session) { - sr_warn("session: %s: session was NULL", __func__); + sr_err("session: %s: session was NULL", __func__); return SR_ERR_BUG; } @@ -103,7 +103,7 @@ SR_API int sr_session_destroy(void) SR_API int sr_session_device_clear(void) { if (!session) { - sr_warn("session: %s: session was NULL", __func__); + sr_err("session: %s: session was NULL", __func__); return SR_ERR_BUG; } @@ -208,13 +208,11 @@ static int sr_session_run_poll(void) fds = NULL; while (session->running) { - if (fds) - free(fds); + /* TODO: Add comment. */ + g_free(fds); /* Construct g_poll()'s array. */ - /* TODO: Check malloc return value. */ - fds = malloc(sizeof(GPollFD) * num_sources); - if (!fds) { + if (!(fds = g_try_malloc(sizeof(GPollFD) * num_sources))) { sr_err("session: %s: fds malloc failed", __func__); return SR_ERR_MALLOC; } @@ -245,7 +243,7 @@ static int sr_session_run_poll(void) } } } - free(fds); + g_free(fds); return SR_OK; } @@ -476,7 +474,6 @@ SR_API int sr_session_bus(struct sr_device *device, /** * TODO. * - * TODO: Switch to g_try_malloc0() / g_free(). * TODO: More error checks etc. * * @param fd TODO. @@ -499,7 +496,7 @@ SR_API int sr_session_source_add(int fd, int events, int timeout, /* Note: user_data can be NULL, that's not a bug. */ - new_sources = calloc(1, sizeof(struct source) * (num_sources + 1)); + new_sources = g_try_malloc0(sizeof(struct source) * (num_sources + 1)); if (!new_sources) { sr_err("session: %s: new_sources malloc failed", __func__); return SR_ERR_MALLOC; @@ -508,7 +505,7 @@ SR_API int sr_session_source_add(int fd, int events, int timeout, if (sources) { memcpy(new_sources, sources, sizeof(struct source) * num_sources); - free(sources); + g_free(sources); } s = &new_sources[num_sources++]; @@ -530,7 +527,6 @@ SR_API int sr_session_source_add(int fd, int events, int timeout, * Remove the source belonging to the specified file descriptor. * * TODO: More error checks. - * TODO: Switch to g_try_malloc0() / g_free(). * * @param fd TODO. * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or @@ -549,7 +545,7 @@ SR_API int sr_session_source_remove(int fd) /* TODO: Check if 'fd' valid. */ - new_sources = calloc(1, sizeof(struct source) * num_sources); + new_sources = g_try_malloc0(sizeof(struct source) * num_sources); if (!new_sources) { sr_err("session: %s: new_sources malloc failed", __func__); return SR_ERR_MALLOC; @@ -562,12 +558,12 @@ SR_API int sr_session_source_remove(int fd) } if (old != new) { - free(sources); + g_free(sources); sources = new_sources; num_sources--; } else { /* Target fd was not found. */ - free(new_sources); + g_free(new_sources); } return SR_OK; diff --git a/session_driver.c b/session_driver.c index 0c1d0d20..7ca8d353 100644 --- a/session_driver.c +++ b/session_driver.c @@ -107,8 +107,7 @@ static int feed_chunk(int fd, int revents, void *session_data) if (!(buf = g_try_malloc(CHUNKSIZE))) { sr_err("session: %s: buf malloc failed", __func__); - // return SR_ERR_MALLOC; - return FALSE; + return FALSE; /* TODO: SR_ERR_MALLOC */ } ret = zip_fread(vdevice->capfile, buf, CHUNKSIZE); @@ -287,20 +286,21 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) vdevice->capturefile); if (!(vdevice->archive = zip_open(sessionfile, 0, &err))) { - sr_warn("Failed to open session file '%s': zip error %d\n", - sessionfile, err); + sr_err("Failed to open session file '%s': zip error %d\n", + sessionfile, err); return SR_ERR; } if (zip_stat(vdevice->archive, vdevice->capturefile, 0, &zs) == -1) { - sr_warn("Failed to check capture file '%s' in session file '%s'.", - vdevice->capturefile, sessionfile); + sr_err("Failed to check capture file '%s' in session file " + "'%s'.", vdevice->capturefile, sessionfile); return SR_ERR; } - if (!(vdevice->capfile = zip_fopen(vdevice->archive, vdevice->capturefile, 0))) { - sr_warn("Failed to open capture file '%s' in session file '%s'.", - vdevice->capturefile, sessionfile); + if (!(vdevice->capfile = zip_fopen(vdevice->archive, + vdevice->capturefile, 0))) { + sr_err("Failed to open capture file '%s' in session file '%s'.", + vdevice->capturefile, sessionfile); return SR_ERR; } diff --git a/session_file.c b/session_file.c index 08de48c2..cb99bf27 100644 --- a/session_file.c +++ b/session_file.c @@ -230,7 +230,7 @@ int sr_session_save(const char *filename) device->plugin_index, SR_DI_CUR_SAMPLERATE)); s = sr_samplerate_string(samplerate); fprintf(meta, "samplerate = %s\n", s); - free(s); + g_free(s); } probecnt = 1; for (p = device->probes; p; p = p->next) { @@ -245,8 +245,14 @@ int sr_session_save(const char *filename) } /* dump datastore into logic-n */ - buf = malloc(ds->num_units * ds->ds_unitsize + + buf = g_try_malloc(ds->num_units * ds->ds_unitsize + DATASTORE_CHUNKSIZE); + if (!buf) { + sr_err("session file: %s: buf malloc failed", + __func__); + return SR_ERR_MALLOC; + } + bufcnt = 0; for (d = ds->chunklist; d; d = d->next) { memcpy(buf + bufcnt, d->data, diff --git a/strutil.c b/strutil.c index ea77f4d9..5e8689e7 100644 --- a/strutil.c +++ b/strutil.c @@ -30,17 +30,20 @@ * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz". * * @param samplerate The samplerate in Hz. - * @return A malloc()ed string representation of the samplerate value, - * or NULL upon errors. The caller is responsible to free() the memory. + * @return A g_try_malloc()ed string representation of the samplerate value, + * or NULL upon errors. The caller is responsible to g_free() the + * memory. */ SR_API char *sr_samplerate_string(uint64_t samplerate) { char *o; int r; - o = malloc(30 + 1); /* Enough for a uint64_t as string + " GHz". */ - if (!o) + /* Allocate enough for a uint64_t as string + " GHz". */ + if (!(o = g_try_malloc0(30 + 1))) { + sr_err("strutil: %s: o malloc failed", __func__); return NULL; + } if (samplerate >= SR_GHZ(1)) r = snprintf(o, 30, "%" PRIu64 " GHz", samplerate / 1000000000); @@ -53,7 +56,7 @@ SR_API char *sr_samplerate_string(uint64_t samplerate) if (r < 0) { /* Something went wrong... */ - free(o); + g_free(o); return NULL; } @@ -67,17 +70,20 @@ SR_API char *sr_samplerate_string(uint64_t samplerate) * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms". * * @param frequency The frequency in Hz. - * @return A malloc()ed string representation of the frequency value, - * or NULL upon errors. The caller is responsible to free() the memory. + * @return A g_try_malloc()ed string representation of the frequency value, + * or NULL upon errors. The caller is responsible to g_free() the + * memory. */ SR_API char *sr_period_string(uint64_t frequency) { char *o; int r; - o = malloc(30 + 1); /* Enough for a uint64_t as string + " ms". */ - if (!o) + /* Allocate enough for a uint64_t as string + " ms". */ + if (!(o = g_try_malloc0(30 + 1))) { + sr_err("strutil: %s: o malloc failed", __func__); return NULL; + } if (frequency >= SR_GHZ(1)) r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000); @@ -90,7 +96,7 @@ SR_API char *sr_period_string(uint64_t frequency) if (r < 0) { /* Something went wrong... */ - free(o); + g_free(o); return NULL; }