Use g_malloc0() consistently, simplify error handling.

Use g_malloc0() for small allocations and assume they always
succeed. Simplify error handling in a few places accordingly.

Don't always sanity-check parameters for non-public (SR_PRIV)
functions, we require the developers to invoke them correctly.
This allows further error handling simplifications.
This commit is contained in:
Uwe Hermann 2014-11-21 19:02:10 +01:00
parent c368e6f3d2
commit 91219afc75
31 changed files with 55 additions and 169 deletions

View File

@ -348,12 +348,7 @@ SR_API int sr_init(struct sr_context **ctx)
} }
/* + 1 to handle when struct sr_context has no members. */ /* + 1 to handle when struct sr_context has no members. */
context = g_try_malloc0(sizeof(struct sr_context) + 1); context = g_malloc0(sizeof(struct sr_context) + 1);
if (!context) {
ret = SR_ERR_MALLOC;
goto done;
}
#ifdef HAVE_LIBUSB_1_0 #ifdef HAVE_LIBUSB_1_0
ret = libusb_init(&context->libusb_ctx); ret = libusb_init(&context->libusb_ctx);

View File

@ -281,7 +281,6 @@ SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
* @param[in] address @copydoc sr_usb_dev_inst::address * @param[in] address @copydoc sr_usb_dev_inst::address
* @param[in] hdl @copydoc sr_usb_dev_inst::devhdl * @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
* *
* @retval NULL Error
* @retval other struct sr_usb_dev_inst * for USB device instance. * @retval other struct sr_usb_dev_inst * for USB device instance.
*/ */
SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
@ -289,11 +288,7 @@ SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
{ {
struct sr_usb_dev_inst *udi; struct sr_usb_dev_inst *udi;
if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) { udi = g_malloc0(sizeof(struct sr_usb_dev_inst));
sr_err("USB device instance malloc failed.");
return NULL;
}
udi->bus = bus; udi->bus = bus;
udi->address = address; udi->address = address;
udi->devhdl = hdl; udi->devhdl = hdl;
@ -322,10 +317,11 @@ SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
* *
* @param[in] port OS-specific serial port specification. Examples: * @param[in] port OS-specific serial port specification. Examples:
* "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1". * "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
* Must not be NULL.
* @param[in] serialcomm A serial communication parameters string, in the form * @param[in] serialcomm A serial communication parameters string, in the form
* of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example * of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
* "9600/8n1" or "600/7o2". This is an optional parameter; * "9600/8n1" or "600/7o2". This is an optional parameter;
* it may be filled in later. * it may be filled in later. Can be NULL.
* *
* @return A pointer to a newly initialized struct sr_serial_dev_inst, * @return A pointer to a newly initialized struct sr_serial_dev_inst,
* or NULL on error. * or NULL on error.
@ -335,16 +331,7 @@ SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
{ {
struct sr_serial_dev_inst *serial; struct sr_serial_dev_inst *serial;
if (!port) { serial = g_malloc0(sizeof(struct sr_serial_dev_inst));
sr_err("Serial port required.");
return NULL;
}
if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) {
sr_err("Serial device instance malloc failed.");
return NULL;
}
serial->port = g_strdup(port); serial->port = g_strdup(port);
if (serialcomm) if (serialcomm)
serial->serialcomm = g_strdup(serialcomm); serial->serialcomm = g_strdup(serialcomm);
@ -369,16 +356,7 @@ SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
{ {
struct sr_usbtmc_dev_inst *usbtmc; struct sr_usbtmc_dev_inst *usbtmc;
if (!device) { usbtmc = g_malloc0(sizeof(struct sr_usbtmc_dev_inst));
sr_err("Device name required.");
return NULL;
}
if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) {
sr_err("USBTMC device instance malloc failed.");
return NULL;
}
usbtmc->device = g_strdup(device); usbtmc->device = g_strdup(device);
usbtmc->fd = -1; usbtmc->fd = -1;

View File

@ -112,8 +112,7 @@ static GSList *scan(GSList *options)
if (!serialcomm) if (!serialcomm)
serialcomm = SERIALCOMM; serialcomm = SERIALCOMM;
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDWR) != SR_OK) if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL; return NULL;

View File

@ -81,8 +81,8 @@ static GSList *scan(GSList *options)
if (!serialcomm) if (!serialcomm)
serialcomm = "9600/8n1"; serialcomm = "9600/8n1";
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDONLY) != SR_OK) if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
return NULL; return NULL;

