libgimp: some code reorganization.

This commit message has been rewritten by the reviewer (Jehan) because
the original message was not right. It was originally meant to fix #13189
but it was only doing so as a side effect of getting rid of the slice
API.

We still push it as a second step (with updated message) because it's
not a bad idea to stop using the slice API where we can as it's anyway
no more than a malloc/free synonym since GLib 2.76 and might get
deprecated some day.
This commit is contained in:
lloyd konneker 2025-04-12 15:28:01 -04:00 committed by Jehan
parent 3f36a16f6d
commit 2d7a1a5d6d

View file

@ -55,7 +55,7 @@ typedef struct
struct _GimpGradientChooser struct _GimpGradientChooser
{ {
GimpResourceChooser parent_instance; GimpResourceChooser parent_instance;
GimpGradientPreviewData *local_grad_data; GimpGradientPreviewData local_grad_data;
GtkWidget *preview; GtkWidget *preview;
}; };
@ -77,6 +77,7 @@ static gboolean gimp_gradient_select_model_change_handler (GimpGradientChooser
gboolean is_closing); gboolean is_closing);
static void local_grad_data_new (GimpGradientChooser *self); static void local_grad_data_new (GimpGradientChooser *self);
static void local_grad_data_free (GimpGradientChooser *self);
static gboolean local_grad_data_exists (GimpGradientChooser *self); static gboolean local_grad_data_exists (GimpGradientChooser *self);
static gboolean local_grad_data_refresh (GimpGradientChooser *self, static gboolean local_grad_data_refresh (GimpGradientChooser *self,
GimpGradient *gradient); GimpGradient *gradient);
@ -140,8 +141,7 @@ gimp_gradient_chooser_finalize (GObject *object)
{ {
GimpGradientChooser *self = GIMP_GRADIENT_CHOOSER (object); GimpGradientChooser *self = GIMP_GRADIENT_CHOOSER (object);
g_free (self->local_grad_data->data); local_grad_data_free (self);
g_slice_free (GimpGradientPreviewData, self->local_grad_data);
/* chain up. */ /* chain up. */
G_OBJECT_CLASS (gimp_gradient_chooser_parent_class)->finalize (object); G_OBJECT_CLASS (gimp_gradient_chooser_parent_class)->finalize (object);
@ -347,9 +347,9 @@ gimp_gradient_select_preview_draw_handler (GtkWidget *widget,
/* Width in pixels of src, since BPP is 4. */ /* Width in pixels of src, since BPP is 4. */
gimp_gradient_select_preview_draw (cr, gimp_gradient_select_preview_draw (cr,
self->local_grad_data->n_samples, self->local_grad_data.n_samples,
self->local_grad_data->allocation_width, self->local_grad_data.allocation_width,
self->local_grad_data->data); self->local_grad_data.data);
return FALSE; return FALSE;
} }
@ -387,13 +387,27 @@ gimp_gradient_select_model_change_handler (GimpGradientChooser *self,
static gboolean static gboolean
local_grad_data_exists (GimpGradientChooser *self) local_grad_data_exists (GimpGradientChooser *self)
{ {
return self->local_grad_data->data != 0; return self->local_grad_data.data != NULL;
} }
static void static void
local_grad_data_new (GimpGradientChooser *self) local_grad_data_new (GimpGradientChooser *self)
{ {
self->local_grad_data = g_slice_new0 (GimpGradientPreviewData); self->local_grad_data.data = NULL;
self->local_grad_data.n_samples = 0;
self->local_grad_data.allocation_width = 0;
}
static void
local_grad_data_free (GimpGradientChooser *self)
{
/* More robust when set to NULL.
* g_free(NULL) is safe but g_free twice is not.
*/
g_free (self->local_grad_data.data);
self->local_grad_data.data = NULL;
self->local_grad_data.n_samples = 0;
} }
/* Called at initial draw to get local data for the model gradient. /* Called at initial draw to get local data for the model gradient.
@ -408,10 +422,10 @@ local_grad_data_refresh (GimpGradientChooser *self, GimpGradient *gradient)
gsize n_samples; gsize n_samples;
/* Must not be called before widget is allocated. */ /* Must not be called before widget is allocated. */
g_assert (self->local_grad_data->allocation_width != 0); g_assert (self->local_grad_data.allocation_width != 0);
if (!get_gradient_data (gradient, if (!get_gradient_data (gradient,
self->local_grad_data->allocation_width, self->local_grad_data.allocation_width,
&n_samples, &n_samples,
&src)) &src))
{ {
@ -420,9 +434,9 @@ local_grad_data_refresh (GimpGradientChooser *self, GimpGradient *gradient)
} }
else else
{ {
g_free (self->local_grad_data->data); local_grad_data_free (self);
self->local_grad_data->data = src; self->local_grad_data.data = src;
self->local_grad_data->n_samples = n_samples; self->local_grad_data.n_samples = n_samples;
return TRUE; return TRUE;
} }
} }
@ -430,5 +444,5 @@ local_grad_data_refresh (GimpGradientChooser *self, GimpGradient *gradient)
static void static void
local_grad_data_set_allocation_width (GimpGradientChooser *self, gint width) local_grad_data_set_allocation_width (GimpGradientChooser *self, gint width)
{ {
self->local_grad_data->allocation_width = width; self->local_grad_data.allocation_width = width;
} }