}
/* + 1 to handle when struct sr_context has no members. */
- context = g_try_malloc0(sizeof(struct sr_context) + 1);
-
- if (!context) {
- ret = SR_ERR_MALLOC;
- goto done;
- }
+ context = g_malloc0(sizeof(struct sr_context) + 1);
#ifdef HAVE_LIBUSB_1_0
ret = libusb_init(&context->libusb_ctx);
* @param[in] address @copydoc sr_usb_dev_inst::address
* @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
*
- * @retval NULL Error
* @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,
{
struct sr_usb_dev_inst *udi;
- if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
- sr_err("USB device instance malloc failed.");
- return NULL;
- }
-
+ udi = g_malloc0(sizeof(struct sr_usb_dev_inst));
udi->bus = bus;
udi->address = address;
udi->devhdl = hdl;
*
* @param[in] port OS-specific serial port specification. Examples:
* "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
+ * Must not be NULL.
* @param[in] serialcomm A serial communication parameters string, in the form
* of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
* "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,
* or NULL on error.
{
struct sr_serial_dev_inst *serial;
- if (!port) {
- 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 = g_malloc0(sizeof(struct sr_serial_dev_inst));
serial->port = g_strdup(port);
if (serialcomm)
serial->serialcomm = g_strdup(serialcomm);
{
struct sr_usbtmc_dev_inst *usbtmc;
- if (!device) {
- 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 = g_malloc0(sizeof(struct sr_usbtmc_dev_inst));
usbtmc->device = g_strdup(device);
usbtmc->fd = -1;
if (!serialcomm)
serialcomm = SERIALCOMM;
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
if (!serialcomm)
serialcomm = "9600/8n1";
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
+
if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
return NULL;
if (!serialcomm)
serialcomm = SERIALCOMM;
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
+
serial_flush(serial);
/* This is how the vendor software scans for hardware. */
uint8_t buf[128];
size_t len;
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
if (!conn)
return NULL;
- if (!(serial = sr_serial_dev_inst_new(conn, SERIALCOMM)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, SERIALCOMM);
if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
return NULL;
devc->cur_meas_range = 0;
devc->cur_data_source = DATA_SOURCE_LIVE;
devc->enable_data_source_memory = FALSE;
-
- if (!(sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM)))
- return NULL;
-
+ sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM);
sdi->inst_type = SR_INST_SERIAL;
sdi->priv = devc;
sdi->driver = di;
struct sr_serial_dev_inst *serial;
GSList *devices;
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
sdi->vendor = g_strdup("Colead");
sdi->model = g_strdup("SL-5868P");
devc = g_malloc0(sizeof(struct dev_context));
- if (!(sdi->conn = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ sdi->conn = sr_serial_dev_inst_new(conn, serialcomm);
sdi->inst_type = SR_INST_SERIAL;
sdi->priv = devc;
sdi->driver = di;
* the device is there.
*/
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
int retry, len, i, s;
char buf[128], *b, **tokens;
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
if (!serialcomm)
serialcomm = SERIALCOMM_2X_RS232;
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
if (serial_open(serial, SERIAL_RDWR) != SR_OK) {
sr_serial_dev_inst_free(serial);
if (!serialcomm)
serialcomm = SERIALCOMM_2X;
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
goto exit_err;
devc->protocol_trigger.mask[i] = 0xff;
}
- if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm))) {
- g_free(devc);
- return devices;
- }
+ devc->serial = sr_serial_dev_inst_new(conn, serialcomm);
struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst));
sdi->status = SR_ST_INACTIVE;
if (!serialcomm)
serialcomm = "9600/8n1";
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
struct sr_serial_dev_inst *serial;
GSList *devices;
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
serialcomm = SERIALCOMM;
/* Init serial port. */
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
goto exit_err;
if (!serialcomm)
serialcomm = SERIALCOMM;
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
if (serialcomm == NULL)
serialcomm = SERIALCOMM;
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
/* The discovery procedure is like this: first send the Reset
* command (0x00) 5 times, since the device could be anywhere
size_t len;
uint8_t buf[128];
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
if (!serialcomm)
serialcomm = "1200/7e1";
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
+
if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
return NULL;
sdi->model = g_strdup("SL-814");
devc = g_malloc0(sizeof(struct dev_context));
- if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
- return NULL;
+ serial = sr_serial_dev_inst_new(conn, serialcomm);
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1");
sdi->channels = g_slist_append(NULL, ch);
- if (!(sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
- libusb_get_device_address(devlist[i]), NULL)))
- return NULL;
+ sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
+ libusb_get_device_address(devlist[i]), NULL);
sdi->inst_type = SR_INST_USB;
drvc->instances = g_slist_append(drvc->instances, sdi);
{
struct sr_config *src;
- if (!(src = g_try_malloc(sizeof(struct sr_config))))
- return NULL;
+ src = g_malloc0(sizeof(struct sr_config));
src->key = key;
src->data = g_variant_ref_sink(data);
{
struct dev_buffer *dbuf;
- if (!(dbuf = g_try_malloc(sizeof(struct dev_buffer) + size))) {
- sr_err("Dev buffer malloc failed (size=%zu).", size);
- return NULL;
- }
-
+ dbuf = g_malloc0(sizeof(struct dev_buffer) + size);
dbuf->size = size;
dbuf->len = 0;
dbuf->offset = 0;
sdi->vendor = g_strdup(vendor);
sdi->model = g_strdup(model);
devc = g_malloc0(sizeof(struct dev_context));
-
- if (!(devc->buf = dev_buffer_new(PACKET_SIZE * 8)))
- goto scan_cleanup;
-
+ devc->buf = dev_buffer_new(PACKET_SIZE * 8);
sdi->inst_type = SR_INST_SERIAL;
sdi->conn = serial;
-
sdi->priv = devc;
if (setup_channels(sdi) != SR_OK)
if (!o || !o->sdi)
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);
if (!strcmp(s, "all"))
ctx->digits = DIGITS_ALL;
(void)options;
- if (!(ctx = g_try_malloc(sizeof(struct context)))) {
- sr_err("%s: ctx malloc failed", __func__);
- return SR_ERR_MALLOC;
- }
+ ctx = g_malloc0(sizeof(struct context));
o->priv = ctx;
-
ctx->samplerate = 0;
ctx->num_samples = 0;
}
g_free(response);
- hw_info = g_try_malloc(sizeof(struct sr_scpi_hw_info));
- if (!hw_info) {
- g_strfreev(tokens);
- return SR_ERR_MALLOC;
- }
-
+ hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info));
hw_info->manufacturer = g_strdup(tokens[0]);
hw_info->model = g_strdup(tokens[1]);
hw_info->serial_number = g_strdup(tokens[2]);
(void)drvc;
(void)params;
- if (!(sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm)))
- return SR_ERR;
+ sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm);
return SR_OK;
}
return SR_ERR_ARG;
}
- if (!(cb_struct = g_try_malloc0(sizeof(struct datafeed_callback))))
- return SR_ERR_MALLOC;
-
+ cb_struct = g_malloc0(sizeof(struct datafeed_callback));
cb_struct->cb = cb;
cb_struct->cb_data = cb_data;
*
* @retval SR_OK Success.
* @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,
int timeout, sr_receive_data_callback cb, void *cb_data, gintptr poll_object)
/* 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));
- if (!new_pollfds) {
- sr_err("%s: new_pollfds malloc failed", __func__);
- return SR_ERR_MALLOC;
- }
-
- new_sources = g_try_realloc(session->sources, sizeof(struct source) *
+ new_sources = g_realloc(session->sources, sizeof(struct source) *
(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;
s = &new_sources[session->num_sources++];
*
* @retval SR_OK Success.
* @retval SR_ERR_ARG Invalid argument.
- * @retval SR_ERR_MALLOC Memory allocation error.
*
* @since 0.3.0
*/
*
* @retval SR_OK Success.
* @retval SR_ERR_ARG Invalid argument.
- * @retval SR_ERR_MALLOC Memory allocation error.
*
* @since 0.3.0
*/
*
* @retval SR_OK Success.
* @retval SR_ERR_ARG Invalid argument.
- * @retval SR_ERR_MALLOC Memory allocation error.
*
* @since 0.3.0
*/
/**
* 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 poll_object The channel for which the source should be removed.
*
* @retval SR_OK Success
* @retval SR_ERR_ARG Invalid arguments
- * @retval SR_ERR_MALLOC Memory allocation error
* @retval SR_ERR_BUG Internal error
*/
static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object)
{
- struct source *new_sources;
- GPollFD *new_pollfds;
unsigned int old;
if (!session->sources || !session->num_sources) {
if (old == session->num_sources)
return SR_OK;
- session->num_sources -= 1;
+ session->num_sources--;
if (old != session->num_sources) {
- memmove(&session->pollfds[old], &session->pollfds[old+1],
+ memmove(&session->pollfds[old], &session->pollfds[old + 1],
(session->num_sources - old) * sizeof(GPollFD));
- memmove(&session->sources[old], &session->sources[old+1],
+ memmove(&session->sources[old], &session->sources[old + 1],
(session->num_sources - old) * sizeof(struct source));
}
- new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
- if (!new_pollfds && session->num_sources > 0) {
- 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;
+ session->pollfds = g_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
+ session->sources = g_realloc(session->sources, sizeof(struct source) * session->num_sources);
return SR_OK;
}
*
* @retval SR_OK Success
* @retval SR_ERR_ARG Invalid argument
- * @retval SR_ERR_MALLOC Memory allocation error.
* @retval SR_ERR_BUG Internal error.
*
* @since 0.3.0
*
* @retval SR_OK Success.
* @retval SR_ERR_ARG Invalid argument.
- * @retval SR_ERR_MALLOC Memory allocation error.
* @return SR_ERR_BUG Internal error.
*
* @since 0.2.0
* @param di The driver instance to use.
* @param[in] prefix A driver-specific prefix string used for log messages.
*
- * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
- * SR_ERR_MALLOC upon memory allocation errors.
+ * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
*/
SR_PRIV int std_init(struct sr_context *sr_ctx, struct sr_dev_driver *di,
const char *prefix)
return SR_ERR_ARG;
}
- if (!(drvc = g_try_malloc(sizeof(struct drv_context)))) {
- sr_err("%s: Driver context malloc failed.", prefix);
- return SR_ERR_MALLOC;
- }
-
+ drvc = g_malloc0(sizeof(struct drv_context));
drvc->sr_ctx = sr_ctx;
drvc->instances = NULL;
di->priv = drvc;
* @param unit The unit to append to the string, or NULL if the string
* 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
* memory.
*
*
* @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
* memory.
*
*
* @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
* memory.
*
int r;
/* Allocate enough for a uint64_t as string + " ms". */
- if (!(o = g_try_malloc0(30 + 1))) {
- sr_err("%s: o malloc failed", __func__);
- return NULL;
- }
+ o = g_malloc0(30 + 1);
if (frequency >= SR_GHZ(1))
r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
* @param v_p The voltage numerator.
* @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
* memory.
*
int r;
char *o;
- if (!(o = g_try_malloc0(30 + 1))) {
- sr_err("%s: o malloc failed", __func__);
- return NULL;
- }
+ o = g_malloc0(30 + 1);
if (v_q == 1000)
r = snprintf(o, 30, "%" PRIu64 "mV", v_p);