if (unitsize <= 0)
return SR_ERR; /* TODO: Different error? */
- if (!(*ds = g_malloc(sizeof(struct sr_datastore))))
+ if (!(*ds = g_try_malloc(sizeof(struct sr_datastore)))) {
+ sr_err("ds: %s: ds malloc failed", __func__);
return SR_ERR_MALLOC;
+ }
(*ds)->ds_unitsize = unitsize;
(*ds)->num_units = 0;
struct sr_device *device;
int i;
- device = g_malloc0(sizeof(struct sr_device));
+ if (!(device = g_try_malloc0(sizeof(struct sr_device)))) {
+ sr_err("dev: %s: device malloc failed", __func__);
+ return NULL;
+ }
+
device->plugin = plugin;
device->plugin_index = plugin_index;
devices = g_slist_append(devices, device);
int probenum;
probenum = g_slist_length(device->probes) + 1;
- p = g_malloc0(sizeof(struct sr_probe));
+
+ if (!(p = g_try_malloc0(sizeof(struct sr_probe)))) {
+ sr_err("dev: %s: p malloc failed", __func__);
+ // return SR_ERR_MALLOC;
+ return; /* FIXME: should return int. */
+ }
+
p->index = probenum;
p->enabled = TRUE;
if (name) {
f = g_fopen(filename, "rb");
if (!f) {
sr_warn("g_fopen(\"%s\", \"rb\")", filename);
- return -1;
+ return SR_ERR;
}
if (-1 == fseek(f, 0, SEEK_END)) {
sr_warn("fseek on %s failed", filename);
fclose(f);
- return -1;
+ return SR_ERR;
}
file_size = ftell(f);
fseek(f, 0, SEEK_SET);
- compressed_buf = g_malloc(file_size);
- firmware = g_malloc(buffer_size);
+ if (!(compressed_buf = g_try_malloc(file_size))) {
+ sr_err("asix: %s: compressed_buf malloc failed", __func__);
+ return SR_ERR_MALLOC;
+ }
- if (!compressed_buf || !firmware) {
- sr_warn("Error allocating buffers");
- return -1;
+ if (!(firmware = g_try_malloc(buffer_size))) {
+ sr_err("asix: %s: firmware malloc failed", __func__);
+ return SR_ERR_MALLOC;
}
csize = 0;
g_free(compressed_buf);
g_free(firmware);
sr_warn("Could not unpack Sigma firmware. (Error %d)\n", ret);
- return -1;
+ return SR_ERR;
}
g_free(compressed_buf);
*buf_size = fwsize * 2 * 8;
- *buf = p = (unsigned char *)g_malloc(*buf_size);
-
+ *buf = p = (unsigned char *)g_try_malloc(*buf_size);
if (!p) {
- sr_warn("Error allocating buffers");
- return -1;
+ sr_err("asix: %s: buf/p malloc failed", __func__);
+ return SR_ERR_MALLOC;
}
for (i = 0; i < fwsize; ++i) {
"offset=%ld, file_size=%ld, buf_size=%zd\n",
filename, offset, file_size, *buf_size);
- return -1;
+ return SR_ERR;
}
- return 0;
+ return SR_OK;
}
static int hw_init(const char *deviceinfo)
{
struct sr_device_instance *sdi;
- struct sigma *sigma = g_malloc(sizeof(struct sigma));
+ struct sigma *sigma;
+ /* Avoid compiler warnings. */
deviceinfo = deviceinfo;
- if (!sigma)
- return 0;
+ if (!(sigma = g_try_malloc(sizeof(struct sigma)))) {
+ sr_err("asix: %s: sigma malloc failed", __func__);
+ return 0; /* FIXME: Should be SR_ERR_MALLOC. */
+ }
ftdi_init(&sigma->ftdic);
snprintf(firmware_path, sizeof(firmware_path), "%s/%s", FIRMWARE_DIR,
firmware_files[firmware_idx]);
- if (-1 == bin2bitbang(firmware_path, &buf, &buf_size)) {
+ if ((ret = bin2bitbang(firmware_path, &buf, &buf_size)) != SR_OK) {
sr_warn("An error occured while reading the firmware: %s",
firmware_path);
- return SR_ERR;
+ return ret;
}
/* Upload firmare. */
{
struct ols_device *ols;
- ols = g_malloc0(sizeof(struct ols_device));
+ if (!(ols = g_try_malloc0(sizeof(struct ols_device)))) {
+ sr_err("ols: %s: ols malloc failed", __func__);
+ return NULL;
+ }
+
ols->trigger_at = -1;
ols->probe_mask = 0xffffffff;
ols->cur_samplerate = SR_KHZ(200);
if (ols->sample[0] & 0x80
&& !(ols->last_sample[0] & 0x80)) {
count = (int)(*ols->sample) & 0x7fffffff;
- buffer = g_malloc(count);
+ if (!(buffer = g_try_malloc(count))) {
+ sr_err("ols: %s: buffer malloc "
+ "failed", __func__);
+ return FALSE;
+ }
+
buflen = 0;
for (i = 0; i < count; i++) {
memcpy(buffer + buflen, ols->last_sample, 4);
sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
session_device_id);
+ if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
+ sr_err("ols: %s: packet malloc failed", __func__);
+ return SR_ERR_MALLOC;
+ }
+
+ if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
+ sr_err("ols: %s: header malloc failed", __func__);
+ return SR_ERR_MALLOC;
+ }
+
/* Send header packet to the session bus. */
- packet = g_malloc(sizeof(struct sr_datafeed_packet));
- header = g_malloc(sizeof(struct sr_datafeed_header));
- if (!packet || !header)
- return SR_ERR;
packet->type = SR_DF_HEADER;
packet->length = sizeof(struct sr_datafeed_header);
packet->payload = (unsigned char *)header;
user_data = transfer->user_data;
/* Fire off a new request. */
- new_buf = g_malloc(4096);
+ if (!(new_buf = g_try_malloc(4096))) {
+ sr_err("saleae: %s: new_buf malloc failed", __func__);
+ // return SR_ERR_MALLOC;
+ return; /* FIXME */
+ }
+
transfer->buffer = new_buf;
transfer->length = 4096;
if (libusb_submit_transfer(transfer) != 0) {
if (!(sdi = sr_get_device_instance(device_instances, device_index)))
return SR_ERR;
- packet = g_malloc(sizeof(struct sr_datafeed_packet));
- header = g_malloc(sizeof(struct sr_datafeed_header));
- if (!packet || !header)
- return SR_ERR;
+ if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
+ sr_err("saleae: %s: packet malloc failed", __func__);
+ return SR_ERR_MALLOC;
+ }
+
+ if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
+ sr_err("saleae: %s: header malloc failed", __func__);
+ return SR_ERR_MALLOC;
+ }
/* Start with 2K transfer, subsequently increased to 4K. */
size = 2048;
for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
- buf = g_malloc(size);
+ if (!(buf = g_try_malloc(size))) {
+ sr_err("saleae: %s: buf malloc failed", __func__);
+ return SR_ERR_MALLOC;
+ }
transfer = libusb_alloc_transfer(0);
libusb_fill_bulk_transfer(transfer, sdi->usb->devhdl,
2 | LIBUSB_ENDPOINT_IN, buf, size,
header.num_analog_probes = 0;
sr_session_bus(session_device_id, &packet);
- buf = g_malloc(PACKET_SIZE);
- if (!buf)
- return SR_ERR;
+ if (!(buf = g_try_malloc(PACKET_SIZE))) {
+ sr_err("zeroplus: %s: buf malloc failed", __func__);
+ return SR_ERR_MALLOC;
+ }
+
analyzer_read_start(sdi->usb->devhdl);
/* Send the incoming transfer to the session bus. */
for (packet_num = 0; packet_num < (memory_size * 4 / PACKET_SIZE);
#include <string.h>
#include <glib.h>
#include <sigrok.h>
+#include <sigrok-internal.h>
#include "config.h"
struct context {
uint64_t samplerate;
int num_enabled_probes;
- if (!(ctx = g_malloc(sizeof(struct context))))
+ if (!(ctx = g_try_malloc(sizeof(struct context)))) {
+ sr_err("ols out: %s: ctx malloc failed", __func__);
return SR_ERR_MALLOC;
+ }
o->internal = ctx;
ctx->num_samples = 0;
/* already done with this instance */
continue;
- buf = g_malloc(CHUNKSIZE);
+ if (!(buf = g_try_malloc(CHUNKSIZE))) {
+ sr_err("session: %s: buf malloc failed", __func__);
+ // return SR_ERR_MALLOC;
+ return FALSE;
+ }
+
ret = zip_fread(vdevice->capfile, buf, CHUNKSIZE);
if (ret > 0) {
got_data = TRUE;
NULL, NULL, NULL);
if (!sdi)
return SR_ERR;
- sdi->priv = g_malloc0(sizeof(struct session_vdevice));
- if (!sdi->priv)
- return SR_ERR;
+
+ if (!(sdi->priv = g_try_malloc0(sizeof(struct session_vdevice)))) {
+ sr_err("session: %s: sdi->priv malloc failed", __func__);
+ return SR_ERR_MALLOC;
+ }
+
device_instances = g_slist_append(device_instances, sdi);
return SR_OK;
/* freewheeling source */
sr_session_source_add(-1, 0, 0, feed_chunk, session_device_id);
+ if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
+ sr_err("session: %s: packet malloc failed", __func__);
+ return SR_ERR_MALLOC;
+ }
+
+ if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
+ sr_err("session: %s: header malloc failed", __func__);
+ return SR_ERR_MALLOC;
+ }
+
/* Send header packet to the session bus. */
- packet = g_malloc(sizeof(struct sr_datafeed_packet));
- header = g_malloc(sizeof(struct sr_datafeed_header));
- if (!packet || !header)
- return SR_ERR;
packet->type = SR_DF_HEADER;
packet->length = sizeof(struct sr_datafeed_header);
packet->payload = (unsigned char *)header;
return SR_OK;
}
-
struct sr_device_plugin session_driver = {
"session",
"Session-emulating driver",
sr_dbg("Not a valid sigrok session file.");
return SR_ERR;
}
- metafile = g_malloc(zs.size);
+
+ if (!(metafile = g_try_malloc(zs.size))) {
+ sr_err("session file: %s: metafile malloc failed", __func__);
+ return SR_ERR_MALLOC;
+ }
+
zf = zip_fopen_index(archive, zs.index, 0);
zip_fread(zf, metafile, zs.size);
zip_fclose(zf);
#include <stdlib.h>
#include <string.h>
#include <sigrok.h>
+#include <sigrok-internal.h>
/**
* Convert a numeric samplerate value to its "natural" string representation.
max_probes = g_slist_length(device->probes);
error = FALSE;
- triggerlist = g_malloc0(max_probes * sizeof(char *));
+
+ if (!(triggerlist = g_try_malloc0(max_probes * sizeof(char *)))) {
+ sr_err("session file: %s: metafile malloc failed", __func__);
+ return NULL;
+ }
+
tokens = g_strsplit(triggerstring, ",", max_probes);
trigger_types = device->plugin->get_device_info(0, SR_DI_TRIGGER_TYPES);
if (trigger_types == NULL)