Merge branch '3155-girepository-more-api-cleanups' into 'main'

girepository: Change various alignments to use size_t

See merge request GNOME/glib!3833
This commit is contained in:
Philip Withnall 2024-01-18 14:50:42 +00:00
commit b13d8883b8
7 changed files with 162 additions and 102 deletions

View File

@ -262,50 +262,63 @@ gi_arg_info_get_scope (GIArgInfo *info)
/**
* gi_arg_info_get_closure_index:
* @info: a #GIArgInfo
* @out_closure_index: (out) (optional): return location for the closure index
*
* Obtain the index of the user data argument. This is only valid
* for arguments which are callbacks.
*
* Returns: Index of the user data argument or `-1` if there is none
* Returns: `TRUE` if the argument has a user data argument
* Since: 2.80
*/
gssize
gi_arg_info_get_closure_index (GIArgInfo *info)
gboolean
gi_arg_info_get_closure_index (GIArgInfo *info,
unsigned int *out_closure_index)
{
GIRealInfo *rinfo = (GIRealInfo *)info;
ArgBlob *blob;
gboolean has_closure_index;
g_return_val_if_fail (info != NULL, -1);
g_return_val_if_fail (GI_IS_ARG_INFO (info), -1);
g_return_val_if_fail (info != NULL, FALSE);
g_return_val_if_fail (GI_IS_ARG_INFO (info), FALSE);
blob = (ArgBlob *)&rinfo->typelib->data[rinfo->offset];
return blob->closure;
has_closure_index = (blob->closure >= 0);
if (out_closure_index != NULL)
*out_closure_index = has_closure_index ? blob->closure : 0;
return has_closure_index;
}
/**
* gi_arg_info_get_destroy_index:
* @info: a #GIArgInfo
* @out_destroy_index: (out) (optional): return location for the destroy index
*
* Obtains the index of the [type@GLib.DestroyNotify] argument. This is only
* valid for arguments which are callbacks.
*
* Returns: Index of the [type@GLib.DestroyNotify] argument or `-1` if there is
* none
* Returns: `TRUE` if the argument has a [type@GLib.DestroyNotify] argument
* Since: 2.80
*/
gssize
gi_arg_info_get_destroy_index (GIArgInfo *info)
gboolean
gi_arg_info_get_destroy_index (GIArgInfo *info,
unsigned int *out_destroy_index)
{
GIRealInfo *rinfo = (GIRealInfo *)info;
ArgBlob *blob;
gboolean has_destroy_index;
g_return_val_if_fail (info != NULL, -1);
g_return_val_if_fail (GI_IS_ARG_INFO (info), -1);
g_return_val_if_fail (info != NULL, FALSE);
g_return_val_if_fail (GI_IS_ARG_INFO (info), FALSE);
blob = (ArgBlob *)&rinfo->typelib->data[rinfo->offset];
return blob->destroy;
has_destroy_index = (blob->destroy >= 0);
if (out_destroy_index != NULL)
*out_destroy_index = has_destroy_index ? blob->destroy : 0;
return has_destroy_index;
}
/**

View File

@ -69,10 +69,12 @@ GI_AVAILABLE_IN_ALL
GIScopeType gi_arg_info_get_scope (GIArgInfo *info);
GI_AVAILABLE_IN_ALL
gssize gi_arg_info_get_closure_index (GIArgInfo *info);
gboolean gi_arg_info_get_closure_index (GIArgInfo *info,
unsigned int *out_closure_index);
GI_AVAILABLE_IN_ALL
gssize gi_arg_info_get_destroy_index (GIArgInfo *info);
gboolean gi_arg_info_get_destroy_index (GIArgInfo *info,
unsigned int *out_destroy_index);
GI_AVAILABLE_IN_ALL
GITypeInfo * gi_arg_info_get_type_info (GIArgInfo *info);

View File

@ -70,6 +70,27 @@ typedef enum
GI_IR_NODE_XREF = 19
} GIIrNodeTypeId;
/**
* GIIrOffsetsState:
* @GI_IR_OFFSETS_UNKNOWN: offsets have not been calculated yet
* @GI_IR_OFFSETS_COMPUTED: offsets have been successfully calculated
* @GI_IR_OFFSETS_FAILED: calculating the offsets failed
* @GI_IR_OFFSETS_IN_PROGRESS: offsets are currently being calculated (used to
* detect type recursion)
*
* State tracking for calculating size and alignment of
* [type@GIRepository.IrNode]s.
*
* Since: 2.80
*/
typedef enum
{
GI_IR_OFFSETS_UNKNOWN,
GI_IR_OFFSETS_COMPUTED,
GI_IR_OFFSETS_FAILED,
GI_IR_OFFSETS_IN_PROGRESS,
} GIIrOffsetsState;
struct _GIIrNode
{
GIIrNodeTypeId type;
@ -261,8 +282,9 @@ struct _GIIrNodeInterface
GList *interfaces;
GList *prerequisites;
gssize alignment;
size_t alignment;
size_t size;
GIIrOffsetsState offsets_state;
GList *members;
};
@ -311,8 +333,9 @@ struct _GIIrNodeBoxed
char *gtype_name;
char *gtype_init;
gssize alignment;
size_t alignment;
size_t size;
GIIrOffsetsState offsets_state;
GList *members;
};
@ -334,8 +357,9 @@ struct _GIIrNodeStruct
char *copy_func;
char *free_func;
gssize alignment;
size_t alignment;
size_t size;
GIIrOffsetsState offsets_state;
GList *members;
};
@ -355,8 +379,9 @@ struct _GIIrNodeUnion
char *copy_func;
char *free_func;
gssize alignment;
size_t alignment;
size_t size;
GIIrOffsetsState offsets_state;
int discriminator_offset;
GIIrNodeType *discriminator_type;

View File

@ -150,7 +150,7 @@ compute_enum_storage_type (GIIrNodeEnum *enum_node)
static gboolean
get_enum_size_alignment (GIIrNodeEnum *enum_node,
size_t *size,
gssize *alignment)
size_t *alignment)
{
ffi_type *type_ffi;
@ -189,7 +189,7 @@ static gboolean
get_interface_size_alignment (GIIrTypelibBuild *build,
GIIrNodeType *type,
size_t *size,
gssize *alignment,
size_t *alignment,
const char *who)
{
GIIrNode *iface;
@ -198,8 +198,8 @@ get_interface_size_alignment (GIIrTypelibBuild *build,
if (!iface)
{
gi_ir_module_fatal (build, 0, "Can't resolve type '%s' for %s", type->giinterface, who);
*size = -1;
*alignment = -1;
*size = 0;
*alignment = 0;
return FALSE;
}
@ -253,9 +253,9 @@ get_interface_size_alignment (GIIrTypelibBuild *build,
g_warning ("%s has is not a pointer and is of type %s",
who,
gi_ir_node_type_to_string (iface->type));
*size = -1;
*alignment = -1;
break;
*size = 0;
*alignment = 0;
return FALSE;
}
}
@ -266,7 +266,7 @@ static gboolean
get_type_size_alignment (GIIrTypelibBuild *build,
GIIrNodeType *type,
size_t *size,
gssize *alignment,
size_t *alignment,
const char *who)
{
ffi_type *type_ffi;
@ -278,14 +278,14 @@ get_type_size_alignment (GIIrTypelibBuild *build,
else if (type->tag == GI_TYPE_TAG_ARRAY)
{
size_t elt_size;
gssize elt_alignment;
size_t elt_alignment;
if (!type->has_size
|| !get_type_size_alignment(build, type->parameter_type1,
&elt_size, &elt_alignment, who))
{
*size = -1;
*alignment = -1;
*size = 0;
*alignment = 0;
return FALSE;
}
@ -307,8 +307,8 @@ get_type_size_alignment (GIIrTypelibBuild *build,
if (type_ffi == &ffi_type_void)
{
g_warning ("%s has void type", who);
*size = -1;
*alignment = -1;
*size = 0;
*alignment = 0;
return FALSE;
}
else if (type_ffi == &ffi_type_pointer)
@ -316,8 +316,8 @@ get_type_size_alignment (GIIrTypelibBuild *build,
g_warning ("%s has is not a pointer and is of type %s",
who,
gi_type_tag_to_string (type->tag));
*size = -1;
*alignment = -1;
*size = 0;
*alignment = 0;
return FALSE;
}
}
@ -335,7 +335,7 @@ get_field_size_alignment (GIIrTypelibBuild *build,
GIIrNodeField *field,
GIIrNode *parent_node,
size_t *size,
gssize *alignment)
size_t *alignment)
{
GIIrModule *module = build->module;
char *who;
@ -363,14 +363,15 @@ compute_struct_field_offsets (GIIrTypelibBuild *build,
GIIrNode *node,
GList *members,
size_t *size_out,
gssize *alignment_out)
size_t *alignment_out,
GIIrOffsetsState *offsets_state_out)
{
int size = 0;
int alignment = 1;
size_t size = 0;
size_t alignment = 1;
GList *l;
gboolean have_error = FALSE;
*alignment_out = -2; /* mark to detect recursion */
*offsets_state_out = GI_IR_OFFSETS_IN_PROGRESS; /* mark to detect recursion */
for (l = members; l; l = l->next)
{
@ -383,7 +384,7 @@ compute_struct_field_offsets (GIIrTypelibBuild *build,
if (!have_error)
{
size_t member_size;
gssize member_alignment;
size_t member_alignment;
if (get_field_size_alignment (build, field, node,
&member_size, &member_alignment))
@ -415,11 +416,13 @@ compute_struct_field_offsets (GIIrTypelibBuild *build,
{
*size_out = size;
*alignment_out = alignment;
*offsets_state_out = GI_IR_OFFSETS_COMPUTED;
}
else
{
*size_out = -1;
*alignment_out = -1;
*size_out = 0;
*alignment_out = 0;
*offsets_state_out = GI_IR_OFFSETS_FAILED;
}
return !have_error;
@ -430,14 +433,15 @@ compute_union_field_offsets (GIIrTypelibBuild *build,
GIIrNode *node,
GList *members,
size_t *size_out,
gssize *alignment_out)
size_t *alignment_out,
GIIrOffsetsState *offsets_state_out)
{
size_t size = 0;
int alignment = 1;
size_t alignment = 1;
GList *l;
gboolean have_error = FALSE;
*alignment_out = -2; /* mark to detect recursion */
*offsets_state_out = GI_IR_OFFSETS_IN_PROGRESS; /* mark to detect recursion */
for (l = members; l; l = l->next)
{
@ -450,7 +454,7 @@ compute_union_field_offsets (GIIrTypelibBuild *build,
if (!have_error)
{
size_t member_size;
gssize member_alignment;
size_t member_alignment;
if (get_field_size_alignment (build,field, node,
&member_size, &member_alignment))
@ -471,11 +475,13 @@ compute_union_field_offsets (GIIrTypelibBuild *build,
{
*size_out = size;
*alignment_out = alignment;
*offsets_state_out = GI_IR_OFFSETS_COMPUTED;
}
else
{
*size_out = -1;
*alignment_out = -1;
*size_out = 0;
*alignment_out = 0;
*offsets_state_out = GI_IR_OFFSETS_FAILED;
}
return !have_error;
@ -484,22 +490,17 @@ compute_union_field_offsets (GIIrTypelibBuild *build,
static gboolean
check_needs_computation (GIIrTypelibBuild *build,
GIIrNode *node,
int alignment)
GIIrOffsetsState offsets_state)
{
GIIrModule *module = build->module;
/*
* 0: Not yet computed
* >0: Previously succeeded
* -1: Previously failed
* -2: In progress
*/
if (alignment == -2)
if (offsets_state == GI_IR_OFFSETS_IN_PROGRESS)
{
g_warning ("Recursion encountered when computing the size of %s.%s",
module->name, node->name);
}
return alignment == 0;
return offsets_state == GI_IR_OFFSETS_UNKNOWN;
}
/*
@ -532,22 +533,22 @@ gi_ir_node_compute_offsets (GIIrTypelibBuild *build,
{
GIIrNodeBoxed *boxed = (GIIrNodeBoxed *)node;
if (!check_needs_computation (build, node, boxed->alignment))
if (!check_needs_computation (build, node, boxed->offsets_state))
return;
compute_struct_field_offsets (build, node, boxed->members,
&boxed->size, &boxed->alignment);
&boxed->size, &boxed->alignment, &boxed->offsets_state);
break;
}
case GI_IR_NODE_STRUCT:
{
GIIrNodeStruct *struct_ = (GIIrNodeStruct *)node;
if (!check_needs_computation (build, node, struct_->alignment))
if (!check_needs_computation (build, node, struct_->offsets_state))
return;
compute_struct_field_offsets (build, node, struct_->members,
&struct_->size, &struct_->alignment);
&struct_->size, &struct_->alignment, &struct_->offsets_state);
break;
}
case GI_IR_NODE_OBJECT:
@ -555,22 +556,22 @@ gi_ir_node_compute_offsets (GIIrTypelibBuild *build,
{
GIIrNodeInterface *iface = (GIIrNodeInterface *)node;
if (!check_needs_computation (build, node, iface->alignment))
if (!check_needs_computation (build, node, iface->offsets_state))
return;
compute_struct_field_offsets (build, node, iface->members,
&iface->size, &iface->alignment);
&iface->size, &iface->alignment, &iface->offsets_state);
break;
}
case GI_IR_NODE_UNION:
{
GIIrNodeUnion *union_ = (GIIrNodeUnion *)node;
if (!check_needs_computation (build, node, union_->alignment))
if (!check_needs_computation (build, node, union_->offsets_state))
return;
compute_union_field_offsets (build, (GIIrNode*)union_, union_->members,
&union_->size, &union_->alignment);
&union_->size, &union_->alignment, &union_->offsets_state);
break;
}
case GI_IR_NODE_ENUM:

View File

@ -244,8 +244,8 @@ write_type_info (const char *ns,
}
else if (tag == GI_TYPE_TAG_ARRAY)
{
gssize length;
gssize size;
unsigned int length_index;
size_t size;
const char *name = NULL;
xml_start_element (file, "array");
@ -271,13 +271,11 @@ write_type_info (const char *ns,
type = gi_type_info_get_param_type (info, 0);
length = gi_type_info_get_array_length_index (info);
if (length >= 0)
xml_printf (file, " length=\"%" G_GSSIZE_FORMAT "\"", length);
if (gi_type_info_get_array_length_index (info, &length_index))
xml_printf (file, " length=\"%u\"", length_index);
size = gi_type_info_get_array_fixed_size (info);
if (size >= 0)
xml_printf (file, " fixed-size=\"%" G_GSSIZE_FORMAT "\"", size);
if (gi_type_info_get_array_fixed_size (info, &size))
xml_printf (file, " fixed-size=\"%zu\"", size);
if (gi_type_info_is_zero_terminated (info))
xml_printf (file, " zero-terminated=\"1\"");
@ -492,6 +490,7 @@ write_callable_info (const char *ns,
for (unsigned int i = 0; i < gi_callable_info_get_n_args (info); i++)
{
GIArgInfo *arg = gi_callable_info_get_arg (info, i);
unsigned int closure_index, destroy_index;
xml_start_element (file, "parameter");
xml_printf (file, " name=\"%s\"",
@ -543,13 +542,11 @@ write_callable_info (const char *ns,
g_assert_not_reached ();
}
if (gi_arg_info_get_closure_index (arg) >= 0)
xml_printf (file, " closure=\"%" G_GSSIZE_FORMAT "\"",
gi_arg_info_get_closure_index (arg));
if (gi_arg_info_get_closure_index (arg, &closure_index))
xml_printf (file, " closure=\"%u\"", closure_index);
if (gi_arg_info_get_destroy_index (arg) >= 0)
xml_printf (file, " destroy=\"%" G_GSSIZE_FORMAT "\"",
gi_arg_info_get_destroy_index (arg));
if (gi_arg_info_get_destroy_index (arg, &destroy_index))
xml_printf (file, " destroy=\"%u\"", destroy_index);
if (gi_arg_info_is_skip (arg))
xml_printf (file, " skip=\"1\"");

View File

@ -232,23 +232,25 @@ gi_type_info_get_interface (GITypeInfo *info)
/**
* gi_type_info_get_array_length_index:
* @info: a #GITypeInfo
* @out_length_index: (out) (optional): return location for the length argument
*
* Obtain the position of the argument which gives the array length of the type.
*
* The type tag must be a `GI_TYPE_TAG_ARRAY` or `-1` will be returned.
* The type tag must be a `GI_TYPE_TAG_ARRAY` with a length argument, or `FALSE`
* will be returned.
*
* Returns: the array length argument index, or `-1` if the type is not an array
* or it has no length argument
* Returns: `TRUE` if the type is an array and has a length argument
* Since: 2.80
*/
gssize
gi_type_info_get_array_length_index (GITypeInfo *info)
gboolean
gi_type_info_get_array_length_index (GITypeInfo *info,
unsigned int *out_length_index)
{
GIRealInfo *rinfo = (GIRealInfo *)info;
SimpleTypeBlob *type;
g_return_val_if_fail (info != NULL, -1);
g_return_val_if_fail (GI_IS_TYPE_INFO (info), -1);
g_return_val_if_fail (info != NULL, FALSE);
g_return_val_if_fail (GI_IS_TYPE_INFO (info), FALSE);
type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
@ -259,32 +261,41 @@ gi_type_info_get_array_length_index (GITypeInfo *info)
if (blob->tag == GI_TYPE_TAG_ARRAY)
{
if (blob->has_length)
return blob->dimensions.length;
{
if (out_length_index != NULL)
*out_length_index = blob->dimensions.length;
return TRUE;
}
}
}
return -1;
if (out_length_index != NULL)
*out_length_index = 0;
return FALSE;
}
/**
* gi_type_info_get_array_fixed_size:
* @info: a #GITypeInfo
* @out_size: (out) (optional): return location for the array size
*
* Obtain the fixed array size of the type, in number of elements (not bytes).
*
* The type tag must be a `GI_TYPE_TAG_ARRAY` or `-1` will be returned.
* The type tag must be a `GI_TYPE_TAG_ARRAY` with a fixed size, or `FALSE` will
* be returned.
*
* Returns: the size or `-1` if the type is not an array or it has no fixed size
* Returns: `TRUE` if the type is an array and has a fixed size
* Since: 2.80
*/
gssize
gi_type_info_get_array_fixed_size (GITypeInfo *info)
gboolean
gi_type_info_get_array_fixed_size (GITypeInfo *info,
size_t *out_size)
{
GIRealInfo *rinfo = (GIRealInfo *)info;
SimpleTypeBlob *type;
g_return_val_if_fail (info != NULL, -1);
g_return_val_if_fail (GI_IS_TYPE_INFO (info), -1);
g_return_val_if_fail (info != NULL, FALSE);
g_return_val_if_fail (GI_IS_TYPE_INFO (info), FALSE);
type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
@ -295,11 +306,17 @@ gi_type_info_get_array_fixed_size (GITypeInfo *info)
if (blob->tag == GI_TYPE_TAG_ARRAY)
{
if (blob->has_size)
return blob->dimensions.size;
{
if (out_size != NULL)
*out_size = blob->dimensions.size;
return TRUE;
}
}
}
return -1;
if (out_size != NULL)
*out_size = 0;
return FALSE;
}
/**
@ -341,10 +358,12 @@ gi_type_info_is_zero_terminated (GITypeInfo *info)
*
* Obtain the array type for this type.
*
* See [enum@GIRepository.ArrayType] for a list of possible values. If the type
* tag of this type is not array, `-1` will be returned.
* See [enum@GIRepository.ArrayType] for a list of possible values.
*
* Returns: the array type or `-1`
* It is an error to call this on an @info which is not an array type. Use
* [method@GIRepository.TypeInfo.get_tag] to check.
*
* Returns: the array type
* Since: 2.80
*/
GIArrayType
@ -366,7 +385,8 @@ gi_type_info_get_array_type (GITypeInfo *info)
return blob->array_type;
}
return -1;
/* Not an array type */
g_assert_not_reached ();
}
/**

View File

@ -96,10 +96,12 @@ GI_AVAILABLE_IN_ALL
GIBaseInfo * gi_type_info_get_interface (GITypeInfo *info);
GI_AVAILABLE_IN_ALL
gssize gi_type_info_get_array_length_index (GITypeInfo *info);
gboolean gi_type_info_get_array_length_index (GITypeInfo *info,
unsigned int *out_length_index);
GI_AVAILABLE_IN_ALL
gssize gi_type_info_get_array_fixed_size (GITypeInfo *info);
gboolean gi_type_info_get_array_fixed_size (GITypeInfo *info,
size_t *out_size);
GI_AVAILABLE_IN_ALL
gboolean gi_type_info_is_zero_terminated (GITypeInfo *info);