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
This commit is contained in:
Christian Persch 2009-05-25 15:07:27 +02:00 committed by Matthias Clasen
parent dfd75d2ead
commit a57c9148cf
4 changed files with 60 additions and 5 deletions

View File

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

View File

@ -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)
g_key_file_remove_group_node (key_file, group_node); g_key_file_remove_group_node (key_file, group_node);
} }
if (key_file->group_hash != NULL)
{
g_hash_table_destroy (key_file->group_hash); g_hash_table_destroy (key_file->group_hash);
key_file->group_hash = NULL; 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)
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,
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
**/ **/
@ -1067,9 +1095,30 @@ 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_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_key_file_clear (key_file);
g_slice_free (GKeyFile, 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
* true for locales that match those in g_get_language_names(). * true for locales that match those in g_get_language_names().

View File

@ -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);

View File

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