Index: glib/gfileutils.c
===================================================================
RCS file: /cvs/gnome/glib/glib/gfileutils.c,v
retrieving revision 1.43
diff -u -r1.43 gfileutils.c
--- glib/gfileutils.c	14 Dec 2003 19:05:29 -0000	1.43
+++ glib/gfileutils.c	22 Jul 2004 22:57:27 -0000
@@ -36,6 +36,8 @@
 #include <fcntl.h>
 #include <stdlib.h>
 
+#include <sys/mman.h>
+
 #ifdef G_OS_WIN32
 #include <io.h>
 #ifndef F_OK
@@ -1174,3 +1176,81 @@
   return NULL;
 #endif
 }
+
+struct _GReadFile {
+  gboolean mmaped;
+  gchar *contents;
+  int length;
+};
+
+GReadFile *
+g_readfile_new (const gchar *filename, GError      **error)
+{
+  GReadFile *readfile;
+  int fd;
+  int ret;
+  struct stat st;
+  
+  readfile = g_new0(GReadFile, 1);
+
+  fd = open (filename, O_RDONLY);
+  if (fd == -1) {
+    g_free (readfile);
+    return NULL;
+  }
+
+  ret = fstat (fd, &st);
+  if (ret == -1) {
+    g_free (readfile);
+    close (fd);
+    return NULL;
+  }
+
+  if (!S_ISREG(st.st_mode)) {
+    g_free (readfile);
+    close (fd);
+    return NULL;
+  }
+  readfile->length = st.st_size;
+
+  readfile->contents = mmap (NULL, readfile->length, PROT_READ, MAP_PRIVATE,
+      fd, 0);
+  close (fd);
+  if (readfile->contents != MAP_FAILED) {
+    readfile->mmaped = TRUE;
+  } else {
+    readfile->mmaped = FALSE;
+    ret = g_file_get_contents(filename, &readfile->contents, &readfile->length,
+        error);
+    if (!ret) {
+      g_free (readfile);
+      return NULL;
+    }
+  }
+
+  return readfile;
+}
+
+int
+g_readfile_get_length (GReadFile *readfile)
+{
+  return readfile->length;
+}
+
+const gchar *
+g_readfile_get_contents (GReadFile *readfile)
+{
+  return readfile->contents;
+}
+
+void
+g_readfile_free (GReadFile *readfile)
+{
+  if (readfile->mmaped) {
+    munmap (readfile->contents, readfile->length);
+  } else {
+    g_free (readfile->contents);
+  }
+  g_free(readfile);
+}
+
Index: glib/gfileutils.h
===================================================================
RCS file: /cvs/gnome/glib/glib/gfileutils.h,v
retrieving revision 1.12
diff -u -r1.12 gfileutils.h
--- glib/gfileutils.h	29 Jul 2003 22:31:39 -0000	1.12
+++ glib/gfileutils.h	22 Jul 2004 22:57:27 -0000
@@ -27,6 +27,8 @@
 
 #define G_FILE_ERROR g_file_error_quark ()
 
+typedef struct _GReadFile GReadFile;
+
 typedef enum
 {
   G_FILE_ERROR_EXIST,
@@ -95,6 +97,12 @@
 gchar *g_build_filename (const gchar *first_element,
 			 ...);
 
+GReadFile *g_readfile_new (const gchar *filename,
+                           GError      **error);
+int g_readfile_get_length (GReadFile *readfile);
+const gchar *g_readfile_get_contents (GReadFile *readfile);
+void g_readfile_free (GReadFile *);
+
 G_END_DECLS
 
 #endif /* __G_FILEUTILS_H__ */
