X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fmisc%2Fblock.c;h=cbe15a805d8204d708df10c84dab0c4740dab2c0;hb=048d2ad16972042be038dc52ec9a891e1b247432;hp=bddd729e2718ccc6513c57a641547b27bc3389d5;hpb=df61d33b06e2b3cbbe746b2f5a9bea5b370c24ff;p=vlc diff --git a/src/misc/block.c b/src/misc/block.c index bddd729e27..cbe15a805d 100644 --- a/src/misc/block.c +++ b/src/misc/block.c @@ -28,18 +28,22 @@ # include "config.h" #endif -#include +#include +#include #include "vlc_block.h" -/***************************************************************************** - * Block functions. - *****************************************************************************/ -/* private */ +/** + * @section Block handling functions. + */ + +/** + * Internal state for heap block. + */ struct block_sys_t { block_t self; size_t i_allocated_buffer; - uint8_t p_allocated_buffer[0]; + uint8_t p_allocated_buffer[]; }; #ifndef NDEBUG @@ -53,9 +57,11 @@ static void BlockNoRelease( block_t *b ) void block_Init( block_t *restrict b, void *buf, size_t size ) { /* Fill all fields to their default */ - b->p_next = b->p_prev = NULL; + b->p_next = NULL; b->i_flags = 0; - b->i_pts = b->i_dts = b->i_length = 0; + b->i_pts = + b->i_dts = VLC_TS_INVALID; + b->i_length = 0; b->i_rate = 0; b->p_buffer = buf; b->i_buffer = size; @@ -105,7 +111,13 @@ block_t *block_Alloc( size_t i_size ) block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body ) { block_sys_t *p_sys = (block_sys_t *)p_block; - ssize_t i_buffer_size; + ssize_t i_buffer_size = i_prebody + i_body; + + if( i_buffer_size <= 0 ) + { + block_Release( p_block ); + return NULL; + } if( p_block->pf_release != BlockRelease ) { @@ -117,14 +129,7 @@ block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body ) return NULL; p_block = p_dup; - } - - i_buffer_size = i_prebody + i_body; - - if( i_buffer_size <= 0 ) - { - block_Release( p_block ); - return NULL; + p_sys = (block_sys_t *)p_block; } /* Adjust reserved header if there is enough room */ @@ -169,27 +174,69 @@ block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body ) return p_rea; } - /* We have a very large reserved footer now? Release some of it. */ - if ((p_sys->p_allocated_buffer + p_sys->i_allocated_buffer) - - (p_block->p_buffer + p_block->i_buffer) > BLOCK_WASTE_SIZE) + /* We have a very large reserved footer now? Release some of it. + * XXX it may not keep the algniment of p_buffer */ + if( (p_sys->p_allocated_buffer + p_sys->i_allocated_buffer) - + (p_block->p_buffer + p_block->i_buffer) > BLOCK_WASTE_SIZE ) { - const size_t news = p_block->i_buffer + 2 * BLOCK_PADDING_SIZE + 16; - block_sys_t *newb = realloc (p_sys, sizeof (*p_sys) + news); + const ptrdiff_t i_prebody = p_block->p_buffer - p_sys->p_allocated_buffer; + const size_t i_new = i_prebody + p_block->i_buffer + 1 * BLOCK_PADDING_SIZE; + block_sys_t *p_new = realloc( p_sys, sizeof (*p_sys) + i_new ); - if (newb != NULL) + if( p_new != NULL ) { - p_sys = newb; - p_sys->i_allocated_buffer = news; + p_sys = p_new; + p_sys->i_allocated_buffer = i_new; p_block = &p_sys->self; - p_block->p_buffer = p_sys->p_allocated_buffer + BLOCK_PADDING_SIZE - + BLOCK_ALIGN - - ((uintptr_t)p_sys->p_allocated_buffer % BLOCK_ALIGN); + p_block->p_buffer = &p_sys->p_allocated_buffer[i_prebody]; } } - return p_block; } + +typedef struct +{ + block_t self; + void *mem; +} block_heap_t; + +static void block_heap_Release (block_t *self) +{ + block_heap_t *block = (block_heap_t *)self; + + free (block->mem); + free (block); +} + +/** + * Creates a block from a heap allocation. + * This is provided by LibVLC so that manually heap-allocated blocks can safely + * be deallocated even after the origin plugin has been unloaded from memory. + * + * When block_Release() is called, VLC will free() the specified pointer. + * + * @param ptr base address of the heap allocation (will be free()'d) + * @param addr base address of the useful buffer data + * @param length bytes length of the useful buffer datan + * @return NULL in case of error (ptr free()'d in that case), or a valid + * block_t pointer. + */ +block_t *block_heap_Alloc (void *ptr, void *addr, size_t length) +{ + block_heap_t *block = malloc (sizeof (*block)); + if (block == NULL) + { + free (addr); + return NULL; + } + + block_Init (&block->self, (uint8_t *)addr, length); + block->self.pf_release = block_heap_Release; + block->mem = ptr; + return &block->self; +} + #ifdef HAVE_MMAP # include @@ -208,6 +255,16 @@ static void block_mmap_Release (block_t *block) free (p_sys); } +/** + * Creates a block from a virtual address memory mapping (mmap). + * This is provided by LibVLC so that mmap blocks can safely be deallocated + * even after the allocating plugin has been unloaded from memory. + * + * @param addr base address of the mapping (as returned by mmap) + * @param length length (bytes) of the mapping (as passed to mmap) + * @return NULL if addr is MAP_FAILED, or an error occurred (in the later + * case, munmap(addr, length) is invoked before returning). + */ block_t *block_mmap_Alloc (void *addr, size_t length) { if (addr == MAP_FAILED) @@ -226,15 +283,125 @@ block_t *block_mmap_Alloc (void *addr, size_t length) block->length = length; return &block->self; } +#else +block_t *block_mmap_Alloc (void *addr, size_t length) +{ + (void)addr; (void)length; return NULL; +} #endif -/***************************************************************************** - * block_fifo_t management - *****************************************************************************/ + +#ifdef WIN32 +#ifdef UNDER_CE +#define _get_osfhandle(a) ((long) (a)) +#endif + +static +ssize_t pread (int fd, void *buf, size_t count, off_t offset) +{ + HANDLE handle = (HANDLE)(intptr_t)_get_osfhandle (fd); + if (handle == INVALID_HANDLE_VALUE) + return -1; + + OVERLAPPED olap; olap.Offset = offset; olap.OffsetHigh = (offset >> 32); + DWORD written; + /* This braindead API will override the file pointer even if we specify + * an explicit read offset... So do not expect this to mix well with + * regular read() calls. */ + if (ReadFile (handle, buf, count, &written, &olap)) + return written; + return -1; +} +#endif + +/** + * Loads a file into a block of memory. If possible a private file mapping is + * created. Otherwise, the file is read normally. On 32-bits platforms, this + * function will not work for very large files, due to memory space + * constraints. Cancellation point. + * + * @param fd file descriptor to load from + * @return a new block with the file content at p_buffer, and file length at + * i_buffer (release it with block_Release()), or NULL upon error (see errno). + */ +block_t *block_File (int fd) +{ + size_t length; + struct stat st; + + /* First, get the file size */ + if (fstat (fd, &st)) + return NULL; + + /* st_size is meaningful for regular files, shared memory and typed memory. + * It's also meaning for symlinks, but that's not possible with fstat(). + * In other cases, it's undefined, and we should really not go further. */ +#ifndef S_TYPEISSHM +# define S_TYPEISSHM( buf ) (0) +#endif + if (S_ISDIR (st.st_mode)) + { + errno = EISDIR; + return NULL; + } + if (!S_ISREG (st.st_mode) && !S_TYPEISSHM (&st)) + { + errno = ESPIPE; + return NULL; + } + + /* Prevent an integer overflow in mmap() and malloc() */ + if (st.st_size >= SIZE_MAX) + { + errno = ENOMEM; + return NULL; + } + length = (size_t)st.st_size; + +#ifdef HAVE_MMAP + if (length > 0) + { + void *addr; + + addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); + if (addr != MAP_FAILED) + return block_mmap_Alloc (addr, length); + } +#endif + + /* If mmap() is not implemented by the OS _or_ the filesystem... */ + block_t *block = block_Alloc (length); + if (block == NULL) + return NULL; + block_cleanup_push (block); + + for (size_t i = 0; i < length;) + { + ssize_t len = pread (fd, block->p_buffer + i, length - i, i); + if (len == -1) + { + block_Release (block); + block = NULL; + break; + } + i += len; + } + vlc_cleanup_pop (); + return block; +} + +/** + * @section Thread-safe block queue functions + */ + +/** + * Internal state for block queues + */ struct block_fifo_t { vlc_mutex_t lock; /* fifo data lock */ - vlc_cond_t wait; /* fifo data conditional variable */ + vlc_cond_t wait; /**< Wait for data */ + vlc_cond_t wait_room; /**< Wait for queue depth to shrink */ block_t *p_first; block_t **pp_last; @@ -243,16 +410,15 @@ struct block_fifo_t bool b_force_wake; }; -block_fifo_t *__block_FifoNew( vlc_object_t *p_obj ) +block_fifo_t *block_FifoNew( void ) { - (void)p_obj; - - block_fifo_t *p_fifo; + block_fifo_t *p_fifo = malloc( sizeof( block_fifo_t ) ); + if( !p_fifo ) + return NULL; - p_fifo = malloc( sizeof( block_fifo_t ) ); - if( !p_fifo ) return NULL; - vlc_mutex_init( p_obj, &p_fifo->lock ); - vlc_cond_init( p_obj, &p_fifo->wait ); + vlc_mutex_init( &p_fifo->lock ); + vlc_cond_init( &p_fifo->wait ); + vlc_cond_init( &p_fifo->wait_room ); p_fifo->p_first = NULL; p_fifo->pp_last = &p_fifo->p_first; p_fifo->i_depth = p_fifo->i_size = 0; @@ -264,6 +430,7 @@ block_fifo_t *__block_FifoNew( vlc_object_t *p_obj ) void block_FifoRelease( block_fifo_t *p_fifo ) { block_FifoEmpty( p_fifo ); + vlc_cond_destroy( &p_fifo->wait_room ); vlc_cond_destroy( &p_fifo->wait ); vlc_mutex_destroy( &p_fifo->lock ); free( p_fifo ); @@ -286,15 +453,52 @@ void block_FifoEmpty( block_fifo_t *p_fifo ) p_fifo->i_depth = p_fifo->i_size = 0; p_fifo->p_first = NULL; p_fifo->pp_last = &p_fifo->p_first; + vlc_cond_broadcast( &p_fifo->wait_room ); vlc_mutex_unlock( &p_fifo->lock ); } +/** + * Wait until the FIFO gets below a certain size (if needed). + * + * Note that if more than one thread writes to the FIFO, you cannot assume that + * the FIFO is actually below the requested size upon return (since another + * thread could have refilled it already). This is typically not an issue, as + * this function is meant for (relaxed) congestion control. + * + * This function may be a cancellation point and it is cancel-safe. + * + * @param fifo queue to wait on + * @param max_depth wait until the queue has no more than this many blocks + * (use SIZE_MAX to ignore this constraint) + * @param max_size wait until the queue has no more than this many bytes + * (use SIZE_MAX to ignore this constraint) + * @return nothing. + */ +void block_FifoPace (block_fifo_t *fifo, size_t max_depth, size_t max_size) +{ + vlc_testcancel (); + + vlc_mutex_lock (&fifo->lock); + while ((fifo->i_depth > max_depth) || (fifo->i_size > max_size)) + { + mutex_cleanup_push (&fifo->lock); + vlc_cond_wait (&fifo->wait_room, &fifo->lock); + vlc_cleanup_pop (); + } + vlc_mutex_unlock (&fifo->lock); +} + +/** + * Immediately queue one block at the end of a FIFO. + * @param fifo queue + * @param block head of a block list to queue (may be NULL) + */ size_t block_FifoPut( block_fifo_t *p_fifo, block_t *p_block ) { size_t i_size = 0; vlc_mutex_lock( &p_fifo->lock ); - do + while (p_block != NULL) { i_size += p_block->i_buffer; @@ -304,10 +508,9 @@ size_t block_FifoPut( block_fifo_t *p_fifo, block_t *p_block ) p_fifo->i_size += p_block->i_buffer; p_block = p_block->p_next; + } - } while( p_block ); - - /* warn there is data in this fifo */ + /* We queued one block: wake up one read-waiting thread */ vlc_cond_signal( &p_fifo->wait ); vlc_mutex_unlock( &p_fifo->lock ); @@ -319,7 +522,7 @@ void block_FifoWake( block_fifo_t *p_fifo ) vlc_mutex_lock( &p_fifo->lock ); if( p_fifo->p_first == NULL ) p_fifo->b_force_wake = true; - vlc_cond_signal( &p_fifo->wait ); + vlc_cond_broadcast( &p_fifo->wait ); vlc_mutex_unlock( &p_fifo->lock ); } @@ -327,15 +530,17 @@ block_t *block_FifoGet( block_fifo_t *p_fifo ) { block_t *b; + vlc_testcancel( ); + vlc_mutex_lock( &p_fifo->lock ); + mutex_cleanup_push( &p_fifo->lock ); /* Remember vlc_cond_wait() may cause spurious wakeups * (on both Win32 and POSIX) */ while( ( p_fifo->p_first == NULL ) && !p_fifo->b_force_wake ) - { vlc_cond_wait( &p_fifo->wait, &p_fifo->lock ); - } + vlc_cleanup_pop(); b = p_fifo->p_first; p_fifo->b_force_wake = false; @@ -355,6 +560,8 @@ block_t *block_FifoGet( block_fifo_t *p_fifo ) p_fifo->pp_last = &p_fifo->p_first; } + /* We don't know how many threads can queue new packets now. */ + vlc_cond_broadcast( &p_fifo->wait_room ); vlc_mutex_unlock( &p_fifo->lock ); b->p_next = NULL; @@ -365,25 +572,27 @@ block_t *block_FifoShow( block_fifo_t *p_fifo ) { block_t *b; + vlc_testcancel( ); + vlc_mutex_lock( &p_fifo->lock ); + mutex_cleanup_push( &p_fifo->lock ); - if( p_fifo->p_first == NULL ) - { + while( p_fifo->p_first == NULL ) vlc_cond_wait( &p_fifo->wait, &p_fifo->lock ); - } b = p_fifo->p_first; - vlc_mutex_unlock( &p_fifo->lock ); - - return( b ); + vlc_cleanup_run (); + return b; } +/* FIXME: not thread-safe */ size_t block_FifoSize( const block_fifo_t *p_fifo ) { return p_fifo->i_size; } +/* FIXME: not thread-safe */ size_t block_FifoCount( const block_fifo_t *p_fifo ) { return p_fifo->i_depth;