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.
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;
}
if (!(sdi = sr_get_device_instance(device_instances, 0)))
return;
- free(sdi->priv);
+ g_free(sdi->priv);
sr_device_instance_free(sdi);
}
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;
}
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;
}
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;
}
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;
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;
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;
}
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;
}
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;
}
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;
}
/* 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;
}
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;
}
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;
}
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;
}
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 (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')
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;
}
* 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;
}
}
return 1;
}
- sr_warn("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
- __func__, samplerate);
+ sr_err("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
+ __func__, samplerate);
return 0;
}
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. */
}
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;
}
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;
/* 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__);
}
/* 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
* 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. */
}
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;
}
/* 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;
}
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;
}
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;
}
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;
}
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;
}
#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;
}
#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;
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
*/
udev = udev_new();
if (!udev) {
- sr_warn("Failed to initialize udev.");
+ sr_err("Failed to initialize udev.");
goto ret;
}
enumerate = udev_enumerate_new(udev);
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;
}
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);
}
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);
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;
}
err_device_instance_free:
sr_device_instance_free(sdi);
err_free_mso:
- free(mso);
+ g_free(mso);
}
udev_enumerate_unref(enumerate);
serial_close(sdi->serial->fd);
if (sdi->priv != NULL)
{
- free(sdi->priv);
+ g_free(sdi->priv);
sdi->priv = NULL;
}
sr_device_instance_free(sdi);
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();
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 */
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);
* 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;
}
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;
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;
}
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 */
(void)deviceinfo;
if (libusb_init(&usb_context) != 0) {
- sr_warn("Failed to initialize USB.");
+ sr_err("Failed to initialize USB.");
return 0;
}
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;
}
/* 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);
}
}
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;
}
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;
/* 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) {
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;
}
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;
}
}
if (zp->num_channels == 0) {
- sr_warn("Unknown ZeroPlus device %04X", des->idProduct);
+ sr_err("Unknown ZeroPlus device %04X", des->idProduct);
return -2;
}
(*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;
}
}
// 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;
}
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;
}
int err;
if (!(sdi = zp_open_device(device_index))) {
- sr_warn("unable to open device");
+ sr_err("unable to open device");
return SR_ERR;
}
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;
}
{
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;
{
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;
{
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;
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)
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;
}
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 {
/* 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;
}
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;
} 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;
}
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;
}
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:
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. */
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;
}
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;
}
(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;
}
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;
}
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;
}
*data_out = outbuf;
*length_out = 4 + 1;
- free(o->internal);
+ g_free(o->internal);
o->internal = NULL;
break;
default:
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;
}
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;
}
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 */
}
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;
}
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;
}
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;
}
/* 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;
}
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;
}
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;
}
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;
}
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;
}
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 */
}
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;
}
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;
}
if (ctx && event_type == SR_DF_END) {
g_string_free(ctx->header, TRUE);
- free(o->internal);
+ g_free(o->internal);
o->internal = NULL;
}
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;
}
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. */
#include <string.h>
#include <glib.h>
#include "sigrok.h"
+#include "sigrok-internal.h"
#include "text.h"
SR_PRIV int init_hex(struct sr_output *o)
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;
}
#include <glib.h>
#include "config.h"
#include "sigrok.h"
+#include "sigrok-internal.h"
#include "text.h"
SR_PRIV void flush_linebufs(struct context *ctx, char *outbuf)
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;
} 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;
}
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;
}
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:
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;
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;
}
/* 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",
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 */
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);
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;
}
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:
*/
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? */
}
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;
}
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;
}
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;
}
}
}
}
- free(fds);
+ g_free(fds);
return SR_OK;
}
/**
* TODO.
*
- * TODO: Switch to g_try_malloc0() / g_free().
* TODO: More error checks etc.
*
* @param fd TODO.
/* 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;
if (sources) {
memcpy(new_sources, sources,
sizeof(struct source) * num_sources);
- free(sources);
+ g_free(sources);
}
s = &new_sources[num_sources++];
* 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
/* 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;
}
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;
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);
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;
}
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) {
}
/* 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,
* 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);
if (r < 0) {
/* Something went wrong... */
- free(o);
+ g_free(o);
return NULL;
}
* 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);
if (r < 0) {
/* Something went wrong... */
- free(o);
+ g_free(o);
return NULL;
}