[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / glib / gmappedfile.c
index 57af411..7c8702d 100644 (file)
@@ -14,9 +14,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "config.h"
 #include <sys/types.h> 
 #include <sys/stat.h> 
 #include <fcntl.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 #ifdef HAVE_MMAP
 #include <sys/mman.h>
 #endif
 
 #include "glibconfig.h"
 
+#ifdef G_OS_UNIX
+#include <unistd.h>
+#endif
+
 #ifdef G_OS_WIN32
 #include <windows.h>
 #include <io.h>
 
+#undef fstat
 #define fstat(a,b) _fstati64(a,b)
+#undef stat
 #define stat _stati64
 
 #ifndef S_ISREG
@@ -56,7 +57,6 @@
 #include "gstdio.h"
 #include "gstrfuncs.h"
 #include "gatomic.h"
-#include "gbufferprivate.h"
 
 #include "glibintl.h"
 
 #define MAP_FAILED ((void *) -1)
 #endif
 
-struct _GMappedFile 
+/**
+ * GMappedFile:
+ *
+ * The #GMappedFile represents a file mapping created with
+ * g_mapped_file_new(). It has only private members and should
+ * not be accessed directly.
+ */
+
+struct _GMappedFile
 {
   gchar *contents;
   gsize  length;
@@ -80,16 +88,6 @@ struct _GMappedFile
 #endif
 };
 
-/* Make sure the layout of GMappedFile is the same as GBuffer's */
-G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, contents) ==
-                 G_STRUCT_OFFSET (GBuffer, data));
-G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, length) ==
-                 G_STRUCT_OFFSET (GBuffer, size));
-G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, ref_count) ==
-                 G_STRUCT_OFFSET (GBuffer, ref_count));
-G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, free_func) ==
-                 G_STRUCT_OFFSET (GBuffer, free_func));
-
 static void
 g_mapped_file_destroy (GMappedFile *file)
 {
@@ -235,7 +233,7 @@ mapped_file_new_from_fd (int           fd,
  * size 0 (e.g. device files such as /dev/null), @error will be set
  * to the #GFileError value #G_FILE_ERROR_INVAL.
  *
- * Return value: a newly allocated #GMappedFile which must be unref'd
+ * Returns: a newly allocated #GMappedFile which must be unref'd
  *    with g_mapped_file_unref(), or %NULL if the mapping failed.
  *
  * Since: 2.8
@@ -293,10 +291,10 @@ g_mapped_file_new (const gchar  *filename,
  * will not be modified, or if all modifications of the file are done
  * atomically (e.g. using g_file_set_contents()).
  *
- * Return value: a newly allocated #GMappedFile which must be unref'd
+ * Returns: a newly allocated #GMappedFile which must be unref'd
  *    with g_mapped_file_unref(), or %NULL if the mapping failed.
  *
- * Since: 2.30
+ * Since: 2.32
  */
 GMappedFile *
 g_mapped_file_new_from_fd (gint          fd,
@@ -370,7 +368,7 @@ g_mapped_file_free (GMappedFile *file)
  * Increments the reference count of @file by one.  It is safe to call
  * this function from any thread.
  *
- * Return value: the passed in #GMappedFile.
+ * Returns: the passed in #GMappedFile.
  *
  * Since: 2.22
  **/
@@ -403,3 +401,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));
+}