summaryrefslogtreecommitdiffstats
path: root/source/widgets/widget.c
diff options
context:
space:
mode:
Diffstat (limited to 'source/widgets/widget.c')
-rw-r--r--source/widgets/widget.c25
1 files changed, 20 insertions, 5 deletions
diff --git a/source/widgets/widget.c b/source/widgets/widget.c
index 1562d3d0..22869959 100644
--- a/source/widgets/widget.c
+++ b/source/widgets/widget.c
@@ -27,6 +27,7 @@
#include <glib.h>
#include <math.h>
+#include <stdatomic.h>
#include "widgets/widget.h"
#include "widgets/widget-internal.h"
#include "theme.h"
@@ -36,6 +37,8 @@
void widget_init ( widget *wid, widget *parent, WidgetType type, const char *name )
{
+ wid->ref_count = 1;
+
wid->type = type;
wid->parent = parent;
wid->name = g_strdup ( name );
@@ -359,14 +362,26 @@ void widget_draw ( widget *widget, cairo_t *d )
}
}
}
+
+void widget_ref ( widget *wid )
+{
+ g_assert ( wid != NULL );
+ g_assert ( wid->ref_count > 0 );
+
+ atomic_fetch_add( &(wid->ref_count), 1);
+}
+
void widget_free ( widget *wid )
{
if ( wid ) {
- if ( wid->name ) {
- g_free ( wid->name );
- }
- if ( wid->free ) {
- wid->free ( wid );
+ if ( atomic_fetch_sub ( &(wid->ref_count), 1) == 1 ) {
+ if ( wid->name ) {
+ g_free ( wid->name );
+ }
+ if ( wid->free ) {
+ wid->free ( wid );
+ }
+ return;
}
}
}