mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-11-10 03:16:17 +01:00
Merge branch '3218-girepository-stack-allocations' into 'main'
gibaseinfo: Fix use of stack-allocated GIBaseInfos Closes #3218 See merge request GNOME/glib!3846
This commit is contained in:
commit
01e197868d
@ -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
|
||||
@ -365,7 +368,7 @@ gi_arg_info_load_type (GIArgInfo *info,
|
||||
g_return_if_fail (info != NULL);
|
||||
g_return_if_fail (GI_IS_ARG_INFO (info));
|
||||
|
||||
gi_type_info_init ((GIBaseInfo *) type, (GIBaseInfo*)info, rinfo->typelib, rinfo->offset + G_STRUCT_OFFSET (ArgBlob, arg_type));
|
||||
gi_type_info_init (type, (GIBaseInfo*)info, rinfo->typelib, rinfo->offset + G_STRUCT_OFFSET (ArgBlob, arg_type));
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -123,12 +123,12 @@ value_base_info_lcopy_value (const GValue *value,
|
||||
static void
|
||||
gi_base_info_finalize (GIBaseInfo *self)
|
||||
{
|
||||
if (self->container && self->container->ref_count != INVALID_REFCOUNT)
|
||||
if (self->ref_count != INVALID_REFCOUNT &&
|
||||
self->container && self->container->ref_count != INVALID_REFCOUNT)
|
||||
gi_base_info_unref (self->container);
|
||||
|
||||
g_clear_object (&self->repository);
|
||||
|
||||
g_type_free_instance ((GTypeInstance *) self);
|
||||
if (self->ref_count != INVALID_REFCOUNT)
|
||||
g_clear_object (&self->repository);
|
||||
}
|
||||
|
||||
static void
|
||||
@ -141,6 +141,9 @@ gi_base_info_class_init (GIBaseInfoClass *klass)
|
||||
static void
|
||||
gi_base_info_init (GIBaseInfo *self)
|
||||
{
|
||||
/* Initialise a dynamically allocated #GIBaseInfo’s members.
|
||||
*
|
||||
* This function *must* be kept in sync with gi_info_init(). */
|
||||
g_atomic_ref_count_init (&self->ref_count);
|
||||
}
|
||||
|
||||
@ -397,7 +400,7 @@ gi_info_new (GIInfoType type,
|
||||
*/
|
||||
void
|
||||
gi_info_init (GIRealInfo *info,
|
||||
GIInfoType type,
|
||||
GType type,
|
||||
GIRepository *repository,
|
||||
GIBaseInfo *container,
|
||||
GITypelib *typelib,
|
||||
@ -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 it’s
|
||||
* 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 don’t need to do that, as
|
||||
* #GIBaseInfo is fundamental so doesn’t 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 don’t 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,
|
||||
@ -465,8 +510,23 @@ gi_type_info_new (GIBaseInfo *container,
|
||||
(type->flags.reserved == 0 && type->flags.reserved2 == 0) ? offset : type->offset);
|
||||
}
|
||||
|
||||
/*< private >
|
||||
* gi_type_info_init:
|
||||
* @info: (out caller-allocates): caller-allocated #GITypeInfo to populate
|
||||
* @container: (nullable): info which contains this one
|
||||
* @typelib: typelib containing the info
|
||||
* @offset: offset of the info within @typelib, in bytes
|
||||
*
|
||||
* Initialise a stack-allocated #GITypeInfo representing an object of type
|
||||
* [type@GIRepository.TypeInfo] from @offset of @typelib.
|
||||
*
|
||||
* This is a specialised form of [func@GIRepository.info_init] for type
|
||||
* information.
|
||||
*
|
||||
* Since: 2.80
|
||||
*/
|
||||
void
|
||||
gi_type_info_init (GIBaseInfo *info,
|
||||
gi_type_info_init (GITypeInfo *info,
|
||||
GIBaseInfo *container,
|
||||
GITypelib *typelib,
|
||||
uint32_t offset)
|
||||
@ -474,7 +534,7 @@ gi_type_info_init (GIBaseInfo *info,
|
||||
GIRealInfo *rinfo = (GIRealInfo*)container;
|
||||
SimpleTypeBlob *type = (SimpleTypeBlob *)&typelib->data[offset];
|
||||
|
||||
gi_info_init ((GIRealInfo*)info, GI_INFO_TYPE_TYPE, rinfo->repository, container, typelib,
|
||||
gi_info_init ((GIRealInfo*)info, GI_TYPE_TYPE_INFO, rinfo->repository, container, typelib,
|
||||
(type->flags.reserved == 0 && type->flags.reserved2 == 0) ? offset : type->offset);
|
||||
}
|
||||
|
||||
@ -544,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
|
||||
@ -556,7 +619,10 @@ gi_base_info_unref (void *info)
|
||||
g_assert (rinfo->ref_count > 0 && rinfo->ref_count != INVALID_REFCOUNT);
|
||||
|
||||
if (g_atomic_ref_count_dec (&rinfo->ref_count))
|
||||
GI_BASE_INFO_GET_CLASS (info)->finalize (info);
|
||||
{
|
||||
GI_BASE_INFO_GET_CLASS (info)->finalize (info);
|
||||
g_type_free_instance ((GTypeInstance *) info);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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
|
||||
@ -215,7 +218,7 @@ gi_callable_info_load_return_type (GICallableInfo *info,
|
||||
|
||||
offset = signature_offset (info);
|
||||
|
||||
gi_type_info_init ((GIBaseInfo *) type, (GIBaseInfo*)info, rinfo->typelib, offset);
|
||||
gi_type_info_init (type, (GIBaseInfo*)info, rinfo->typelib, offset);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -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
|
||||
@ -407,7 +413,7 @@ gi_callable_info_load_arg (GICallableInfo *info,
|
||||
offset = signature_offset (info);
|
||||
header = (Header *)rinfo->typelib->data;
|
||||
|
||||
gi_info_init ((GIRealInfo*)arg, GI_INFO_TYPE_ARG, rinfo->repository, (GIBaseInfo*)info, rinfo->typelib,
|
||||
gi_info_init ((GIRealInfo*)arg, GI_TYPE_ARG_INFO, rinfo->repository, (GIBaseInfo*)info, rinfo->typelib,
|
||||
offset + header->signature_blob_size + n * header->arg_blob_size);
|
||||
}
|
||||
|
||||
|
@ -48,6 +48,7 @@ struct _GIBaseInfo
|
||||
GTypeInstance parent_instance;
|
||||
gatomicrefcount ref_count;
|
||||
|
||||
/* these are both reffed if the GIBaseInfo is heap-allocated, but not reffed if it’s stack-allocated */
|
||||
GIRepository *repository;
|
||||
GIBaseInfo *container;
|
||||
|
||||
@ -222,7 +223,7 @@ void gi_unresolved_info_class_init (gpointer g_class,
|
||||
gpointer class_data);
|
||||
|
||||
void gi_info_init (GIRealInfo *info,
|
||||
GIInfoType type,
|
||||
GType type,
|
||||
GIRepository *repository,
|
||||
GIBaseInfo *container,
|
||||
GITypelib *typelib,
|
||||
@ -242,7 +243,7 @@ GITypeInfo * gi_type_info_new (GIBaseInfo *container,
|
||||
GITypelib *typelib,
|
||||
uint32_t offset);
|
||||
|
||||
void gi_type_info_init (GIBaseInfo *info,
|
||||
void gi_type_info_init (GITypeInfo *info,
|
||||
GIBaseInfo *container,
|
||||
GITypelib *typelib,
|
||||
uint32_t offset);
|
||||
|
@ -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;
|
||||
@ -266,6 +269,9 @@ gi_callable_info_get_ffi_return_type (GICallableInfo *callable_info)
|
||||
* by a language binding could contain a [type@GIRepository.FunctionInvoker]
|
||||
* structure inside the binding’s function mapping.
|
||||
*
|
||||
* @invoker must be freed using [method@GIRepository.FunctionInvoker.clear]
|
||||
* when it’s finished with.
|
||||
*
|
||||
* Returns: `TRUE` on success, `FALSE` otherwise with @error set.
|
||||
* Since: 2.80
|
||||
*/
|
||||
@ -336,7 +342,7 @@ gi_function_invoker_new_for_address (void *addr,
|
||||
}
|
||||
|
||||
/**
|
||||
* gi_function_invoker_destroy:
|
||||
* gi_function_invoker_clear:
|
||||
* @invoker: (transfer none): A #GIFunctionInvoker
|
||||
*
|
||||
* Release all resources allocated for the internals of @invoker.
|
||||
@ -347,7 +353,7 @@ gi_function_invoker_new_for_address (void *addr,
|
||||
* Since: 2.80
|
||||
*/
|
||||
void
|
||||
gi_function_invoker_destroy (GIFunctionInvoker *invoker)
|
||||
gi_function_invoker_clear (GIFunctionInvoker *invoker)
|
||||
{
|
||||
g_free (invoker->cif.arg_types);
|
||||
}
|
||||
|
@ -108,7 +108,7 @@ gboolean gi_function_invoker_new_for_address (void *addr,
|
||||
GError **error);
|
||||
|
||||
GI_AVAILABLE_IN_ALL
|
||||
void gi_function_invoker_destroy (GIFunctionInvoker *invoker);
|
||||
void gi_function_invoker_clear (GIFunctionInvoker *invoker);
|
||||
|
||||
|
||||
GI_AVAILABLE_IN_ALL
|
||||
|
88
girepository/tests/function-info.c
Normal file
88
girepository/tests/function-info.c
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 2024 Philip Chimento
|
||||
* Copyright 2024 GNOME Foundation, Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General
|
||||
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Philip Withnall <pwithnall@gnome.org>
|
||||
*/
|
||||
|
||||
#include "glib.h"
|
||||
#include "girepository.h"
|
||||
#include "girffi.h"
|
||||
|
||||
static GIRepository *
|
||||
load_typelib_from_builddir (const char *name,
|
||||
const char *version)
|
||||
{
|
||||
GIRepository *repository;
|
||||
char *gobject_typelib_dir = NULL;
|
||||
GITypelib *typelib = NULL;
|
||||
GError *local_error = NULL;
|
||||
|
||||
gobject_typelib_dir = g_test_build_filename (G_TEST_BUILT, "..", "..", "introspection", NULL);
|
||||
g_test_message ("Using GI_TYPELIB_DIR = %s", gobject_typelib_dir);
|
||||
gi_repository_prepend_search_path (gobject_typelib_dir);
|
||||
g_free (gobject_typelib_dir);
|
||||
|
||||
repository = gi_repository_new ();
|
||||
g_assert_nonnull (repository);
|
||||
|
||||
typelib = gi_repository_require (repository, name, version, 0, &local_error);
|
||||
g_assert_no_error (local_error);
|
||||
g_assert_nonnull (typelib);
|
||||
|
||||
return g_steal_pointer (&repository);
|
||||
}
|
||||
|
||||
static void
|
||||
test_function_info_invoker (void)
|
||||
{
|
||||
GIRepository *repository;
|
||||
GIFunctionInfo *function_info = NULL;
|
||||
GIFunctionInvoker invoker;
|
||||
GError *local_error = NULL;
|
||||
|
||||
g_test_summary ("Test preparing a function invoker");
|
||||
|
||||
repository = load_typelib_from_builddir ("GLib", "2.0");
|
||||
|
||||
function_info = (GIFunctionInfo *) gi_repository_find_by_name (repository, "GLib", "get_locale_variants");
|
||||
g_assert_nonnull (function_info);
|
||||
|
||||
gi_function_info_prep_invoker (function_info, &invoker, &local_error);
|
||||
g_assert_no_error (local_error);
|
||||
|
||||
gi_function_invoker_clear (&invoker);
|
||||
g_clear_pointer (&function_info, gi_base_info_unref);
|
||||
|
||||
g_clear_object (&repository);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
/* Isolate from the system typelibs and GIRs. */
|
||||
g_setenv ("GI_TYPELIB_PATH", "/dev/null", TRUE);
|
||||
g_setenv ("GI_GIR_PATH", "/dev/null", TRUE);
|
||||
|
||||
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
|
||||
|
||||
g_test_add_func ("/function-info/invoker", test_function_info_invoker);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
@ -6,6 +6,9 @@ if enable_gir
|
||||
'cmph-bdz': {
|
||||
'dependencies': [cmph_dep],
|
||||
},
|
||||
'function-info' : {
|
||||
'depends': [glib_gir],
|
||||
},
|
||||
'gthash' : {
|
||||
'dependencies': [girepo_gthash_dep],
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user