[glib] GMappedFile: Add g_mapped_file_get_bytes()



commit 056d39c9f7e058397beaed7b4f5637857510b4e2
Author: Colin Walters <walters verbum org>
Date:   Tue May 29 18:54:58 2012 -0400

    GMappedFile: Add g_mapped_file_get_bytes()
    
    This is yet another API that has a data/length/refcount combination
    that one might often want to turn into a GBytes.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=677065

 docs/reference/glib/glib-sections.txt |    1 +
 glib/glib.symbols                     |    1 +
 glib/gmappedfile.c                    |   24 ++++++++++++++++++++++++
 glib/gmappedfile.h                    |    2 ++
 glib/tests/mappedfile.c               |   19 +++++++++++++++++++
 5 files changed, 47 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 4c0071c..a7f0a65 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -1225,6 +1225,7 @@ g_mapped_file_unref
 g_mapped_file_free
 g_mapped_file_get_length
 g_mapped_file_get_contents
+g_mapped_file_get_bytes
 
 <SUBSECTION>
 g_open
diff --git a/glib/glib.symbols b/glib/glib.symbols
index dbd9246..49c6442 100644
--- a/glib/glib.symbols
+++ b/glib/glib.symbols
@@ -665,6 +665,7 @@ g_mapped_file_new
 g_mapped_file_new_from_fd
 g_mapped_file_get_length
 g_mapped_file_get_contents
+g_mapped_file_get_bytes
 g_mapped_file_ref
 g_mapped_file_unref
 g_mapped_file_free
diff --git a/glib/gmappedfile.c b/glib/gmappedfile.c
index b541870..a0bd4ca 100644
--- a/glib/gmappedfile.c
+++ b/glib/gmappedfile.c
@@ -400,3 +400,27 @@ g_mapped_file_unref (GMappedFile *file)
   if (g_atomic_int_dec_and_test (&file->ref_count))
     g_mapped_file_destroy (file);
 }
+
+/**
+ * g_mapped_file_get_bytes:
+ * @file: a #GMappedFile
+ *
+ * Creates a new #GBytes which references the data mapped from @file.
+ * The mapped contents of the file must not be modified after creating this
+ * bytes object, because a #GBytes should be immutable.
+ *
+ * Returns: (transfer full): A newly allocated #GBytes referencing data
+ *     from @file
+ *
+ * Since: 2.34
+ **/
+GBytes *
+g_mapped_file_get_bytes (GMappedFile *file)
+{
+  g_return_val_if_fail (file != NULL, NULL);
+
+  return g_bytes_new_with_free_func (file->contents,
+				     file->length,
+				     (GDestroyNotify) g_mapped_file_unref,
+				     g_mapped_file_ref (file));
+}
diff --git a/glib/gmappedfile.h b/glib/gmappedfile.h
index 52ba31d..cf122e4 100644
--- a/glib/gmappedfile.h
+++ b/glib/gmappedfile.h
@@ -41,6 +41,8 @@ GMappedFile *g_mapped_file_new_from_fd  (gint          fd,
 					 GError      **error) G_GNUC_MALLOC;
 gsize        g_mapped_file_get_length   (GMappedFile  *file);
 gchar       *g_mapped_file_get_contents (GMappedFile  *file);
+GLIB_AVAILABLE_IN_2_34
+GBytes *     g_mapped_file_get_bytes    (GMappedFile  *file);
 GMappedFile *g_mapped_file_ref          (GMappedFile  *file);
 void         g_mapped_file_unref        (GMappedFile  *file);
 
diff --git a/glib/tests/mappedfile.c b/glib/tests/mappedfile.c
index 5387a64..f61f44e 100644
--- a/glib/tests/mappedfile.c
+++ b/glib/tests/mappedfile.c
@@ -147,6 +147,24 @@ test_writable_fd (void)
 
 }
 
+static void
+test_gbytes (void)
+{
+  GMappedFile *file;
+  GBytes *bytes;
+  GError *error;
+
+  error = NULL;
+  file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
+  g_assert_no_error (error);
+
+  bytes = g_mapped_file_get_bytes (file);
+  g_mapped_file_unref (file);
+
+  g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
+  g_bytes_unref (bytes);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -158,6 +176,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
   g_test_add_func ("/mappedfile/writable", test_writable);
   g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
+  g_test_add_func ("/mappedfile/gbytes", test_gbytes);
 
   return g_test_run ();
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]