[glib/wip/mapped-bytes: 2/4] gbytes: Make GMappedFile a GBytes



commit b6a6bf231038c5c4ef7aa73eabe8157396f76638
Author: Christian Persch <chpe gnome org>
Date:   Wed Feb 15 18:08:00 2012 +0100

    gbytes: Make GMappedFile a GBytes
    
    Make the GMappedFile constructors return a GBytes that just wraps the
    mapped data. Deprecate the GMappedFile accessors and make them just forward
    to the corresponding GBytes accessors.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=666854

 glib/gbytes.c      |    4 +-
 glib/gmappedfile.c |   57 ++++++++++++++++++++++-----------------------------
 glib/gmappedfile.h |    8 +++++-
 3 files changed, 33 insertions(+), 36 deletions(-)
---
diff --git a/glib/gbytes.c b/glib/gbytes.c
index 16a04f5..0dfc4cb 100644
--- a/glib/gbytes.c
+++ b/glib/gbytes.c
@@ -50,8 +50,8 @@
  *
  * A #GBytes can come from many different origins that may have
  * different procedures for freeing the memory region.  Examples are
- * memory from g_malloc(), from memory slices, from a #GMappedFile or
- * memory from other allocators.
+ * memory from g_malloc(), from memory slices, memory from other allocators,
+ * or from a mapped file using g_mapped_file_new() and g_mapped_file_new_from_fd().
  *
  * #GBytes work well as keys in #GHashTable. Use g_bytes_equal() and
  * g_bytes_hash() as parameters to g_hash_table_new() or g_hash_table_new_full().
diff --git a/glib/gmappedfile.c b/glib/gmappedfile.c
index a303a36..1f63b58 100644
--- a/glib/gmappedfile.c
+++ b/glib/gmappedfile.c
@@ -76,19 +76,17 @@
  * not be accessed directly.
  */
 
-struct _GMappedFile
+typedef struct 
 {
   gchar *contents;
   gsize  length;
-  gpointer free_func;
-  int    ref_count;
 #ifdef G_OS_WIN32
   HANDLE mapping;
 #endif
-};
+} GMappedFileData;
 
 static void
-g_mapped_file_destroy (GMappedFile *file)
+g_mapped_file_destroy (GMappedFileData *file)
 {
   if (file->length)
     {
@@ -101,7 +99,7 @@ g_mapped_file_destroy (GMappedFile *file)
 #endif
     }
 
-  g_slice_free (GMappedFile, file);
+  g_slice_free (GMappedFileData, file);
 }
 
 static GMappedFile*
