Commit a57c9148 authored by Christian Persch's avatar Christian Persch Committed by Matthias Clasen

GKeyFile: Add refcounting API

Adds g_key_file_ref and g_key_file_unref, to be used by a future
GKeyFile boxed type for language bindings.

Based on the patch by Christian Persch and Emmanuele Bassi.

Author: Christian Persch
Signed-off-by: Johan Dahlin
Signed-off-by: Giovanni Campagna

https://bugzilla.gnome.org/show_bug.cgi?id=590808
parent dfd75d2e
...@@ -1807,6 +1807,8 @@ GKeyFileFlags ...@@ -1807,6 +1807,8 @@ GKeyFileFlags
<SUBSECTION> <SUBSECTION>
g_key_file_new g_key_file_new
g_key_file_free g_key_file_free
g_key_file_ref
g_key_file_unref
g_key_file_set_list_separator g_key_file_set_list_separator
g_key_file_load_from_file g_key_file_load_from_file
g_key_file_load_from_data g_key_file_load_from_data
......
...@@ -446,6 +446,8 @@ struct _GKeyFile ...@@ -446,6 +446,8 @@ struct _GKeyFile
GKeyFileFlags flags; GKeyFileFlags flags;
gchar **locales; gchar **locales;
volatile gint ref_count;
}; };
typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair; typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
...@@ -602,8 +604,11 @@ g_key_file_clear (GKeyFile *key_file) ...@@ -602,8 +604,11 @@ g_key_file_clear (GKeyFile *key_file)
g_key_file_remove_group_node (key_file, group_node); g_key_file_remove_group_node (key_file, group_node);
} }
g_hash_table_destroy (key_file->group_hash); if (key_file->group_hash != NULL)
key_file->group_hash = NULL; {
g_hash_table_destroy (key_file->group_hash);
key_file->group_hash = NULL;
}
g_warn_if_fail (key_file->groups == NULL); g_warn_if_fail (key_file->groups == NULL);
} }
...@@ -627,6 +632,7 @@ g_key_file_new (void) ...@@ -627,6 +632,7 @@ g_key_file_new (void)
GKeyFile *key_file; GKeyFile *key_file;
key_file = g_slice_new0 (GKeyFile); key_file = g_slice_new0 (GKeyFile);
key_file->ref_count = 1;
g_key_file_init (key_file); g_key_file_init (key_file);
return key_file; return key_file;
...@@ -1054,11 +1060,33 @@ g_key_file_load_from_data_dirs (GKeyFile *key_file, ...@@ -1054,11 +1060,33 @@ g_key_file_load_from_data_dirs (GKeyFile *key_file,
return found_file; return found_file;
} }
/**
* g_key_file_ref:
* @key_file: a #GKeyFile
*
* Increases the reference count of @key_file.
*
* Returns: the same @key_file.
*
* Since: 2.32
**/
GKeyFile *
g_key_file_ref (GKeyFile *key_file)
{
g_return_val_if_fail (key_file != NULL, NULL);
g_atomic_int_inc (&key_file->ref_count);
return key_file;
}
/** /**
* g_key_file_free: * g_key_file_free:
* @key_file: a #GKeyFile * @key_file: a #GKeyFile
* *
* Frees a #GKeyFile. * Clears all keys and groups from @key_file, and decreases the
* reference count by 1. If the reference count reaches zero,
* frees the key file and all its allocated memory.
* *
* Since: 2.6 * Since: 2.6
**/ **/
...@@ -1066,9 +1094,30 @@ void ...@@ -1066,9 +1094,30 @@ void
g_key_file_free (GKeyFile *key_file) g_key_file_free (GKeyFile *key_file)
{ {
g_return_if_fail (key_file != NULL); g_return_if_fail (key_file != NULL);
g_key_file_clear (key_file); g_key_file_clear (key_file);
g_slice_free (GKeyFile, key_file); g_key_file_unref (key_file);
}
/**
* g_key_file_unref:
* @key_file: a #GKeyFile
*
* Decreases the reference count of @key_file by 1. If the reference count
* reaches zero, frees the key file and all its allocated memory.
*
* Since: 2.32
**/
void
g_key_file_unref (GKeyFile *key_file)
{
g_return_if_fail (key_file != NULL);
if (g_atomic_int_dec_and_test (&key_file->ref_count))
{
g_key_file_clear (key_file);
g_slice_free (GKeyFile, key_file);
}
} }
/* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
......
...@@ -55,6 +55,8 @@ typedef enum ...@@ -55,6 +55,8 @@ typedef enum
} GKeyFileFlags; } GKeyFileFlags;
GKeyFile *g_key_file_new (void); GKeyFile *g_key_file_new (void);
GKeyFile *g_key_file_ref (GKeyFile *key_file);
void g_key_file_unref (GKeyFile *key_file);
void g_key_file_free (GKeyFile *key_file); void g_key_file_free (GKeyFile *key_file);
void g_key_file_set_list_separator (GKeyFile *key_file, void g_key_file_set_list_separator (GKeyFile *key_file,
gchar separator); gchar separator);
......
...@@ -480,6 +480,8 @@ g_io_channel_win32_poll ...@@ -480,6 +480,8 @@ g_io_channel_win32_poll
g_io_channel_win32_set_debug g_io_channel_win32_set_debug
#endif #endif
g_key_file_error_quark g_key_file_error_quark
g_key_file_ref
g_key_file_unref
g_key_file_free g_key_file_free
g_key_file_get_boolean g_key_file_get_boolean
g_key_file_get_boolean_list g_key_file_get_boolean_list
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment