GVariant: avoid byteswapping in some cases

Make g_variant_byteswap() merely return a new reference on the given
value in the event that we know that byteswapping will have no effect
(ie: types which have no alignment requirement).

This fixes a somewhat complicated interaction between GVariant,
GSettings and GVDB on big endian machines:  GSettings assumes that it
can unref values returned from GVDB without losing access to the
underlying data.  This only works if the underlying data is in the
mapped file -- not a freshly-allocated buffer that GVariant byteswapped
into.
This commit is contained in:
Ryan Lortie 2010-10-03 22:39:47 -04:00
parent 63adeda086
commit 2ce2d587ed

View File

@ -4566,10 +4566,20 @@ g_variant_get_normal_form (GVariant *value)
GVariant * GVariant *
g_variant_byteswap (GVariant *value) g_variant_byteswap (GVariant *value)
{ {
GVariantTypeInfo *type_info;
guint alignment;
GVariant *new;
type_info = g_variant_get_type_info (value);
g_variant_type_info_query (type_info, &alignment, NULL);
if (alignment)
/* (potentially) contains multi-byte numeric data */
{
GVariantSerialised serialised; GVariantSerialised serialised;
GVariant *trusted; GVariant *trusted;
GBuffer *buffer; GBuffer *buffer;
GVariant *new;
trusted = g_variant_get_normal_form (value); trusted = g_variant_get_normal_form (value);
serialised.type_info = g_variant_get_type_info (trusted); serialised.type_info = g_variant_get_type_info (trusted);
@ -4583,6 +4593,10 @@ g_variant_byteswap (GVariant *value)
buffer = g_buffer_new_take_data (serialised.data, serialised.size); buffer = g_buffer_new_take_data (serialised.data, serialised.size);
new = g_variant_new_from_buffer (g_variant_get_type (value), buffer, TRUE); new = g_variant_new_from_buffer (g_variant_get_type (value), buffer, TRUE);
g_buffer_unref (buffer); g_buffer_unref (buffer);
}
else
/* contains no multi-byte data */
new = value;
return g_variant_ref_sink (new); return g_variant_ref_sink (new);
} }