Skip to content
Snippets Groups Projects
Commit fe82ce9f authored by George Kiagiadakis's avatar George Kiagiadakis
Browse files

properties: add wp_properties_ensure_unique_owner() method

parent 1f1c690b
No related branches found
No related tags found
No related merge requests found
...@@ -49,6 +49,7 @@ enum { ...@@ -49,6 +49,7 @@ enum {
struct _WpProperties struct _WpProperties
{ {
grefcount ref;
guint32 flags; guint32 flags;
union { union {
struct pw_properties *props; struct pw_properties *props;
...@@ -68,7 +69,8 @@ G_DEFINE_BOXED_TYPE(WpProperties, wp_properties, wp_properties_ref, wp_propertie ...@@ -68,7 +69,8 @@ G_DEFINE_BOXED_TYPE(WpProperties, wp_properties, wp_properties_ref, wp_propertie
WpProperties * WpProperties *
wp_properties_new_empty (void) wp_properties_new_empty (void)
{ {
WpProperties * self = g_rc_box_new (WpProperties); WpProperties * self = g_slice_new0 (WpProperties);
g_ref_count_init (&self->ref);
self->flags = 0; self->flags = 0;
self->props = pw_properties_new (NULL, NULL); self->props = pw_properties_new (NULL, NULL);
return self; return self;
...@@ -139,7 +141,8 @@ wp_properties_new_string (const gchar * str) ...@@ -139,7 +141,8 @@ wp_properties_new_string (const gchar * str)
g_return_val_if_fail (str != NULL, NULL); g_return_val_if_fail (str != NULL, NULL);
self = g_rc_box_new (WpProperties); self = g_slice_new0 (WpProperties);
g_ref_count_init (&self->ref);
self->flags = 0; self->flags = 0;
self->props = pw_properties_new_string (str); self->props = pw_properties_new_string (str);
return self; return self;
...@@ -167,7 +170,8 @@ wp_properties_new_wrap (struct pw_properties * props) ...@@ -167,7 +170,8 @@ wp_properties_new_wrap (struct pw_properties * props)
g_return_val_if_fail (props != NULL, NULL); g_return_val_if_fail (props != NULL, NULL);
self = g_rc_box_new (WpProperties); self = g_slice_new0 (WpProperties);
g_ref_count_init (&self->ref);
self->flags = FLAG_NO_OWNERSHIP; self->flags = FLAG_NO_OWNERSHIP;
self->props = props; self->props = props;
return self; return self;
...@@ -193,7 +197,8 @@ wp_properties_new_take (struct pw_properties * props) ...@@ -193,7 +197,8 @@ wp_properties_new_take (struct pw_properties * props)
g_return_val_if_fail (props != NULL, NULL); g_return_val_if_fail (props != NULL, NULL);
self = g_rc_box_new (WpProperties); self = g_slice_new0 (WpProperties);
g_ref_count_init (&self->ref);
self->flags = 0; self->flags = 0;
self->props = props; self->props = props;
return self; return self;
...@@ -215,7 +220,8 @@ wp_properties_new_copy (const struct pw_properties * props) ...@@ -215,7 +220,8 @@ wp_properties_new_copy (const struct pw_properties * props)
g_return_val_if_fail (props != NULL, NULL); g_return_val_if_fail (props != NULL, NULL);
self = g_rc_box_new (WpProperties); self = g_slice_new0 (WpProperties);
g_ref_count_init (&self->ref);
self->flags = 0; self->flags = 0;
self->props = pw_properties_copy (props); self->props = pw_properties_copy (props);
return self; return self;
...@@ -245,7 +251,8 @@ wp_properties_new_wrap_dict (const struct spa_dict * dict) ...@@ -245,7 +251,8 @@ wp_properties_new_wrap_dict (const struct spa_dict * dict)
g_return_val_if_fail (dict != NULL, NULL); g_return_val_if_fail (dict != NULL, NULL);
self = g_rc_box_new (WpProperties); self = g_slice_new0 (WpProperties);
g_ref_count_init (&self->ref);
self->flags = FLAG_NO_OWNERSHIP | FLAG_IS_DICT; self->flags = FLAG_NO_OWNERSHIP | FLAG_IS_DICT;
self->dict = dict; self->dict = dict;
return self; return self;
...@@ -267,7 +274,8 @@ wp_properties_new_copy_dict (const struct spa_dict * dict) ...@@ -267,7 +274,8 @@ wp_properties_new_copy_dict (const struct spa_dict * dict)
g_return_val_if_fail (dict != NULL, NULL); g_return_val_if_fail (dict != NULL, NULL);
self = g_rc_box_new (WpProperties); self = g_slice_new0 (WpProperties);
g_ref_count_init (&self->ref);
self->flags = 0; self->flags = 0;
self->props = pw_properties_new_dict (dict); self->props = pw_properties_new_dict (dict);
return self; return self;
...@@ -293,6 +301,7 @@ wp_properties_free (WpProperties * self) ...@@ -293,6 +301,7 @@ wp_properties_free (WpProperties * self)
{ {
if (!(self->flags & FLAG_NO_OWNERSHIP)) if (!(self->flags & FLAG_NO_OWNERSHIP))
pw_properties_free (self->props); pw_properties_free (self->props);
g_slice_free (WpProperties, self);
} }
/** /**
...@@ -304,7 +313,8 @@ wp_properties_free (WpProperties * self) ...@@ -304,7 +313,8 @@ wp_properties_free (WpProperties * self)
WpProperties * WpProperties *
wp_properties_ref (WpProperties * self) wp_properties_ref (WpProperties * self)
{ {
return g_rc_box_acquire (self); g_ref_count_inc (&self->ref);
return self;
} }
/** /**
...@@ -317,7 +327,35 @@ wp_properties_ref (WpProperties * self) ...@@ -317,7 +327,35 @@ wp_properties_ref (WpProperties * self)
void void
wp_properties_unref (WpProperties * self) wp_properties_unref (WpProperties * self)
{ {
g_rc_box_release_full (self, (GDestroyNotify) wp_properties_free); if (g_ref_count_dec (&self->ref))
wp_properties_free (self);
}
/**
* wp_properties_ensure_unique_owner:
* @self: (transfer full): a properties object
*
* Ensures that the given properties set is uniquely owned, which means:
* - its reference count is 1
* - it is not wrapping a native `spa_dict` or `pw_properties` object
*
* If @self is not uniquely owned already, then it is unrefed and a copy of
* it is returned instead. You should always consider @self as unsafe to use
* after this call and you should use the returned object instead.
*
* Returns: (transfer full): the uniquely owned properties object
*/
WpProperties *
wp_properties_ensure_unique_owner (WpProperties * self)
{
if (!g_ref_count_compare (&self->ref, 1) ||
self->flags & (FLAG_IS_DICT | FLAG_NO_OWNERSHIP))
{
WpProperties *copy = wp_properties_copy (self);
wp_properties_unref (self);
return copy;
}
return self;
} }
/** /**
......
...@@ -66,6 +66,9 @@ WpProperties * wp_properties_ref (WpProperties * self); ...@@ -66,6 +66,9 @@ WpProperties * wp_properties_ref (WpProperties * self);
WP_API WP_API
void wp_properties_unref (WpProperties * self); void wp_properties_unref (WpProperties * self);
WP_API
WpProperties * wp_properties_ensure_unique_owner (WpProperties * self);
/* update */ /* update */
WP_API WP_API
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment