Make TMTag reference-counted

git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@5559 ea778897-0a13-0410-b9d1-a72fbfd435f5
This commit is contained in:
Colomban Wendling 2011-03-05 22:48:25 +00:00
parent 88aa81b6a8
commit b99d06d47c
5 changed files with 42 additions and 9 deletions

View File

@ -147,6 +147,7 @@ typedef struct _TMTag
gboolean inactive; /*!< Whether this file is to be parsed */ gboolean inactive; /*!< Whether this file is to be parsed */
} file; } file;
} atts; } atts;
gint refcount; /*!< the reference count of the tag */
} TMTag; } TMTag;
/*! /*!
@ -306,13 +307,27 @@ void tm_tags_array_free(GPtrArray *tags_array, gboolean free_all);
\sa tm_tag_free() \sa tm_tag_free()
*/ */
void tm_tag_destroy(TMTag *tag); void tm_tag_destroy(TMTag *tag);
#endif
/*! /*!
Destroys all data in the tag and frees the tag structure as well. Destroys all data in the tag and frees the tag structure as well.
\param tag Pointer to a TMTag structure \param tag Pointer to a TMTag structure
*/ */
void tm_tag_free(gpointer tag); void tm_tag_free(gpointer tag);
#endif
/*!
Drops a reference from a TMTag. If the reference count reaches 0, this function
destroys all data in the tag and frees the tag structure as well.
\param tag Pointer to a TMTag structure
*/
void tm_tag_unref(TMTag *tag);
/*!
Adds a reference to a TMTag.
\param tag Pointer to a TMTag structure
\return the passed-in TMTag
*/
TMTag *tm_tag_ref(TMTag *tag);
/*! /*!
Returns the type of tag as a string Returns the type of tag as a string

View File

@ -396,7 +396,7 @@ gboolean tm_project_open(TMProject *project, gboolean force)
#endif #endif
if (!force) if (!force)
{ {
tm_tag_free(tag); tm_tag_unref(tag);
fclose(fp); fclose(fp);
return FALSE; return FALSE;
} }
@ -412,7 +412,7 @@ gboolean tm_project_open(TMProject *project, gboolean force)
project->file_list = g_ptr_array_new(); project->file_list = g_ptr_array_new();
g_ptr_array_add(project->file_list, source_file); g_ptr_array_add(project->file_list, source_file);
} }
tm_tag_free(tag); tm_tag_unref(tag);
} }
else else
{ {
@ -421,7 +421,7 @@ gboolean tm_project_open(TMProject *project, gboolean force)
#ifdef TM_DEBUG #ifdef TM_DEBUG
g_warning("Dangling tag %s", tag->name); g_warning("Dangling tag %s", tag->name);
#endif #endif
tm_tag_free(tag); tm_tag_unref(tag);
if (!force) if (!force)
{ {
fclose(fp); fclose(fp);

View File

@ -324,7 +324,7 @@ gboolean tm_source_file_write(TMWorkObject *source_file, FILE *fp, guint attrs)
if (NULL != (tag = tm_tag_new(TM_SOURCE_FILE(source_file), NULL))) if (NULL != (tag = tm_tag_new(TM_SOURCE_FILE(source_file), NULL)))
{ {
tm_tag_write(tag, fp, tm_tag_attr_max_t); tm_tag_write(tag, fp, tm_tag_attr_max_t);
tm_tag_free(tag); tm_tag_unref(tag);
if (NULL != source_file->tags_array) if (NULL != source_file->tags_array)
{ {
for (i=0; i < source_file->tags_array->len; ++i) for (i=0; i < source_file->tags_array->len; ++i)

View File

@ -129,6 +129,7 @@ static int get_tag_type(const char *tag_name)
gboolean tm_tag_init(TMTag *tag, TMSourceFile *file, const tagEntryInfo *tag_entry) gboolean tm_tag_init(TMTag *tag, TMSourceFile *file, const tagEntryInfo *tag_entry)
{ {
tag->refcount = 1;
if (NULL == tag_entry) if (NULL == tag_entry)
{ {
/* This is a file tag */ /* This is a file tag */
@ -224,6 +225,7 @@ gboolean tm_tag_init_from_file(TMTag *tag, TMSourceFile *file, FILE *fp)
gboolean status; gboolean status;
guchar changed_char = TA_NAME; guchar changed_char = TA_NAME;
tag->refcount = 1;
if ((NULL == fgets((gchar*)buf, BUFSIZ, fp)) || ('\0' == *buf)) if ((NULL == fgets((gchar*)buf, BUFSIZ, fp)) || ('\0' == *buf))
return FALSE; return FALSE;
for (start = end = buf, status = TRUE; (TRUE == status); start = end, ++ end) for (start = end = buf, status = TRUE; (TRUE == status); start = end, ++ end)
@ -327,6 +329,7 @@ gboolean tm_tag_init_from_file_alt(TMTag *tag, TMSourceFile *file, FILE *fp)
gboolean status; gboolean status;
/*guchar changed_char = TA_NAME;*/ /*guchar changed_char = TA_NAME;*/
tag->refcount = 1;
if ((NULL == fgets((gchar*)buf, BUFSIZ, fp)) || ('\0' == *buf)) if ((NULL == fgets((gchar*)buf, BUFSIZ, fp)) || ('\0' == *buf))
return FALSE; return FALSE;
{ {
@ -436,15 +439,30 @@ static void tm_tag_destroy(TMTag *tag)
} }
} }
#if 0
void tm_tag_free(gpointer tag) void tm_tag_free(gpointer tag)
{ {
if (NULL != tag) tm_tag_unref(tag);
}
#endif
void tm_tag_unref(TMTag *tag)
{
/* be NULL-proof because tm_tag_free() was NULL-proof and we indent to be a
* drop-in replacment of it */
if (NULL != tag && g_atomic_int_dec_and_test(&tag->refcount))
{ {
tm_tag_destroy((TMTag *) tag); tm_tag_destroy(tag);
TAG_FREE(tag); TAG_FREE(tag);
} }
} }
TMTag *tm_tag_ref(TMTag *tag)
{
g_atomic_int_inc(&tag->refcount);
return tag;
}
int tm_tag_compare(const void *ptr1, const void *ptr2) int tm_tag_compare(const void *ptr1, const void *ptr2)
{ {
unsigned int *sort_attr; unsigned int *sort_attr;
@ -603,7 +621,7 @@ void tm_tags_array_free(GPtrArray *tags_array, gboolean free_all)
{ {
guint i; guint i;
for (i = 0; i < tags_array->len; ++i) for (i = 0; i < tags_array->len; ++i)
tm_tag_free(tags_array->pdata[i]); tm_tag_unref(tags_array->pdata[i]);
if (free_all) if (free_all)
g_ptr_array_free(tags_array, TRUE); g_ptr_array_free(tags_array, TRUE);
else else

View File

@ -82,7 +82,7 @@ void tm_workspace_free(gpointer workspace)
if (theWorkspace->global_tags) if (theWorkspace->global_tags)
{ {
for (i=0; i < theWorkspace->global_tags->len; ++i) for (i=0; i < theWorkspace->global_tags->len; ++i)
tm_tag_free(theWorkspace->global_tags->pdata[i]); tm_tag_unref(theWorkspace->global_tags->pdata[i]);
g_ptr_array_free(theWorkspace->global_tags, TRUE); g_ptr_array_free(theWorkspace->global_tags, TRUE);
} }
tm_work_object_destroy(TM_WORK_OBJECT(theWorkspace)); tm_work_object_destroy(TM_WORK_OBJECT(theWorkspace));