1 /* GLIB - Library of useful routines for C programming
2 * gmappedfile.c: Simplified wrapper around the mmap() function.
4 * Copyright 2005 Matthias Clasen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
25 #include <sys/types.h>
35 #include "glibconfig.h"
41 #define fstat(a,b) _fstati64(a,b)
48 #include "gfileutils.h"
49 #include "gmappedfile.h"
51 #include "gmessages.h"
53 #include "gstrfuncs.h"
65 #define MAP_FAILED ((void *) -1)
79 /* Make sure the layout of GMappedFile is the same as GBuffer's */
80 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile
, contents
) ==
81 G_STRUCT_OFFSET (GBuffer
, data
));
82 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile
, length
) ==
83 G_STRUCT_OFFSET (GBuffer
, size
));
84 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile
, ref_count
) ==
85 G_STRUCT_OFFSET (GBuffer
, ref_count
));
86 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile
, free_func
) ==
87 G_STRUCT_OFFSET (GBuffer
, free_func
));
90 g_mapped_file_destroy (GMappedFile
*file
)
95 munmap (file
->contents
, file
->length
);
98 UnmapViewOfFile (file
->contents
);
99 CloseHandle (file
->mapping
);
103 g_slice_free (GMappedFile
, file
);
108 * @filename: The path of the file to load, in the GLib filename encoding
109 * @writable: whether the mapping should be writable
110 * @error: return location for a #GError, or %NULL
112 * Maps a file into memory. On UNIX, this is using the mmap() function.
114 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
115 * it is an error to modify the mapped buffer. Modifications to the buffer
116 * are not visible to other processes mapping the same file, and are not
117 * written back to the file.
119 * Note that modifications of the underlying file might affect the contents
120 * of the #GMappedFile. Therefore, mapping should only be used if the file
121 * will not be modified, or if all modifications of the file are done
122 * atomically (e.g. using g_file_set_contents()).
124 * Return value: a newly allocated #GMappedFile which must be unref'd
125 * with g_mapped_file_unref(), or %NULL if the mapping failed.
130 g_mapped_file_new (const gchar
*filename
,
138 g_return_val_if_fail (filename
!= NULL
, NULL
);
139 g_return_val_if_fail (!error
|| *error
== NULL
, NULL
);
141 fd
= g_open (filename
, (writable
? O_RDWR
: O_RDONLY
) | _O_BINARY
, 0);
144 int save_errno
= errno
;
145 gchar
*display_filename
= g_filename_display_name (filename
);
149 g_file_error_from_errno (save_errno
),
150 _("Failed to open file '%s': open() failed: %s"),
152 g_strerror (save_errno
));
153 g_free (display_filename
);
157 file
= g_slice_new0 (GMappedFile
);
159 file
->free_func
= g_mapped_file_destroy
;
161 if (fstat (fd
, &st
) == -1)
163 int save_errno
= errno
;
164 gchar
*display_filename
= g_filename_display_name (filename
);
168 g_file_error_from_errno (save_errno
),
169 _("Failed to get attributes of file '%s': fstat() failed: %s"),
171 g_strerror (save_errno
));
172 g_free (display_filename
);
179 file
->contents
= NULL
;
184 file
->contents
= MAP_FAILED
;
187 if (st
.st_size
> G_MAXSIZE
)
193 file
->length
= (gsize
) st
.st_size
;
194 file
->contents
= (gchar
*) mmap (NULL
, file
->length
,
195 writable
? PROT_READ
|PROT_WRITE
: PROT_READ
,
200 file
->length
= st
.st_size
;
201 file
->mapping
= CreateFileMapping ((HANDLE
) _get_osfhandle (fd
), NULL
,
202 writable
? PAGE_WRITECOPY
: PAGE_READONLY
,
205 if (file
->mapping
!= NULL
)
207 file
->contents
= MapViewOfFile (file
->mapping
,
208 writable
? FILE_MAP_COPY
: FILE_MAP_READ
,
211 if (file
->contents
== NULL
)
213 file
->contents
= MAP_FAILED
;
214 CloseHandle (file
->mapping
);
215 file
->mapping
= NULL
;
221 if (file
->contents
== MAP_FAILED
)
223 int save_errno
= errno
;
224 gchar
*display_filename
= g_filename_display_name (filename
);
228 g_file_error_from_errno (save_errno
),
229 _("Failed to map file '%s': mmap() failed: %s"),
231 g_strerror (save_errno
));
232 g_free (display_filename
);
241 g_slice_free (GMappedFile
, file
);
247 * g_mapped_file_get_length:
248 * @file: a #GMappedFile
250 * Returns the length of the contents of a #GMappedFile.
252 * Returns: the length of the contents of @file.
257 g_mapped_file_get_length (GMappedFile
*file
)
259 g_return_val_if_fail (file
!= NULL
, 0);
265 * g_mapped_file_get_contents:
266 * @file: a #GMappedFile
268 * Returns the contents of a #GMappedFile.
270 * Note that the contents may not be zero-terminated,
271 * even if the #GMappedFile is backed by a text file.
273 * If the file is empty then %NULL is returned.
275 * Returns: the contents of @file, or %NULL.
280 g_mapped_file_get_contents (GMappedFile
*file
)
282 g_return_val_if_fail (file
!= NULL
, NULL
);
284 return file
->contents
;
288 * g_mapped_file_free:
289 * @file: a #GMappedFile
291 * This call existed before #GMappedFile had refcounting and is currently
292 * exactly the same as g_mapped_file_unref().
295 * Deprecated:2.22: Use g_mapped_file_unref() instead.
298 g_mapped_file_free (GMappedFile
*file
)
300 g_mapped_file_unref (file
);
305 * @file: a #GMappedFile
307 * Increments the reference count of @file by one. It is safe to call
308 * this function from any thread.
310 * Return value: the passed in #GMappedFile.
315 g_mapped_file_ref (GMappedFile
*file
)
317 g_return_val_if_fail (file
!= NULL
, NULL
);
318 g_return_val_if_fail (file
->ref_count
> 0, file
);
320 g_atomic_int_inc (&file
->ref_count
);
326 * g_mapped_file_unref:
327 * @file: a #GMappedFile
329 * Decrements the reference count of @file by one. If the reference count
330 * drops to 0, unmaps the buffer of @file and frees it.
332 * It is safe to call this function from any thread.
337 g_mapped_file_unref (GMappedFile
*file
)
339 g_return_if_fail (file
!= NULL
);
340 g_return_if_fail (file
->ref_count
> 0);
342 if (g_atomic_int_dec_and_test (&file
->ref_count
))
343 g_mapped_file_destroy (file
);