From 74c5bb270d76ac0194df01ae07faa080f4f1c4e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BCllner?= Date: Wed, 25 Jan 2023 21:45:49 +0100 Subject: [PATCH] st: Stop implementing custom refcounting Glib has generic RcBox/ArcBox types nowadays that can be used to add ref-counting to a plain struct types, use those instead. Part-of: --- src/st/st-icon-cache.c | 30 +++++++++++------------------- src/st/st-icon-colors.c | 15 +++------------ src/st/st-icon-colors.h | 2 -- src/st/st-shadow.c | 11 +++-------- src/st/st-shadow.h | 1 - 5 files changed, 17 insertions(+), 42 deletions(-) diff --git a/src/st/st-icon-cache.c b/src/st/st-icon-cache.c index 55638a5cf..99c3692f5 100644 --- a/src/st/st-icon-cache.c +++ b/src/st/st-icon-cache.c @@ -43,34 +43,29 @@ struct _StIconCache { - int ref_count; - GMappedFile *map; char *buffer; guint32 last_chain_offset; }; +static void +clear_icon_cache (gpointer data) +{ + StIconCache *cache = data; + g_clear_pointer (&cache->map, g_mapped_file_unref); +} + StIconCache * st_icon_cache_ref (StIconCache *cache) { - cache->ref_count++; - return cache; + return g_atomic_rc_box_acquire (cache); } void st_icon_cache_unref (StIconCache *cache) { - cache->ref_count --; - - if (cache->ref_count == 0) - { - g_debug ("unmapping icon cache"); - - if (cache->map) - g_mapped_file_unref (cache->map); - g_free (cache); - } + g_atomic_rc_box_release_full (cache, clear_icon_cache); } StIconCache * @@ -115,8 +110,7 @@ st_icon_cache_new_for_path (const char *path) g_debug ("found icon cache for %s", path); - cache = g_new0 (StIconCache, 1); - cache->ref_count = 1; + cache = g_atomic_rc_box_new0 (StIconCache); cache->map = map; cache->buffer = g_mapped_file_get_contents (map); @@ -133,9 +127,7 @@ st_icon_cache_new (const char *data) { StIconCache *cache; - cache = g_new0 (StIconCache, 1); - cache->ref_count = 1; - cache->map = NULL; + cache = g_atomic_rc_box_new0 (StIconCache); cache->buffer = (char *)data; return cache; diff --git a/src/st/st-icon-colors.c b/src/st/st-icon-colors.c index c6a082add..89257e0b7 100644 --- a/src/st/st-icon-colors.c +++ b/src/st/st-icon-colors.c @@ -31,12 +31,7 @@ StIconColors * st_icon_colors_new (void) { - StIconColors *colors; - - colors = g_new0 (StIconColors, 1); - colors->ref_count = 1; - - return colors; + return g_atomic_rc_box_new0 (StIconColors); } /** @@ -51,10 +46,8 @@ StIconColors * st_icon_colors_ref (StIconColors *colors) { g_return_val_if_fail (colors != NULL, NULL); - g_return_val_if_fail (colors->ref_count > 0, colors); - g_atomic_int_inc ((volatile int *)&colors->ref_count); - return colors; + return g_atomic_rc_box_acquire (colors); } /** @@ -69,10 +62,8 @@ void st_icon_colors_unref (StIconColors *colors) { g_return_if_fail (colors != NULL); - g_return_if_fail (colors->ref_count > 0); - if (g_atomic_int_dec_and_test ((volatile int *)&colors->ref_count)) - g_free (colors); + g_atomic_rc_box_release (colors); } /** diff --git a/src/st/st-icon-colors.h b/src/st/st-icon-colors.h index e994a7522..764825ddd 100644 --- a/src/st/st-icon-colors.h +++ b/src/st/st-icon-colors.h @@ -21,8 +21,6 @@ typedef struct _StIconColors StIconColors; * icon. */ struct _StIconColors { - volatile guint ref_count; - ClutterColor foreground; ClutterColor warning; ClutterColor error; diff --git a/src/st/st-shadow.c b/src/st/st-shadow.c index 0a8e319ad..97edbe90c 100644 --- a/src/st/st-shadow.c +++ b/src/st/st-shadow.c @@ -59,7 +59,7 @@ st_shadow_new (ClutterColor *color, { StShadow *shadow; - shadow = g_new (StShadow, 1); + shadow = g_atomic_rc_box_new (StShadow); shadow->color = *color; shadow->xoffset = xoffset; @@ -67,7 +67,6 @@ st_shadow_new (ClutterColor *color, shadow->blur = blur; shadow->spread = spread; shadow->inset = inset; - shadow->ref_count = 1; return shadow; } @@ -84,10 +83,8 @@ StShadow * st_shadow_ref (StShadow *shadow) { g_return_val_if_fail (shadow != NULL, NULL); - g_return_val_if_fail (shadow->ref_count > 0, shadow); - g_atomic_int_inc (&shadow->ref_count); - return shadow; + return g_atomic_rc_box_acquire (shadow); } /** @@ -102,10 +99,8 @@ void st_shadow_unref (StShadow *shadow) { g_return_if_fail (shadow != NULL); - g_return_if_fail (shadow->ref_count > 0); - if (g_atomic_int_dec_and_test (&shadow->ref_count)) - g_free (shadow); + g_atomic_rc_box_release (shadow); } /** diff --git a/src/st/st-shadow.h b/src/st/st-shadow.h index 267d48f7b..e4271c59c 100644 --- a/src/st/st-shadow.h +++ b/src/st/st-shadow.h @@ -51,7 +51,6 @@ struct _StShadow { gdouble blur; gdouble spread; gboolean inset; - volatile int ref_count; }; GType st_shadow_get_type (void) G_GNUC_CONST;