X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Faccess%2Ffile.c;h=9f5750d317f29589b0f74841bf3b04a87b7cfd00;hb=f49cbfc01af00fce8f94b6b8ba8d0f9bed20858b;hp=6d01affc343b659aaca245ca11a237b619223e01;hpb=ecc5eb9db29f4ae9c0ee1c667c51dd7fd0a6b887;p=vlc diff --git a/modules/access/file.c b/modules/access/file.c index 6d01affc34..9f5750d317 100644 --- a/modules/access/file.c +++ b/modules/access/file.c @@ -208,7 +208,14 @@ static int Open( vlc_object_t *p_this ) /* Autodetect mmap() support */ if (p_sys->b_pace_control && S_ISREG (st.st_mode) && (st.st_size > 0)) { - void *addr = mmap (NULL, 1, PROT_READ, MAP_PRIVATE, fd, 0); + /* TODO: Do not allow PROT_WRITE, we should not need it. + * However, this far, "block" ownership seems such that whoever + * "receives" a block can freely modify its content. Hence we _may_ + * need PROT_WRITE not to default memory protection. + * NOTE: With MAP_PRIVATE, changes are not committed to the underlying + * file, write open permission is not required. + */ + void *addr = mmap (NULL, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); if (addr != MAP_FAILED) { /* Does the file system support mmap? */ @@ -345,23 +352,6 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len ) #ifdef HAVE_MMAP # define MMAP_SIZE (1 << 20) -struct block_sys_t -{ - block_t self; - //vlc_object_t *owner; - void *base_addr; - size_t length; -}; - -static void mmapRelease (block_t *block) -{ - block_sys_t *p_sys = (block_sys_t *)block; - - munmap (p_sys->base_addr, p_sys->length); - //vlc_object_release (p_sys->owner); - free (p_sys); -} - static block_t *mmapBlock (access_t *p_access) { access_sys_t *p_sys = p_access->p_sys; @@ -421,34 +411,30 @@ static block_t *mmapBlock (access_t *p_access) p_access->info.i_pos = offset + length; - msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx", - (unsigned long)length, addr, (unsigned long)offset); - block_sys_t *block = malloc (sizeof (*block)); + block_t *block = block_mmap_Alloc (addr, length); if (block == NULL) - { - munmap (addr, length); return NULL; - } - block_Init (&block->self, ((uint8_t *)addr) + align, length - align); - block->self.pf_release = mmapRelease; - block->base_addr = addr; - block->length = length; - //vlc_object_yield (block->owner = VLC_OBJECT (p_access)); + block->p_buffer += align; + block->i_buffer -= align; #ifndef NDEBUG + msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx", + (unsigned long)length, addr, (unsigned long)offset); + /* Compare normal I/O with memory mapping */ - char buf[block->self.i_buffer]; - ssize_t i_read = read (p_sys->fd, buf, block->self.i_buffer); + char *buf = malloc (block->i_buffer); + ssize_t i_read = read (p_sys->fd, buf, block->i_buffer); - if (i_read != (ssize_t)block->self.i_buffer) + if (i_read != (ssize_t)block->i_buffer) msg_Err (p_access, "read %u instead of %u bytes", (unsigned)i_read, - (unsigned)block->self.i_buffer); - if (memcmp (buf, block->self.p_buffer, block->self.i_buffer)) + (unsigned)block->i_buffer); + if (memcmp (buf, block->p_buffer, block->i_buffer)) msg_Err (p_access, "inconsistent data buffer"); + free (buf); #endif - return &block->self; + return block; } #endif