- Generally avoid assigning values to variables at declaration time,
especially so for complex and/or run-time dependent values.
- - Consistently use g_try_malloc() / g_try_malloc0(). Do not use standard
+ - 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).
- Always properly match allocations with the proper *free() functions. If
- glib's g_try_malloc()/g_try_malloc0() was used, use g_free() to free the
+ glib's g_*malloc()/g_*malloc0() was used, use g_free() to free the
memory. Otherwise use standard free(). Never use the wrong function!
- - Never use g_malloc() or g_malloc0(). These functions do not return NULL
- if not enough memory is available but rather lead to an exit() or segfault
- instead. This behaviour is not acceptable for libraries.
- Use g_try_malloc()/g_try_malloc0() instead and check the return value.
+ - We assume that "small" memory allocations (< 1MB) will always succeed.
+ Thus, it's fine to use g_malloc() or g_malloc0() for allocations of
+ simple/small structs and such (instead of using g_try_malloc()), and
+ there's no need to check the return value.
+
+ Do use g_try_malloc() 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
elsewhere) "manually" via e.g. printf() or g_log() or similar functions.
break;
}
- if (!(pdch = g_try_malloc(sizeof(struct srd_channel)))) {
- srd_err("Failed to g_malloc() struct srd_channel.");
- ret = SRD_ERR_MALLOC;
- break;
- }
+ pdch = g_malloc(sizeof(struct srd_channel));
if ((py_dictitem_as_str(py_entry, "id", &pdch->id)) != SRD_OK) {
ret = SRD_ERR_PYTHON;
py_basedec = py_method = py_attr = NULL;
- if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) {
- srd_dbg("Failed to g_malloc() struct srd_decoder.");
- ret = SRD_ERR_MALLOC;
- goto err_out;
- }
+ d = g_malloc0(sizeof(struct srd_decoder));
ret = SRD_ERR_PYTHON;
Py_CLEAR(py_basedec);
/*
- * Check that thіs decoder has the correct PD API version.
+ * Check that this decoder has the correct PD API version.
* PDs of different API versions are incompatible and cannot work.
*/
py_long = PyObject_GetAttrString(d->py_dec, "api_version");
return SRD_ERR_ARG;
}
- new_channelmap = NULL;
-
- if (!(new_channelmap = g_try_malloc(sizeof(int) * di->dec_num_channels))) {
- srd_err("Failed to g_malloc() new channel map.");
- return SRD_ERR_MALLOC;
- }
+ new_channelmap = g_malloc(sizeof(int) * di->dec_num_channels);
/*
* For now, map all indexes to channel -1 (can be overridden later).
return NULL;
}
- if (!(di = g_try_malloc0(sizeof(struct srd_decoder_inst)))) {
- srd_err("Failed to g_malloc() instance.");
- return NULL;
- }
+ di = g_malloc0(sizeof(struct srd_decoder_inst));
di->decoder = dec;
di->sess = sess;
di->dec_num_channels = g_slist_length(di->decoder->channels) +
g_slist_length(di->decoder->opt_channels);
if (di->dec_num_channels) {
- if (!(di->dec_channelmap =
- g_try_malloc(sizeof(int) * di->dec_num_channels))) {
- srd_err("Failed to g_malloc() channel map.");
- g_free(di);
- return NULL;
- }
+ di->dec_channelmap =
+ g_malloc(sizeof(int) * di->dec_num_channels);
for (i = 0; i < di->dec_num_channels; i++)
di->dec_channelmap[i] = i;
di->data_unitsize = (di->dec_num_channels + 7) / 8;
* Will be used to prepare a sample at every iteration
* of the instance's decode() method.
*/
- if (!(di->channel_samples = g_try_malloc(di->dec_num_channels))) {
- srd_err("Failed to g_malloc() sample buffer.");
- g_free(di->dec_channelmap);
- g_free(di);
- return NULL;
- }
+ di->channel_samples = g_malloc(di->dec_num_channels);
}
/* Create a new instance of this decoder class. */
return SRD_ERR_ARG;
}
- if (!(*sess = g_try_malloc(sizeof(struct srd_session))))
- return SRD_ERR_MALLOC;
+ *sess = g_malloc(sizeof(struct srd_session));
(*sess)->session_id = ++max_session_id;
(*sess)->di_list = (*sess)->callbacks = NULL;
srd_dbg("Registering new callback for output type %d.", output_type);
- if (!(pd_cb = g_try_malloc(sizeof(struct srd_pd_callback)))) {
- srd_err("Failed to g_malloc() struct srd_pd_callback.");
- return SRD_ERR_MALLOC;
- }
-
+ pd_cb = g_malloc(sizeof(struct srd_pd_callback));
pd_cb->output_type = output_type;
pd_cb->cb = cb;
pd_cb->cb_data = cb_data;
/* Convert to wide chars. */
wc_len = sizeof(wchar_t) * (new_path->len + 1);
- if (!(wc_new_path = g_try_malloc(wc_len))) {
- srd_dbg("malloc failed");
- return SRD_ERR_MALLOC;
- }
+ wc_new_path = g_malloc(wc_len);
mbstowcs(wc_new_path, new_path->str, wc_len);
PySys_SetPath(wc_new_path);
g_string_free(new_path, TRUE);
return SRD_ERR_PYTHON;
}
- if (!(pda = g_try_malloc(sizeof(struct srd_proto_data_annotation))))
- return SRD_ERR_MALLOC;
+ pda = g_malloc(sizeof(struct srd_proto_data_annotation));
pda->ann_class = ann_class;
pda->ann_text = ann_text;
pdata->data = pda;
return SRD_ERR_PYTHON;
}
- if (!(pdb = g_try_malloc(sizeof(struct srd_proto_data_binary))))
- return SRD_ERR_MALLOC;
+ pdb = g_malloc(sizeof(struct srd_proto_data_binary));
if (PyBytes_AsStringAndSize(py_tmp, &buf, &size) == -1)
return SRD_ERR_PYTHON;
pdb->bin_class = bin_class;
di->inst_id, start_sample, end_sample,
OUTPUT_TYPES[pdo->output_type], output_id);
- if (!(pdata = g_try_malloc0(sizeof(struct srd_proto_data)))) {
- srd_err("Failed to g_malloc() struct srd_proto_data.");
- return NULL;
- }
+ pdata = g_malloc0(sizeof(struct srd_proto_data));
pdata->start_sample = start_sample;
pdata->end_sample = end_sample;
pdata->pdo = pdo;
srd_dbg("Instance %s creating new output type %d for %s.",
di->inst_id, output_type, proto_id);
- if (!(pdo = g_try_malloc(sizeof(struct srd_pd_output)))) {
- PyErr_SetString(PyExc_MemoryError, "struct srd_pd_output");
- return NULL;
- }
+ pdo = g_malloc(sizeof(struct srd_pd_output));
/* pdo_id is just a simple index, nothing is deleted from this list anyway. */
pdo->pdo_id = g_slist_length(di->pd_output);
goto err_out;
}
- if (!(*outstr = g_strdup(str))) {
- srd_dbg("Failed to g_malloc() outstr.");
- ret = SRD_ERR_MALLOC;
- goto err_out;
- }
+ *outstr = g_strdup(str);
err_out:
if (py_encstr)