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"
42 #define fstat(a,b) _fstat(a,b)
49 #include "gfileutils.h"
50 #include "gmappedfile.h"
52 #include "gmessages.h"
54 #include "gstrfuncs.h"
66 #define MAP_FAILED ((void *) -1)
81 * @filename: The path of the file to load, in the GLib filename encoding
82 * @writable: whether the mapping should be writable
83 * @error: return location for a #GError, or %NULL
85 * Maps a file into memory. On UNIX, this is using the mmap() function.
87 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
88 * it is an error to modify the mapped buffer. Modifications to the buffer
89 * are not visible to other processes mapping the same file, and are not
90 * written back to the file.
92 * Note that modifications of the underlying file might affect the contents
93 * of the #GMappedFile. Therefore, mapping should only be used if the file
94 * will not be modified, or if all modifications of the file are done
95 * atomically (e.g. using g_file_set_contents()).
97 * Return value: a newly allocated #GMappedFile which must be unref'd
98 * with g_mapped_file_unref(), or %NULL if the mapping failed.
103 g_mapped_file_new (const gchar *filename,
111 g_return_val_if_fail (filename != NULL, NULL);
112 g_return_val_if_fail (!error || *error == NULL, NULL);
114 fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
117 int save_errno = errno;
118 gchar *display_filename = g_filename_display_name (filename);
122 g_file_error_from_errno (save_errno),
123 _("Failed to open file '%s': open() failed: %s"),
125 g_strerror (save_errno));
126 g_free (display_filename);
130 file = g_slice_new0 (GMappedFile);
133 if (fstat (fd, &st) == -1)
135 int save_errno = errno;
136 gchar *display_filename = g_filename_display_name (filename);
140 g_file_error_from_errno (save_errno),
141 _("Failed to get attributes of file '%s': fstat() failed: %s"),
143 g_strerror (save_errno));
144 g_free (display_filename);
156 file->contents = MAP_FAILED;
159 if (st.st_size > G_MAXSIZE)
165 file->length = (gsize) st.st_size;
166 file->contents = (gchar *) mmap (NULL, file->length,
167 writable ? PROT_READ|PROT_WRITE : PROT_READ,
172 file->length = st.st_size;
173 file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
174 writable ? PAGE_WRITECOPY : PAGE_READONLY,
177 if (file->mapping != NULL)
179 file->contents = MapViewOfFile (file->mapping,
180 writable ? FILE_MAP_COPY : FILE_MAP_READ,
183 if (file->contents == NULL)
185 file->contents = MAP_FAILED;
186 CloseHandle (file->mapping);
187 file->mapping = NULL;
193 if (file->contents == MAP_FAILED)
195 int save_errno = errno;
196 gchar *display_filename = g_filename_display_name (filename);
200 g_file_error_from_errno (save_errno),
201 _("Failed to map file '%s': mmap() failed: %s"),
203 g_strerror (save_errno));
204 g_free (display_filename);
213 g_slice_free (GMappedFile, file);
219 * g_mapped_file_get_length:
220 * @file: a #GMappedFile
222 * Returns the length of the contents of a #GMappedFile.
224 * Returns: the length of the contents of @file.
229 g_mapped_file_get_length (GMappedFile *file)
231 g_return_val_if_fail (file != NULL, 0);
237 * g_mapped_file_get_contents:
238 * @file: a #GMappedFile
240 * Returns the contents of a #GMappedFile.
242 * Note that the contents may not be zero-terminated,
243 * even if the #GMappedFile is backed by a text file.
245 * Returns: the contents of @file.
250 g_mapped_file_get_contents (GMappedFile *file)
252 g_return_val_if_fail (file != NULL, NULL);
254 return file->contents;
258 * g_mapped_file_free:
259 * @file: a #GMappedFile
261 * This call existed before #GMappedFile had refcounting and is currently
262 * exactly the same as g_mapped_file_unref().
265 * Deprecated:2.22: Use g_mapped_file_unref() instead.
268 g_mapped_file_free (GMappedFile *file)
270 g_mapped_file_unref (file);
275 * @file: a #GMappedFile
277 * Increments the reference count of @file by one. It is safe to call
278 * this function from any thread.
280 * Return value: the passed in #GMappedFile.
285 g_mapped_file_ref (GMappedFile *file)
287 g_return_val_if_fail (file != NULL, NULL);
288 g_return_val_if_fail (file->ref_count > 0, file);
290 g_atomic_int_inc (&file->ref_count);
296 * g_mapped_file_unref:
297 * @file: a #GMappedFile
299 * Decrements the reference count of @file by one. If the reference count
300 * drops to 0, unmaps the buffer of @file and frees it.
302 * It is safe to call this function from any thread.
307 g_mapped_file_unref (GMappedFile *file)
309 g_return_if_fail (file != NULL);
310 g_return_if_fail (file->ref_count > 0);
312 if (g_atomic_int_dec_and_test (&file->ref_count))
317 munmap (file->contents, file->length);
320 UnmapViewOfFile (file->contents);
321 CloseHandle (file->mapping);
325 g_slice_free (GMappedFile, file);
329 #define __G_MAPPED_FILE_C__
330 #include "galiasdef.c"