gibaseinfo: Fix use of stack-allocated GIBaseInfos

Most of the code for handling stack-allocated infos was correct, it was
just missing code to initialise the `GTypeInstance` member.

Since `GTypeInstance` isn’t really designed for stack allocation, this
is a little hacky — it requires setting up the member within
`GTypeInstance` manually. It works, though.

The externally visible consequence of this, though, is that
stack-allocated `GIBaseInfo`s now need to be cleared when they’re
finished being used. This allows the `GTypeClass` ref to be dropped.

All users of the stack-allocated APIs in libgirepository will need to
adapt to this change.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Fixes: #3218
This commit is contained in:
Philip Withnall 2024-01-23 18:07:17 +00:00
parent eb94acce21
commit bf4cf2d464
5 changed files with 63 additions and 0 deletions

View File

@ -354,6 +354,9 @@ gi_arg_info_get_type_info (GIArgInfo *info)
*
* The initialized @type must not be referenced after @info is deallocated.
*
* Once you are done with @type, it must be cleared using
* [method@GIRepository.BaseInfo.clear].
*
* Since: 2.80
*/
void

View File

@ -141,6 +141,9 @@ gi_base_info_class_init (GIBaseInfoClass *klass)
static void
gi_base_info_init (GIBaseInfo *self)
{
/* Initialise a dynamically allocated #GIBaseInfos members.
*
* This function *must* be kept in sync with gi_info_init(). */
g_atomic_ref_count_init (&self->ref_count);
}
@ -405,6 +408,20 @@ gi_info_init (GIRealInfo *info,
{
memset (info, 0, sizeof (GIRealInfo));
/* Evil setup of a stack allocated #GTypeInstance. This is not something its
* really designed to do.
*
* This function *must* be kept in sync with gi_base_info_init(), which is
* the equivalent function for dynamically allocated types. */
info->parent_instance.g_class = g_type_class_ref (type);
/* g_type_create_instance() calls the #GInstanceInitFunc for each of the
* parent types, down to (and including) @type. We dont need to do that, as
* #GIBaseInfo is fundamental so doesnt have a parent type, the instance init
* function for #GIBaseInfo is gi_base_info_init() (which only sets the
* refcount, which we already do here), and subtypes of #GIBaseInfo dont have
* instance init functions (see gi_base_info_type_register_static()). */
/* Invalid refcount used to flag stack-allocated infos */
info->ref_count = INVALID_REFCOUNT;
info->typelib = typelib;
@ -417,6 +434,34 @@ gi_info_init (GIRealInfo *info,
info->repository = repository;
}
/**
* gi_base_info_clear:
* @info: (type GIRepository.BaseInfo): a #GIBaseInfo
*
* Clears memory allocated internally by a stack-allocated
* [type@GIRepository.BaseInfo].
*
* This does not deallocate the [type@GIRepository.BaseInfo] struct itself.
*
* This must only be called on stack-allocated [type@GIRepository.BaseInfo]s.
* Use [method@GIRepository.BaseInfo.unref] for heap-allocated ones.
*
* Since: 2.80
*/
void
gi_base_info_clear (void *info)
{
GIBaseInfo *rinfo = (GIBaseInfo *) info;
g_return_if_fail (GI_IS_BASE_INFO (rinfo));
g_assert (rinfo->ref_count == INVALID_REFCOUNT);
GI_BASE_INFO_GET_CLASS (info)->finalize (rinfo);
g_type_class_unref (rinfo->parent_instance.g_class);
}
GIBaseInfo *
gi_info_from_entry (GIRepository *repository,
GITypelib *typelib,
@ -559,6 +604,9 @@ gi_base_info_ref (void *info)
* Decreases the reference count of @info. When its reference count
* drops to 0, the info is freed.
*
* This must not be called on stack-allocated [type@GIRepository.BaseInfo]s
* use [method@GIRepository.BaseInfo.clear] for that.
*
* Since: 2.80
*/
void

View File

@ -94,6 +94,9 @@ GIBaseInfo * gi_base_info_ref (void *info);
GI_AVAILABLE_IN_ALL
void gi_base_info_unref (void *info);
GI_AVAILABLE_IN_ALL
void gi_base_info_clear (void *info);
GI_AVAILABLE_IN_ALL
GIInfoType gi_base_info_get_info_type (GIBaseInfo *info);

View File

@ -201,6 +201,9 @@ gi_callable_info_get_return_type (GICallableInfo *info)
*
* The initialized @type must not be referenced after @info is deallocated.
*
* Once you are done with @type, it must be cleared using
* [method@GIRepository.BaseInfo.clear].
*
* Since: 2.80
*/
void
@ -389,6 +392,9 @@ gi_callable_info_get_arg (GICallableInfo *info,
*
* The initialized @arg must not be referenced after @info is deallocated.
*
* Once you are done with @arg, it must be cleared using
* [method@GIRepository.BaseInfo.clear].
*
* Since: 2.80
*/
void

View File

@ -220,6 +220,9 @@ gi_callable_info_get_ffi_arg_types (GICallableInfo *callable_info,
default:
g_assert_not_reached ();
}
gi_base_info_clear (&arg_type);
gi_base_info_clear (&arg_info);
}
arg_types[n_invoke_args] = NULL;