From: Uwe Hermann Date: Fri, 21 Nov 2014 18:02:10 +0000 (+0100) Subject: Use g_malloc0() consistently, simplify error handling. X-Git-Tag: libsigrok-0.4.0~727 X-Git-Url: http://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=91219afc75c9aa1d0c5e2da5c03343c1e43eb6df 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. --- diff --git a/src/backend.c b/src/backend.c index 6500bf54..637f7de9 100644 --- a/src/backend.c +++ b/src/backend.c @@ -348,12 +348,7 @@ SR_API int sr_init(struct sr_context **ctx) } /* + 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); diff --git a/src/device.c b/src/device.c index 1945f01a..9dd2201e 100644 --- a/src/device.c +++ b/src/device.c @@ -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] 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, @@ -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; - 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; @@ -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: * "/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 \/\\\, 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. @@ -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; - 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); @@ -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; - 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; diff --git a/src/hardware/agilent-dmm/api.c b/src/hardware/agilent-dmm/api.c index eb723bfa..374c206a 100644 --- a/src/hardware/agilent-dmm/api.c +++ b/src/hardware/agilent-dmm/api.c @@ -112,8 +112,7 @@ static GSList *scan(GSList *options) 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; diff --git a/src/hardware/appa-55ii/api.c b/src/hardware/appa-55ii/api.c index c36e72c8..771a686c 100644 --- a/src/hardware/appa-55ii/api.c +++ b/src/hardware/appa-55ii/api.c @@ -81,8 +81,8 @@ static GSList *scan(GSList *options) 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; diff --git a/src/hardware/atten-pps3xxx/api.c b/src/hardware/atten-pps3xxx/api.c index 580bc51d..b72eeb4a 100644 --- a/src/hardware/atten-pps3xxx/api.c +++ b/src/hardware/atten-pps3xxx/api.c @@ -122,11 +122,11 @@ static GSList *scan(GSList *options, int modelid) 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. */ diff --git a/src/hardware/brymen-dmm/api.c b/src/hardware/brymen-dmm/api.c index 797d05a9..aef784e8 100644 --- a/src/hardware/brymen-dmm/api.c +++ b/src/hardware/brymen-dmm/api.c @@ -51,8 +51,7 @@ static GSList *brymen_scan(const char *conn, const char *serialcomm) 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; diff --git a/src/hardware/cem-dt-885x/api.c b/src/hardware/cem-dt-885x/api.c index 29db2451..d91c5509 100644 --- a/src/hardware/cem-dt-885x/api.c +++ b/src/hardware/cem-dt-885x/api.c @@ -97,8 +97,7 @@ static GSList *scan(GSList *options) 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; @@ -119,10 +118,7 @@ static GSList *scan(GSList *options) 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; diff --git a/src/hardware/center-3xx/api.c b/src/hardware/center-3xx/api.c index 25df2eb0..c4008d48 100644 --- a/src/hardware/center-3xx/api.c +++ b/src/hardware/center-3xx/api.c @@ -76,8 +76,7 @@ static GSList *center_scan(const char *conn, const char *serialcomm, int idx) 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; diff --git a/src/hardware/colead-slm/api.c b/src/hardware/colead-slm/api.c index 4c35f0fb..34bebd8e 100644 --- a/src/hardware/colead-slm/api.c +++ b/src/hardware/colead-slm/api.c @@ -87,8 +87,7 @@ static GSList *scan(GSList *options) 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; diff --git a/src/hardware/conrad-digi-35-cpu/api.c b/src/hardware/conrad-digi-35-cpu/api.c index 7f061fbb..ed7378ee 100644 --- a/src/hardware/conrad-digi-35-cpu/api.c +++ b/src/hardware/conrad-digi-35-cpu/api.c @@ -83,8 +83,7 @@ static GSList *scan(GSList *options) * 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; diff --git a/src/hardware/fluke-dmm/api.c b/src/hardware/fluke-dmm/api.c index fc3355c2..e590a4cc 100644 --- a/src/hardware/fluke-dmm/api.c +++ b/src/hardware/fluke-dmm/api.c @@ -75,8 +75,7 @@ static GSList *fluke_scan(const char *conn, const char *serialcomm) 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; diff --git a/src/hardware/gmc-mh-1x-2x/api.c b/src/hardware/gmc-mh-1x-2x/api.c index 8e7d2c1b..91267d98 100644 --- a/src/hardware/gmc-mh-1x-2x/api.c +++ b/src/hardware/gmc-mh-1x-2x/api.c @@ -195,8 +195,7 @@ static GSList *scan_1x_2x_rs232(GSList *options) 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); @@ -288,8 +287,7 @@ static GSList *scan_2x_bd232(GSList *options) 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; diff --git a/src/hardware/link-mso19/api.c b/src/hardware/link-mso19/api.c index 46270ffe..451cc934 100644 --- a/src/hardware/link-mso19/api.c +++ b/src/hardware/link-mso19/api.c @@ -195,10 +195,7 @@ static GSList *scan(GSList *options) 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; diff --git a/src/hardware/manson-hcs-3xxx/api.c b/src/hardware/manson-hcs-3xxx/api.c index 6355d004..5f51b82c 100644 --- a/src/hardware/manson-hcs-3xxx/api.c +++ b/src/hardware/manson-hcs-3xxx/api.c @@ -125,8 +125,7 @@ static GSList *scan(GSList *options) 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; diff --git a/src/hardware/mic-985xx/api.c b/src/hardware/mic-985xx/api.c index 6e834bd4..3bc65faa 100644 --- a/src/hardware/mic-985xx/api.c +++ b/src/hardware/mic-985xx/api.c @@ -75,8 +75,7 @@ static GSList *mic_scan(const char *conn, const char *serialcomm, int idx) 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; diff --git a/src/hardware/motech-lps-30x/api.c b/src/hardware/motech-lps-30x/api.c index 9c01ea45..f118ebe3 100644 --- a/src/hardware/motech-lps-30x/api.c +++ b/src/hardware/motech-lps-30x/api.c @@ -402,8 +402,7 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o 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; diff --git a/src/hardware/norma-dmm/api.c b/src/hardware/norma-dmm/api.c index 34d7de14..83e11351 100644 --- a/src/hardware/norma-dmm/api.c +++ b/src/hardware/norma-dmm/api.c @@ -112,8 +112,7 @@ static GSList *do_scan(struct sr_dev_driver* drv, GSList *options) 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; diff --git a/src/hardware/openbench-logic-sniffer/api.c b/src/hardware/openbench-logic-sniffer/api.c index cb55bf19..acbb9718 100644 --- a/src/hardware/openbench-logic-sniffer/api.c +++ b/src/hardware/openbench-logic-sniffer/api.c @@ -128,8 +128,7 @@ static GSList *scan(GSList *options) 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 diff --git a/src/hardware/serial-dmm/api.c b/src/hardware/serial-dmm/api.c index f240266f..8a0d12ce 100644 --- a/src/hardware/serial-dmm/api.c +++ b/src/hardware/serial-dmm/api.c @@ -409,8 +409,7 @@ static GSList *sdmm_scan(const char *conn, const char *serialcomm, int dmm) 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; diff --git a/src/hardware/teleinfo/api.c b/src/hardware/teleinfo/api.c index 6089f3a5..5e1dfba1 100644 --- a/src/hardware/teleinfo/api.c +++ b/src/hardware/teleinfo/api.c @@ -74,8 +74,8 @@ static GSList *scan(GSList *options) 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; diff --git a/src/hardware/tondaj-sl-814/api.c b/src/hardware/tondaj-sl-814/api.c index 1b40b9f6..2ea66ebf 100644 --- a/src/hardware/tondaj-sl-814/api.c +++ b/src/hardware/tondaj-sl-814/api.c @@ -90,8 +90,7 @@ static GSList *scan(GSList *options) 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; diff --git a/src/hardware/victor-dmm/api.c b/src/hardware/victor-dmm/api.c index de7e94b2..f2e3d783 100644 --- a/src/hardware/victor-dmm/api.c +++ b/src/hardware/victor-dmm/api.c @@ -95,9 +95,8 @@ static GSList *scan(GSList *options) 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); diff --git a/src/hwdriver.c b/src/hwdriver.c index 13be087a..ca579f67 100644 --- a/src/hwdriver.c +++ b/src/hwdriver.c @@ -395,8 +395,7 @@ SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data) { 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); diff --git a/src/lcr/es51919.c b/src/lcr/es51919.c index 0c9054a8..55ea6627 100644 --- a/src/lcr/es51919.c +++ b/src/lcr/es51919.c @@ -41,11 +41,7 @@ static struct dev_buffer *dev_buffer_new(size_t size) { 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; @@ -846,13 +842,9 @@ SR_PRIV struct sr_dev_inst *es51919_serial_scan(GSList *options, 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) diff --git a/src/output/analog.c b/src/output/analog.c index 77cf8b10..e99c3c00 100644 --- a/src/output/analog.c +++ b/src/output/analog.c @@ -47,7 +47,7 @@ static int init(struct sr_output *o, GHashTable *options) 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; diff --git a/src/output/ols.c b/src/output/ols.c index 8d68bdaf..7feb4db9 100644 --- a/src/output/ols.c +++ b/src/output/ols.c @@ -44,12 +44,8 @@ static int init(struct sr_output *o, GHashTable *options) (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; diff --git a/src/scpi/scpi.c b/src/scpi/scpi.c index bb709978..76044113 100644 --- a/src/scpi/scpi.c +++ b/src/scpi/scpi.c @@ -742,12 +742,7 @@ SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi, } 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]); diff --git a/src/scpi/scpi_serial.c b/src/scpi/scpi_serial.c index 4f6e0678..650f9711 100644 --- a/src/scpi/scpi_serial.c +++ b/src/scpi/scpi_serial.c @@ -79,8 +79,7 @@ static int scpi_serial_dev_inst_new(void *priv, struct drv_context *drvc, (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; } diff --git a/src/session.c b/src/session.c index 16bd2ab3..f325e78b 100644 --- a/src/session.c +++ b/src/session.c @@ -306,9 +306,7 @@ SR_API int sr_session_datafeed_callback_add(struct sr_session *session, 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; @@ -712,7 +710,6 @@ SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi, * * @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) @@ -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. */ - 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++]; @@ -769,7 +757,6 @@ static int _sr_session_source_add(struct sr_session *session, GPollFD *pollfd, * * @retval SR_OK Success. * @retval SR_ERR_ARG Invalid argument. - * @retval SR_ERR_MALLOC Memory allocation error. * * @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_ERR_ARG Invalid argument. - * @retval SR_ERR_MALLOC Memory allocation error. * * @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_ERR_ARG Invalid argument. - * @retval SR_ERR_MALLOC Memory allocation error. * * @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. * - * @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) { @@ -872,29 +852,17 @@ static int _sr_session_source_remove(struct sr_session *session, gintptr poll_ob 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; } @@ -907,7 +875,6 @@ static int _sr_session_source_remove(struct sr_session *session, gintptr poll_ob * * @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 @@ -943,7 +910,6 @@ SR_API int sr_session_source_remove_pollfd(struct sr_session *session, * * @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 diff --git a/src/std.c b/src/std.c index fed47feb..9755ff0a 100644 --- a/src/std.c +++ b/src/std.c @@ -41,8 +41,7 @@ * @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) @@ -54,11 +53,7 @@ SR_PRIV int std_init(struct sr_context *sr_ctx, struct sr_dev_driver *di, 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; diff --git a/src/strutil.c b/src/strutil.c index 0306ea7c..050f37bf 100644 --- a/src/strutil.c +++ b/src/strutil.c @@ -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 * 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. * @@ -272,7 +272,7 @@ SR_API char *sr_si_string_u64(uint64_t x, const char *unit) * * @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. * @@ -291,7 +291,7 @@ SR_API char *sr_samplerate_string(uint64_t samplerate) * * @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. * @@ -303,10 +303,7 @@ SR_API char *sr_period_string(uint64_t frequency) 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); @@ -336,7 +333,7 @@ SR_API char *sr_period_string(uint64_t frequency) * @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. * @@ -347,10 +344,7 @@ SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q) 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);