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

@@ -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
*/