View File

@ -122,11 +122,11 @@ static GSList *scan(GSList *options, int modelid)
if (!serialcomm) if (!serialcomm)
serialcomm = SERIALCOMM; serialcomm = SERIALCOMM;
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDWR) != SR_OK) if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL; return NULL;
serial_flush(serial); serial_flush(serial);
/* This is how the vendor software scans for hardware. */ /* This is how the vendor software scans for hardware. */

View File

@ -51,8 +51,7 @@ static GSList *brymen_scan(const char *conn, const char *serialcomm)
uint8_t buf[128]; uint8_t buf[128];
size_t len; size_t len;
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDWR) != SR_OK) if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL; return NULL;

View File

@ -97,8 +97,7 @@ static GSList *scan(GSList *options)
if (!conn) if (!conn)
return NULL; return NULL;
if (!(serial = sr_serial_dev_inst_new(conn, SERIALCOMM))) serial = sr_serial_dev_inst_new(conn, SERIALCOMM);
return NULL;
if (serial_open(serial, SERIAL_RDONLY) != SR_OK) if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
return NULL; return NULL;
@ -119,10 +118,7 @@ static GSList *scan(GSList *options)
devc->cur_meas_range = 0; devc->cur_meas_range = 0;
devc->cur_data_source = DATA_SOURCE_LIVE; devc->cur_data_source = DATA_SOURCE_LIVE;
devc->enable_data_source_memory = FALSE; devc->enable_data_source_memory = FALSE;
sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM);
if (!(sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM)))
return NULL;
sdi->inst_type = SR_INST_SERIAL; sdi->inst_type = SR_INST_SERIAL;
sdi->priv = devc; sdi->priv = devc;
sdi->driver = di; sdi->driver = di;

View File

@ -76,8 +76,7 @@ static GSList *center_scan(const char *conn, const char *serialcomm, int idx)
struct sr_serial_dev_inst *serial; struct sr_serial_dev_inst *serial;
GSList *devices; GSList *devices;
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDWR) != SR_OK) if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL; return NULL;

View File

@ -87,8 +87,7 @@ static GSList *scan(GSList *options)
sdi->vendor = g_strdup("Colead"); sdi->vendor = g_strdup("Colead");
sdi->model = g_strdup("SL-5868P"); sdi->model = g_strdup("SL-5868P");
devc = g_malloc0(sizeof(struct dev_context)); devc = g_malloc0(sizeof(struct dev_context));
if (!(sdi->conn = sr_serial_dev_inst_new(conn, serialcomm))) sdi->conn = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
sdi->inst_type = SR_INST_SERIAL; sdi->inst_type = SR_INST_SERIAL;
sdi->priv = devc; sdi->priv = devc;
sdi->driver = di; sdi->driver = di;

View File

@ -83,8 +83,7 @@ static GSList *scan(GSList *options)
* the device is there. * the device is there.
*/ */
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDWR) != SR_OK) if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL; return NULL;

View File

@ -75,8 +75,7 @@ static GSList *fluke_scan(const char *conn, const char *serialcomm)
int retry, len, i, s; int retry, len, i, s;
char buf[128], *b, **tokens; char buf[128], *b, **tokens;
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDWR) != SR_OK) if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL; return NULL;

View File

