1 /*****************************************************************************
2 * block.c: Data blocks management functions
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VLC authors and VideoLAN
5 * Copyright (C) 2007-2009 RĂ©mi Denis-Courmont
7 * Authors: Laurent Aimar <fenrir@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
39 #include <vlc_common.h>
40 #include <vlc_block.h>
44 * @section Block handling functions.
48 static void BlockNoRelease( block_t *b )
50 fprintf( stderr, "block %p has no release callback! This is a bug!\n", b );
54 static void block_Check (block_t *block)
58 unsigned char *start = block->p_start;
59 unsigned char *end = block->p_start + block->i_size;
60 unsigned char *bufstart = block->p_buffer;
61 unsigned char *bufend = block->p_buffer + block->i_buffer;
63 assert (block->pf_release != BlockNoRelease);
64 assert (start <= end);
65 assert (bufstart <= bufend);
66 assert (bufstart >= start);
67 assert (bufend <= end);
69 block = block->p_next;
73 static void block_Invalidate (block_t *block)
77 block->pf_release = BlockNoRelease;
80 # define block_Check(b) ((void)(b))
81 # define block_Invalidate(b) ((void)(b))
84 void block_Init( block_t *restrict b, void *buf, size_t size )
86 /* Fill all fields to their default */
95 b->i_dts = VLC_TS_INVALID;
98 b->pf_release = BlockNoRelease;
102 static void block_generic_Release (block_t *block)
104 /* That is always true for blocks allocated with block_Alloc(). */
105 assert (block->p_start == (unsigned char *)(block + 1));
106 block_Invalidate (block);
110 static void BlockMetaCopy( block_t *restrict out, const block_t *in )
112 out->p_next = in->p_next;
113 out->i_nb_samples = in->i_nb_samples;
114 out->i_dts = in->i_dts;
115 out->i_pts = in->i_pts;
116 out->i_flags = in->i_flags;
117 out->i_length = in->i_length;
120 /* Memory alignment (must be a multiple of sizeof(void*) and a power of two) */
121 #define BLOCK_ALIGN 16
122 /* Initial reserved header and footer size (must be multiple of alignment) */
123 #define BLOCK_PADDING 32
124 /* Maximum size of reserved footer before we release with realloc() */
125 #define BLOCK_WASTE_SIZE 2048
127 block_t *block_Alloc (size_t size)
129 /* 2 * BLOCK_PADDING: pre + post padding */
130 const size_t alloc = sizeof (block_t) + BLOCK_ALIGN + (2 * BLOCK_PADDING)
132 if (unlikely(alloc <= size))
135 block_t *b = malloc (alloc);
136 if (unlikely(b == NULL))
139 block_Init (b, b + 1, alloc - sizeof (*b));
140 static_assert ((BLOCK_PADDING % BLOCK_ALIGN) == 0,
141 "BLOCK_PADDING must be a multiple of BLOCK_ALIGN");
142 b->p_buffer += BLOCK_PADDING + BLOCK_ALIGN - 1;
143 b->p_buffer = (void *)(((uintptr_t)b->p_buffer) & ~(BLOCK_ALIGN - 1));
145 b->pf_release = block_generic_Release;
149 block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
151 size_t requested = i_prebody + i_body;
153 block_Check( p_block );
155 /* Corner case: empty block requested */
156 if( i_prebody <= 0 && i_body <= (size_t)(-i_prebody) )
158 block_Release( p_block );
162 assert( p_block->p_start <= p_block->p_buffer );
163 assert( p_block->p_start + p_block->i_size
164 >= p_block->p_buffer + p_block->i_buffer );
166 /* Corner case: the current payload is discarded completely */
167 if( i_prebody <= 0 && p_block->i_buffer <= (size_t)-i_prebody )
168 p_block->i_buffer = 0; /* discard current payload */
169 if( p_block->i_buffer == 0 )
171 if( requested <= p_block->i_size )
172 { /* Enough room: recycle buffer */
173 size_t extra = p_block->i_size - requested;
175 p_block->p_buffer = p_block->p_start + (extra / 2);
176 p_block->i_buffer = requested;
179 /* Not enough room: allocate a new buffer */
180 block_t *p_rea = block_Alloc( requested );
182 BlockMetaCopy( p_rea, p_block );
183 block_Release( p_block );
187 /* First, shrink payload */
189 /* Pull payload start */
192 assert( p_block->i_buffer >= (size_t)-i_prebody );
193 p_block->p_buffer -= i_prebody;
194 p_block->i_buffer += i_prebody;
199 /* Trim payload end */
200 if( p_block->i_buffer > i_body )
201 p_block->i_buffer = i_body;
203 uint8_t *p_start = p_block->p_start;
204 uint8_t *p_end = p_start + p_block->i_size;
206 /* Second, reallocate the buffer if we lack space. This is done now to
207 * minimize the payload size for memory copy. */
208 assert( i_prebody >= 0 );
209 if( (size_t)(p_block->p_buffer - p_start) < (size_t)i_prebody
210 || (size_t)(p_end - p_block->p_buffer) < i_body )
212 block_t *p_rea = block_Alloc( requested );
215 BlockMetaCopy( p_rea, p_block );
216 p_rea->p_buffer += i_prebody;
217 p_rea->i_buffer -= i_prebody;
218 memcpy( p_rea->p_buffer, p_block->p_buffer, p_block->i_buffer );
220 block_Release( p_block );
226 /* We have a very large reserved footer now? Release some of it.
227 * XXX it might not preserve the alignment of p_buffer */
228 if( p_end - (p_block->p_buffer + i_body) > BLOCK_WASTE_SIZE )
230 block_t *p_rea = block_Alloc( requested );
233 BlockMetaCopy( p_rea, p_block );
234 p_rea->p_buffer += i_prebody;
235 p_rea->i_buffer -= i_prebody;
236 memcpy( p_rea->p_buffer, p_block->p_buffer, p_block->i_buffer );
237 block_Release( p_block );
242 /* NOTE: p_start and p_end are corrupted from this point */
244 /* Third, expand payload */
246 /* Push payload start */
249 p_block->p_buffer -= i_prebody;
250 p_block->i_buffer += i_prebody;
255 /* Expand payload to requested size */
256 p_block->i_buffer = i_body;
262 static void block_heap_Release (block_t *block)
264 block_Invalidate (block);
265 free (block->p_start);
270 * Creates a block from a heap allocation.
271 * This is provided by LibVLC so that manually heap-allocated blocks can safely
272 * be deallocated even after the origin plugin has been unloaded from memory.
274 * When block_Release() is called, VLC will free() the specified pointer.
276 * @param ptr base address of the heap allocation (will be free()'d)
277 * @param length bytes length of the heap allocation
278 * @return NULL in case of error (ptr free()'d in that case), or a valid
281 block_t *block_heap_Alloc (void *addr, size_t length)
283 block_t *block = malloc (sizeof (*block));
290 block_Init (block, addr, length);
291 block->pf_release = block_heap_Release;
296 # include <sys/mman.h>
298 static void block_mmap_Release (block_t *block)
300 block_Invalidate (block);
301 munmap (block->p_start, block->i_size);
306 * Creates a block from a virtual address memory mapping (mmap).
307 * This is provided by LibVLC so that mmap blocks can safely be deallocated
308 * even after the allocating plugin has been unloaded from memory.
310 * @param addr base address of the mapping (as returned by mmap)
311 * @param length length (bytes) of the mapping (as passed to mmap)
312 * @return NULL if addr is MAP_FAILED, or an error occurred (in the later
313 * case, munmap(addr, length) is invoked before returning).
315 block_t *block_mmap_Alloc (void *addr, size_t length)
317 if (addr == MAP_FAILED)
320 block_t *block = malloc (sizeof (*block));
323 munmap (addr, length);
327 block_Init (block, addr, length);
328 block->pf_release = block_mmap_Release;
332 block_t *block_mmap_Alloc (void *addr, size_t length)
334 (void)addr; (void)length; return NULL;
338 #ifdef HAVE_SYS_SHM_H
339 # include <sys/shm.h>
341 typedef struct block_shm_t
347 static void block_shm_Release (block_t *block)
349 block_shm_t *p_sys = (block_shm_t *)block;
351 shmdt (p_sys->base_addr);
356 * Creates a block from a System V shared memory segment (shmget()).
357 * This is provided by LibVLC so that segments can safely be deallocated
358 * even after the allocating plugin has been unloaded from memory.
360 * @param addr base address of the segment (as returned by shmat())
361 * @param length length (bytes) of the segment (as passed to shmget())
362 * @return NULL if an error occurred (in that case, shmdt(addr) is invoked
363 * before returning NULL).
365 block_t *block_shm_Alloc (void *addr, size_t length)
367 block_shm_t *block = malloc (sizeof (*block));
368 if (unlikely(block == NULL))
374 block_Init (&block->self, (uint8_t *)addr, length);
375 block->self.pf_release = block_shm_Release;
376 block->base_addr = addr;
380 block_t *block_shm_Alloc (void *addr, size_t length)
382 (void) addr; (void) length;
392 ssize_t pread (int fd, void *buf, size_t count, off_t offset)
394 HANDLE handle = (HANDLE)(intptr_t)_get_osfhandle (fd);
395 if (handle == INVALID_HANDLE_VALUE)
398 OVERLAPPED olap; olap.Offset = offset; olap.OffsetHigh = (offset >> 32);
400 /* This braindead API will override the file pointer even if we specify
401 * an explicit read offset... So do not expect this to mix well with
402 * regular read() calls. */
403 if (ReadFile (handle, buf, count, &written, &olap))
410 * Loads a file into a block of memory through a file descriptor.
411 * If possible a private file mapping is created. Otherwise, the file is read
412 * normally. This function is a cancellation point.
414 * @note On 32-bits platforms,
415 * this function will not work for very large files,
416 * due to memory space constraints.
418 * @param fd file descriptor to load from
419 * @return a new block with the file content at p_buffer, and file length at
420 * i_buffer (release it with block_Release()), or NULL upon error (see errno).
422 block_t *block_File (int fd)
427 /* First, get the file size */
431 /* st_size is meaningful for regular files, shared memory and typed memory.
432 * It's also meaning for symlinks, but that's not possible with fstat().
433 * In other cases, it's undefined, and we should really not go further. */
435 # define S_TYPEISSHM( buf ) (0)
437 if (S_ISDIR (st.st_mode))
442 if (!S_ISREG (st.st_mode) && !S_TYPEISSHM (&st))
448 /* Prevent an integer overflow in mmap() and malloc() */
449 if (st.st_size >= SIZE_MAX)
454 length = (size_t)st.st_size;
461 addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
462 if (addr != MAP_FAILED)
463 return block_mmap_Alloc (addr, length);
467 /* If mmap() is not implemented by the OS _or_ the filesystem... */
468 block_t *block = block_Alloc (length);
471 block_cleanup_push (block);
473 for (size_t i = 0; i < length;)
475 ssize_t len = pread (fd, block->p_buffer + i, length - i, i);
478 block_Release (block);
489 * Loads a file into a block of memory from the file path.
490 * See also block_File().
492 block_t *block_FilePath (const char *path)
494 int fd = vlc_open (path, O_RDONLY);
498 block_t *block = block_File (fd);
504 * @section Thread-safe block queue functions
508 * Internal state for block queues
512 vlc_mutex_t lock; /* fifo data lock */
513 vlc_cond_t wait; /**< Wait for data */
514 vlc_cond_t wait_room; /**< Wait for queue depth to shrink */
523 block_fifo_t *block_FifoNew( void )
525 block_fifo_t *p_fifo = malloc( sizeof( block_fifo_t ) );
529 vlc_mutex_init( &p_fifo->lock );
530 vlc_cond_init( &p_fifo->wait );
531 vlc_cond_init( &p_fifo->wait_room );
532 p_fifo->p_first = NULL;
533 p_fifo->pp_last = &p_fifo->p_first;
534 p_fifo->i_depth = p_fifo->i_size = 0;
535 p_fifo->b_force_wake = false;
540 void block_FifoRelease( block_fifo_t *p_fifo )
542 block_FifoEmpty( p_fifo );
543 vlc_cond_destroy( &p_fifo->wait_room );
544 vlc_cond_destroy( &p_fifo->wait );
545 vlc_mutex_destroy( &p_fifo->lock );
549 void block_FifoEmpty( block_fifo_t *p_fifo )
553 vlc_mutex_lock( &p_fifo->lock );
554 block = p_fifo->p_first;
557 p_fifo->i_depth = p_fifo->i_size = 0;
558 p_fifo->p_first = NULL;
559 p_fifo->pp_last = &p_fifo->p_first;
561 vlc_cond_broadcast( &p_fifo->wait_room );
562 vlc_mutex_unlock( &p_fifo->lock );
564 while (block != NULL)
569 block_Release (block);
575 * Wait until the FIFO gets below a certain size (if needed).
577 * Note that if more than one thread writes to the FIFO, you cannot assume that
578 * the FIFO is actually below the requested size upon return (since another
579 * thread could have refilled it already). This is typically not an issue, as
580 * this function is meant for (relaxed) congestion control.
582 * This function may be a cancellation point and it is cancel-safe.
584 * @param fifo queue to wait on
585 * @param max_depth wait until the queue has no more than this many blocks
586 * (use SIZE_MAX to ignore this constraint)
587 * @param max_size wait until the queue has no more than this many bytes
588 * (use SIZE_MAX to ignore this constraint)
591 void block_FifoPace (block_fifo_t *fifo, size_t max_depth, size_t max_size)
595 vlc_mutex_lock (&fifo->lock);
596 while ((fifo->i_depth > max_depth) || (fifo->i_size > max_size))
598 mutex_cleanup_push (&fifo->lock);
599 vlc_cond_wait (&fifo->wait_room, &fifo->lock);
602 vlc_mutex_unlock (&fifo->lock);
606 * Immediately queue one block at the end of a FIFO.
608 * @param block head of a block list to queue (may be NULL)
609 * @return total number of bytes appended to the queue
611 size_t block_FifoPut( block_fifo_t *p_fifo, block_t *p_block )
613 size_t i_size = 0, i_depth = 0;
618 for (p_last = p_block; ; p_last = p_last->p_next)
620 i_size += p_last->i_buffer;
626 vlc_mutex_lock (&p_fifo->lock);
627 *p_fifo->pp_last = p_block;
628 p_fifo->pp_last = &p_last->p_next;
629 p_fifo->i_depth += i_depth;
630 p_fifo->i_size += i_size;
631 /* We queued at least one block: wake up one read-waiting thread */
632 vlc_cond_signal( &p_fifo->wait );
633 vlc_mutex_unlock( &p_fifo->lock );
638 void block_FifoWake( block_fifo_t *p_fifo )
640 vlc_mutex_lock( &p_fifo->lock );
641 if( p_fifo->p_first == NULL )
642 p_fifo->b_force_wake = true;
643 vlc_cond_broadcast( &p_fifo->wait );
644 vlc_mutex_unlock( &p_fifo->lock );
648 * Dequeue the first block from the FIFO. If necessary, wait until there is
649 * one block in the queue. This function is (always) cancellation point.
651 * @return a valid block, or NULL if block_FifoWake() was called.
653 block_t *block_FifoGet( block_fifo_t *p_fifo )
659 vlc_mutex_lock( &p_fifo->lock );
660 mutex_cleanup_push( &p_fifo->lock );
662 /* Remember vlc_cond_wait() may cause spurious wakeups
663 * (on both Win32 and POSIX) */
664 while( ( p_fifo->p_first == NULL ) && !p_fifo->b_force_wake )
665 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
670 p_fifo->b_force_wake = false;
674 vlc_mutex_unlock( &p_fifo->lock );
678 p_fifo->p_first = b->p_next;
680 p_fifo->i_size -= b->i_buffer;
682 if( p_fifo->p_first == NULL )
684 p_fifo->pp_last = &p_fifo->p_first;
687 /* We don't know how many threads can queue new packets now. */
688 vlc_cond_broadcast( &p_fifo->wait_room );
689 vlc_mutex_unlock( &p_fifo->lock );
696 * Peeks the first block in the FIFO.
697 * If necessary, wait until there is one block.
698 * This function is (always) a cancellation point.
700 * @warning This function leaves the block in the FIFO.
701 * You need to protect against concurrent threads who could dequeue the block.
702 * Preferrably, there should be only one thread reading from the FIFO.
704 * @return a valid block.
706 block_t *block_FifoShow( block_fifo_t *p_fifo )
712 vlc_mutex_lock( &p_fifo->lock );
713 mutex_cleanup_push( &p_fifo->lock );
715 while( p_fifo->p_first == NULL )
716 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
724 /* FIXME: not thread-safe */
725 size_t block_FifoSize( const block_fifo_t *p_fifo )
727 return p_fifo->i_size;
730 /* FIXME: not thread-safe */
731 size_t block_FifoCount( const block_fifo_t *p_fifo )
733 return p_fifo->i_depth;