mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-11-07 11:33:18 +01:00
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
This commit is contained in:
@@ -212,19 +212,23 @@ g_bytes_new_from_bytes (GBytes *bytes,
|
|||||||
/**
|
/**
|
||||||
* g_bytes_get_data:
|
* g_bytes_get_data:
|
||||||
* @bytes: a #GBytes
|
* @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.
|
* 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.
|
* 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
|
* Since: 2.32
|
||||||
*/
|
*/
|
||||||
gconstpointer
|
gconstpointer
|
||||||
g_bytes_get_data (GBytes *bytes)
|
g_bytes_get_data (GBytes *bytes,
|
||||||
|
gsize *size)
|
||||||
{
|
{
|
||||||
g_return_val_if_fail (bytes != NULL, NULL);
|
g_return_val_if_fail (bytes != NULL, NULL);
|
||||||
|
if (size)
|
||||||
|
*size = bytes->size;
|
||||||
return bytes->data;
|
return bytes->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,8 @@ GBytes * g_bytes_new_from_bytes (GBytes *bytes,
|
|||||||
gsize offset,
|
gsize offset,
|
||||||
gsize length);
|
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);
|
gsize g_bytes_get_size (GBytes *bytes);
|
||||||
|
|
||||||
|
|||||||
@@ -395,8 +395,8 @@ g_time_zone_new (const gchar *identifier)
|
|||||||
|
|
||||||
if (tz->zoneinfo != NULL)
|
if (tz->zoneinfo != NULL)
|
||||||
{
|
{
|
||||||
const struct tzhead *header = g_bytes_get_data (tz->zoneinfo);
|
gsize size;
|
||||||
gsize size = g_bytes_get_size (tz->zoneinfo);
|
const struct tzhead *header = g_bytes_get_data (tz->zoneinfo, &size);
|
||||||
|
|
||||||
/* we only bother to support version 2 */
|
/* we only bother to support version 2 */
|
||||||
if (size < sizeof (struct tzhead) || memcmp (header, "TZif2", 5))
|
if (size < sizeof (struct tzhead) || memcmp (header, "TZif2", 5))
|
||||||
|
|||||||
@@ -448,7 +448,7 @@ g_variant_ensure_serialised (GVariant *value)
|
|||||||
g_variant_release_children (value);
|
g_variant_release_children (value);
|
||||||
|
|
||||||
bytes = g_bytes_new_take (data, value->size);
|
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->contents.serialised.bytes = bytes;
|
||||||
value->state |= STATE_SERIALISED;
|
value->state |= STATE_SERIALISED;
|
||||||
}
|
}
|
||||||
@@ -529,8 +529,7 @@ g_variant_new_from_bytes (const GVariantType *type,
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
value->contents.serialised.data = g_bytes_get_data (bytes);
|
value->contents.serialised.data = g_bytes_get_data (bytes, &value->size);
|
||||||
value->size = g_bytes_get_size (bytes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
|
|||||||
@@ -781,6 +781,7 @@ byte_array_free_to_bytes (void)
|
|||||||
GByteArray *gbarray;
|
GByteArray *gbarray;
|
||||||
gpointer memory;
|
gpointer memory;
|
||||||
GBytes *bytes;
|
GBytes *bytes;
|
||||||
|
gsize size;
|
||||||
|
|
||||||
gbarray = g_byte_array_new ();
|
gbarray = g_byte_array_new ();
|
||||||
g_byte_array_append (gbarray, (guint8 *)"woooweeewow", 11);
|
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);
|
bytes = g_byte_array_free_to_bytes (gbarray);
|
||||||
g_assert (bytes != NULL);
|
g_assert (bytes != NULL);
|
||||||
g_assert_cmpuint (g_bytes_get_size (bytes), ==, 11);
|
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);
|
g_bytes_unref (bytes);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,13 +25,15 @@ test_new (void)
|
|||||||
{
|
{
|
||||||
const gchar *data;
|
const gchar *data;
|
||||||
GBytes *bytes;
|
GBytes *bytes;
|
||||||
|
gsize size;
|
||||||
|
|
||||||
data = "test";
|
data = "test";
|
||||||
bytes = g_bytes_new (data, 4);
|
bytes = g_bytes_new (data, 4);
|
||||||
g_assert (bytes != NULL);
|
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_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);
|
g_bytes_unref (bytes);
|
||||||
}
|
}
|
||||||
@@ -41,11 +43,13 @@ test_new_take (void)
|
|||||||
{
|
{
|
||||||
gchar *data;
|
gchar *data;
|
||||||
GBytes *bytes;
|
GBytes *bytes;
|
||||||
|
gsize size;
|
||||||
|
|
||||||
data = g_strdup ("test");
|
data = g_strdup ("test");
|
||||||
bytes = g_bytes_new_take (data, 4);
|
bytes = g_bytes_new_take (data, 4);
|
||||||
g_assert (bytes != NULL);
|
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_cmpuint (g_bytes_get_size (bytes), ==, 4);
|
||||||
|
|
||||||
g_bytes_unref (bytes);
|
g_bytes_unref (bytes);
|
||||||
@@ -56,11 +60,13 @@ test_new_static (void)
|
|||||||
{
|
{
|
||||||
const gchar *data;
|
const gchar *data;
|
||||||
GBytes *bytes;
|
GBytes *bytes;
|
||||||
|
gsize size;
|
||||||
|
|
||||||
data = "test";
|
data = "test";
|
||||||
bytes = g_bytes_new_static (data, 4);
|
bytes = g_bytes_new_static (data, 4);
|
||||||
g_assert (bytes != NULL);
|
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_cmpuint (g_bytes_get_size (bytes), ==, 4);
|
||||||
|
|
||||||
g_bytes_unref (bytes);
|
g_bytes_unref (bytes);
|
||||||
@@ -77,11 +83,11 @@ test_new_from_bytes (void)
|
|||||||
sub = g_bytes_new_from_bytes (bytes, 10, 4);
|
sub = g_bytes_new_from_bytes (bytes, 10, 4);
|
||||||
|
|
||||||
g_assert (sub != NULL);
|
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_assert (g_bytes_get_size (sub) == 4);
|
||||||
g_bytes_unref (bytes);
|
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);
|
g_bytes_unref (sub);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,12 +105,14 @@ test_new_with_free_func (void)
|
|||||||
GBytes *bytes;
|
GBytes *bytes;
|
||||||
gchar *data;
|
gchar *data;
|
||||||
gint count = 0;
|
gint count = 0;
|
||||||
|
gsize size;
|
||||||
|
|
||||||
data = "test";
|
data = "test";
|
||||||
bytes = g_bytes_new_with_free_func (data, 4, on_destroy_increment, &count);
|
bytes = g_bytes_new_with_free_func (data, 4, on_destroy_increment, &count);
|
||||||
g_assert (bytes != NULL);
|
g_assert (bytes != NULL);
|
||||||
g_assert_cmpint (count, ==, 0);
|
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_assert_cmpuint (g_bytes_get_size (bytes), ==, 4);
|
||||||
|
|
||||||
g_bytes_unref (bytes);
|
g_bytes_unref (bytes);
|
||||||
@@ -201,7 +209,7 @@ test_to_data_transferred (void)
|
|||||||
|
|
||||||
/* Memory transferred: one reference, and allocated with g_malloc */
|
/* Memory transferred: one reference, and allocated with g_malloc */
|
||||||
bytes = g_bytes_new (NYAN, N_NYAN);
|
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);
|
data = g_bytes_unref_to_data (bytes, &size);
|
||||||
g_assert (data == memory);
|
g_assert (data == memory);
|
||||||
g_assert_cmpuint (size, ==, N_NYAN);
|
g_assert_cmpuint (size, ==, N_NYAN);
|
||||||
@@ -220,13 +228,14 @@ test_to_data_two_refs (void)
|
|||||||
/* Memory copied: two references */
|
/* Memory copied: two references */
|
||||||
bytes = g_bytes_new (NYAN, N_NYAN);
|
bytes = g_bytes_new (NYAN, N_NYAN);
|
||||||
bytes = g_bytes_ref (bytes);
|
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);
|
data = g_bytes_unref_to_data (bytes, &size);
|
||||||
g_assert (data != memory);
|
g_assert (data != memory);
|
||||||
g_assert_cmpuint (size, ==, N_NYAN);
|
g_assert_cmpuint (size, ==, N_NYAN);
|
||||||
g_assert (memcmp (data, NYAN, N_NYAN) == 0);
|
g_assert (memcmp (data, NYAN, N_NYAN) == 0);
|
||||||
g_free (data);
|
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_assert_cmpuint (g_bytes_get_size (bytes), ==, N_NYAN);
|
||||||
g_bytes_unref (bytes);
|
g_bytes_unref (bytes);
|
||||||
}
|
}
|
||||||
@@ -240,7 +249,7 @@ test_to_data_non_malloc (void)
|
|||||||
|
|
||||||
/* Memory copied: non malloc memory */
|
/* Memory copied: non malloc memory */
|
||||||
bytes = g_bytes_new_static (NYAN, N_NYAN);
|
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);
|
data = g_bytes_unref_to_data (bytes, &size);
|
||||||
g_assert (data != (gpointer)NYAN);
|
g_assert (data != (gpointer)NYAN);
|
||||||
g_assert_cmpuint (size, ==, N_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 */
|
/* Memory transferred: one reference, and allocated with g_malloc */
|
||||||
bytes = g_bytes_new (NYAN, N_NYAN);
|
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);
|
array = g_bytes_unref_to_array (bytes);
|
||||||
g_assert (array != NULL);
|
g_assert (array != NULL);
|
||||||
g_assert (array->data == memory);
|
g_assert (array->data == memory);
|
||||||
@@ -272,18 +281,20 @@ test_to_array_two_refs (void)
|
|||||||
gconstpointer memory;
|
gconstpointer memory;
|
||||||
GByteArray *array;
|
GByteArray *array;
|
||||||
GBytes *bytes;
|
GBytes *bytes;
|
||||||
|
gsize size;
|
||||||
|
|
||||||
/* Memory copied: two references */
|
/* Memory copied: two references */
|
||||||
bytes = g_bytes_new (NYAN, N_NYAN);
|
bytes = g_bytes_new (NYAN, N_NYAN);
|
||||||
bytes = g_bytes_ref (bytes);
|
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);
|
array = g_bytes_unref_to_array (bytes);
|
||||||
g_assert (array != NULL);
|
g_assert (array != NULL);
|
||||||
g_assert (array->data != memory);
|
g_assert (array->data != memory);
|
||||||
g_assert_cmpuint (array->len, ==, N_NYAN);
|
g_assert_cmpuint (array->len, ==, N_NYAN);
|
||||||
g_assert (memcmp (array->data, NYAN, N_NYAN) == 0);
|
g_assert (memcmp (array->data, NYAN, N_NYAN) == 0);
|
||||||
g_byte_array_unref (array);
|
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_assert_cmpuint (g_bytes_get_size (bytes), ==, N_NYAN);
|
||||||
g_bytes_unref (bytes);
|
g_bytes_unref (bytes);
|
||||||
}
|
}
|
||||||
@@ -296,7 +307,7 @@ test_to_array_non_malloc (void)
|
|||||||
|
|
||||||
/* Memory copied: non malloc memory */
|
/* Memory copied: non malloc memory */
|
||||||
bytes = g_bytes_new_static (NYAN, N_NYAN);
|
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);
|
array = g_bytes_unref_to_array (bytes);
|
||||||
g_assert (array != NULL);
|
g_assert (array != NULL);
|
||||||
g_assert (array->data != (gpointer)NYAN);
|
g_assert (array->data != (gpointer)NYAN);
|
||||||
|
|||||||
Reference in New Issue
Block a user