Commit 66dad217 authored by Debarshi Ray's avatar Debarshi Ray

gegl: Simplify code

Leverage modern GLib API like g_clear_* and g_set_object; and the facts
that g_free and g_(s)list_free* are already NULL-safe, and a GObject's
finalize virtual method is invoked only once.

https://bugzilla.gnome.org/show_bug.cgi?id=789543
parent 5499882d
......@@ -63,8 +63,7 @@ load_info_destroy (LoadInfo *info)
{
if (!info)
return;
if (info->path)
g_free (info->path);
g_free (info->path);
if (info->i != -1)
close (info->i);
if (info->tiles != NULL)
......
......@@ -106,8 +106,7 @@ save_info_destroy (SaveInfo *info)
{
if (!info)
return;
if (info->path)
g_free (info->path);
g_free (info->path);
if (info->o != -1)
close (info->o);
if (info->tiles != NULL)
......
......@@ -211,8 +211,7 @@ gegl_buffer_set_property (GObject *gobject,
break;
case PROP_PATH:
if (buffer->path)
g_free (buffer->path);
g_free (buffer->path);
buffer->path = g_value_dup_string (value);
break;
......@@ -264,8 +263,7 @@ gegl_buffer_set_property (GObject *gobject,
break;
case PROP_BACKEND:
if (buffer->backend)
g_object_unref (buffer->backend);
g_clear_object (&buffer->backend);
buffer->backend = g_value_dup_object (value);
break;
......@@ -388,11 +386,7 @@ gegl_buffer_dispose (GObject *object)
_gegl_buffer_drop_hot_tile (buffer);
if (buffer->backend)
{
g_object_unref (buffer->backend);
buffer->backend = NULL;
}
g_clear_object (&buffer->backend);
G_OBJECT_CLASS (parent_class)->dispose (object);
}
......@@ -508,8 +502,7 @@ gegl_buffer_constructor (GType type,
buffer->tile_width = gegl_tile_backend_get_tile_width (backend);
buffer->tile_height = gegl_tile_backend_get_tile_height (backend);
if (buffer->path)
g_free (buffer->path);
g_free (buffer->path);
if (GEGL_IS_TILE_BACKEND_FILE (backend))
g_object_get (backend, "path", &buffer->path, NULL);
......
......@@ -311,8 +311,7 @@ set_property (GObject *object,
break;
case PROP_TYPE:
if (self->type)
g_free (self->type);
g_free (self->type);
self->type = g_value_dup_string (value);
break;
......
......@@ -562,8 +562,7 @@ gegl_buffer_sample_at_level (GeglBuffer *buffer,
if (buffer->sampler)
{
g_object_unref (buffer->sampler);
buffer->sampler = NULL;
g_clear_object (&buffer->sampler);
buffer->sampler_type = 0;
}
......@@ -612,11 +611,7 @@ gegl_buffer_sample_cleanup (GeglBuffer *buffer)
if (threaded)
g_mutex_lock (&gegl_buffer_sampler_mutex);
if (buffer->sampler)
{
g_object_unref (buffer->sampler);
buffer->sampler = NULL;
}
g_clear_object (&buffer->sampler);
if (threaded)
g_mutex_unlock (&gegl_buffer_sampler_mutex);
......
......@@ -879,8 +879,7 @@ gegl_tile_backend_file_set_property (GObject *object,
switch (property_id)
{
case PROP_PATH:
if (self->path)
g_free (self->path);
g_free (self->path);
self->path = g_value_dup_string (value);
break;
......@@ -971,11 +970,8 @@ gegl_tile_backend_file_finalize (GObject *object)
g_free (self->path);
}
if (self->monitor)
g_object_unref (self->monitor);
if (self->file)
g_object_unref (self->file);
g_clear_object (&self->monitor);
g_clear_object (&self->file);
g_cond_clear (&self->cond);
......
......@@ -321,8 +321,7 @@ gegl_tile_handler_cache_wash (GeglTileHandlerCache *cache)
{
gegl_tile_store (last_dirty);
gegl_tile_unref (last_dirty);
if (last_dirty->tile_storage)
g_object_unref (last_dirty->tile_storage);
g_clear_object (&last_dirty->tile_storage);
return TRUE;
}
return FALSE;
......
......@@ -49,11 +49,7 @@ gegl_tile_handler_dispose (GObject *object)
{
GeglTileHandler *handler = GEGL_TILE_HANDLER (object);
if (handler->source)
{
g_object_unref (handler->source);
handler->source = NULL;
}
g_clear_object (&handler->source);
G_OBJECT_CLASS (gegl_tile_handler_parent_class)->dispose (object);
}
......@@ -145,16 +141,7 @@ void
gegl_tile_handler_set_source (GeglTileHandler *handler,
GeglTileSource *source)
{
if (source != handler->source)
{
if (handler->source)
g_object_unref (handler->source);
handler->source = source;
if (handler->source)
g_object_ref (handler->source);
}
g_set_object (&handler->source, source);
}
void
......
......@@ -132,8 +132,7 @@ gegl_config_set_property (GObject *gobject,
config->quality = g_value_get_double (value);
return;
case PROP_SWAP:
if (config->swap)
g_free (config->swap);
g_free (config->swap);
config->swap = g_value_dup_string (value);
break;
case PROP_THREADS:
......@@ -146,8 +145,7 @@ gegl_config_set_property (GObject *gobject,
config->queue_size = g_value_get_int (value);
break;
case PROP_APPLICATION_LICENSE:
if (config->application_license)
g_free (config->application_license);
g_free (config->application_license);
config->application_license = g_value_dup_string (value);
break;
default:
......@@ -161,11 +159,8 @@ gegl_config_finalize (GObject *gobject)
{
GeglConfig *config = GEGL_CONFIG (gobject);
if (config->swap)
g_free (config->swap);
if (config->application_license)
g_free (config->application_license);
g_free (config->swap);
g_free (config->application_license);
G_OBJECT_CLASS (gegl_config_parent_class)->finalize (gobject);
}
......
......@@ -146,8 +146,7 @@ gegl_init_swap_dir (void)
! g_file_test (swapdir, G_FILE_TEST_IS_DIR) &&
g_mkdir_with_parents (swapdir, S_IRUSR | S_IWUSR | S_IXUSR) != 0)
{
g_free (swapdir);
swapdir = NULL;
g_clear_pointer (&swapdir, g_free);
}
g_object_set (config, "swap", swapdir, NULL);
......@@ -475,11 +474,7 @@ gegl_exit (void)
gegl_temp_buffer_free ();
if (module_db != NULL)
{
g_object_unref (module_db);
module_db = NULL;
}
g_clear_object (&module_db);
babl_exit ();
......@@ -543,8 +538,7 @@ gegl_exit (void)
g_pattern_spec_free (pattern);
}
g_object_unref (config);
config = NULL;
g_clear_object (&config);
global_time = 0;
}
......
......@@ -489,8 +489,7 @@ set_property (GObject *gobject,
break;
#define property_string(name, label, def_val) \
case PROP_##name: \
if (properties->name) \
g_free (properties->name); \
g_free (properties->name); \
properties->name = g_value_dup_string (value); \
break;
#define property_boolean(name, label, def_val) \
......@@ -499,38 +498,32 @@ set_property (GObject *gobject,
break;
#define property_file_path(name, label, def_val) \
case PROP_##name: \
if (properties->name) \
g_free (properties->name); \
g_free (properties->name); \
properties->name = g_value_dup_string (value); \
break;
#define property_uri(name, label, def_val) \
case PROP_##name: \
if (properties->name) \
g_free (properties->name); \
g_free (properties->name); \
properties->name = g_value_dup_string (value); \
break;
#define property_object(name, label, def_val) \
case PROP_##name: \
if (properties->name != NULL) \
g_object_unref (properties->name); \
g_clear_object (&properties->name); \
properties->name = g_value_dup_object (value); \
break;
#define property_curve(name, label, def_val) \
case PROP_##name: \
if (properties->name != NULL) \
g_object_unref (properties->name); \
g_clear_object (&properties->name); \
properties->name = g_value_dup_object (value); \
break;
#define property_color(name, label, def_val) \
case PROP_##name: \
if (properties->name != NULL) \
g_object_unref (properties->name); \
g_clear_object (&properties->name); \
properties->name = g_value_dup_object (value); \
break;
#define property_audio_fragment(name, label, def_val) \
case PROP_##name: \
if (properties->name != NULL) \
g_object_unref (properties->name); \
g_clear_object (&properties->name); \
properties->name = g_value_dup_object (value); \
break;
#define property_path(name, label, def_val) \
......@@ -605,54 +598,22 @@ static void gegl_op_destroy_notify (gpointer data)
#define property_double(name, label, def_val)
#define property_int(name, label, def_val)
#define property_string(name, label, ...) \
if (properties->name) \
{ \
g_free (properties->name); \
properties->name = NULL; \
}
g_clear_pointer (&properties->name, g_free);
#define property_boolean(name, label, def_val)
#define property_file_path(name, label, def_val) \
if (properties->name) \
{ \
g_free (properties->name); \
properties->name = NULL; \
}
g_clear_pointer (&properties->name, g_free);
#define property_uri(name, label, def_val) \
if (properties->name) \
{ \
g_free (properties->name); \
properties->name = NULL; \
}
g_clear_pointer (&properties->name, g_free);
#define property_object(name, label, def_val) \
if (properties->name) \
{ \
g_object_unref (properties->name); \
properties->name = NULL; \
}
g_clear_object (&properties->name);
#define property_curve(name, label, def_val) \
if (properties->name) \
{ \
g_object_unref (properties->name); \
properties->name = NULL; \
}
g_clear_object (&properties->name);
#define property_color(name, label, def_val) \
if (properties->name) \
{ \
g_object_unref (properties->name); \
properties->name = NULL; \
}
g_clear_object (&properties->name);
#define property_audio_fragment(name, label, def_val) \
if (properties->name) \
{ \
g_object_unref (properties->name); \
properties->name = NULL; \
}
g_clear_object (&properties->name);
#define property_path(name, label, def_val) \
if (properties->name) \
{ \
g_object_unref (properties->name); \
properties->name = NULL; \
}
g_clear_object (&properties->name);
#define property_pointer(name, label, ...)
#define property_format(name, label, ...)
#define property_enum(name, label, ...)
......
......@@ -300,8 +300,7 @@ gegl_create_chain_argv (char **ops,
{
string = g_string_new ("");
in_strkeyframes = 1;
if (prop)
g_free (prop);
g_free (prop);
prop = g_strdup (key);
}
else
......@@ -312,8 +311,7 @@ gegl_create_chain_argv (char **ops,
anim_quark = g_quark_from_string (tmpbuf);
path = gegl_path_new ();
in_keyframes = 1;
if (prop)
g_free (prop);
g_free (prop);
prop = g_strdup (key);
g_object_set_qdata_full (G_OBJECT (
......@@ -671,10 +669,7 @@ gegl_create_chain_argv (char **ops,
level_pad[level]);
}
if (prop)
{
g_free (prop);
}
g_free (prop);
g_hash_table_unref (ht);
gegl_node_link_many (iter[level], proxy, NULL);
}
......
......@@ -231,9 +231,7 @@ set_clone_prop_as_well:
if (pd->curve)
{
gegl_node_set (new, param_name, pd->curve, NULL);
g_object_unref (pd->curve);
pd->curve = NULL;
g_clear_object (&pd->curve);
}
}
else if (paramspec->value_type == GEGL_TYPE_PATH)
......@@ -492,8 +490,7 @@ static void end_element (GMarkupParseContext *context,
}
else if (!strcmp (element_name, "param"))
{
g_free (pd->param);
pd->param = NULL;
g_clear_pointer (&pd->param, g_free);
}
else if (!strcmp (element_name, "curve"))
{
......@@ -587,11 +584,7 @@ GeglNode *gegl_node_new_from_xml (const gchar *xmldata,
}
else
{
if (pd.gegl)
{
g_object_unref (pd.gegl);
pd.gegl = NULL;
}
g_clear_object (&pd.gegl);
}
g_list_free (pd.refs);
......
......@@ -252,17 +252,8 @@ gegl_node_dispose (GObject *gobject)
}
gegl_node_remove_children (self);
if (self->cache)
{
g_object_unref (self->cache);
self->cache = NULL;
}
if (self->priv->eval_manager)
{
g_object_unref (self->priv->eval_manager);
self->priv->eval_manager = NULL;
}
g_clear_object (&self->cache);
g_clear_object (&self->priv->eval_manager);
G_OBJECT_CLASS (gegl_node_parent_class)->dispose (gobject);
}
......@@ -275,31 +266,13 @@ gegl_node_finalize (GObject *gobject)
gegl_node_disconnect_sources (self);
gegl_node_disconnect_sinks (self);
if (self->pads)
{
g_slist_foreach (self->pads, (GFunc) g_object_unref, NULL);
g_slist_free (self->pads);
self->pads = NULL;
}
g_slist_free_full (self->pads, g_object_unref);
g_slist_free (self->input_pads);
g_slist_free (self->output_pads);
if (self->operation)
{
g_object_unref (self->operation);
self->operation = NULL;
}
if (self->priv->name)
{
g_free (self->priv->name);
}
if (self->priv->debug_name)
{
g_free (self->priv->debug_name);
}
g_clear_object (&self->operation);
g_free (self->priv->name);
g_free (self->priv->debug_name);
g_mutex_clear (&self->mutex);
......@@ -1035,8 +1008,7 @@ gegl_node_blit (GeglNode *self,
if (buffer && destination_buf)
gegl_buffer_get (buffer, roi, scale, format, destination_buf, rowstride, GEGL_ABYSS_NONE);
if (buffer)
g_object_unref (buffer);
g_clear_object (&buffer);
}
else if (flags & GEGL_BLIT_CACHE)
{
......@@ -1257,10 +1229,7 @@ gegl_node_set_operation_object (GeglNode *self,
gegl_node_disconnect_sources (self);
gegl_node_disconnect_sinks (self);
if (self->operation)
g_object_unref (self->operation);
self->operation = g_object_ref (operation);
g_set_object (&self->operation, operation);
/* Delete all the pads from the previous operation */
while (self->pads)
......@@ -1600,8 +1569,7 @@ gegl_node_update_debug_name (GeglNode *node)
g_return_if_fail (GEGL_IS_NODE (node));
if (node->priv->debug_name)
g_free (node->priv->debug_name);
g_free (node->priv->debug_name);
if (name && *name)
new_name = g_strdup_printf ("%s '%s' %p", operation ? operation : "(none)", name, node);
......@@ -1872,10 +1840,7 @@ gegl_node_get_cache (GeglNode *node)
}
if (node->cache && gegl_buffer_get_format ((GeglBuffer *)(node->cache)) != format)
{
g_object_unref (node->cache);
node->cache = NULL;
}
g_clear_object (&node->cache);
if (node->cache)
return node->cache;
......@@ -1919,9 +1884,7 @@ gegl_node_set_name (GeglNode *self,
{
g_return_if_fail (GEGL_IS_NODE (self));
if (self->priv->name)
g_free (self->priv->name);
g_free (self->priv->name);
self->priv->name = g_strdup (name);
gegl_node_update_debug_name (self);
......
......@@ -80,17 +80,8 @@ finalize (GObject *gobject)
g_assert (self->connections == NULL);
if (self->param_spec)
{
g_param_spec_unref (self->param_spec);
self->param_spec = NULL;
}
if (self->name)
{
g_free (self->name);
self->name = NULL;
}
g_clear_pointer (&self->param_spec, (GDestroyNotify) g_param_spec_unref);
g_free (self->name);
G_OBJECT_CLASS (gegl_pad_parent_class)->finalize (gobject);
}
......@@ -215,8 +206,7 @@ gegl_pad_get_name (GeglPad *self)
void gegl_pad_set_name (GeglPad *self,
const gchar *name)
{
if (self->name)
g_free (self->name);
g_free (self->name);
self->name = g_strdup (name);
}
......
......@@ -151,8 +151,7 @@ gegl_path_parse (const gchar *path,
static void
gegl_path_free (GList *path)
{
g_list_foreach (path, (GFunc) g_free, NULL);
g_list_free (path);
g_list_free_full (path, g_free);
}
void
......
......@@ -98,23 +98,10 @@ gegl_module_finalize (GObject *object)
{
GeglModule *module = GEGL_MODULE (object);
if (module->info)
{
gegl_module_info_free (module->info);
module->info = NULL;
}
g_clear_pointer (&module->info, (GDestroyNotify) gegl_module_info_free);
if (module->last_module_error)
{
g_free (module->last_module_error);
module->last_module_error = NULL;
}
if (module->filename)
{
g_free (module->filename);
module->filename = NULL;
}
g_free (module->last_module_error);
g_free (module->filename);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
......@@ -396,9 +383,7 @@ static void
gegl_module_error_message (GeglModule *module,
const gchar *error_str)
{
if (module->last_module_error)
g_free (module->last_module_error);
g_free (module->last_module_error);
module->last_module_error = g_strdup (error_str);
g_message (_("Module '%s' load error: %s"),
......
......@@ -118,17 +118,8 @@ gegl_module_db_finalize (GObject *object)
{
GeglModuleDB *db = GEGL_MODULE_DB (object);
if (db->modules)
{
g_list_free (db->modules);
db->modules = NULL;
}
if (db->load_inhibit)
{
g_free (db->load_inhibit);
db->load_inhibit = NULL;
}
g_list_free (db->modules);
g_free (db->load_inhibit);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
......@@ -207,9 +198,7 @@ gegl_module_db_set_load_inhibit (GeglModuleDB *db,
g_return_if_fail (GEGL_IS_MODULE_DB (db));
if (db->load_inhibit)
g_free (db->load_inhibit);
g_free (db->load_inhibit);
db->load_inhibit = g_strdup (load_inhibit);
for (list = db->modules; list; list = g_list_next (list))
......
......@@ -218,10 +218,8 @@ gegl_operation_composer_process (GeglOperation *operation,
success = klass->process (operation, input, aux, output, result, level);
}
if (input)
g_object_unref (input);
if (aux)
g_object_unref (aux);
g_clear_object (&input);
g_clear_object (&aux);
}
else
{
......
......@@ -241,12 +241,9 @@ gegl_operation_composer3_process (GeglOperation *operation,
success = klass->process (operation, input, aux, aux2, output, result, level);
}
if (input)
g_object_unref (input);
if (aux)
g_object_unref (aux);
if (aux2)
g_object_unref (aux2);
g_clear_object (&input);
g_clear_object (&aux);
g_clear_object (&aux2);
}
else
{
......
......@@ -119,10 +119,9 @@ redirect_destroy (Redirect *self)
{
if (!self)
return;
if (self->name)
g_free (self->name);
if (self->internal_name)
g_free (self->internal_name);
g_free (self->name);
g_free (self->internal_name);
g_slice_free (Redirect, self);
}
......
......@@ -153,10 +153,8 @@ gegl_operation_composer_process (GeglOperation *operation,
{
success = klass->process (operation, input, aux, output, result, level);
if (input)
g_object_unref (input);
if (aux)
g_object_unref (aux);
g_clear_object (&input);
g_clear_object (&aux);
}
else
{
......
......@@ -151,12 +151,9 @@ gegl_operation_composer3_process (GeglOperation *operation,
{
success = klass->process (operation, input, aux, aux2, output, result, level);
if (input)
g_object_unref (input);
if (aux)
g_object_unref (aux);
if (aux2)
g_object_unref (aux2);
g_clear_object (&input);
g_clear_object (&aux);
g_clear_object (&aux2);
}
else
{
......
......@@ -634,10 +634,8 @@ gegl_operation_cl_set_kernel_args (GeglOperation *operation,
}
}
if (self)
g_free (self);
if (parent)
g_free (parent);
g_free (self);
g_free (parent);
return TRUE;
}
......
......@@ -58,17 +58,8 @@ gegl_eval_manager_finalize (GObject *self_object)
{
GeglEvalManager *self = GEGL_EVAL_MANAGER (self_object);
if (self->pad_name)
{
g_free (self->pad_name);
self->pad_name = NULL;
}
if (self->traversal)
{
gegl_graph_free (self->traversal);
self->traversal = NULL;
}
g_free (self->pad_name);
g_clear_pointer (&self->traversal, (GDestroyNotify) gegl_graph_free);
g_signal_handlers_disconnect_by_data (self->node, self);