@ -195,8 +195,7 @@ static GSList *scan_1x_2x_rs232(GSList *options)
if (!serialcomm) if (!serialcomm)
serialcomm = SERIALCOMM_2X_RS232; serialcomm = SERIALCOMM_2X_RS232;
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDWR) != SR_OK) { if (serial_open(serial, SERIAL_RDWR) != SR_OK) {
sr_serial_dev_inst_free(serial); sr_serial_dev_inst_free(serial);
@ -288,8 +287,7 @@ static GSList *scan_2x_bd232(GSList *options)
if (!serialcomm) if (!serialcomm)
serialcomm = SERIALCOMM_2X; serialcomm = SERIALCOMM_2X;
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDWR) != SR_OK) if (serial_open(serial, SERIAL_RDWR) != SR_OK)
goto exit_err; goto exit_err;

View File

@ -195,10 +195,7 @@ static GSList *scan(GSList *options)
devc->protocol_trigger.mask[i] = 0xff; devc->protocol_trigger.mask[i] = 0xff;
} }
if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm))) { devc->serial = sr_serial_dev_inst_new(conn, serialcomm);
g_free(devc);
return devices;
}
struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst)); struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst));
sdi->status = SR_ST_INACTIVE; sdi->status = SR_ST_INACTIVE;

View File

@ -125,8 +125,7 @@ static GSList *scan(GSList *options)
if (!serialcomm) if (!serialcomm)
serialcomm = "9600/8n1"; serialcomm = "9600/8n1";
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDWR) != SR_OK) if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL; return NULL;

View File

@ -75,8 +75,7 @@ static GSList *mic_scan(const char *conn, const char *serialcomm, int idx)
struct sr_serial_dev_inst *serial; struct sr_serial_dev_inst *serial;
GSList *devices; GSList *devices;
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDWR) != SR_OK) if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL; return NULL;

View File

@ -402,8 +402,7 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o
serialcomm = SERIALCOMM; serialcomm = SERIALCOMM;
/* Init serial port. */ /* Init serial port. */
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDWR) != SR_OK) if (serial_open(serial, SERIAL_RDWR) != SR_OK)
goto exit_err; goto exit_err;

View File

@ -112,8 +112,7 @@ static GSList *do_scan(struct sr_dev_driver* drv, GSList *options)
if (!serialcomm) if (!serialcomm)
serialcomm = SERIALCOMM; serialcomm = SERIALCOMM;
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDWR) != SR_OK) if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL; return NULL;

View File

