From 9debaffe0e77e5bb7732a47205c73362f79d77bf Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 17 Jan 2024 12:30:35 +0000 Subject: [PATCH] gibaseinfo: Remove need for casting for gi_base_info_ref() and unref() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Just like is done with `g_object_{ref,unref}()`, make these functions take a `void*` rather than a `GIBaseInfo*`, since they’ll most likely be called with a type which is derived from `GIBaseInfo*` rather than a `GIBaseInfo*` itself. Add some runtime type checks to make up for lowering the compile time type safety. Signed-off-by: Philip Withnall Helps: #3216 --- girepository/gibaseinfo.c | 12 ++++++++---- girepository/gibaseinfo.h | 4 ++-- girepository/tests/repository.c | 8 ++++---- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/girepository/gibaseinfo.c b/girepository/gibaseinfo.c index e6454c100..19406ac83 100644 --- a/girepository/gibaseinfo.c +++ b/girepository/gibaseinfo.c @@ -517,7 +517,7 @@ gi_type_info_init (GIBaseInfo *info, /** * gi_base_info_ref: - * @info: a #GIBaseInfo + * @info: (type GIRepository.BaseInfo): a #GIBaseInfo * * Increases the reference count of @info. * @@ -525,10 +525,12 @@ gi_type_info_init (GIBaseInfo *info, * Since: 2.80 */ GIBaseInfo * -gi_base_info_ref (GIBaseInfo *info) +gi_base_info_ref (void *info) { GIRealInfo *rinfo = (GIRealInfo*)info; + g_return_val_if_fail (GI_IS_BASE_INFO (info), NULL); + g_assert (rinfo->ref_count != INVALID_REFCOUNT); g_atomic_ref_count_inc (&rinfo->ref_count); @@ -537,7 +539,7 @@ gi_base_info_ref (GIBaseInfo *info) /** * gi_base_info_unref: - * @info: (transfer full): a #GIBaseInfo + * @info: (type GIRepository.BaseInfo) (transfer full): a #GIBaseInfo * * Decreases the reference count of @info. When its reference count * drops to 0, the info is freed. @@ -545,10 +547,12 @@ gi_base_info_ref (GIBaseInfo *info) * Since: 2.80 */ void -gi_base_info_unref (GIBaseInfo *info) +gi_base_info_unref (void *info) { GIRealInfo *rinfo = (GIRealInfo*)info; + g_return_if_fail (GI_IS_BASE_INFO (info)); + g_assert (rinfo->ref_count > 0 && rinfo->ref_count != INVALID_REFCOUNT); if (g_atomic_ref_count_dec (&rinfo->ref_count)) diff --git a/girepository/gibaseinfo.h b/girepository/gibaseinfo.h index c28fcb7ba..eb4fff9b1 100644 --- a/girepository/gibaseinfo.h +++ b/girepository/gibaseinfo.h @@ -65,10 +65,10 @@ GI_AVAILABLE_IN_ALL GType gi_base_info_get_type (void) G_GNUC_CONST; GI_AVAILABLE_IN_ALL -GIBaseInfo * gi_base_info_ref (GIBaseInfo *info); +GIBaseInfo * gi_base_info_ref (void *info); GI_AVAILABLE_IN_ALL -void gi_base_info_unref (GIBaseInfo *info); +void gi_base_info_unref (void *info); GI_AVAILABLE_IN_ALL GIInfoType gi_base_info_get_info_type (GIBaseInfo *info); diff --git a/girepository/tests/repository.c b/girepository/tests/repository.c index c74d66605..eb8c2208d 100644 --- a/girepository/tests/repository.c +++ b/girepository/tests/repository.c @@ -116,16 +116,16 @@ test_repository_info (void) g_assert_nonnull (method_info); g_assert_true (gi_callable_info_is_method ((GICallableInfo *) method_info)); g_assert_cmpuint (gi_callable_info_get_n_args ((GICallableInfo *) method_info), ==, 2); - g_clear_pointer ((GIBaseInfo **) &method_info, gi_base_info_unref); + g_clear_pointer (&method_info, gi_base_info_unref); method_info = gi_object_info_get_method (object_info, gi_object_info_get_n_methods (object_info) - 1); g_assert_true (gi_callable_info_is_method ((GICallableInfo *) method_info)); g_assert_cmpuint (gi_callable_info_get_n_args ((GICallableInfo *) method_info), >, 0); - g_clear_pointer ((GIBaseInfo **) &method_info, gi_base_info_unref); + g_clear_pointer (&method_info, gi_base_info_unref); - gi_base_info_unref ((GIBaseInfo *) signal_info); - gi_base_info_unref ((GIBaseInfo *) object_info); + gi_base_info_unref (signal_info); + gi_base_info_unref (object_info); g_clear_object (&repository); }