From: Uwe Hermann Date: Sat, 7 Mar 2015 18:12:15 +0000 (+0100) Subject: Use g_malloc*() consistently, simplify error handling. X-Git-Tag: libsigrokdecode-0.4.0~127 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=commitdiff_plain;h=077fa8acbcb8b585af6f5323f16221940a27a72e Use g_malloc*() consistently, simplify error handling. Use g_malloc*() for small allocations and assume they always succeed. Simplify error handling in a few places accordingly. Document the rules in the README file. --- diff --git a/HACKING b/HACKING index 6c64fdb..6aaedac 100644 --- a/HACKING +++ b/HACKING @@ -38,18 +38,21 @@ Random notes - 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. diff --git a/decoder.c b/decoder.c index 15c03ab..a5341b3 100644 --- a/decoder.c +++ b/decoder.c @@ -132,11 +132,7 @@ static int get_channels(const struct srd_decoder *d, const char *attr, 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; @@ -331,11 +327,7 @@ SRD_API int srd_decoder_load(const char *module_name) 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; @@ -367,7 +359,7 @@ SRD_API int srd_decoder_load(const char *module_name) 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"); diff --git a/instance.c b/instance.c index d0e9d0a..5f0ce80 100644 --- a/instance.c +++ b/instance.c @@ -220,12 +220,7 @@ SRD_API int srd_inst_channel_set_all(struct srd_decoder_inst *di, 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). @@ -325,10 +320,7 @@ SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *sess, 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; @@ -346,12 +338,8 @@ SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *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; @@ -359,12 +347,7 @@ SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *sess, * 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. */ diff --git a/session.c b/session.c index 6963a23..0c86405 100644 --- a/session.c +++ b/session.c @@ -76,8 +76,7 @@ SRD_API int srd_session_new(struct srd_session **sess) 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; @@ -312,11 +311,7 @@ SRD_API int srd_pd_output_callback_add(struct srd_session *sess, 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; diff --git a/srd.c b/srd.c index 097ed29..68cfe0a 100644 --- a/srd.c +++ b/srd.c @@ -248,10 +248,7 @@ SRD_PRIV int srd_decoder_searchpath_add(const char *path) /* 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); diff --git a/type_decoder.c b/type_decoder.c index ef0e9d8..eeafe58 100644 --- a/type_decoder.c +++ b/type_decoder.c @@ -88,8 +88,7 @@ static int convert_annotation(struct srd_decoder_inst *di, PyObject *obj, 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; @@ -151,8 +150,7 @@ static int convert_binary(struct srd_decoder_inst *di, PyObject *obj, 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; @@ -233,10 +231,7 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args) 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; @@ -350,10 +345,7 @@ static PyObject *Decoder_register(PyObject *self, PyObject *args, 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); diff --git a/util.c b/util.c index c1b9040..764474b 100644 --- a/util.c +++ b/util.c @@ -145,11 +145,7 @@ SRD_PRIV int py_str_as_str(const PyObject *py_str, char **outstr) 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)