2
0
forked from Ivasoft/DSView

replace malloc with g_malloc

This commit is contained in:
dreamsource-tai
2024-07-25 10:53:30 +08:00
parent 66ed68ea17
commit c50e8f714a
31 changed files with 116 additions and 122 deletions

View File

@@ -205,7 +205,7 @@ namespace pv
struct ds_device_base_info *dev = (array + count - 1);
ds_device_handle dev_handle = dev->handle;
free(array);
g_free(array);
if (set_device(dev_handle))
{

View File

@@ -1142,7 +1142,7 @@ namespace pv
if (i == select_index)
cur_dev_handle = p->handle;
}
free(array);
g_free(array);
_device_selector.setCurrentIndex(select_index);

View File

@@ -317,13 +317,12 @@ SR_PRIV int sr_init(struct sr_context **ctx)
}
/* + 1 to handle when struct sr_context has no members. */
context = malloc(sizeof(struct sr_context));
context = g_try_malloc0(sizeof(struct sr_context));
if (!context) {
sr_err("%s,ERROR:failed to alloc memory.", __func__);
ret = SR_ERR_MALLOC;
goto done;
}
memset(context, 0, sizeof(struct sr_context));
ret = libusb_init(&context->libusb_ctx);

View File

@@ -47,12 +47,11 @@ SR_PRIV struct sr_channel *sr_channel_new(uint16_t index, int type, gboolean ena
{
struct sr_channel *probe;
probe = malloc(sizeof(struct sr_channel));
probe = g_try_malloc0(sizeof(struct sr_channel));
if (probe == NULL) {
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return NULL;
}
memset(probe, 0, sizeof(struct sr_channel));
probe->index = index;
probe->type = type;
@@ -184,7 +183,7 @@ SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int mode, int status,
const char *vendor, const char *model, const char *version)
{
struct sr_dev_inst *sdi;
if (!(sdi = malloc(sizeof(struct sr_dev_inst)))) {
if (!(sdi = g_try_malloc0(sizeof(struct sr_dev_inst)))) {
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return NULL;
}
@@ -245,7 +244,7 @@ SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus, uint8_t address)
{
struct sr_usb_dev_inst *udi;
if (!(udi = malloc(sizeof(struct sr_usb_dev_inst)))) {
if (!(udi = g_try_malloc0(sizeof(struct sr_usb_dev_inst)))) {
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return NULL;
}
@@ -291,7 +290,7 @@ SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
return NULL;
}
if (!(serial = malloc(sizeof(struct sr_serial_dev_inst)))) {
if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) {
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return NULL;
}

View File

@@ -124,7 +124,7 @@ static struct DSL_context *DSCope_dev_new(const struct DSL_profile *prof)
assert(prof);
if (!(devc = malloc(sizeof(struct DSL_context)))) {
if (!(devc = g_try_malloc0(sizeof(struct DSL_context)))) {
sr_err("Device context malloc failed.");
return NULL;
}
@@ -329,7 +329,7 @@ static GSList *scan(GSList *options)
prof->vendor, prof->model, prof->model_version);
if (sdi == NULL) {
free(devc);
g_free(devc);
break;
}
@@ -356,7 +356,7 @@ static GSList *scan(GSList *options)
else {
char *firmware;
char *res_path = DS_RES_PATH;
if (!(firmware = malloc(strlen(res_path)+strlen(prof->firmware) + 5))) {
if (!(firmware = g_try_malloc0(strlen(res_path)+strlen(prof->firmware) + 5))) {
sr_err("Firmware path malloc error!");
break;
}
@@ -2091,7 +2091,7 @@ static int dev_acquisition_start(struct sr_dev_inst *sdi, void *cb_data)
lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
for (i = 0; lupfd[i]; i++);
if (!(devc->usbfd = malloc(sizeof(struct libusb_pollfd) * (i + 1)))){
if (!(devc->usbfd = g_try_malloc0(sizeof(struct libusb_pollfd) * (i + 1)))){
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return SR_ERR;
}
@@ -2103,7 +2103,7 @@ static int dev_acquisition_start(struct sr_dev_inst *sdi, void *cb_data)
}
devc->usbfd[i] = -1;
free(lupfd);
g_free(lupfd);
wr_cmd.header.dest = DSL_CTL_START;
wr_cmd.header.size = 0;

View File

@@ -105,7 +105,7 @@ SR_PRIV void dsl_probe_init(struct sr_dev_inst *sdi)
for (i = 0; devc->profile->dev_caps.vdivs[i]; i++){
}
probe->vga_ptr = malloc((i+1)*sizeof(struct DSL_vga));
probe->vga_ptr = g_try_malloc0((i+1)*sizeof(struct DSL_vga));
if (probe->vga_ptr == NULL){
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return;
@@ -1324,7 +1324,7 @@ SR_PRIV int dsl_fpga_config(struct libusb_device_handle *hdl, const char *filena
filesize = (uint64_t)f_stat.st_size;
if ((buf = malloc(filesize)) == NULL) {
if ((buf = g_try_malloc0(filesize)) == NULL) {
sr_err("FPGA configure buf malloc failed.");
fclose(fw);
return SR_ERR;
@@ -1898,7 +1898,7 @@ SR_PRIV int dsl_dev_open(struct sr_dev_driver *di, struct sr_dev_inst *sdi, gboo
char *fpga_bit;
char *res_path = DS_RES_PATH;
if (!(fpga_bit = malloc(strlen(res_path)+strlen(devc->profile->fpga_bit33) + 5))) {
if (!(fpga_bit = g_try_malloc0(strlen(res_path)+strlen(devc->profile->fpga_bit33) + 5))) {
sr_err("fpag_bit path malloc error!");
return SR_ERR_MALLOC;
}
@@ -2505,12 +2505,12 @@ SR_PRIV int dsl_start_transfers(const struct sr_dev_inst *sdi)
size = get_buffer_size(sdi);
/* trigger packet transfer */
if (!(trigger_pos = malloc(dsl_header_size(devc)))) {
if (!(trigger_pos = g_try_malloc0(dsl_header_size(devc)))) {
sr_err("%s: USB trigger_pos buffer malloc failed.", __func__);
return SR_ERR_MALLOC;
}
devc->transfers = malloc(sizeof(*devc->transfers) * (num_transfers + 1));
devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * (num_transfers + 1));
if (!devc->transfers) {
sr_err("%s: USB transfer malloc failed.", __func__);
return SR_ERR_MALLOC;
@@ -2534,7 +2534,7 @@ SR_PRIV int dsl_start_transfers(const struct sr_dev_inst *sdi)
/* data packet transfer */
for (i = 1; i <= num_transfers; i++) {
if (!(buf = malloc(size))) {
if (!(buf = g_try_malloc0(size))) {
sr_err("%s: USB transfer buffer malloc failed.", __func__);
return SR_ERR_MALLOC;
}

View File

@@ -204,7 +204,7 @@ static struct DSL_context *DSLogic_dev_new(const struct DSL_profile *prof)
assert(prof);
if (!(devc = malloc(sizeof(struct DSL_context)))) {
if (!(devc = g_try_malloc0(sizeof(struct DSL_context)))) {
sr_err("Device context malloc failed.");
return NULL;
}
@@ -414,7 +414,7 @@ static GSList *scan(GSList *options)
sdi = sr_dev_inst_new(channel_modes[devc->ch_mode].mode, SR_ST_INITIALIZING,
prof->vendor, prof->model, prof->model_version);
if (sdi == NULL) {
free(devc);
g_free(devc);
break;
}
@@ -441,7 +441,7 @@ static GSList *scan(GSList *options)
else {
char *firmware;
char *res_path = DS_RES_PATH;
if (!(firmware = malloc(strlen(res_path)+strlen(prof->firmware) + 5))) {
if (!(firmware = g_try_malloc0(strlen(res_path)+strlen(prof->firmware) + 5))) {
sr_err("Firmware path malloc error!");
break;
}
@@ -455,7 +455,7 @@ static GSList *scan(GSList *options)
sr_err("Firmware upload failed for device %s.", prof->model);
}
free(firmware);
g_free(firmware);
libusb_unref_device(device_handle);
#ifdef _WIN32
@@ -1069,7 +1069,7 @@ static int config_set(int id, GVariant *data, struct sr_dev_inst *sdi,
char *fpga_bit;
char *res_path = DS_RES_PATH;
if (!(fpga_bit = malloc(strlen(res_path) + strlen(devc->profile->fpga_bit33) + 5))) {
if (!(fpga_bit = g_try_malloc0(strlen(res_path) + strlen(devc->profile->fpga_bit33) + 5))) {
sr_err("fpag_bit path malloc error!");
return SR_ERR_MALLOC;
}
@@ -1489,7 +1489,7 @@ static int dev_acquisition_start(struct sr_dev_inst *sdi, void *cb_data)
lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
for (i = 0; lupfd[i]; i++);
if (!(devc->usbfd = malloc(sizeof(struct libusb_pollfd) * (i + 1)))){
if (!(devc->usbfd = g_try_malloc0(sizeof(struct libusb_pollfd) * (i + 1)))){
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return SR_ERR;
}
@@ -1500,7 +1500,7 @@ static int dev_acquisition_start(struct sr_dev_inst *sdi, void *cb_data)
devc->usbfd[i] = lupfd[i]->fd;
}
devc->usbfd[i] = -1;
free(lupfd);
g_free(lupfd);
wr_cmd.header.dest = DSL_CTL_START;
wr_cmd.header.size = 0;

View File

@@ -316,7 +316,7 @@ static int init_analog_random_data(struct session_vdev * vdev)
{
safe_free(vdev->data_buf);
vdev->data_buf = malloc(DSO_BUF_LEN);
vdev->data_buf = g_try_malloc0(DSO_BUF_LEN);
if (vdev->data_buf == NULL)
{
sr_err("%s: vdev->data_buf malloc failed", __func__);
@@ -640,7 +640,7 @@ static GSList *hw_scan(GSList *options)
(void)options;
devices = NULL;
vdev = malloc(sizeof(struct session_vdev));
vdev = g_try_malloc0(sizeof(struct session_vdev));
if (vdev == NULL)
{
sr_err("%s: sdi->priv malloc failed", __func__);
@@ -1265,11 +1265,11 @@ static int hw_dev_acquisition_start(struct sr_dev_inst *sdi,
if (sdi->mode == LOGIC)
{
vdev->data_buf = malloc(LOGIC_BUF_LEN);
vdev->data_buf = g_try_malloc0(LOGIC_BUF_LEN);
vdev->data_buf_len = LOGIC_BUF_LEN;
}
else{
vdev->data_buf = malloc(DSO_PACKET_LEN);
vdev->data_buf = g_try_malloc0(DSO_PACKET_LEN);
vdev->data_buf_len = DSO_PACKET_LEN;
}
@@ -1341,7 +1341,7 @@ static int hw_dev_acquisition_start(struct sr_dev_inst *sdi,
vdev->logci_cur_packet_num = 1;
safe_free(logic_post_buf);
logic_post_buf = malloc(vdev->enabled_probes * vdev->packet_len);
logic_post_buf = g_try_malloc0(vdev->enabled_probes * vdev->packet_len);
if(logic_post_buf == NULL)
{
sr_err("%s: logic_post_buf malloc error", __func__);
@@ -1571,7 +1571,7 @@ static void free_temp_buffer(struct session_vdev *vdev)
for (i = 0; i < SESSION_MAX_CHANNEL_COUNT; i++){
if (pack_buf->block_bufs[i] != NULL)
{
free(pack_buf->block_bufs[i]);
g_free(pack_buf->block_bufs[i]);
pack_buf->block_bufs[i] = NULL;
}
else{
@@ -1640,7 +1640,7 @@ static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_i
{
vdev->cur_block = 0;
vdev->packet_buffer = malloc(sizeof(struct session_packet_buffer));
vdev->packet_buffer = g_try_malloc0(sizeof(struct session_packet_buffer));
if (vdev->packet_buffer == NULL)
{
sr_err("%s: vdev->packet_buffer malloc failed", __func__);
@@ -1656,7 +1656,7 @@ static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_i
vdev->packet_buffer->post_buf_len = chan_num * vdev->packet_len;
vdev->packet_buffer->post_buf = malloc(vdev->packet_buffer->post_buf_len + 1);
vdev->packet_buffer->post_buf = g_try_malloc0(vdev->packet_buffer->post_buf_len + 1);
if (vdev->packet_buffer->post_buf == NULL)
{
sr_err("%s: vdev->packet_buffer->post_buf malloc failed", __func__);
@@ -1693,7 +1693,7 @@ static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_i
pack_buffer->post_buf_len = chan_num * vdev->packet_len;
safe_free(pack_buffer->post_buf);
pack_buffer->post_buf = malloc(pack_buffer->post_buf_len);
pack_buffer->post_buf = g_try_malloc0(pack_buffer->post_buf_len);
if (pack_buffer->post_buf == NULL)
{
sr_err("%s: pack_buffer->post_buf malloc failed", __func__);
@@ -1748,7 +1748,7 @@ static int receive_data_logic_decoder(int fd, int revents, const struct sr_dev_i
pack_buffer->block_bufs[malloc_chan_index] = NULL;
}
pack_buffer->block_bufs[malloc_chan_index] = malloc(pack_buffer->block_data_len + 1);
pack_buffer->block_bufs[malloc_chan_index] = g_try_malloc0(pack_buffer->block_data_len + 1);
if (pack_buffer->block_bufs[malloc_chan_index] == NULL)
{
sr_err("%s: block buffer malloc failed", __func__);
@@ -1925,7 +1925,7 @@ static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi)
{
vdev->cur_block = 0;
vdev->packet_buffer = malloc(sizeof(struct session_packet_buffer));
vdev->packet_buffer = g_try_malloc0(sizeof(struct session_packet_buffer));
if (vdev->packet_buffer == NULL)
{
sr_err("%s: vdev->packet_buffer malloc failed", __func__);
@@ -1941,7 +1941,7 @@ static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi)
vdev->packet_buffer->post_buf_len = chan_num * 10000;
vdev->packet_buffer->post_buf = malloc(vdev->packet_buffer->post_buf_len);
vdev->packet_buffer->post_buf = g_try_malloc0(vdev->packet_buffer->post_buf_len);
if (vdev->packet_buffer->post_buf == NULL)
{
sr_err("%s: vdev->packet_buffer->post_buf malloc failed", __func__);
@@ -1961,7 +1961,7 @@ static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi)
vdev->packet_buffer->post_buf_len = chan_num * 10000;
safe_free(pack_buffer->post_buf);
pack_buffer->post_buf = malloc(pack_buffer->post_buf_len);
pack_buffer->post_buf = g_try_malloc0(pack_buffer->post_buf_len);
if (pack_buffer->post_buf == NULL)
{
sr_err("%s: pack_buffer->post_buf malloc failed", __func__);
@@ -2050,7 +2050,7 @@ static int receive_data_dso(int fd, int revents, const struct sr_dev_inst *sdi)
pack_buffer->block_bufs[malloc_chan_index] = NULL;
}
pack_buffer->block_bufs[malloc_chan_index] = malloc(pack_buffer->block_data_len + 1);
pack_buffer->block_bufs[malloc_chan_index] = g_try_malloc0(pack_buffer->block_data_len + 1);
if (pack_buffer->block_bufs[malloc_chan_index] == NULL)
{
sr_err("%s: block buffer malloc failed", __func__);
@@ -2338,7 +2338,7 @@ static int receive_data_analog(int fd, int revents, const struct sr_dev_inst *sd
{
vdev->data_buf_len = 0;
void* analog_data = malloc(ANALOG_DATA_LEN_PER_CYCLE);
void* analog_data = g_try_malloc0(ANALOG_DATA_LEN_PER_CYCLE);
if(analog_data == NULL)
{
sr_err("%s:analog_data malloc failed",__func__);
@@ -2376,7 +2376,7 @@ static int receive_data_analog(int fd, int revents, const struct sr_dev_inst *sd
safe_free(vdev->data_buf);
vdev->data_buf = malloc(total_buf_len);
vdev->data_buf = g_try_malloc0(total_buf_len);
if (vdev->data_buf == NULL)
{
sr_err("%s: vdev->data_buf malloc failed", __func__);
@@ -2449,7 +2449,7 @@ static int receive_data_analog(int fd, int revents, const struct sr_dev_inst *sd
if(vdev->analog_post_buf_len != vdev->packet_len)
{
safe_free(vdev->analog_post_buf);
vdev->analog_post_buf = malloc(vdev->packet_len);
vdev->analog_post_buf = g_try_malloc0(vdev->packet_len);
if(vdev->analog_post_buf == NULL)
{
sr_err("%s: buf malloc failed", __func__);
@@ -2571,7 +2571,7 @@ static int load_virtual_device_session(struct sr_dev_inst *sdi)
return SR_ERR;
}
if (!(metafile = malloc(fileInfo.uncompressed_size)))
if (!(metafile = g_try_malloc0(fileInfo.uncompressed_size)))
{
sr_err("%s: metafile malloc failed", __func__);
return SR_ERR_MALLOC;

View File

@@ -173,7 +173,7 @@ SR_PRIV struct sr_config *sr_config_new(int key, GVariant *data)
struct sr_config *src;
assert(data);
if (!(src = malloc(sizeof(struct sr_config)))){
if (!(src = g_try_malloc0(sizeof(struct sr_config)))){
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return NULL;
}

View File

@@ -57,7 +57,7 @@ static int init(struct sr_input *in, const char *filename)
(void)filename;
if (!(ctx = malloc(sizeof(struct context)))) {
if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
sr_err("Input format context malloc failed.");
return SR_ERR_MALLOC;
}

View File

@@ -272,7 +272,7 @@ static gboolean parse_header(FILE *file, struct context *ctx)
else
{
sr_info("Probe %d is '%s' identified by '%s'.", ctx->probecount, parts[3], parts[2]);
probe = malloc(sizeof(struct probe));
probe = g_try_malloc0(sizeof(struct probe));
if (probe != NULL){
probe->identifier = g_strdup(parts[2]);
@@ -331,7 +331,7 @@ static int init(struct sr_input *in, const char *filename)
(void)filename;
if (!(ctx = malloc(sizeof(struct context)))) {
if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
sr_err("Input format context malloc failed.");
return SR_ERR_MALLOC;
}

View File

@@ -97,7 +97,7 @@ static int init(struct sr_input *in, const char *filename)
if (get_wav_header(filename, buf) != SR_OK)
return SR_ERR;
if (!(ctx = malloc(sizeof(struct context)))){
if (!(ctx = g_try_malloc0(sizeof(struct context)))){
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return SR_ERR_MALLOC;
}

View File

@@ -281,10 +281,6 @@ SR_API void ds_set_datafeed_callback(ds_datafeed_callback_t cb)
lib_ctx.data_forward_callback = cb;
}
/**
* Get the device list, if the field _handle is 0, the list visited to end.
* User need call free() to release the buffer. If the list is empty, the out_list is null.
*/
SR_API int ds_get_device_list(struct ds_device_base_info **out_list, int *out_count)
{
int num;
@@ -308,7 +304,7 @@ SR_API int ds_get_device_list(struct ds_device_base_info **out_list, int *out_co
return SR_OK;
}
buf = malloc(sizeof(struct ds_device_base_info) * (num + 1));
buf = g_try_malloc0(sizeof(struct ds_device_base_info) * (num + 1));
if (buf == NULL)
{
sr_err("%s,ERROR:failed to alloc memory.", __func__);

View File

@@ -52,7 +52,7 @@
#define USB_EV_HOTPLUG_ATTACH 1
#define USB_EV_HOTPLUG_DETTACH 2
#define safe_free(p) if((p)) free((p)); ((p)) = NULL;
#define safe_free(p) if((p)) g_free((p)); ((p)) = NULL;
#define g_safe_free_list(p) if((p)) g_slist_free((p)); ((p)) = NULL;
#define DS_VENDOR_ID 0x2A0E

View File

@@ -1406,7 +1406,7 @@ SR_API void ds_set_user_data_dir(const char *dir);
/**
* Get the device list, if the field _handle is 0, the list visited to end.
* User need call free() to release the buffer. If the list is empty, the out_list is null.
* User need call g_free() to release the buffer. If the list is empty, the out_list is null.
*/
SR_API int ds_get_device_list(struct ds_device_base_info** out_list, int *out_count);

View File

@@ -74,7 +74,7 @@ static int init(struct sr_output *o, GHashTable *options)
if (!o || !o->sdi)
return SR_ERR_ARG;
ctx = malloc(sizeof(struct context));
ctx = g_try_malloc0(sizeof(struct context));
if (ctx == NULL){
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return SR_ERR;
@@ -102,12 +102,12 @@ static int init(struct sr_output *o, GHashTable *options)
ctx->num_enabled_channels++;
}
ctx->channel_index = malloc(sizeof(int) * ctx->num_enabled_channels);
ctx->channel_unit = malloc(sizeof(int) * ctx->num_enabled_channels);
ctx->channel_scale = malloc(sizeof(float) * ctx->num_enabled_channels);
ctx->channel_offset = malloc(sizeof(uint16_t) * ctx->num_enabled_channels);
ctx->channel_mmax = malloc(sizeof(double) * ctx->num_enabled_channels);
ctx->channel_mmin = malloc(sizeof(double) * ctx->num_enabled_channels);
ctx->channel_index = g_try_malloc0(sizeof(int) * ctx->num_enabled_channels);
ctx->channel_unit = g_try_malloc0(sizeof(int) * ctx->num_enabled_channels);
ctx->channel_scale = g_try_malloc0(sizeof(float) * ctx->num_enabled_channels);
ctx->channel_offset = g_try_malloc0(sizeof(uint16_t) * ctx->num_enabled_channels);
ctx->channel_mmax = g_try_malloc0(sizeof(double) * ctx->num_enabled_channels);
ctx->channel_mmin = g_try_malloc0(sizeof(double) * ctx->num_enabled_channels);
if (ctx->channel_index == NULL || ctx->channel_mmin == NULL){
sr_err("%s,ERROR:failed to alloc memory.", __func__);

View File

@@ -60,7 +60,7 @@ static int init(struct sr_output *o, GHashTable *options)
if (!o || !o->sdi)
return SR_ERR_ARG;
ctx = malloc(sizeof(struct context));
ctx = g_try_malloc0(sizeof(struct context));
if (ctx == NULL){
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return SR_ERR;
@@ -83,7 +83,7 @@ static int init(struct sr_output *o, GHashTable *options)
sr_err("No logic channel enabled.");
return SR_ERR;
}
ctx->channel_index = malloc(sizeof(int) * ctx->num_enabled_channels);
ctx->channel_index = g_try_malloc0(sizeof(int) * ctx->num_enabled_channels);
if (ctx->channel_index == NULL){
sr_err("%s,ERROR:failed to alloc memory.", __func__);
@@ -183,7 +183,7 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p
if (!ctx->prevsample) {
/* Can't allocate this until we know the stream's unitsize. */
ctx->prevsample = malloc(logic->unitsize);
ctx->prevsample = g_try_malloc0(logic->unitsize);
if (ctx->prevsample == NULL){
sr_err("%s,ERROR:failed to alloc memory.", __func__);

View File

@@ -197,7 +197,7 @@ SR_API const struct sr_option **sr_output_options_get(const struct sr_output_mod
for (size = 0; mod_opts[size].id; size++);
opts = malloc((size + 1) * sizeof(struct sr_option *));
opts = g_try_malloc0((size + 1) * sizeof(struct sr_option *));
if (opts == NULL){
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return NULL;
@@ -261,7 +261,7 @@ SR_API const struct sr_output* sr_output_new(const struct sr_output_module *omod
gpointer key, value;
int i;
op = malloc(sizeof(struct sr_output));
op = g_try_malloc0(sizeof(struct sr_output));
if (op == NULL){
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return NULL;

View File

@@ -48,7 +48,7 @@ static int init(struct sr_output *o, GHashTable *options)
{
struct out_context *outc;
outc = malloc(sizeof(struct out_context));
outc = g_try_malloc0(sizeof(struct out_context));
if (outc == NULL){
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return SR_ERR;

View File

@@ -65,7 +65,7 @@ static int init(struct sr_output *o, GHashTable *options)
return SR_ERR;
}
ctx = malloc(sizeof(struct context));
ctx = g_try_malloc0(sizeof(struct context));
if (ctx == NULL){
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return SR_ERR;
@@ -74,7 +74,7 @@ static int init(struct sr_output *o, GHashTable *options)
o->priv = ctx;
ctx->num_enabled_channels = num_enabled_channels;
ctx->channel_index = malloc(sizeof(int) * ctx->num_enabled_channels);
ctx->channel_index = g_try_malloc0(sizeof(int) * ctx->num_enabled_channels);
if (ctx->channel_index == NULL){
sr_err("%s,ERROR:failed to alloc memory.", __func__);
@@ -211,7 +211,7 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p
if (!ctx->prevsample) {
/* Can't allocate this until we know the stream's unitsize. */
ctx->prevsample = malloc(logic->unitsize);
ctx->prevsample = g_try_malloc0(logic->unitsize);
if (ctx->prevsample == NULL){
sr_err("%s,ERROR:failed to alloc memory.", __func__);

View File

@@ -75,7 +75,7 @@ SR_PRIV struct sr_session *sr_session_new(void)
sr_session_destroy(); // Destory the old.
}
session = malloc(sizeof(struct sr_session));
session = g_try_malloc0(sizeof(struct sr_session));
if (session == NULL) {
sr_err("%s,ERROR:failed to alloc memory.", __func__);
return NULL;

View File

@@ -463,7 +463,7 @@ static int receive_data_logic_dso_v2(int fd, int revents, const struct sr_dev_in
if (vdev->packet_buffer == NULL){
vdev->cur_block = 0;
vdev->packet_buffer = malloc(sizeof(struct session_packet_buffer));
vdev->packet_buffer = g_try_malloc0(sizeof(struct session_packet_buffer));
if (vdev->packet_buffer == NULL){
sr_err("%s: vdev->packet_buffer malloc failed", __func__);
return SR_ERR_MALLOC;
@@ -479,7 +479,7 @@ static int receive_data_logic_dso_v2(int fd, int revents, const struct sr_dev_in
else
vdev->packet_buffer->post_buf_len = chan_num * 10000;
vdev->packet_buffer->post_buf = malloc(vdev->packet_buffer->post_buf_len + 1);
vdev->packet_buffer->post_buf = g_try_malloc0(vdev->packet_buffer->post_buf_len + 1);
if (vdev->packet_buffer->post_buf == NULL){
sr_err("%s: vdev->packet_buffer->post_buf malloc failed", __func__);
return SR_ERR_MALLOC;
@@ -567,11 +567,11 @@ static int receive_data_logic_dso_v2(int fd, int revents, const struct sr_dev_in
for (malloc_chan_index = 0; malloc_chan_index < chan_num; malloc_chan_index++){
// Release the old buffer.
if (pack_buffer->block_bufs[malloc_chan_index] != NULL){
free(pack_buffer->block_bufs[malloc_chan_index]);
g_free(pack_buffer->block_bufs[malloc_chan_index]);
pack_buffer->block_bufs[malloc_chan_index] = NULL;
}
pack_buffer->block_bufs[malloc_chan_index] = malloc(pack_buffer->block_data_len + 1);
pack_buffer->block_bufs[malloc_chan_index] = g_try_malloc(pack_buffer->block_data_len + 1);
if (pack_buffer->block_bufs[malloc_chan_index] == NULL){
sr_err("%s: block buffer malloc failed", __func__);
send_error_packet(sdi, vdev, &packet);
@@ -747,7 +747,7 @@ static int dev_open(struct sr_dev_inst *sdi)
assert(sdi->priv == NULL);
vdev = malloc(sizeof(struct session_vdev));
vdev = g_try_malloc(sizeof(struct session_vdev));
if (vdev == NULL)
{
sr_err("%s: sdi->priv malloc failed", __func__);
@@ -763,7 +763,7 @@ static int dev_open(struct sr_dev_inst *sdi)
sdi->priv = vdev;
sdi->status = SR_ST_ACTIVE;
vdev->buf = malloc(CHUNKSIZE + sizeof(uint64_t));
vdev->buf = g_try_malloc0(CHUNKSIZE + sizeof(uint64_t));
if (vdev->buf == NULL){
sr_err("%s: vdev->buf malloc failed", __func__);
return SR_ERR_MALLOC;
@@ -794,7 +794,7 @@ static void free_temp_buffer(struct session_vdev *vdev)
for (i = 0; i < SESSION_MAX_CHANNEL_COUNT; i++){
if (pack_buf->block_bufs[i] != NULL){
free(pack_buf->block_bufs[i]);
g_free(pack_buf->block_bufs[i]);
pack_buf->block_bufs[i] = NULL;
}
else{
@@ -1127,7 +1127,7 @@ static int config_set(int id, GVariant *data, struct sr_dev_inst *sdi,
{
if (sdi->mode == LOGIC)
{
if (!(vdev->logic_buf = malloc(CHUNKSIZE / 16 * vdev->num_probes)))
if (!(vdev->logic_buf = g_try_malloc0(CHUNKSIZE / 16 * vdev->num_probes)))
{
sr_err("%s: vdev->logic_buf malloc failed", __func__);
}
@@ -1512,7 +1512,7 @@ SR_PRIV int sr_new_virtual_device(const char *filename, struct sr_dev_inst **out
return SR_ERR;
}
if (!(metafile = malloc(fileInfo.uncompressed_size)))
if (!(metafile = g_try_malloc0(fileInfo.uncompressed_size)))
{
sr_err("%s: metafile malloc failed", __func__);
return SR_ERR_MALLOC;
@@ -1629,7 +1629,7 @@ static int sr_load_virtual_device_session(struct sr_dev_inst *sdi)
return SR_ERR;
}
if (!(metafile = malloc(fileInfo.uncompressed_size)))
if (!(metafile = g_try_malloc0(fileInfo.uncompressed_size)))
{
sr_err("%s: metafile malloc failed", __func__);
return SR_ERR_MALLOC;

View File

@@ -48,7 +48,7 @@ SR_PRIV int std_hw_init(struct sr_context *sr_ctx, struct sr_dev_driver *di,
return SR_ERR_ARG;
}
if (!(drvc = malloc(sizeof(struct drv_context)))) {
if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
sr_err("%sDriver context malloc failed.", prefix);
return SR_ERR_MALLOC;
}

View File

@@ -53,7 +53,7 @@
* @param unit The unit to append to the string, or NULL if the string
* has no units.
*
* @return A malloc()ed string representation of the samplerate value,
* @return A g_try_malloc0()ed string representation of the samplerate value,
* or NULL upon errors. The caller is responsible to g_free() the
* memory.
*/
@@ -98,7 +98,7 @@ SR_API char *sr_si_string_u64(uint64_t x, const char *unit)
* @param unit The unit to append to the string, or NULL if the string
* has no units.
*
* @return A malloc()ed string representation of the samplerate value,
* @return A g_try_malloc0()ed string representation of the samplerate value,
* or NULL upon errors. The caller is responsible to g_free() the
* memory.
*/
@@ -140,7 +140,7 @@ SR_API char *sr_iec_string_u64(uint64_t x, const char *unit)
*
* @param samplerate The samplerate in Hz.
*
* @return A malloc()ed string representation of the samplerate value,
* @return A g_try_malloc0()ed string representation of the samplerate value,
* or NULL upon errors. The caller is responsible to g_free() the
* memory.
*/
@@ -156,7 +156,7 @@ SR_API char *sr_samplerate_string(uint64_t samplerate)
*
* @param samplecount.
*
* @return A malloc()ed string representation of the samplecount value,
* @return A g_try_malloc0()ed string representation of the samplecount value,
* or NULL upon errors. The caller is responsible to g_free() the
* memory.
*/
@@ -173,7 +173,7 @@ SR_API char *sr_samplecount_string(uint64_t samplecount)
*
* @param frequency The frequency in Hz.
*
* @return A malloc()ed string representation of the frequency value,
* @return A g_try_malloc0()ed string representation of the frequency value,
* or NULL upon errors. The caller is responsible to g_free() the
* memory.
*/
@@ -183,7 +183,7 @@ SR_API char *sr_period_string(uint64_t frequency)
int r;
/* Allocate enough for a uint64_t as string + " ms". */
if (!(o = malloc(30 + 1))) {
if (!(o = g_try_malloc0(30 + 1))) {
sr_err("%s: o malloc failed", __func__);
return NULL;
}
@@ -214,7 +214,7 @@ SR_API char *sr_period_string(uint64_t frequency)
*
* @param time The time in ns.
*
* @return A malloc()ed string representation of the time value,
* @return A g_try_malloc0()ed string representation of the time value,
* or NULL upon errors. The caller is responsible to g_free() the
* memory.
*/
@@ -224,7 +224,7 @@ SR_API char *sr_time_string(uint64_t time)
int r;
/* Allocate enough for a uint64_t as string + " ms". */
if (!(o = malloc(30 + 1))) {
if (!(o = g_try_malloc0(30 + 1))) {
sr_err("%s: o malloc failed", __func__);
return NULL;
}
@@ -263,7 +263,7 @@ SR_API char *sr_time_string(uint64_t time)
* @param v_p The voltage numerator.
* @param v_q The voltage denominator.
*
* @return A malloc()ed string representation of the voltage value,
* @return A g_try_malloc0()ed string representation of the voltage value,
* or NULL upon errors. The caller is responsible to g_free() the
* memory.
*/
@@ -272,7 +272,7 @@ SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q)
int r;
char *o;
if (!(o = malloc(30 + 1))) {
if (!(o = g_try_malloc0(30 + 1))) {
sr_err("%s: o malloc failed", __func__);
return NULL;
}

View File

@@ -56,7 +56,7 @@ SR_PRIV int ds_trigger_init(void)
int i, j;
if (!trigger) {
if (!(trigger = malloc(sizeof(struct ds_trigger)))) {
if (!(trigger = g_try_malloc0(sizeof(struct ds_trigger)))) {
sr_err("Trigger malloc failed.");
return SR_ERR_MALLOC;
}

View File

@@ -42,19 +42,19 @@ Random notes
especially so for complex and/or run-time dependent values.
- Consistently use g_*malloc() / g_*malloc0(). Do not use standard
malloc()/calloc() if it can be avoided (sometimes other libs such
as libftdi can return malloc()'d memory, for example).
g_try_malloc0()/calloc() if it can be avoided (sometimes other libs such
as libftdi can return g_try_malloc0()'d memory, for example).
- Always properly match allocations with the proper *free() functions. If
glib's g_*malloc()/g_*malloc0() was used, use g_free() to free the
memory. Otherwise use standard free(). Never use the wrong function!
memory. Otherwise use standard g_free(). Never use the wrong function!
- We assume that "small" memory allocations (< 1MB) will always succeed.
Thus, it's fine to use malloc() or malloc() for allocations of
simple/small structs and such (instead of using malloc()), and
Thus, it's fine to use g_try_malloc0() or g_try_malloc0() for allocations of
simple/small structs and such (instead of using g_try_malloc0()), and
there's no need to check the return value.
Do use malloc() or malloc() for large (>= 1MB) allocations
Do use g_try_malloc0() or g_try_malloc0() for large (>= 1MB) allocations
and check the return value.
- You should never print any messages (neither to stdout nor stderr nor

View File

@@ -227,7 +227,7 @@ static int get_channels(const struct srd_decoder *d, const char *attr,
"a list of dict elements.", d->name, attr);
goto err_out;
}
pdch = malloc(sizeof(struct srd_channel));
pdch = g_try_malloc0(sizeof(struct srd_channel));
if (pdch == NULL){
srd_err("%s,ERROR:failed to alloc memory.", __func__);
goto err_out;
@@ -312,7 +312,7 @@ static int get_options(struct srd_decoder *d)
goto err_out;
}
o = malloc(sizeof(struct srd_decoder_option));
o = g_try_malloc0(sizeof(struct srd_decoder_option));
if (o == NULL){
srd_err("%s,ERROR:failed to alloc memory.", __func__);
goto err_out;
@@ -522,7 +522,7 @@ static int get_annotation_rows(struct srd_decoder *dec)
dec->name);
goto err_out;
}
ann_row = malloc(sizeof(struct srd_decoder_annotation_row));
ann_row = g_try_malloc0(sizeof(struct srd_decoder_annotation_row));
if (ann_row == NULL){
srd_err("%s,ERROR:failed to alloc memory.", __func__);
goto err_out;
@@ -749,7 +749,7 @@ SRD_API int srd_decoder_load(const char *module_name)
return SRD_OK;
}
d = malloc(sizeof(struct srd_decoder));
d = g_try_malloc0(sizeof(struct srd_decoder));
if (d == NULL){
srd_err("%s,ERROR:failed to alloc memory.", __func__);
goto err_out;

View File

@@ -251,7 +251,7 @@ SRD_API int srd_inst_channel_set_all(struct srd_decoder_inst *di,
return SRD_ERR_ARG;
}
new_channelmap = malloc(sizeof(int) * di->dec_num_channels);
new_channelmap = g_try_malloc0(sizeof(int) * di->dec_num_channels);
if (new_channelmap == NULL){
srd_err("%s,ERROR:failed to alloc memory.", __func__);
return SRD_ERR;
@@ -355,7 +355,7 @@ SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *sess,
return NULL;
}
di = malloc(sizeof(struct srd_decoder_inst));
di = g_try_malloc0(sizeof(struct srd_decoder_inst));
if (di == NULL){
srd_err("%s,ERROR:failed to alloc memory.", __func__);
return NULL;
@@ -392,7 +392,7 @@ SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *sess,
g_slist_length(di->decoder->opt_channels);
if (di->dec_num_channels > 0) {
di->dec_channelmap = malloc(sizeof(int) * di->dec_num_channels);
di->dec_channelmap = g_try_malloc0(sizeof(int) * di->dec_num_channels);
if (di->dec_channelmap == NULL){
PyGILState_Release(gstate);

View File

@@ -68,7 +68,7 @@ SRD_API int srd_session_new(struct srd_session **sess)
if (!sess)
return SRD_ERR_ARG;
se = malloc(sizeof(struct srd_session));
se = g_try_malloc0(sizeof(struct srd_session));
if (se == NULL){
srd_err("%s,ERROR:failed to alloc memory.", __func__);
return SRD_ERR;
@@ -388,7 +388,7 @@ SRD_API int srd_pd_output_callback_add(struct srd_session *sess,
srd_dbg("Registering new callback for output type %s.",
output_type_name(output_type));
pd_cb = malloc(sizeof(struct srd_pd_callback));
pd_cb = g_try_malloc0(sizeof(struct srd_pd_callback));
if (pd_cb == NULL){
srd_err("%s,ERROR:failed to alloc memory.", __func__);
return SRD_ERR;

View File

@@ -337,7 +337,7 @@ static int convert_binary(struct srd_decoder_inst *di, PyObject *obj,
pdb = pdata->data;
pdb->bin_class = bin_class;
pdb->size = size;
if (!(pdb->data = malloc(pdb->size))){
if (!(pdb->data = g_try_malloc0(pdb->size))){
srd_err("%s,ERROR:failed to alloc memory.", __func__);
return SRD_ERR_MALLOC;
}
@@ -675,7 +675,7 @@ static PyObject *Decoder_register(PyObject *self, PyObject *args,
return py_new_output_id;
}
pdo = malloc(sizeof(struct srd_pd_output));
pdo = g_try_malloc0(sizeof(struct srd_pd_output));
if (pdo == NULL){
PyGILState_Release(gstate);
srd_err("%s,ERROR:failed to alloc memory.", __func__);
@@ -826,7 +826,7 @@ static int create_term_list(PyObject *py_dict, GSList **term_list, gboolean cur_
goto err;
}
term = malloc(sizeof(struct srd_term));
term = g_try_malloc0(sizeof(struct srd_term));
if (term != NULL){
memset(term, 0, sizeof(struct srd_term));
term->type = get_term_type(term_str);
@@ -845,7 +845,7 @@ static int create_term_list(PyObject *py_dict, GSList **term_list, gboolean cur_
srd_err("Failed to get number of samples to skip.");
goto err;
}
term = malloc(sizeof(struct srd_term));
term = g_try_malloc0(sizeof(struct srd_term));
if (term != NULL){
memset(term, 0, sizeof(struct srd_term));
term->type = SRD_TERM_SKIP;
@@ -1016,7 +1016,7 @@ static int set_skip_condition(struct srd_decoder_inst *di, uint64_t count)
condition_list_free(di);
term = malloc(sizeof(struct srd_term));
term = g_try_malloc0(sizeof(struct srd_term));
if (term != NULL){
memset(term, 0, sizeof(struct srd_term));
term->type = SRD_TERM_SKIP;

View File

@@ -64,7 +64,7 @@ SRD_PRIV PyObject *py_import_by_name(const char *name)
* @param[out] outstr ptr to char * storage to be filled in.
*
* @return SRD_OK upon success, a (negative) error code otherwise.
* The 'outstr' argument points to a malloc()ed string upon success.
* The 'outstr' argument points to a g_try_malloc0()ed string upon success.
*
* @private
*/
@@ -108,7 +108,7 @@ err:
* @param[out] outstrlist ptr to GSList of char * storage to be filled in.
*
* @return SRD_OK upon success, a (negative) error code otherwise.
* The 'outstrlist' argument points to a GSList of malloc()ed strings
* The 'outstrlist' argument points to a GSList of g_try_malloc0()ed strings
* upon success.
*
* @private
@@ -170,7 +170,7 @@ err:
* @param[out] outstr Pointer to char * storage to be filled in.
*
* @return SRD_OK upon success, a (negative) error code otherwise.
* The 'outstr' argument points to a malloc()ed string upon success.
* The 'outstr' argument points to a g_try_malloc0()ed string upon success.
*
* @private
*/
@@ -245,7 +245,7 @@ err:
* @param[out] outstr Pointer to char * storage to be filled in.
*
* @return SRD_OK upon success, a (negative) error code otherwise.
* The 'outstr' argument points to a malloc()ed string upon success.
* The 'outstr' argument points to a g_try_malloc0()ed string upon success.
*
* @private
*/
@@ -286,7 +286,7 @@ err:
* @param outstr Pointer to char * storage to be filled in.
*
* @return SRD_OK upon success, a (negative) error code otherwise.
* The 'outstr' argument points to a malloc()ed string upon success.
* The 'outstr' argument points to a g_try_malloc0()ed string upon success.
*
* @private
*/
@@ -384,7 +384,7 @@ err:
* @param[out] outstr ptr to char * storage to be filled in.
*
* @return SRD_OK upon success, a (negative) error code otherwise.
* The 'outstr' argument points to a malloc()ed string upon success.
* The 'outstr' argument points to a g_try_malloc0()ed string upon success.
*
* @private
*/