/** * g_variant_unref: * @value: a #GVariant * * Decreases the reference count of @value. When its reference count * drops to 0, the memory used by the variant is freed. * * Since: 2.24 **/ void g_variant_unref (GVariant *value) { g_return_if_fail (value != NULL); g_return_if_fail (value->ref_count > 0); if (g_atomic_int_dec_and_test (&value->ref_count)) { if G_UNLIKELY (value->state & STATE_LOCKED) g_critical ("attempting to free a locked GVariant instance. " "This should never happen."); value->state |= STATE_LOCKED; g_variant_type_info_unref (value->type_info); if (value->state & STATE_SERIALISED) g_bytes_unref (value->contents.serialised.bytes); else g_variant_release_children (value); memset (value, 0, sizeof (GVariant)); g_slice_free (GVariant, value); } }
/* < private > * g_variant_ensure_serialised: * @value: a #GVariant * * Ensures that @value is in serialised form. * * If @value is in tree form then this function ensures that the * serialised size is known and then allocates a buffer of that size and * serialises the instance into the buffer. The 'children' array is * then released and the instance is set to serialised form based on the * contents of the buffer. * * The current thread must hold the lock on @value. */ static void g_variant_ensure_serialised (GVariant *value) { g_assert (value->state & STATE_LOCKED); if (~value->state & STATE_SERIALISED) { GBytes *bytes; gpointer data; g_variant_ensure_size (value); data = g_malloc (value->size); g_variant_serialise (value, data); g_variant_release_children (value); bytes = g_bytes_new_take (data, value->size); value->contents.serialised.data = g_bytes_get_data (bytes, NULL); value->contents.serialised.bytes = bytes; value->state |= STATE_SERIALISED; } }
/* < private > * g_variant_ensure_serialised: * @value: a #GVariant * * Ensures that @value is in serialised form. * * If @value is in tree form then this function ensures that the * serialised size is known and then allocates a buffer of that size and * serialises the instance into the buffer. The 'children' array is * then released and the instance is set to serialised form based on the * contents of the buffer. * * The current thread must hold the lock on @value. */ static void g_variant_ensure_serialised (GVariant *value) { g_assert (value->state & STATE_LOCKED); if (~value->state & STATE_SERIALISED) { GBuffer *buffer; gpointer data; g_variant_ensure_size (value); data = g_malloc (value->size); g_variant_serialise (value, data); g_variant_release_children (value); buffer = g_buffer_new_take_data (data, value->size); value->contents.serialised.data = buffer->data; value->contents.serialised.buffer = buffer; value->state |= STATE_SERIALISED; } }