gst/gstregistryxml.c: If compiling against GLib-2.8 or newer, try to read the registr...
authorTim-Philipp Müller <tim@centricular.net>
Thu, 23 Mar 2006 11:54:51 +0000 (11:54 +0000)
committerTim-Philipp Müller <tim@centricular.net>
Thu, 23 Mar 2006 11:54:51 +0000 (11:54 +0000)
Original commit message from CVS:
* gst/gstregistryxml.c: (gst_registry_xml_read_cache):
If compiling against GLib-2.8 or newer, try to read the
registry file using GMappedFile first before falling back
to fopen() + fread() (#332151).

ChangeLog
gst/gstregistryxml.c

index 4a3d73e..784f157 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2006-03-23  Tim-Philipp Müller  <tim at centricular dot net>
+
+       * gst/gstregistryxml.c: (gst_registry_xml_read_cache):
+         If compiling against GLib-2.8 or newer, try to read the
+         registry file using GMappedFile first before falling back
+         to fopen() + fread() (#332151).
+
 2006-03-22  Wim Taymans  <wim@fluendo.com>
 
        * gst/gstinfo.c: (gst_debug_set_active),
index 3689b0d..1e877de 100644 (file)
@@ -466,12 +466,15 @@ load_plugin (xmlTextReaderPtr reader, GList ** feature_list)
 gboolean
 gst_registry_xml_read_cache (GstRegistry * registry, const char *location)
 {
+#if GLIB_CHECK_VERSION(2,8,0)
+  GMappedFile *mapped = NULL;
+#endif
   GTimer *timer;
   gdouble seconds;
-  xmlTextReaderPtr reader;
+  xmlTextReaderPtr reader = NULL;
   int ret;
   gboolean in_registry = FALSE;
-  FILE *file;
+  FILE *file = NULL;
 
   /* make sure these types exist */
   GST_TYPE_ELEMENT_FACTORY;
@@ -480,17 +483,31 @@ gst_registry_xml_read_cache (GstRegistry * registry, const char *location)
 
   timer = g_timer_new ();
 
-  file = fopen (location, "r");
-  if (file == NULL) {
-    g_timer_destroy (timer);
-    return FALSE;
+#if GLIB_CHECK_VERSION(2,8,0)
+  mapped = g_mapped_file_new (location, FALSE, NULL);
+  if (mapped) {
+    reader = xmlReaderForMemory (g_mapped_file_get_contents (mapped),
+        g_mapped_file_get_length (mapped), NULL, NULL, 0);
+    if (reader == NULL) {
+      g_mapped_file_free (mapped);
+      mapped = NULL;
+    }
   }
+#endif
 
-  reader = xmlReaderForFd (fileno (file), NULL, NULL, 0);
-  if (!reader) {
-    fclose (file);
-    g_timer_destroy (timer);
-    return FALSE;
+  if (reader == NULL) {
+    file = fopen (location, "r");
+    if (file == NULL) {
+      g_timer_destroy (timer);
+      return FALSE;
+    }
+
+    reader = xmlReaderForFd (fileno (file), NULL, NULL, 0);
+    if (!reader) {
+      fclose (file);
+      g_timer_destroy (timer);
+      return FALSE;
+    }
   }
 
   while ((ret = xmlTextReaderRead (reader)) == 1) {
@@ -523,7 +540,12 @@ gst_registry_xml_read_cache (GstRegistry * registry, const char *location)
   xmlFreeTextReader (reader);
   if (ret != 0) {
     GST_ERROR ("parsing registry cache: %s", location);
-    fclose (file);
+#if GLIB_CHECK_VERSION(2,8,0)
+    if (mapped)
+      g_mapped_file_free (mapped);
+#endif
+    if (file)
+      fclose (file);
     g_timer_destroy (timer);
     return FALSE;
   }
@@ -534,7 +556,13 @@ gst_registry_xml_read_cache (GstRegistry * registry, const char *location)
 
   GST_INFO ("loaded %s in %f seconds", location, seconds);
 
-  fclose (file);
+#if GLIB_CHECK_VERSION(2,8,0)
+  if (mapped)
+    g_mapped_file_free (mapped);
+#endif
+
+  if (file)
+    fclose (file);
 
   return TRUE;
 }