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"
66 #define MAP_FAILED ((void *) -1)
80 /* Make sure the layout of GMappedFile is the same as GBuffer's */
81 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, contents) ==
82 G_STRUCT_OFFSET (GBuffer, data));
83 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, length) ==
84 G_STRUCT_OFFSET (GBuffer, size));
85 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, ref_count) ==
86 G_STRUCT_OFFSET (GBuffer, ref_count));
87 G_STATIC_ASSERT (G_STRUCT_OFFSET (GMappedFile, free_func) ==
88 G_STRUCT_OFFSET (GBuffer, free_func));
91 g_mapped_file_destroy (GMappedFile *file)
96 munmap (file->contents, file->length);
99 UnmapViewOfFile (file->contents);
100 CloseHandle (file->mapping);
104 g_slice_free (GMappedFile, file);
109 * @filename: The path of the file to load, in the GLib filename encoding
110 * @writable: whether the mapping should be writable
111 * @error: return location for a #GError, or %NULL
113 * Maps a file into memory. On UNIX, this is using the mmap() function.
115 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
116 * it is an error to modify the mapped buffer. Modifications to the buffer
117 * are not visible to other processes mapping the same file, and are not
118 * written back to the file.
120 * Note that modifications of the underlying file might affect the contents
121 * of the #GMappedFile. Therefore, mapping should only be used if the file
122 * will not be modified, or if all modifications of the file are done
123 * atomically (e.g. using g_file_set_contents()).
125 * Return value: a newly allocated #GMappedFile which must be unref'd
126 * with g_mapped_file_unref(), or %NULL if the mapping failed.
131 g_mapped_file_new (const gchar *filename,
139 g_return_val_if_fail (filename != NULL, NULL);
140 g_return_val_if_fail (!error || *error == NULL, NULL);
142 fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
145 int save_errno = errno;
146 gchar *display_filename = g_filename_display_name (filename);
150 g_file_error_from_errno (save_errno),
151 _("Failed to open file '%s': open() failed: %s"),
153 g_strerror (save_errno));
154 g_free (display_filename);
158 file = g_slice_new0 (GMappedFile);
160 file->free_func = g_mapped_file_destroy;
162 if (fstat (fd, &st) == -1)
164 int save_errno = errno;
165 gchar *display_filename = g_filename_display_name (filename);
169 g_file_error_from_errno (save_errno),
170 _("Failed to get attributes of file '%s': fstat() failed: %s"),
172 g_strerror (save_errno));
173 g_free (display_filename);
180 file->contents = NULL;
185 file->contents = MAP_FAILED;
188 if (st.st_size > G_MAXSIZE)
194 file->length = (gsize) st.st_size;
195 file->contents = (gchar *) mmap (NULL, file->length,
196 writable ? PROT_READ|PROT_WRITE : PROT_READ,
201 file->length = st.st_size;
202 file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
203 writable ? PAGE_WRITECOPY : PAGE_READONLY,
206 if (file->mapping != NULL)
208 file->contents = MapViewOfFile (file->mapping,
209 writable ? FILE_MAP_COPY : FILE_MAP_READ,
212 if (file->contents == NULL)
214 file->contents = MAP_FAILED;
215 CloseHandle (file->mapping);
216 file->mapping = NULL;
222 if (file->contents == MAP_FAILED)
224 int save_errno = errno;
225 gchar *display_filename = g_filename_display_name (filename);
229 g_file_error_from_errno (save_errno),
230 _("Failed to map file '%s': mmap() failed: %s"),
232 g_strerror (save_errno));
233 g_free (display_filename);
242 g_slice_free (GMappedFile, file);
248 * g_mapped_file_get_length:
249 * @file: a #GMappedFile
251 * Returns the length of the contents of a #GMappedFile.
253 * Returns: the length of the contents of @file.
258 g_mapped_file_get_length (GMappedFile *file)
260 g_return_val_if_fail (file != NULL, 0);
266 * g_mapped_file_get_contents:
267 * @file: a #GMappedFile
269 * Returns the contents of a #GMappedFile.
271 * Note that the contents may not be zero-terminated,
272 * even if the #GMappedFile is backed by a text file.
274 * If the file is empty then %NULL is returned.
276 * Returns: the contents of @file, or %NULL.
281 g_mapped_file_get_contents (GMappedFile *file)
283 g_return_val_if_fail (file != NULL, NULL);
285 return file->contents;
289 * g_mapped_file_free:
290 * @file: a #GMappedFile
292 * This call existed before #GMappedFile had refcounting and is currently
293 * exactly the same as g_mapped_file_unref().
296 * Deprecated:2.22: Use g_mapped_file_unref() instead.
299 g_mapped_file_free (GMappedFile *file)
301 g_mapped_file_unref (file);
306 * @file: a #GMappedFile
308 * Increments the reference count of @file by one. It is safe to call
309 * this function from any thread.
311 * Return value: the passed in #GMappedFile.
316 g_mapped_file_ref (GMappedFile *file)
318 g_return_val_if_fail (file != NULL, NULL);
319 g_return_val_if_fail (file->ref_count > 0, file);
321 g_atomic_int_inc (&file->ref_count);
327 * g_mapped_file_unref:
328 * @file: a #GMappedFile
330 * Decrements the reference count of @file by one. If the reference count
331 * drops to 0, unmaps the buffer of @file and frees it.
333 * It is safe to call this function from any thread.
338 g_mapped_file_unref (GMappedFile *file)
340 g_return_if_fail (file != NULL);
341 g_return_if_fail (file->ref_count > 0);
343 if (g_atomic_int_dec_and_test (&file->ref_count))
344 g_mapped_file_destroy (file);
347 #define __G_MAPPED_FILE_C__
348 #include "galiasdef.c"