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: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2679>
This commit is contained in:
Florian Müllner 2023-01-25 21:45:49 +01:00 committed by Marge Bot
parent b6a7cac28f
commit 74c5bb270d
5 changed files with 17 additions and 42 deletions

View File

@ -43,34 +43,29 @@
struct _StIconCache { struct _StIconCache {
int ref_count;
GMappedFile *map; GMappedFile *map;
char *buffer; char *buffer;
guint32 last_chain_offset; guint32 last_chain_offset;
}; };
static void
clear_icon_cache (gpointer data)
{
StIconCache *cache = data;
g_clear_pointer (&cache->map, g_mapped_file_unref);
}
StIconCache * StIconCache *
st_icon_cache_ref (StIconCache *cache) st_icon_cache_ref (StIconCache *cache)
{ {
cache->ref_count++; return g_atomic_rc_box_acquire (cache);
return cache;
} }
void void
st_icon_cache_unref (StIconCache *cache) st_icon_cache_unref (StIconCache *cache)
{ {
cache->ref_count --; g_atomic_rc_box_release_full (cache, clear_icon_cache);
if (cache->ref_count == 0)
{
g_debug ("unmapping icon cache");
if (cache->map)
g_mapped_file_unref (cache->map);
g_free (cache);
}
} }
StIconCache * StIconCache *
@ -115,8 +110,7 @@ st_icon_cache_new_for_path (const char *path)
g_debug ("found icon cache for %s", path); g_debug ("found icon cache for %s", path);
cache = g_new0 (StIconCache, 1); cache = g_atomic_rc_box_new0 (StIconCache);
cache->ref_count = 1;
cache->map = map; cache->map = map;
cache->buffer = g_mapped_file_get_contents (map); cache->buffer = g_mapped_file_get_contents (map);
@ -133,9 +127,7 @@ st_icon_cache_new (const char *data)
{ {
StIconCache *cache; StIconCache *cache;
cache = g_new0 (StIconCache, 1); cache = g_atomic_rc_box_new0 (StIconCache);
cache->ref_count = 1;
cache->map = NULL;
cache->buffer = (char *)data; cache->buffer = (char *)data;
return cache; return cache;

View File

@ -31,12 +31,7 @@
StIconColors * StIconColors *
st_icon_colors_new (void) st_icon_colors_new (void)
{ {
StIconColors *colors; return g_atomic_rc_box_new0 (StIconColors);
colors = g_new0 (StIconColors, 1);
colors->ref_count = 1;
return colors;
} }
/** /**
@ -51,10 +46,8 @@ StIconColors *
st_icon_colors_ref (StIconColors *colors) st_icon_colors_ref (StIconColors *colors)
{ {
g_return_val_if_fail (colors != NULL, NULL); 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 g_atomic_rc_box_acquire (colors);
return colors;
} }
/** /**
@ -69,10 +62,8 @@ void
st_icon_colors_unref (StIconColors *colors) st_icon_colors_unref (StIconColors *colors)
{ {
g_return_if_fail (colors != NULL); 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_atomic_rc_box_release (colors);
g_free (colors);
} }
/** /**

View File

@ -21,8 +21,6 @@ typedef struct _StIconColors StIconColors;
* icon. * icon.
*/ */
struct _StIconColors { struct _StIconColors {
volatile guint ref_count;
ClutterColor foreground; ClutterColor foreground;
ClutterColor warning; ClutterColor warning;
ClutterColor error; ClutterColor error;

View File

@ -59,7 +59,7 @@ st_shadow_new (ClutterColor *color,
{ {
StShadow *shadow; StShadow *shadow;
shadow = g_new (StShadow, 1); shadow = g_atomic_rc_box_new (StShadow);
shadow->color = *color; shadow->color = *color;
shadow->xoffset = xoffset; shadow->xoffset = xoffset;
@ -67,7 +67,6 @@ st_shadow_new (ClutterColor *color,
shadow->blur = blur; shadow->blur = blur;
shadow->spread = spread; shadow->spread = spread;
shadow->inset = inset; shadow->inset = inset;
shadow->ref_count = 1;
return shadow; return shadow;
} }
@ -84,10 +83,8 @@ StShadow *
st_shadow_ref (StShadow *shadow) st_shadow_ref (StShadow *shadow)
{ {
g_return_val_if_fail (shadow != NULL, NULL); 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 g_atomic_rc_box_acquire (shadow);
return shadow;
} }
/** /**
@ -102,10 +99,8 @@ void
st_shadow_unref (StShadow *shadow) st_shadow_unref (StShadow *shadow)
{ {
g_return_if_fail (shadow != NULL); 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_atomic_rc_box_release (shadow);
g_free (shadow);
} }
/** /**

View File

@ -51,7 +51,6 @@ struct _StShadow {
gdouble blur; gdouble blur;
gdouble spread; gdouble spread;
gboolean inset; gboolean inset;
volatile int ref_count;
}; };
GType st_shadow_get_type (void) G_GNUC_CONST; GType st_shadow_get_type (void) G_GNUC_CONST;