@@ -110,12 +108,10 @@ mapped_file_new_from_fd (int           fd,
                          const gchar  *filename,
                          GError      **error)
 {
-  GMappedFile *file;
+  GMappedFileData *file;
   struct stat st;
 
-  file = g_slice_new0 (GMappedFile);
-  file->ref_count = 1;
-  file->free_func = g_mapped_file_destroy;
+  file = g_slice_new0 (GMappedFileData);
 
   if (fstat (fd, &st) == -1)
     {
@@ -141,9 +137,8 @@ mapped_file_new_from_fd (int           fd,
    */
   if (st.st_size == 0 && S_ISREG (st.st_mode))
     {
-      file->length = 0;
-      file->contents = NULL;
-      return file;
+      g_slice_free (GMappedFileData, file);
+      return (GMappedFile *) g_bytes_new_static (NULL, 0);
     }
 
   file->contents = MAP_FAILED;
@@ -182,7 +177,6 @@ mapped_file_new_from_fd (int           fd,
     }
 #endif
 
-  
   if (file->contents == MAP_FAILED)
     {
       int save_errno = errno;
@@ -201,10 +195,12 @@ mapped_file_new_from_fd (int           fd,
       goto out;
     }
 
-  return file;
+  return (GMappedFile *) g_bytes_new_with_free_func (file->contents, file->length,
+                                                     (GDestroyNotify) g_mapped_file_destroy,
+                                                     file);
 
  out:
-  g_slice_free (GMappedFile, file);
+  g_slice_free (GMappedFileData, file);
 
   return NULL;
 }
@@ -312,13 +308,13 @@ g_mapped_file_new_from_fd (gint          fd,
  * Returns: the length of the contents of @file.
  *
  * Since: 2.8
+ *
+ * Deprecated: 2.32: Use g_bytes_get_size() instead.
  */
 gsize
 g_mapped_file_get_length (GMappedFile *file)
 {
-  g_return_val_if_fail (file != NULL, 0);
-
-  return file->length;
+  return g_bytes_get_size ((GBytes *) file);
 }
 
 /**
@@ -335,13 +331,13 @@ g_mapped_file_get_length (GMappedFile *file)
  * Returns: the contents of @file, or %NULL.
  *
  * Since: 2.8
+ * 
+ * Deprecated: 2.32: Use g_bytes_get_data() instead.
  */
 gchar *
 g_mapped_file_get_contents (GMappedFile *file)
 {
-  g_return_val_if_fail (file != NULL, NULL);
-
-  return file->contents;
+  return (gchar *) g_bytes_get_data ((GBytes *) file, NULL);
 }
 
 /**
@@ -357,7 +353,7 @@ g_mapped_file_get_contents (GMappedFile *file)
 void
 g_mapped_file_free (GMappedFile *file)
 {
-  g_mapped_file_unref (file);
+  g_bytes_unref ((GBytes *) file);
 }
 
 /**
@@ -370,15 +366,13 @@ g_mapped_file_free (GMappedFile *file)
  * Return value: the passed in #GMappedFile.
  *
  * Since: 2.22
+ *
+ * Deprecated: 2.32: Use g_bytes_unref() instead.
  **/
 GMappedFile *
 g_mapped_file_ref (GMappedFile *file)
 {
-  g_return_val_if_fail (file != NULL, NULL);
-
-  g_atomic_int_inc (&file->ref_count);
-
-  return file;
+  return (GMappedFile *) g_bytes_ref ((GBytes *) file);
 }
 
 /**
@@ -391,12 +385,11 @@ g_mapped_file_ref (GMappedFile *file)
  * It is safe to call this function from any thread.
  *
  * Since 2.22
+ *
+ * Deprecated: 2.32: Use g_bytes_unref() instead.
  **/
 void
 g_mapped_file_unref (GMappedFile *file)
 {
-  g_return_if_fail (file != NULL);
-
-  if (g_atomic_int_dec_and_test (&file->ref_count))
-    g_mapped_file_destroy (file);
+  g_bytes_unref ((GBytes *) file);
 }
diff --git a/glib/gmappedfile.h b/glib/gmappedfile.h
index 52ba31d..4f4ac59 100644
--- a/glib/gmappedfile.h
+++ b/glib/gmappedfile.h
@@ -39,12 +39,16 @@ GMappedFile *g_mapped_file_new          (const gchar  *filename,
 GMappedFile *g_mapped_file_new_from_fd  (gint          fd,
 					 gboolean      writable,
 					 GError      **error) G_GNUC_MALLOC;
+
+GLIB_DEPRECATED_FOR(g_bytes_get_size)
 gsize        g_mapped_file_get_length   (GMappedFile  *file);
+GLIB_DEPRECATED_FOR(g_bytes_get_data)
 gchar       *g_mapped_file_get_contents (GMappedFile  *file);
+GLIB_DEPRECATED_FOR(g_bytes_ref)
 GMappedFile *g_mapped_file_ref          (GMappedFile  *file);
+GLIB_DEPRECATED_FOR(g_bytes_unref)
 void         g_mapped_file_unref        (GMappedFile  *file);
-
-GLIB_DEPRECATED_FOR(g_mapped_file_unref)
+GLIB_DEPRECATED_FOR(g_bytes_unref)
 void         g_mapped_file_free         (GMappedFile  *file);
 
 G_END_DECLS



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