@ -128,8 +128,7 @@ static GSList *scan(GSList *options)
if (serialcomm == NULL) if (serialcomm == NULL)
serialcomm = SERIALCOMM; serialcomm = SERIALCOMM;
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
/* The discovery procedure is like this: first send the Reset /* The discovery procedure is like this: first send the Reset
* command (0x00) 5 times, since the device could be anywhere * command (0x00) 5 times, since the device could be anywhere

View File

@ -409,8 +409,7 @@ static GSList *sdmm_scan(const char *conn, const char *serialcomm, int dmm)
size_t len; size_t len;
uint8_t buf[128]; uint8_t buf[128];
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDWR) != SR_OK) if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL; return NULL;

View File

@ -74,8 +74,8 @@ static GSList *scan(GSList *options)
if (!serialcomm) if (!serialcomm)
serialcomm = "1200/7e1"; serialcomm = "1200/7e1";
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDONLY) != SR_OK) if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
return NULL; return NULL;

View File

@ -90,8 +90,7 @@ static GSList *scan(GSList *options)
sdi->model = g_strdup("SL-814"); sdi->model = g_strdup("SL-814");
devc = g_malloc0(sizeof(struct dev_context)); devc = g_malloc0(sizeof(struct dev_context));
if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) serial = sr_serial_dev_inst_new(conn, serialcomm);
return NULL;
if (serial_open(serial, SERIAL_RDWR) != SR_OK) if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL; return NULL;

View File

@ -95,9 +95,8 @@ static GSList *scan(GSList *options)
ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1"); ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1");
sdi->channels = g_slist_append(NULL, ch); sdi->channels = g_slist_append(NULL, ch);
if (!(sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
libusb_get_device_address(devlist[i]), NULL))) libusb_get_device_address(devlist[i]), NULL);
return NULL;
sdi->inst_type = SR_INST_USB; sdi->inst_type = SR_INST_USB;
drvc->instances = g_slist_append(drvc->instances, sdi); drvc->instances = g_slist_append(drvc->instances, sdi);

View File

@ -395,8 +395,7 @@ SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data)
{ {
struct sr_config *src; struct sr_config *src;
if (!(src = g_try_malloc(sizeof(struct sr_config)))) src = g_malloc0(sizeof(struct sr_config));
return NULL;
src->key = key; src->key = key;
src->data = g_variant_ref_sink(data); src->data = g_variant_ref_sink(data);

View File

@ -41,11 +41,7 @@ static struct dev_buffer *dev_buffer_new(size_t size)
{ {
struct dev_buffer *dbuf; struct dev_buffer *dbuf;
if (!(dbuf = g_try_malloc(sizeof(struct dev_buffer) + size))) { dbuf = g_malloc0(sizeof(struct dev_buffer) + size);
sr_err("Dev buffer malloc failed (size=%zu).", size);
return NULL;
}
dbuf->size = size; dbuf->size = size;
dbuf->len = 0; dbuf->len = 0;
dbuf->offset = 0; dbuf->offset = 0;
@ -846,13 +842,9 @@ SR_PRIV struct sr_dev_inst *es51919_serial_scan(GSList *options,
sdi->vendor = g_strdup(vendor); sdi->vendor = g_strdup(vendor);
sdi->model = g_strdup(model); sdi->model = g_strdup(model);
devc = g_malloc0(sizeof(struct dev_context)); devc = g_malloc0(sizeof(struct dev_context));
devc->buf = dev_buffer_new(PACKET_SIZE * 8);
if (!(devc->buf = dev_buffer_new(PACKET_SIZE * 8)))
goto scan_cleanup;
sdi->inst_type = SR_INST_SERIAL; sdi->inst_type = SR_INST_SERIAL;
sdi->conn = serial; sdi->conn = serial;
sdi->priv = devc; sdi->priv = devc;
if (setup_channels(sdi) != SR_OK) if (setup_channels(sdi) != SR_OK)

View File

@ -47,7 +47,7 @@ static int init(struct sr_output *o, GHashTable *options)
if (!o || !o->sdi) if (!o || !o->sdi)
return SR_ERR_ARG; return SR_ERR_ARG;
o->priv = ctx = g_try_malloc0(sizeof(struct context)); o->priv = ctx = g_malloc0(sizeof(struct context));
s = g_variant_get_string(g_hash_table_lookup(options, "digits"), NULL); s = g_variant_get_string(g_hash_table_lookup(options, "digits"), NULL);
if (!strcmp(s, "all")) if (!strcmp(s, "all"))
ctx->digits = DIGITS_ALL; ctx->digits = DIGITS_ALL;

View File

@ -44,12 +44,8 @@ static int init(struct sr_output *o, GHashTable *options)
(void)options; (void)options;
if (!(ctx = g_try_malloc(sizeof(struct context)))) { ctx = g_malloc0(sizeof(struct context));
sr_err("%s: ctx malloc failed", __func__);
return SR_ERR_MALLOC;
}
o->priv = ctx; o->priv = ctx;
ctx->samplerate = 0; ctx->samplerate = 0;
ctx->num_samples = 0; ctx->num_samples = 0;

View File

@ -742,12 +742,7 @@ SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi,
} }
g_free(response); g_free(response);
hw_info = g_try_malloc(sizeof(struct sr_scpi_hw_info)); hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info));
if (!hw_info) {
g_strfreev(tokens);
return SR_ERR_MALLOC;
}
hw_info->manufacturer = g_strdup(tokens[0]); hw_info->manufacturer = g_strdup(tokens[0]);
hw_info->model = g_strdup(tokens[1]); hw_info->model = g_strdup(tokens[1]);
hw_info->serial_number = g_strdup(tokens[2]); hw_info->serial_number = g_strdup(tokens[2]);

View File

@ -79,8 +79,7 @@ static int scpi_serial_dev_inst_new(void *priv, struct drv_context *drvc,
(void)drvc; (void)drvc;
(void)params; (void)params;
if (!(sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm))) sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm);
return SR_ERR;
return SR_OK; return SR_OK;
} }

View File

@ -306,9 +306,7 @@ SR_API int sr_session_datafeed_callback_add(struct sr_session *session,
return SR_ERR_ARG; return SR_ERR_ARG;
} }
if (!(cb_struct = g_try_malloc0(sizeof(struct datafeed_callback)))) cb_struct = g_malloc0(sizeof(struct datafeed_callback));
return SR_ERR_MALLOC;
cb_struct->cb = cb; cb_struct->cb = cb;
cb_struct->cb_data = cb_data; cb_struct->cb_data = cb_data;
@ -712,7 +710,6 @@ SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
* *
* @retval SR_OK Success. * @retval SR_OK Success.
* @retval SR_ERR_ARG Invalid argument. * @retval SR_ERR_ARG Invalid argument.
* @retval SR_ERR_MALLOC Memory allocation error.
*/ */
static int _sr_session_source_add(struct sr_session *session, GPollFD *pollfd, static int _sr_session_source_add(struct sr_session *session, GPollFD *pollfd,
int timeout, sr_receive_data_callback cb, void *cb_data, gintptr poll_object) int timeout, sr_receive_data_callback cb, void *cb_data, gintptr poll_object)
@ -727,19 +724,10 @@ static int _sr_session_source_add(struct sr_session *session, GPollFD *pollfd,
/* Note: cb_data can be NULL, that's not a bug. */ /* Note: cb_data can be NULL, that's not a bug. */
new_pollfds = g_try_realloc(session->pollfds, new_pollfds = g_realloc(session->pollfds,
sizeof(GPollFD) * (session->num_sources + 1)); sizeof(GPollFD) * (session->num_sources + 1));
if (!new_pollfds) { new_sources = g_realloc(session->sources, sizeof(struct source) *
sr_err("%s: new_pollfds malloc failed", __func__);
return SR_ERR_MALLOC;
}
new_sources = g_try_realloc(session->sources, sizeof(struct source) *
(session->num_sources + 1)); (session->num_sources + 1));
if (!new_sources) {
sr_err("%s: new_sources malloc failed", __func__);
return SR_ERR_MALLOC;
}
new_pollfds[session->num_sources] = *pollfd; new_pollfds[session->num_sources] = *pollfd;
s = &new_sources[session->num_sources++]; s = &new_sources[session->num_sources++];
@ -769,7 +757,6 @@ static int _sr_session_source_add(struct sr_session *session, GPollFD *pollfd,
* *
* @retval SR_OK Success. * @retval SR_OK Success.
* @retval SR_ERR_ARG Invalid argument. * @retval SR_ERR_ARG Invalid argument.
* @retval SR_ERR_MALLOC Memory allocation error.
* *
* @since 0.3.0 * @since 0.3.0
*/ */
@ -795,7 +782,6 @@ SR_API int sr_session_source_add(struct sr_session *session, int fd,
* *
* @retval SR_OK Success. * @retval SR_OK Success.
* @retval SR_ERR_ARG Invalid argument. * @retval SR_ERR_ARG Invalid argument.
* @retval SR_ERR_MALLOC Memory allocation error.
* *
* @since 0.3.0 * @since 0.3.0
*/ */
@ -819,7 +805,6 @@ SR_API int sr_session_source_add_pollfd(struct sr_session *session,
* *
* @retval SR_OK Success. * @retval SR_OK Success.
* @retval SR_ERR_ARG Invalid argument. * @retval SR_ERR_ARG Invalid argument.
* @retval SR_ERR_MALLOC Memory allocation error.
* *
* @since 0.3.0 * @since 0.3.0
*/ */
@ -842,20 +827,15 @@ SR_API int sr_session_source_add_channel(struct sr_session *session,
/** /**
* Remove the source belonging to the specified channel. * Remove the source belonging to the specified channel.
* *
* @todo Add more error checks and logging.
*
* @param session The session to use. Must not be NULL. * @param session The session to use. Must not be NULL.
* @param poll_object The channel for which the source should be removed. * @param poll_object The channel for which the source should be removed.
* *
* @retval SR_OK Success * @retval SR_OK Success
* @retval SR_ERR_ARG Invalid arguments * @retval SR_ERR_ARG Invalid arguments
* @retval SR_ERR_MALLOC Memory allocation error
* @retval SR_ERR_BUG Internal error * @retval SR_ERR_BUG Internal error
*/ */
static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object) static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object)
{ {
struct source *new_sources;
GPollFD *new_pollfds;
unsigned int old; unsigned int old;
if (!session->sources || !session->num_sources) { if (!session->sources || !session->num_sources) {
@ -872,7 +852,7 @@ static int _sr_session_source_remove(struct sr_session *session, gintptr poll_ob
if (old == session->num_sources) if (old == session->num_sources)
return SR_OK; return SR_OK;
session->num_sources -= 1; session->num_sources--;
if (old != session->num_sources) { if (old != session->num_sources) {
memmove(&session->pollfds[old], &session->pollfds[old + 1], memmove(&session->pollfds[old], &session->pollfds[old + 1],
@ -881,20 +861,8 @@ static int _sr_session_source_remove(struct sr_session *session, gintptr poll_ob
(session->num_sources - old) * sizeof(struct source)); (session->num_sources - old) * sizeof(struct source));
} }
new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources); session->pollfds = g_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
if (!new_pollfds && session->num_sources > 0) { session->sources = g_realloc(session->sources, sizeof(struct source) * session->num_sources);
sr_err("%s: new_pollfds malloc failed", __func__);
return SR_ERR_MALLOC;
}
new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources);
if (!new_sources && session->num_sources > 0) {
sr_err("%s: new_sources malloc failed", __func__);
return SR_ERR_MALLOC;
}
session->pollfds = new_pollfds;
session->sources = new_sources;
return SR_OK; return SR_OK;
} }
@ -907,7 +875,6 @@ static int _sr_session_source_remove(struct sr_session *session, gintptr poll_ob
* *
* @retval SR_OK Success * @retval SR_OK Success
* @retval SR_ERR_ARG Invalid argument * @retval SR_ERR_ARG Invalid argument
* @retval SR_ERR_MALLOC Memory allocation error.
* @retval SR_ERR_BUG Internal error. * @retval SR_ERR_BUG Internal error.
* *
* @since 0.3.0 * @since 0.3.0
@ -943,7 +910,6 @@ SR_API int sr_session_source_remove_pollfd(struct sr_session *session,
* *
* @retval SR_OK Success. * @retval SR_OK Success.
* @retval SR_ERR_ARG Invalid argument. * @retval SR_ERR_ARG Invalid argument.
* @retval SR_ERR_MALLOC Memory allocation error.
* @return SR_ERR_BUG Internal error. * @return SR_ERR_BUG Internal error.
* *
* @since 0.2.0 * @since 0.2.0

View File

@ -41,8 +41,7 @@
* @param di The driver instance to use. * @param di The driver instance to use.
* @param[in] prefix A driver-specific prefix string used for log messages. * @param[in] prefix A driver-specific prefix string used for log messages.
* *
* @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
* SR_ERR_MALLOC upon memory allocation errors.
*/ */
SR_PRIV int std_init(struct sr_context *sr_ctx, struct sr_dev_driver *di, SR_PRIV int std_init(struct sr_context *sr_ctx, struct sr_dev_driver *di,
const char *prefix) const char *prefix)
@ -54,11 +53,7 @@ SR_PRIV int std_init(struct sr_context *sr_ctx, struct sr_dev_driver *di,
return SR_ERR_ARG; return SR_ERR_ARG;
} }
if (!(drvc = g_try_malloc(sizeof(struct drv_context)))) { drvc = g_malloc0(sizeof(struct drv_context));
sr_err("%s: Driver context malloc failed.", prefix);
return SR_ERR_MALLOC;
}
drvc->sr_ctx = sr_ctx; drvc->sr_ctx = sr_ctx;
drvc->instances = NULL; drvc->instances = NULL;
di->priv = drvc; di->priv = drvc;

View File

@ -230,7 +230,7 @@ SR_PRIV int sr_atof_ascii(const char *str, float *ret)
* @param unit The unit to append to the string, or NULL if the string * @param unit The unit to append to the string, or NULL if the string
* has no units. * has no units.
* *
* @return A g_try_malloc()ed string representation of the samplerate value, * @return A newly allocated string representation of the samplerate value,
* or NULL upon errors. The caller is responsible to g_free() the * or NULL upon errors. The caller is responsible to g_free() the
* memory. * memory.
* *
@ -272,7 +272,7 @@ SR_API char *sr_si_string_u64(uint64_t x, const char *unit)
* *
* @param samplerate The samplerate in Hz. * @param samplerate The samplerate in Hz.
* *
* @return A g_try_malloc()ed string representation of the samplerate value, * @return A newly allocated string representation of the samplerate value,
* or NULL upon errors. The caller is responsible to g_free() the * or NULL upon errors. The caller is responsible to g_free() the
* memory. * memory.
* *
@ -291,7 +291,7 @@ SR_API char *sr_samplerate_string(uint64_t samplerate)
* *
* @param frequency The frequency in Hz. * @param frequency The frequency in Hz.
* *
* @return A g_try_malloc()ed string representation of the frequency value, * @return A newly allocated string representation of the frequency value,
* or NULL upon errors. The caller is responsible to g_free() the * or NULL upon errors. The caller is responsible to g_free() the
* memory. * memory.
* *
@ -303,10 +303,7 @@ SR_API char *sr_period_string(uint64_t frequency)
int r; int r;
/* Allocate enough for a uint64_t as string + " ms". */ /* Allocate enough for a uint64_t as string + " ms". */
if (!(o = g_try_malloc0(30 + 1))) { o = g_malloc0(30 + 1);
sr_err("%s: o malloc failed", __func__);
return NULL;
}
if (frequency >= SR_GHZ(1)) if (frequency >= SR_GHZ(1))
r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000); r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
@ -336,7 +333,7 @@ SR_API char *sr_period_string(uint64_t frequency)
* @param v_p The voltage numerator. * @param v_p The voltage numerator.
* @param v_q The voltage denominator. * @param v_q The voltage denominator.
* *
* @return A g_try_malloc()ed string representation of the voltage value, * @return A newly allocated string representation of the voltage value,
* or NULL upon errors. The caller is responsible to g_free() the * or NULL upon errors. The caller is responsible to g_free() the
* memory. * memory.
* *
@ -347,10 +344,7 @@ SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q)
int r; int r;
char *o; char *o;
if (!(o = g_try_malloc0(30 + 1))) { o = g_malloc0(30 + 1);
sr_err("%s: o malloc failed", __func__);
return NULL;
}
if (v_q == 1000) if (v_q == 1000)
r = snprintf(o, 30, "%" PRIu64 "mV", v_p); r = snprintf(o, 30, "%" PRIu64 "mV", v_p);