X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=glib%2Fgvariant-core.c;h=a9f5d94236cdf464d389d5f630211f55648b4f9f;hb=4454b815367831a71b1ae00b0182b5b389a78df2;hp=fab3a1466b9564f9ca76a7cd0e28433ee34ff35f;hpb=ebb544f549c77d0e98431ee38c55824ff4de81ab;p=platform%2Fupstream%2Fglib.git diff --git a/glib/gvariant-core.c b/glib/gvariant-core.c index fab3a14..a9f5d94 100644 --- a/glib/gvariant-core.c +++ b/glib/gvariant-core.c @@ -13,18 +13,18 @@ * 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, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. + * License along with this library; if not, see . */ +#include "config.h" + #include #include #include #include #include -#include +#include #include #include #include @@ -61,7 +61,7 @@ struct _GVariant { struct { - GBuffer *buffer; + GBytes *bytes; gconstpointer data; } serialised; @@ -133,16 +133,16 @@ struct _GVariant * never be changed. It is therefore valid to access * them without holding a lock. * - * .buffer: the #GBuffer that contains the memory pointed to by + * .bytes: the #GBytes that contains the memory pointed to by * .data, or %NULL if .data is %NULL. In the event that * the instance was deserialised from another instance, - * then the buffer will be shared by both of them. When + * then the bytes will be shared by both of them. When * the instance is freed, this reference must be released - * with g_buffer_unref(). + * with g_bytes_unref(). * * .data: the serialised data (of size 'size') of the instance. * This pointer should not be freed or modified in any way. - * #GBuffer is responsible for memory management. + * #GBytes is responsible for memory management. * * This pointer may be %NULL in two cases: * @@ -438,7 +438,7 @@ g_variant_ensure_serialised (GVariant *value) if (~value->state & STATE_SERIALISED) { - GBuffer *buffer; + GBytes *bytes; gpointer data; g_variant_ensure_size (value); @@ -447,9 +447,9 @@ g_variant_ensure_serialised (GVariant *value) 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; + 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; } } @@ -459,11 +459,12 @@ g_variant_ensure_serialised (GVariant *value) * @type: the type of the new instance * @serialised: if the instance will be in serialised form * @trusted: if the instance will be trusted - * @returns: a new #GVariant with a floating reference * * Allocates a #GVariant instance and does some common work (such as * looking up and filling in the type info), setting the state field, * and setting the ref_count to 1. + * + * Returns: a new #GVariant with a floating reference */ static GVariant * g_variant_alloc (const GVariantType *type, @@ -483,24 +484,26 @@ g_variant_alloc (const GVariantType *type, return value; } -/* -- internal -- */ -/* < internal > - * g_variant_new_from_buffer: +/** + * g_variant_new_from_bytes: * @type: a #GVariantType - * @buffer: a #GBuffer - * @trusted: if the contents of @buffer are trusted - * @returns: a new #GVariant with a floating reference + * @bytes: a #GBytes + * @trusted: if the contents of @bytes are trusted * * Constructs a new serialised-mode #GVariant instance. This is the * inner interface for creation of new serialised values that gets * called from various functions in gvariant.c. * - * A reference is taken on @buffer. + * A reference is taken on @bytes. + * + * Returns: (transfer none): a new #GVariant with a floating reference + * + * Since: 2.36 */ GVariant * -g_variant_new_from_buffer (const GVariantType *type, - GBuffer *buffer, - gboolean trusted) +g_variant_new_from_bytes (const GVariantType *type, + GBytes *bytes, + gboolean trusted) { GVariant *value; guint alignment; @@ -508,14 +511,14 @@ g_variant_new_from_buffer (const GVariantType *type, value = g_variant_alloc (type, TRUE, trusted); - value->contents.serialised.buffer = g_buffer_ref (buffer); + value->contents.serialised.bytes = g_bytes_ref (bytes); g_variant_type_info_query (value->type_info, &alignment, &size); - if (size && buffer->size != size) + if (size && g_bytes_get_size (bytes) != size) { - /* Creating a fixed-sized GVariant with a buffer of the wrong + /* Creating a fixed-sized GVariant with a bytes of the wrong * size. * * We should do the equivalent of pulling a fixed-sized child out @@ -527,20 +530,20 @@ g_variant_new_from_buffer (const GVariantType *type, } else { - value->contents.serialised.data = buffer->data; - value->size = buffer->size; + value->contents.serialised.data = g_bytes_get_data (bytes, &value->size); } return value; } +/* -- internal -- */ + /* < internal > * g_variant_new_from_children: * @type: a #GVariantType * @children: an array of #GVariant pointers. Consumed. * @n_children: the length of @children * @trusted: %TRUE if every child in @children in trusted - * @returns: a new #GVariant with a floating reference * * Constructs a new tree-mode #GVariant instance. This is the inner * interface for creation of new serialised values that gets called from @@ -548,6 +551,8 @@ g_variant_new_from_buffer (const GVariantType *type, * * @children is consumed by this function. g_free() will be called on * it some time later. + * + * Returns: a new #GVariant with a floating reference */ GVariant * g_variant_new_from_children (const GVariantType *type, @@ -567,11 +572,12 @@ g_variant_new_from_children (const GVariantType *type, /* < internal > * g_variant_get_type_info: * @value: a #GVariant - * @returns: the #GVariantTypeInfo for @value * * Returns the #GVariantTypeInfo corresponding to the type of @value. A * reference is not added, so the return value is only good for the * duration of the life of @value. + * + * Returns: the #GVariantTypeInfo for @value */ GVariantTypeInfo * g_variant_get_type_info (GVariant *value) @@ -582,7 +588,6 @@ g_variant_get_type_info (GVariant *value) /* < internal > * g_variant_is_trusted: * @value: a #GVariant - * @returns: if @value is trusted * * Determines if @value is trusted by #GVariant to contain only * fully-valid data. All values constructed solely via #GVariant APIs @@ -593,6 +598,8 @@ g_variant_get_type_info (GVariant *value) * skipped. For example, we don't need to check that a string is * properly nul-terminated or that an object path is actually a * properly-formatted object path. + * + * Returns: if @value is trusted */ gboolean g_variant_is_trusted (GVariant *value) @@ -614,6 +621,9 @@ g_variant_is_trusted (GVariant *value) 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) @@ -625,7 +635,7 @@ g_variant_unref (GVariant *value) g_variant_type_info_unref (value->type_info); if (value->state & STATE_SERIALISED) - g_buffer_unref (value->contents.serialised.buffer); + g_bytes_unref (value->contents.serialised.bytes); else g_variant_release_children (value); @@ -637,15 +647,19 @@ g_variant_unref (GVariant *value) /** * g_variant_ref: * @value: a #GVariant - * @returns: the same @value * * Increases the reference count of @value. * + * Returns: the same @value + * * Since: 2.24 **/ GVariant * g_variant_ref (GVariant *value) { + g_return_val_if_fail (value != NULL, NULL); + g_return_val_if_fail (value->ref_count > 0, NULL); + g_atomic_int_inc (&value->ref_count); return value; @@ -654,10 +668,9 @@ g_variant_ref (GVariant *value) /** * g_variant_ref_sink: * @value: a #GVariant - * @returns: the same @value * * #GVariant uses a floating reference count system. All functions with - * names starting with g_variant_new_ return floating + * names starting with `g_variant_new_` return floating * references. * * Calling g_variant_ref_sink() on a #GVariant with a floating reference @@ -679,11 +692,16 @@ g_variant_ref (GVariant *value) * maintaining normal refcounting semantics in situations where values * are not floating. * + * Returns: the same @value + * * Since: 2.24 **/ GVariant * g_variant_ref_sink (GVariant *value) { + g_return_val_if_fail (value != NULL, NULL); + g_return_val_if_fail (value->ref_count > 0, NULL); + g_variant_lock (value); if (~value->state & STATE_FLOATING) @@ -699,7 +717,6 @@ g_variant_ref_sink (GVariant *value) /** * g_variant_take_ref: * @value: a #GVariant - * @returns: the same @value * * If @value is floating, sink it. Otherwise, do nothing. * @@ -733,11 +750,14 @@ g_variant_ref_sink (GVariant *value) * be that the floating reference is converted to a hard reference and * an additional reference on top of that one is added. It is best to * avoid this situation. + * + * Returns: the same @value **/ GVariant * g_variant_take_ref (GVariant *value) { g_return_val_if_fail (value != NULL, NULL); + g_return_val_if_fail (value->ref_count > 0, NULL); g_atomic_int_and (&value->state, ~STATE_FLOATING); @@ -747,7 +767,6 @@ g_variant_take_ref (GVariant *value) /** * g_variant_is_floating: * @value: a #GVariant - * @returns: whether @value is floating * * Checks whether @value has a floating reference count. * @@ -759,6 +778,8 @@ g_variant_take_ref (GVariant *value) * See g_variant_ref_sink() for more information about floating reference * counts. * + * Returns: whether @value is floating + * * Since: 2.26 **/ gboolean @@ -772,7 +793,6 @@ g_variant_is_floating (GVariant *value) /** * g_variant_get_size: * @value: a #GVariant instance - * @returns: the serialised size of @value * * Determines the number of bytes that would be required to store @value * with g_variant_store(). @@ -786,6 +806,8 @@ g_variant_is_floating (GVariant *value) * operation which is approximately O(n) in the number of values * involved. * + * Returns: the serialised size of @value + * * Since: 2.24 **/ gsize @@ -801,7 +823,6 @@ g_variant_get_size (GVariant *value) /** * g_variant_get_data: * @value: a #GVariant instance - * @returns: (transfer none): the serialised form of @value, or %NULL * * Returns a pointer to the serialised form of a #GVariant instance. * The returned data may not be in fully-normalised form if read from an @@ -823,12 +844,14 @@ g_variant_get_size (GVariant *value) * serialised data, you must know the type of the #GVariant, and (if the * machine might be different) the endianness of the machine that stored * it. As a result, file formats or network messages that incorporate - * serialised #GVariants must include this information either + * serialised #GVariants must include this information either * implicitly (for instance "the file always contains a * %G_VARIANT_TYPE_VARIANT and it is always in little-endian order") or * explicitly (by storing the type and/or endianness in addition to the * serialised data). * + * Returns: (transfer none): the serialised form of @value, or %NULL + * * Since: 2.24 **/ gconstpointer @@ -842,9 +865,45 @@ g_variant_get_data (GVariant *value) } /** + * g_variant_get_data_as_bytes: + * @value: a #GVariant + * + * Returns a pointer to the serialised form of a #GVariant instance. + * The semantics of this function are exactly the same as + * g_variant_get_data(), except that the returned #GBytes holds + * a reference to the variant data. + * + * Returns: (transfer full): A new #GBytes representing the variant data + * + * Since: 2.36 + */ +GBytes * +g_variant_get_data_as_bytes (GVariant *value) +{ + const gchar *bytes_data; + const gchar *data; + gsize bytes_size; + gsize size; + + g_variant_lock (value); + g_variant_ensure_serialised (value); + g_variant_unlock (value); + + bytes_data = g_bytes_get_data (value->contents.serialised.bytes, &bytes_size); + data = value->contents.serialised.data; + size = value->size; + + if (data == bytes_data && size == bytes_size) + return g_bytes_ref (value->contents.serialised.bytes); + else + return g_bytes_new_from_bytes (value->contents.serialised.bytes, + data - bytes_data, size); +} + + +/** * g_variant_n_children: * @value: a container #GVariant - * @returns: the number of children in the container * * Determines the number of children in a container #GVariant instance. * This includes variants, maybes, arrays, tuples and dictionary @@ -858,6 +917,8 @@ g_variant_get_data (GVariant *value) * * This function is O(1). * + * Returns: the number of children in the container + * * Since: 2.24 **/ gsize @@ -889,7 +950,6 @@ g_variant_n_children (GVariant *value) * g_variant_get_child_value: * @value: a container #GVariant * @index_: the index of the child to fetch - * @returns: (transfer full): the child at the specified index * * Reads a child item out of a container #GVariant instance. This * includes variants, maybes, arrays, tuples and dictionary @@ -899,8 +959,13 @@ g_variant_n_children (GVariant *value) * It is an error if @index_ is greater than the number of child items * in the container. See g_variant_n_children(). * + * The returned value is never floating. You should free it with + * g_variant_unref() when you're done with it. + * * This function is O(1). * + * Returns: (transfer full): the child at the specified index + * * Since: 2.24 **/ GVariant * @@ -947,8 +1012,8 @@ g_variant_get_child_value (GVariant *value, STATE_SERIALISED; child->size = s_child.size; child->ref_count = 1; - child->contents.serialised.buffer = - g_buffer_ref (value->contents.serialised.buffer); + child->contents.serialised.bytes = + g_bytes_ref (value->contents.serialised.bytes); child->contents.serialised.data = s_child.data; return child; @@ -997,7 +1062,6 @@ g_variant_store (GVariant *value, /** * g_variant_is_normal_form: * @value: a #GVariant instance - * @returns: %TRUE if @value is in normal form * * Checks if @value is in normal form. * @@ -1010,6 +1074,8 @@ g_variant_store (GVariant *value, * being trusted. If the value was already marked as being trusted then * this function will immediately return %TRUE. * + * Returns: %TRUE if @value is in normal form + * * Since: 2.24 **/ gboolean