Changeset 2ec9d996a737986148acb5c1ce61058e9b3e4ff4

Show
Ignore:
Timestamp:
20/05/08 22:19:45 (6 months ago)
Author:
Rémi Denis-Courmont <rem@videolan.org>
git-committer:
Rémi Denis-Courmont <rem@videolan.org> 1211314785 +0300
git-parent:

[c65b4ea96c47550a8876513222aab4b81cfc42dd]

git-author:
Rémi Denis-Courmont <rem@videolan.org> 1211310912 +0300
Message:

block_File: loads a file into a block_t

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • include/vlc_block.h

    rb2b9fb4 r2ec9d99  
    149149} 
    150150 
    151 /** 
    152  * Creates a block from a virtual address memory mapping (mmap). 
    153  * This is provided by LibVLC so that mmap blocks can safely be deallocated 
    154  * even after the allocating plugin has been unloaded from memory. 
    155  * 
    156  * @param addr base address of the mapping (as returned by mmap) 
    157  * @param length length (bytes) of the mapping (as passed to mmap) 
    158  * @return NULL if addr is MAP_FAILED, or an error occurred (in the later 
    159  * case, munmap(addr, length) is invoked before returning). 
    160  */ 
    161151VLC_EXPORT( block_t *, block_mmap_Alloc, (void *addr, size_t length) ); 
     152VLC_EXPORT( block_t *, block_File, (int fd) ); 
    162153 
    163154/**************************************************************************** 
  • src/libvlccore.sym

    r73edaec r2ec9d99  
    4242block_FifoSize 
    4343block_FifoWake 
     44block_File 
    4445block_Init 
    4546block_mmap_Alloc 
  • src/misc/block.c

    rc65b4ea r2ec9d99  
    3030 
    3131#include <vlc/vlc.h> 
     32#include <sys/stat.h> 
    3233#include "vlc_block.h" 
    3334 
     
    209210} 
    210211 
     212/** 
     213 * Creates a block from a virtual address memory mapping (mmap). 
     214 * This is provided by LibVLC so that mmap blocks can safely be deallocated 
     215 * even after the allocating plugin has been unloaded from memory. 
     216 * 
     217 * @param addr base address of the mapping (as returned by mmap) 
     218 * @param length length (bytes) of the mapping (as passed to mmap) 
     219 * @return NULL if addr is MAP_FAILED, or an error occurred (in the later 
     220 * case, munmap(addr, length) is invoked before returning). 
     221 */ 
    211222block_t *block_mmap_Alloc (void *addr, size_t length) 
    212223{ 
     
    248259#endif 
    249260 
     261/** 
     262 * Loads a file into a block of memory. If possible a private file mapping is 
     263 * created. Otherwise, the file is read normally. On 32-bits platforms, this 
     264 * function will not work for very large files, due to memory space 
     265 * constraints. 
     266 * 
     267 * @param fd file descriptor to load from 
     268 * @return a new block with the file content at p_buffer, and file length at 
     269 * i_buffer (release it with block_Release()), or NULL upon error (see errno). 
     270 */ 
     271block_t *block_File (int fd) 
     272{ 
     273    size_t length; 
     274    struct stat st; 
     275 
     276    /* First, get the file size */ 
     277    if (fstat (fd, &st)) 
     278        return NULL; 
     279 
     280    /* st_size is meaningful for regular files, shared memory and typed memory. 
     281     * It's also meaning for symlinks, but that's not possible with fstat(). 
     282     * In other cases, it's undefined, and we should really not go further. */ 
     283#ifndef S_TYPEISSHM 
     284# define S_TYPEISSHM( buf ) (0) 
     285#endif 
     286    if (S_ISDIR (st.st_mode)) 
     287    { 
     288        errno = EISDIR; 
     289        return NULL; 
     290    } 
     291    if (!S_ISREG (st.st_mode) && !S_TYPEISSHM (&st)) 
     292    { 
     293        errno = ESPIPE; 
     294        return NULL; 
     295    } 
     296 
     297    /* Prevent an integer overflow in mmap() and malloc() */ 
     298    if (st.st_size >= SIZE_MAX) 
     299    { 
     300        errno = ENOMEM; 
     301        return NULL; 
     302    } 
     303    length = (size_t)st.st_size; 
     304 
     305#ifdef HAVE_MMAP 
     306    if (length > 0) 
     307    { 
     308        void *addr; 
     309 
     310        addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); 
     311        if (addr != MAP_FAILED) 
     312            return block_mmap_Alloc (addr, length); 
     313    } 
     314#endif 
     315 
     316    /* If mmap() is not implemented by the OS _or_ the filesystem... */ 
     317    block_t *block = block_Alloc (length); 
     318    if (block == NULL) 
     319        return NULL; 
     320 
     321    for (size_t i = 0; i < length;) 
     322    { 
     323        ssize_t len = pread (fd, block->p_buffer + i, length - i, i); 
     324        if (len == -1) 
     325        { 
     326            block_Release (block); 
     327            return NULL; 
     328        } 
     329        i += len; 
     330    } 
     331    return block; 
     332} 
     333 
    250334/***************************************************************************** 
    251335 * block_fifo_t management