1 /*****************************************************************************
2 * mmap.c: memory-mapped file input
3 *****************************************************************************
4 * Copyright © 2007-2008 Rémi Denis-Courmont
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
26 #include <vlc_common.h>
27 #include <vlc_plugin.h>
28 #include <vlc_access.h>
29 #include <vlc_input.h>
30 #include <vlc_charset.h>
31 #include <vlc_dialog.h>
36 #include <sys/types.h>
42 #define FILE_MMAP_TEXT N_("Use file memory mapping")
43 #define FILE_MMAP_LONGTEXT N_( \
44 "Try to use memory mapping to read files and block devices." )
47 /*# define MMAP_DEBUG 1*/
50 static int Open (vlc_object_t *);
51 static void Close (vlc_object_t *);
54 set_shortname (N_("MMap"))
55 set_description (N_("Memory-mapped file input"))
56 set_category (CAT_INPUT)
57 set_subcategory (SUBCAT_INPUT_ACCESS)
58 set_capability ("access", 52)
60 set_callbacks (Open, Close)
62 add_bool ("file-mmap", true, NULL,
63 FILE_MMAP_TEXT, FILE_MMAP_LONGTEXT, true)
65 add_bool ("file-mmap", false, NULL,
66 FILE_MMAP_TEXT, FILE_MMAP_LONGTEXT, true)
70 static block_t *Block (access_t *);
71 static int Seek (access_t *, int64_t);
72 static int Control (access_t *, int, va_list);
81 #define MMAP_SIZE (1 << 20)
83 static int Open (vlc_object_t *p_this)
85 access_t *p_access = (access_t *)p_this;
87 const char *path = p_access->psz_path;
90 assert ((INT64_C(1) << 63) == ((off_t)(INT64_C(1) << 63)));
92 if (!var_CreateGetBool (p_this, "file-mmap"))
93 return VLC_EGENERIC; /* disabled */
95 STANDARD_BLOCK_ACCESS_INIT;
97 if (!strcmp (p_access->psz_path, "-"))
101 msg_Dbg (p_access, "opening file %s", path);
102 fd = utf8_open (path, O_RDONLY | O_NOCTTY);
107 msg_Warn (p_access, "cannot open %s: %m", path);
111 /* mmap() is only safe for regular and block special files.
112 * For other types, it may be some idiosyncrasic interface (e.g. packet
113 * sockets are a good example), if it works at all. */
118 msg_Err (p_access, "cannot stat %s: %m", path);
122 if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode))
124 msg_Dbg (p_access, "skipping non-regular file %s", path);
128 #if defined(HAVE_FCNTL_H)
129 /* We'd rather use any available memory for reading ahead
130 * than for caching what we've already mmap'ed */
131 # if defined(F_RDAHEAD)
132 fcntl (fd, F_RDAHEAD, 1);
134 # if defined(F_NOCACHE)
135 fcntl (fd, F_NOCACHE, 1);
139 /* Autodetect mmap() support */
142 void *addr = mmap (NULL, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
143 if (addr != MAP_FAILED)
149 p_sys->page_size = sysconf (_SC_PAGE_SIZE);
150 p_sys->mtu = MMAP_SIZE;
151 if (p_sys->mtu < p_sys->page_size)
152 p_sys->mtu = p_sys->page_size;
155 p_access->info.i_size = st.st_size;
156 #ifdef HAVE_POSIX_FADVISE
157 posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
170 static void Close (vlc_object_t * p_this)
172 access_t *p_access = (access_t *)p_this;
173 access_sys_t *p_sys = p_access->p_sys;
175 close (p_sys->fd); /* don't care about error when only reading */
179 static block_t *Block (access_t *p_access)
181 access_sys_t *p_sys = p_access->p_sys;
183 /* Check if file size changed... */
186 if ((fstat (p_sys->fd, &st) == 0)
187 && (st.st_size != p_access->info.i_size))
189 p_access->info.i_size = st.st_size;
190 p_access->info.i_update |= INPUT_UPDATE_SIZE;
193 if ((uint64_t)p_access->info.i_pos >= (uint64_t)p_access->info.i_size)
195 /* We are at end of file */
196 p_access->info.b_eof = true;
197 msg_Dbg (p_access, "at end of memory mapped file");
202 int64_t dbgpos = lseek (p_sys->fd, 0, SEEK_CUR);
203 if (dbgpos != p_access->info.i_pos)
204 msg_Err (p_access, "position: 0x%016"PRIx64" instead of 0x%016"PRIx64,
205 p_access->info.i_pos, dbgpos);
208 const uintptr_t page_mask = p_sys->page_size - 1;
209 /* Start the mapping on a page boundary: */
210 off_t outer_offset = p_access->info.i_pos & ~(off_t)page_mask;
211 /* Skip useless bytes at the beginning of the first page: */
212 size_t inner_offset = p_access->info.i_pos & page_mask;
213 /* Map no more bytes than remain: */
214 size_t length = p_sys->mtu;
215 if (outer_offset + length > p_access->info.i_size)
216 length = p_access->info.i_size - outer_offset;
218 assert (outer_offset <= p_access->info.i_pos); /* and */
219 assert (p_access->info.i_pos < p_access->info.i_size); /* imply */
220 assert (outer_offset < p_access->info.i_size); /* imply */
223 /* NOTE: We use PROT_WRITE and MAP_PRIVATE so that the block can be
224 * modified down the chain, without messing up with the underlying
225 * original file. This does NOT need open write permission. */
226 void *addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE
230 , p_sys->fd, outer_offset);
231 if (addr == MAP_FAILED)
233 msg_Err (p_access, "memory mapping failed (%m)");
234 dialog_Fatal (p_access, _("File reading failed"), "%s",
235 _("VLC could not read the file."));
238 #ifdef HAVE_POSIX_MADVISE
239 posix_madvise (addr, length, POSIX_MADV_SEQUENTIAL);
242 block_t *block = block_mmap_Alloc (addr, length);
246 block->p_buffer += inner_offset;
247 block->i_buffer -= inner_offset;
250 msg_Dbg (p_access, "mapped 0x%zx bytes at %p from offset 0x%"PRIx64,
251 length, addr, (uint64_t)outer_offset);
253 /* Compare normal I/O with memory mapping */
254 char *buf = malloc (block->i_buffer);
255 ssize_t i_read = read (p_sys->fd, buf, block->i_buffer);
257 if (i_read != (ssize_t)block->i_buffer)
258 msg_Err (p_access, "read %zd instead of %zu bytes", i_read,
260 if (memcmp (buf, block->p_buffer, block->i_buffer))
261 msg_Err (p_access, "inconsistent data buffer");
265 p_access->info.i_pos = outer_offset + length;
269 p_access->info.b_eof = true;
274 static int Seek (access_t *p_access, int64_t i_pos)
277 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
280 p_access->info.i_pos = i_pos;
281 p_access->info.b_eof = false;
286 static int Control (access_t *p_access, int query, va_list args)
290 case ACCESS_CAN_SEEK:
291 case ACCESS_CAN_FASTSEEK:
292 case ACCESS_CAN_PAUSE:
293 case ACCESS_CAN_CONTROL_PACE:
294 *va_arg(args, bool *) = true;
297 case ACCESS_GET_PTS_DELAY:
299 int delay_ms = var_CreateGetInteger (p_access, "file-caching");
300 *va_arg(args, int64_t *) = delay_ms * INT64_C (1000);
304 case ACCESS_GET_TITLE_INFO:
305 case ACCESS_GET_META:
308 case ACCESS_SET_PAUSE_STATE:
311 case ACCESS_SET_TITLE:
312 case ACCESS_SET_SEEKPOINT:
313 case ACCESS_SET_PRIVATE_ID_STATE:
314 case ACCESS_SET_PRIVATE_ID_CA:
315 case ACCESS_GET_PRIVATE_ID_STATE:
316 case ACCESS_GET_CONTENT_TYPE:
320 msg_Warn (p_access, "unimplemented query %d in control", query);