[goffice] Clean up some memory allocation.
- From: Morten Welinder <mortenw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [goffice] Clean up some memory allocation.
- Date: Fri, 3 Dec 2021 00:38:22 +0000 (UTC)
commit 59715501fa73c341fbf279647f6061b34efa2395
Author: Morten Welinder <terra gnome org>
Date: Thu Dec 2 19:36:36 2021 -0500
Clean up some memory allocation.
Prefer g_new over g_malloc.
Prefer go_memdup and new go_memdup_n over alloc+copy.
ChangeLog | 17 +++++++++++++++++
goffice/canvas/goc-group.c | 2 +-
goffice/component/go-component.c | 3 +--
goffice/data/go-data-simple.c | 5 ++---
goffice/utils/go-emf.c | 8 +++-----
goffice/utils/go-glib-extras.c | 22 +++++++++++++++++++++-
goffice/utils/go-glib-extras.h | 1 +
goffice/utils/go-spectre.c | 5 ++---
goffice/utils/go-svg.c | 7 +++----
plugins/plot_barcol/gog-barcol.c | 4 ++--
plugins/plot_barcol/gog-line.c | 4 ++--
11 files changed, 55 insertions(+), 23 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 84de7864..7fb1cd78 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2021-12-02 Morten Welinder <terra gnome org>
+
+ * goffice/utils/go-glib-extras.c (go_memdup_n): New function.
+
+ * goffice/utils/go-spectre.c (go_spectre_load_data): Use go_memdup.
+
+ * goffice/utils/go-emf.c (go_emf_load_data): Use go_memdup.
+ (go_emf_new_from_data): Ditto.
+
+ * goffice/component/go-component.c (go_component_duplicate): Use go_memdup.
+
+ * goffice/data/go-data-simple.c (go_data_vector_str_new_copy): Use
+ g_new, not g_malloc.
+ (go_data_vector_val_new_copy): Use go_memdup_n.
+
+ * goffice/canvas/goc-group.c (goc_group_init): Use g_new0, not g_malloc0.
+
2021-12-01 Morten Welinder <terra gnome org>
* goffice/utils/go-glib-extras.c (go_memdup): Replacement for
diff --git a/goffice/canvas/goc-group.c b/goffice/canvas/goc-group.c
index 8ac0b08f..f138c8fd 100644
--- a/goffice/canvas/goc-group.c
+++ b/goffice/canvas/goc-group.c
@@ -314,7 +314,7 @@ goc_group_class_init (GocItemClass *item_klass)
static void
goc_group_init (GocGroup *group)
{
- group->priv = g_malloc0 (sizeof (struct GocGroupPriv));
+ group->priv = g_new0 (struct GocGroupPriv, 1);
group->priv->children = g_ptr_array_new ();
}
diff --git a/goffice/component/go-component.c b/goffice/component/go-component.c
index 9e2c5e9c..51ab0288 100644
--- a/goffice/component/go-component.c
+++ b/goffice/component/go-component.c
@@ -971,8 +971,7 @@ go_component_duplicate (GOComponent const *component)
g_value_unset (&value);
}
/* and now the data */
- new_data = g_malloc (component->length);
- memcpy (new_data, component->data, component->length);
+ new_data = go_memdup (component->data, component->length);
go_component_set_data (res, new_data, component->length);
res->destroy_notify = g_free;
res->destroy_data = new_data;
diff --git a/goffice/data/go-data-simple.c b/goffice/data/go-data-simple.c
index 84fc3b35..85ed0063 100644
--- a/goffice/data/go-data-simple.c
+++ b/goffice/data/go-data-simple.c
@@ -522,8 +522,7 @@ GOData *
go_data_vector_val_new_copy (double *val, unsigned n)
{
GODataVectorVal *res = g_object_new (GO_TYPE_DATA_VECTOR_VAL, NULL);
- res->val = g_malloc (n * sizeof (double));
- memcpy (res->val, val, n * sizeof (double));
+ res->val = go_memdup_n (val, n, sizeof (double));
res->n = n;
res->notify = g_free;
return GO_DATA (res);
@@ -830,7 +829,7 @@ go_data_vector_str_new_copy (char const * const *str, unsigned n)
{
GODataVectorStr *res = g_object_new (GO_TYPE_DATA_VECTOR_STR, NULL);
unsigned i;
- char **cpy = g_malloc ((n + 1) * sizeof (char*));
+ char **cpy = g_new (char *, n + 1);
for (i = 0; i < n; i++)
cpy[i] = g_strdup (str[i]);
cpy[i] = NULL;
diff --git a/goffice/utils/go-emf.c b/goffice/utils/go-emf.c
index ce95e5b8..ef0754e5 100644
--- a/goffice/utils/go-emf.c
+++ b/goffice/utils/go-emf.c
@@ -84,8 +84,7 @@ go_emf_load_data (GOImage *image, GsfXMLIn *xin)
GsfInput *input;
#endif
image->data_length = gsf_base64_decode_simple (xin->content->str, strlen(xin->content->str));
- image->data = g_malloc (image->data_length);
- memcpy (image->data, xin->content->str, image->data_length);
+ image->data = go_memdup (xin->content->str, image->data_length);
#ifdef GOFFICE_EMF_SUPPORT
input = gsf_input_memory_new (image->data, image->data_length, FALSE);
go_emf_parse (emf, input, &error);
@@ -226,7 +225,7 @@ go_emf_new_from_file (char const *filename, GError **error)
if (input == NULL)
return NULL;
size = gsf_input_size (input);
- data = g_malloc (size);
+ data = g_try_malloc (size);
if (!data || !gsf_input_read (input, size, data)) {
g_free (data);
if (error)
@@ -276,8 +275,7 @@ go_emf_new_from_data (char const *data, size_t length, GError **error)
emf = g_object_new (GO_TYPE_EMF, NULL);
image = GO_IMAGE (emf);
image->data_length = gsf_input_size (input);
- image->data = g_malloc (length);
- memcpy (image->data, data, length);
+ image->data = go_memdup (data, length);
if (!go_emf_parse (emf, input, error)) {
if (image->width < 1.) {
/* we load the size for an EMF file, so it should be WMF */
diff --git a/goffice/utils/go-glib-extras.c b/goffice/utils/go-glib-extras.c
index 1a2b54df..7b9863bc 100644
--- a/goffice/utils/go-glib-extras.c
+++ b/goffice/utils/go-glib-extras.c
@@ -859,7 +859,7 @@ go_destroy_password (char *passwd)
* @mem: (nullable): Memory to copy
* @byte_size: size of memory block to copy
*
- * Like g_memdup or g_memdup2. This function is meant to easy transition
+ * Like g_memdup or g_memdup2. This function is meant to ease transition
* to g_memdup2 without having to require very new glib.
**/
gpointer
@@ -873,6 +873,26 @@ go_memdup (gconstpointer mem, gsize byte_size)
return NULL;
}
+/**
+ * go_memdup_n:
+ * @mem: (nullable): Memory to copy
+ * @n_blocks: Number of blocks to copy.
+ * @block_size: Number of bytes per blocks.
+ *
+ * Like go_memdup (@mem, @n_blocks * @block_size), but with overflow check.
+ * Like a potential future g_memdup_n.
+ **/
+gpointer
+go_memdup_n (gconstpointer mem, gsize n_blocks, gsize block_size)
+{
+ if (mem && n_blocks > 0 && block_size > 0) {
+ gpointer new_mem = g_malloc_n (n_blocks, block_size);
+ memcpy (new_mem, mem, n_blocks * block_size);
+ return new_mem;
+ } else
+ return NULL;
+}
+
/**
diff --git a/goffice/utils/go-glib-extras.h b/goffice/utils/go-glib-extras.h
index 4424474e..0e041d15 100644
--- a/goffice/utils/go-glib-extras.h
+++ b/goffice/utils/go-glib-extras.h
@@ -90,6 +90,7 @@ char const *go_get_real_name (void);
void go_destroy_password (char *passwd);
gpointer go_memdup (gconstpointer mem, gsize byte_size);
+gpointer go_memdup_n (gconstpointer mem, gsize n_blocks, gsize block_size);
GType go_mem_chunk_get_type (void);
GOMemChunk *go_mem_chunk_new (char const *name, gsize user_atom_size, gsize chunk_size);
diff --git a/goffice/utils/go-spectre.c b/goffice/utils/go-spectre.c
index c4010fe4..62508d9a 100644
--- a/goffice/utils/go-spectre.c
+++ b/goffice/utils/go-spectre.c
@@ -105,8 +105,7 @@ go_spectre_load_data (GOImage *image, GsfXMLIn *xin)
#endif
image->data_length = gsf_base64_decode_simple (xin->content->str, strlen(xin->content->str));
- image->data = g_malloc (image->data_length);
- memcpy (image->data, xin->content->str, image->data_length);
+ image->data = go_memdup (xin->content->str, image->data_length);
#ifdef GOFFICE_WITH_EPS
spectre->doc = spectre_document_new ();
if (spectre->doc == NULL)
@@ -273,7 +272,7 @@ go_spectre_new_from_file (char const *filename, GError **error)
return NULL;
image = GO_IMAGE (spectre);
image->data_length = gsf_input_size (input);
- data = g_malloc (image->data_length);
+ data = g_try_malloc (image->data_length);
if (!data || !gsf_input_read (input, image->data_length, data)) {
g_object_unref (spectre);
g_free (data);
diff --git a/goffice/utils/go-svg.c b/goffice/utils/go-svg.c
index 593c3b9c..a24823ec 100644
--- a/goffice/utils/go-svg.c
+++ b/goffice/utils/go-svg.c
@@ -67,8 +67,7 @@ go_svg_load_data (GOImage *image, GsfXMLIn *xin)
GOSvg *svg = GO_SVG (image);
double dpi_x, dpi_y;
image->data_length = gsf_base64_decode_simple (xin->content->str, strlen(xin->content->str));
- image->data = g_malloc (image->data_length);
- memcpy (image->data, xin->content->str, image->data_length);
+ image->data = go_memdup (xin->content->str, image->data_length);
svg->handle = rsvg_handle_new_from_data (image->data, image->data_length, NULL);
go_image_get_default_dpi (&dpi_x, &dpi_y);
rsvg_handle_set_dpi_x_y (svg->handle, dpi_x, dpi_y);
@@ -178,7 +177,7 @@ go_svg_new_from_file (char const *filename, GError **error)
svg = g_object_new (GO_TYPE_SVG, NULL);
image = GO_IMAGE (svg);
image->data_length = gsf_input_size (input);
- data = g_malloc (image->data_length);
+ data = g_try_malloc (image->data_length);
if (!data || !gsf_input_read (input, image->data_length, data)) {
g_object_unref (svg);
g_free (data);
@@ -210,7 +209,7 @@ go_svg_new_from_data (char const *data, size_t length, GError **error)
svg = g_object_new (GO_TYPE_SVG, NULL);
image = GO_IMAGE (svg);
image->data_length = length;
- image->data = g_malloc (length);
+ image->data = g_try_malloc (length);
if (image->data == NULL) {
g_object_unref (svg);
return NULL;
diff --git a/plugins/plot_barcol/gog-barcol.c b/plugins/plot_barcol/gog-barcol.c
index f297dfa7..75e28a23 100644
--- a/plugins/plot_barcol/gog-barcol.c
+++ b/plugins/plot_barcol/gog-barcol.c
@@ -629,7 +629,7 @@ gog_barcol_view_render (GogView *view, GogViewAllocation const *bbox)
errors[i] = series->errors;
overrides[i] = gog_series_get_overrides (GOG_SERIES (series));
if (gog_error_bar_is_visible (series->errors))
- error_data[i] = g_malloc (sizeof (ErrorBarData) * lengths[i]);
+ error_data[i] = g_new (ErrorBarData, lengths[i]);
else
error_data[i] = NULL;
if (series->has_series_lines && lengths[i] > 0) {
@@ -645,7 +645,7 @@ gog_barcol_view_render (GogView *view, GogViewAllocation const *bbox)
lbl_role = gog_object_find_role_by_name (GOG_OBJECT (series), "Data labels");
labels[i] = (GogSeriesLabels *) gog_object_get_child_by_role (GOG_OBJECT (series), lbl_role);
if (labels[i]) {
- label_pos[i] = g_malloc (sizeof (LabelData) * lengths[i]);
+ label_pos[i] = g_new (LabelData, lengths[i]);
for (j = 0; j < lengths[i]; j++)
label_pos[i][j].elt = gog_series_labels_vector_get_element (labels[i] , j);
} else
diff --git a/plugins/plot_barcol/gog-line.c b/plugins/plot_barcol/gog-line.c
index a5e8aee9..f06a9be9 100644
--- a/plugins/plot_barcol/gog-line.c
+++ b/plugins/plot_barcol/gog-line.c
@@ -979,11 +979,11 @@ gog_line_view_render (GogView *view, GogViewAllocation const *bbox)
interpolations[i] = GO_LINE_INTERPOLATION_SPLINE;
if (!is_area_plot)
- points[i] = g_malloc (sizeof (Point) * (type == GOG_1_5D_NORMAL? lengths[i]:
num_elements));
+ points[i] = g_new (Point, type == GOG_1_5D_NORMAL? lengths[i]: num_elements);
errors[i] = series[i]->errors;
if (gog_error_bar_is_visible (series[i]->errors))
- error_data[i] = g_malloc (sizeof (ErrorBarData) * num_elements);
+ error_data[i] = g_new (ErrorBarData, num_elements);
else
error_data[i] = NULL;
if (series[i]->has_drop_lines) {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]