object-interest: add _ref and _unref APIs

This commit is contained in:
Julian Bouzas 2021-01-19 10:52:50 -05:00
parent ac87519841
commit b7b3ce212b
4 changed files with 43 additions and 18 deletions

View file

@ -32,6 +32,7 @@ struct constraint
struct _WpObjectInterest
{
grefcount ref;
gboolean valid;
GType gtype;
struct pw_array constraints;
@ -48,7 +49,7 @@ struct _WpObjectInterest
* are satisfied.
*/
G_DEFINE_BOXED_TYPE (WpObjectInterest, wp_object_interest,
wp_object_interest_copy, wp_object_interest_free)
wp_object_interest_copy, wp_object_interest_unref)
/**
* wp_object_interest_new:
@ -163,6 +164,7 @@ wp_object_interest_new_type (GType gtype)
{
WpObjectInterest *self = g_slice_new0 (WpObjectInterest);
g_return_val_if_fail (self != NULL, NULL);
g_ref_count_init (&self->ref);
self->gtype = gtype;
pw_array_init (&self->constraints, sizeof (struct constraint));
return self;
@ -276,12 +278,19 @@ wp_object_interest_copy (WpObjectInterest * self)
}
/**
* wp_object_interest_free:
* @self: (transfer full): the object interest to free
* wp_object_interest_ref:
* @self: the object interest to ref
*
* Releases @self and all the memory associated with it
* Returns: (transfer full): @self with an additional reference count on it
*/
void
WpObjectInterest *
wp_object_interest_ref (WpObjectInterest *self)
{
g_ref_count_inc (&self->ref);
return self;
}
static void
wp_object_interest_free (WpObjectInterest * self)
{
struct constraint *c;
@ -296,6 +305,20 @@ wp_object_interest_free (WpObjectInterest * self)
g_slice_free (WpObjectInterest, self);
}
/**
* wp_object_interest_unref:
* @self: (transfer full): the object interest to unref
*
* Decreases the reference count on @self and frees it when the ref count
* reaches zero.
*/
void
wp_object_interest_unref (WpObjectInterest * self)
{
if (g_ref_count_dec (&self->ref))
wp_object_interest_free (self);
}
/**
* wp_object_interest_validate:
* @self: the object interest to validate

View file

@ -85,7 +85,10 @@ WP_API
WpObjectInterest * wp_object_interest_copy (WpObjectInterest * self);
WP_API
void wp_object_interest_free (WpObjectInterest * self);
WpObjectInterest * wp_object_interest_ref (WpObjectInterest *self);
WP_API
void wp_object_interest_unref (WpObjectInterest * self);
WP_API
gboolean wp_object_interest_validate (WpObjectInterest * self, GError ** error);
@ -98,7 +101,7 @@ gboolean wp_object_interest_matches_full (WpObjectInterest * self,
GType object_type, gpointer object, WpProperties * pw_props,
WpProperties * pw_global_props);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (WpObjectInterest, wp_object_interest_free)
G_DEFINE_AUTOPTR_CLEANUP_FUNC (WpObjectInterest, wp_object_interest_unref)
G_END_DECLS

View file

@ -86,8 +86,8 @@ static void
wp_object_manager_init (WpObjectManager * self)
{
g_weak_ref_init (&self->core, NULL);
self->interests =
g_ptr_array_new_with_free_func ((GDestroyNotify) wp_object_interest_free);
self->interests = g_ptr_array_new_with_free_func (
(GDestroyNotify) wp_object_interest_unref);
self->features = g_hash_table_new (g_direct_hash, g_direct_equal);
self->objects = g_ptr_array_new ();
self->installed = FALSE;
@ -272,7 +272,7 @@ wp_object_manager_add_interest_full (WpObjectManager *self,
if (G_UNLIKELY (!wp_object_interest_validate (interest, &error))) {
wp_critical_object (self, "interest validation failed: %s",
error->message);
wp_object_interest_free (interest);
wp_object_interest_unref (interest);
return;
}
g_ptr_array_add (self->interests, interest);
@ -393,8 +393,7 @@ static void
om_iterator_finalize (WpIterator *it)
{
struct om_iterator_data *it_data = wp_iterator_get_user_data (it);
if (it_data->interest)
wp_object_interest_free (it_data->interest);
g_clear_pointer (&it_data->interest, wp_object_interest_unref);
g_object_unref (it_data->om);
}
@ -485,7 +484,7 @@ wp_object_manager_iterate_filtered_full (WpObjectManager * self,
if (G_UNLIKELY (!wp_object_interest_validate (interest, &error))) {
wp_critical_object (self, "interest validation failed: %s",
error->message);
wp_object_interest_free (interest);
wp_object_interest_unref (interest);
return NULL;
}

View file

@ -230,7 +230,7 @@ test_object_interest_teardown (TestFixture * f, gconstpointer data)
\
g_assert_true (wp_object_interest_matches (interest, f->object)); \
\
g_clear_pointer (&interest, wp_object_interest_free); \
g_clear_pointer (&interest, wp_object_interest_unref); \
} G_STMT_END
#define TEST_EXPECT_NO_MATCH(interest) \
@ -246,7 +246,7 @@ test_object_interest_teardown (TestFixture * f, gconstpointer data)
\
g_assert_false (wp_object_interest_matches (interest, f->object)); \
\
g_clear_pointer (&interest, wp_object_interest_free); \
g_clear_pointer (&interest, wp_object_interest_unref); \
} G_STMT_END
#define TEST_EXPECT_MATCH_WP_PROPS(interest, props, global_props) \
@ -263,7 +263,7 @@ test_object_interest_teardown (TestFixture * f, gconstpointer data)
g_assert_true (wp_object_interest_matches_full (interest, \
WP_TYPE_NODE, NULL, props, global_props)); \
\
g_clear_pointer (&interest, wp_object_interest_free); \
g_clear_pointer (&interest, wp_object_interest_unref); \
} G_STMT_END
#define TEST_EXPECT_NO_MATCH_WP_PROPS(interest, props, global_props) \
@ -280,7 +280,7 @@ test_object_interest_teardown (TestFixture * f, gconstpointer data)
g_assert_false (wp_object_interest_matches_full (interest, \
WP_TYPE_NODE, NULL, props, global_props)); \
\
g_clear_pointer (&interest, wp_object_interest_free); \
g_clear_pointer (&interest, wp_object_interest_unref); \
} G_STMT_END
#define TEST_EXPECT_VALIDATION_ERROR(interest) \
@ -294,7 +294,7 @@ test_object_interest_teardown (TestFixture * f, gconstpointer data)
g_assert_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT); \
g_assert_false (ret); \
\
g_clear_pointer (&interest, wp_object_interest_free); \
g_clear_pointer (&interest, wp_object_interest_unref); \
} G_STMT_END
static void