From 14fb10d14b07cef6af8952bfdd0bd4d864876607 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Fri, 9 Dec 2011 18:19:24 +0100 Subject: GBytes: add a size argument to g_bytes_get_data * An out size argument so that this is more easily bindable by gobject-introspection. https://bugzilla.gnome.org/show_bug.cgi?id=665879 --- glib/gbytes.c | 8 ++++++-- glib/gbytes.h | 3 ++- glib/gtimezone.c | 4 ++-- glib/gvariant-core.c | 5 ++--- glib/tests/array-test.c | 4 +++- glib/tests/bytes.c | 41 ++++++++++++++++++++++++++--------------- 6 files changed, 41 insertions(+), 24 deletions(-) (limited to 'glib') diff --git a/glib/gbytes.c b/glib/gbytes.c index dcf0dfb2e..daf564245 100644 --- a/glib/gbytes.c +++ b/glib/gbytes.c @@ -212,19 +212,23 @@ g_bytes_new_from_bytes (GBytes *bytes, /** * g_bytes_get_data: * @bytes: a #GBytes + * @size: (out) (allow-none): location to return size of byte data * * Get the byte data in the #GBytes. This data should not be modified. * * This function will always return the same pointer for a given #GBytes. * - * Returns: a pointer to the byte data + * Returns: (array length=size) (type guint8): a pointer to the byte data * * Since: 2.32 */ gconstpointer -g_bytes_get_data (GBytes *bytes) +g_bytes_get_data (GBytes *bytes, + gsize *size) { g_return_val_if_fail (bytes != NULL, NULL); + if (size) + *size = bytes->size; return bytes->data; } diff --git a/glib/gbytes.h b/glib/gbytes.h index cf7b32688..353232868 100644 --- a/glib/gbytes.h +++ b/glib/gbytes.h @@ -45,7 +45,8 @@ GBytes * g_bytes_new_from_bytes (GBytes *bytes, gsize offset, gsize length); -gconstpointer g_bytes_get_data (GBytes *bytes); +gconstpointer g_bytes_get_data (GBytes *bytes, + gsize *size); gsize g_bytes_get_size (GBytes *bytes); diff --git a/glib/gtimezone.c b/glib/gtimezone.c index 19358b1b5..2785d2271 100644 --- a/glib/gtimezone.c +++ b/glib/gtimezone.c @@ -395,8 +395,8 @@ g_time_zone_new (const gchar *identifier) if (tz->zoneinfo != NULL) { - const struct tzhead *header = g_bytes_get_data (tz->zoneinfo); - gsize size = g_bytes_get_size (tz->zoneinfo); + gsize size; + const struct tzhead *header = g_bytes_get_data (tz->zoneinfo, &size); /* we only bother to support version 2 */ if (size < sizeof (struct tzhead) || memcmp (header, "TZif2", 5)) diff --git a/glib/gvariant-core.c b/glib/gvariant-core.c index fdbe0341b..1633463a0 100644 --- a/glib/gvariant-core.c +++ b/glib/gvariant-core.c @@ -448,7 +448,7 @@ g_variant_ensure_serialised (GVariant *value) g_variant_release_children (value); bytes = g_bytes_new_take (data, value->size); - value->contents.serialised.data = g_bytes_get_data (bytes); + value->contents.serialised.data = g_bytes_get_data (bytes, NULL); value->contents.serialised.bytes = bytes; value->state |= STATE_SERIALISED; } @@ -529,8 +529,7 @@ g_variant_new_from_bytes (const GVariantType *type, } else { - value->contents.serialised.data = g_bytes_get_data (bytes); - value->size = g_bytes_get_size (bytes); + value->contents.serialised.data = g_bytes_get_data (bytes, &value->size); } return value; diff --git a/glib/tests/array-test.c b/glib/tests/array-test.c index de3a8ff6f..7caab8716 100644 --- a/glib/tests/array-test.c +++ b/glib/tests/array-test.c @@ -781,6 +781,7 @@ byte_array_free_to_bytes (void) GByteArray *gbarray; gpointer memory; GBytes *bytes; + gsize size; gbarray = g_byte_array_new (); g_byte_array_append (gbarray, (guint8 *)"woooweeewow", 11); @@ -789,7 +790,8 @@ byte_array_free_to_bytes (void) bytes = g_byte_array_free_to_bytes (gbarray); g_assert (bytes != NULL); g_assert_cmpuint (g_bytes_get_size (bytes), ==, 11); - g_assert (g_bytes_get_data (bytes) == memory); + g_assert (g_bytes_get_data (bytes, &size) == memory); + g_assert_cmpuint (size, ==, 11); g_bytes_unref (bytes); } diff --git a/glib/tests/bytes.c b/glib/tests/bytes.c index 104bdcee4..83589fedc 100644 --- a/glib/tests/bytes.c +++ b/glib/tests/bytes.c @@ -25,13 +25,15 @@ test_new (void) { const gchar *data; GBytes *bytes; + gsize size; data = "test"; bytes = g_bytes_new (data, 4); g_assert (bytes != NULL); - g_assert (g_bytes_get_data (bytes) != data); + g_assert (g_bytes_get_data (bytes, &size) != data); + g_assert_cmpuint (size, ==, 4); g_assert_cmpuint (g_bytes_get_size (bytes), ==, 4); - g_assert (memcmp (data, g_bytes_get_data (bytes), g_bytes_get_size (bytes)) == 0); + g_assert (memcmp (data, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes)) == 0); g_bytes_unref (bytes); } @@ -41,11 +43,13 @@ test_new_take (void) { gchar *data; GBytes *bytes; + gsize size; data = g_strdup ("test"); bytes = g_bytes_new_take (data, 4); g_assert (bytes != NULL); - g_assert (g_bytes_get_data (bytes) == data); + g_assert (g_bytes_get_data (bytes, &size) == data); + g_assert_cmpuint (size, ==, 4); g_assert_cmpuint (g_bytes_get_size (bytes), ==, 4); g_bytes_unref (bytes); @@ -56,11 +60,13 @@ test_new_static (void) { const gchar *data; GBytes *bytes; + gsize size; data = "test"; bytes = g_bytes_new_static (data, 4); g_assert (bytes != NULL); - g_assert (g_bytes_get_data (bytes) == data); + g_assert (g_bytes_get_data (bytes, &size) == data); + g_assert_cmpuint (size, ==, 4); g_assert_cmpuint (g_bytes_get_size (bytes), ==, 4); g_bytes_unref (bytes); @@ -77,11 +83,11 @@ test_new_from_bytes (void) sub = g_bytes_new_from_bytes (bytes, 10, 4); g_assert (sub != NULL); - g_assert (g_bytes_get_data (sub) == ((gchar *)g_bytes_get_data (bytes)) + 10); + g_assert (g_bytes_get_data (sub, NULL) == ((gchar *)g_bytes_get_data (bytes, NULL)) + 10); g_assert (g_bytes_get_size (sub) == 4); g_bytes_unref (bytes); - g_assert (memcmp (g_bytes_get_data (sub), "wave", 4) == 0); + g_assert (memcmp (g_bytes_get_data (sub, NULL), "wave", 4) == 0); g_bytes_unref (sub); } @@ -99,12 +105,14 @@ test_new_with_free_func (void) GBytes *bytes; gchar *data; gint count = 0; + gsize size; data = "test"; bytes = g_bytes_new_with_free_func (data, 4, on_destroy_increment, &count); g_assert (bytes != NULL); g_assert_cmpint (count, ==, 0); - g_assert (g_bytes_get_data (bytes) == data); + g_assert (g_bytes_get_data (bytes, &size) == data); + g_assert_cmpuint (size, ==, 4); g_assert_cmpuint (g_bytes_get_size (bytes), ==, 4); g_bytes_unref (bytes); @@ -201,7 +209,7 @@ test_to_data_transferred (void) /* Memory transferred: one reference, and allocated with g_malloc */ bytes = g_bytes_new (NYAN, N_NYAN); - memory = g_bytes_get_data (bytes); + memory = g_bytes_get_data (bytes, NULL); data = g_bytes_unref_to_data (bytes, &size); g_assert (data == memory); g_assert_cmpuint (size, ==, N_NYAN); @@ -220,13 +228,14 @@ test_to_data_two_refs (void) /* Memory copied: two references */ bytes = g_bytes_new (NYAN, N_NYAN); bytes = g_bytes_ref (bytes); - memory = g_bytes_get_data (bytes); + memory = g_bytes_get_data (bytes, NULL); data = g_bytes_unref_to_data (bytes, &size); g_assert (data != memory); g_assert_cmpuint (size, ==, N_NYAN); g_assert (memcmp (data, NYAN, N_NYAN) == 0); g_free (data); - g_assert (g_bytes_get_data (bytes) == memory); + g_assert (g_bytes_get_data (bytes, &size) == memory); + g_assert_cmpuint (size, ==, N_NYAN); g_assert_cmpuint (g_bytes_get_size (bytes), ==, N_NYAN); g_bytes_unref (bytes); } @@ -240,7 +249,7 @@ test_to_data_non_malloc (void) /* Memory copied: non malloc memory */ bytes = g_bytes_new_static (NYAN, N_NYAN); - g_assert (g_bytes_get_data (bytes) == NYAN); + g_assert (g_bytes_get_data (bytes, NULL) == NYAN); data = g_bytes_unref_to_data (bytes, &size); g_assert (data != (gpointer)NYAN); g_assert_cmpuint (size, ==, N_NYAN); @@ -257,7 +266,7 @@ test_to_array_transferred (void) /* Memory transferred: one reference, and allocated with g_malloc */ bytes = g_bytes_new (NYAN, N_NYAN); - memory = g_bytes_get_data (bytes); + memory = g_bytes_get_data (bytes, NULL); array = g_bytes_unref_to_array (bytes); g_assert (array != NULL); g_assert (array->data == memory); @@ -272,18 +281,20 @@ test_to_array_two_refs (void) gconstpointer memory; GByteArray *array; GBytes *bytes; + gsize size; /* Memory copied: two references */ bytes = g_bytes_new (NYAN, N_NYAN); bytes = g_bytes_ref (bytes); - memory = g_bytes_get_data (bytes); + memory = g_bytes_get_data (bytes, NULL); array = g_bytes_unref_to_array (bytes); g_assert (array != NULL); g_assert (array->data != memory); g_assert_cmpuint (array->len, ==, N_NYAN); g_assert (memcmp (array->data, NYAN, N_NYAN) == 0); g_byte_array_unref (array); - g_assert (g_bytes_get_data (bytes) == memory); + g_assert (g_bytes_get_data (bytes, &size) == memory); + g_assert_cmpuint (size, ==, N_NYAN); g_assert_cmpuint (g_bytes_get_size (bytes), ==, N_NYAN); g_bytes_unref (bytes); } @@ -296,7 +307,7 @@ test_to_array_non_malloc (void) /* Memory copied: non malloc memory */ bytes = g_bytes_new_static (NYAN, N_NYAN); - g_assert (g_bytes_get_data (bytes) == NYAN); + g_assert (g_bytes_get_data (bytes, NULL) == NYAN); array = g_bytes_unref_to_array (bytes); g_assert (array != NULL); g_assert (array->data != (gpointer)NYAN); -- cgit v1.2.1