audiobargraph_v: simplify callback
[vlc/gmpfix.git] / src / misc / block.c
blob3e953f167ab8f5d78aed4996b7f4dbf932ad99dc
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <sys/stat.h>
32 #include <assert.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <fcntl.h>
37 #include <vlc_common.h>
38 #include <vlc_block.h>
39 #include <vlc_fs.h>
41 /**
42 * @section Block handling functions.
45 #ifndef NDEBUG
46 static void BlockNoRelease( block_t *b )
48 fprintf( stderr, "block %p has no release callback! This is a bug!\n", b );
49 abort();
52 static void block_Check (block_t *block)
54 while (block != NULL)
56 unsigned char *start = block->p_start;
57 unsigned char *end = block->p_start + block->i_size;
58 unsigned char *bufstart = block->p_buffer;
59 unsigned char *bufend = block->p_buffer + block->i_buffer;
61 assert (block->pf_release != BlockNoRelease);
62 assert (start <= end);
63 assert (bufstart <= bufend);
64 assert (bufstart >= start);
65 assert (bufend <= end);
67 block = block->p_next;
71 static void block_Invalidate (block_t *block)
73 block->p_next = NULL;
74 block_Check (block);
75 block->pf_release = BlockNoRelease;
77 #else
78 # define block_Check(b) ((void)(b))
79 # define block_Invalidate(b) ((void)(b))
80 #endif
82 void block_Init( block_t *restrict b, void *buf, size_t size )
84 /* Fill all fields to their default */
85 b->p_next = NULL;
86 b->p_buffer = buf;
87 b->i_buffer = size;
88 b->p_start = buf;
89 b->i_size = size;
90 b->i_flags = 0;
91 b->i_nb_samples = 0;
92 b->i_pts =
93 b->i_dts = VLC_TS_INVALID;
94 b->i_length = 0;
95 #ifndef NDEBUG
96 b->pf_release = BlockNoRelease;
97 #endif
100 static void block_generic_Release (block_t *block)
102 /* That is always true for blocks allocated with block_Alloc(). */
103 assert (block->p_start == (unsigned char *)(block + 1));
104 block_Invalidate (block);
105 free (block);
108 static void BlockMetaCopy( block_t *restrict out, const block_t *in )
110 out->p_next = in->p_next;
111 out->i_nb_samples = in->i_nb_samples;
112 out->i_dts = in->i_dts;
113 out->i_pts = in->i_pts;
114 out->i_flags = in->i_flags;
115 out->i_length = in->i_length;
118 /** Initial memory alignment of data block.
119 * @note This must be a multiple of sizeof(void*) and a power of two.
120 * libavcodec AVX optimizations require at least 32-bytes. */
121 #define BLOCK_ALIGN 32
123 /** Initial reserved header and footer size. */
124 #define BLOCK_PADDING 32
126 /* Maximum size of reserved footer before shrinking with realloc(). */
127 #define BLOCK_WASTE_SIZE 2048
129 block_t *block_Alloc (size_t size)
131 /* 2 * BLOCK_PADDING: pre + post padding */
132 const size_t alloc = sizeof (block_t) + BLOCK_ALIGN + (2 * BLOCK_PADDING)
133 + size;
134 if (unlikely(alloc <= size))
135 return NULL;
137 block_t *b = malloc (alloc);
138 if (unlikely(b == NULL))
139 return NULL;
141 block_Init (b, b + 1, alloc - sizeof (*b));
142 static_assert ((BLOCK_PADDING % BLOCK_ALIGN) == 0,
143 "BLOCK_PADDING must be a multiple of BLOCK_ALIGN");
144 b->p_buffer += BLOCK_PADDING + BLOCK_ALIGN - 1;
145 b->p_buffer = (void *)(((uintptr_t)b->p_buffer) & ~(BLOCK_ALIGN - 1));
146 b->i_buffer = size;
147 b->pf_release = block_generic_Release;
148 return b;
151 block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
153 size_t requested = i_prebody + i_body;
155 block_Check( p_block );
157 /* Corner case: empty block requested */
158 if( i_prebody <= 0 && i_body <= (size_t)(-i_prebody) )
160 block_Release( p_block );
161 return NULL;
164 assert( p_block->p_start <= p_block->p_buffer );
165 assert( p_block->p_start + p_block->i_size
166 >= p_block->p_buffer + p_block->i_buffer );
168 /* Corner case: the current payload is discarded completely */
169 if( i_prebody <= 0 && p_block->i_buffer <= (size_t)-i_prebody )
170 p_block->i_buffer = 0; /* discard current payload */
171 if( p_block->i_buffer == 0 )
173 if( requested <= p_block->i_size )
174 { /* Enough room: recycle buffer */
175 size_t extra = p_block->i_size - requested;
177 p_block->p_buffer = p_block->p_start + (extra / 2);
178 p_block->i_buffer = requested;
179 return p_block;
181 /* Not enough room: allocate a new buffer */
182 block_t *p_rea = block_Alloc( requested );
183 if( p_rea )
184 BlockMetaCopy( p_rea, p_block );
185 block_Release( p_block );
186 return p_rea;
189 /* First, shrink payload */
191 /* Pull payload start */
192 if( i_prebody < 0 )
194 assert( p_block->i_buffer >= (size_t)-i_prebody );
195 p_block->p_buffer -= i_prebody;
196 p_block->i_buffer += i_prebody;
197 i_body += i_prebody;
198 i_prebody = 0;
201 /* Trim payload end */
202 if( p_block->i_buffer > i_body )
203 p_block->i_buffer = i_body;
205 uint8_t *p_start = p_block->p_start;
206 uint8_t *p_end = p_start + p_block->i_size;
208 /* Second, reallocate the buffer if we lack space. This is done now to
209 * minimize the payload size for memory copy. */
210 assert( i_prebody >= 0 );
211 if( (size_t)(p_block->p_buffer - p_start) < (size_t)i_prebody
212 || (size_t)(p_end - p_block->p_buffer) < i_body )
214 block_t *p_rea = block_Alloc( requested );
215 if( p_rea )
217 BlockMetaCopy( p_rea, p_block );
218 p_rea->p_buffer += i_prebody;
219 p_rea->i_buffer -= i_prebody;
220 memcpy( p_rea->p_buffer, p_block->p_buffer, p_block->i_buffer );
222 block_Release( p_block );
223 if( p_rea == NULL )
224 return NULL;
225 p_block = p_rea;
227 else
228 /* We have a very large reserved footer now? Release some of it.
229 * XXX it might not preserve the alignment of p_buffer */
230 if( p_end - (p_block->p_buffer + i_body) > BLOCK_WASTE_SIZE )
232 block_t *p_rea = block_Alloc( requested );
233 if( p_rea )
235 BlockMetaCopy( p_rea, p_block );
236 p_rea->p_buffer += i_prebody;
237 p_rea->i_buffer -= i_prebody;
238 memcpy( p_rea->p_buffer, p_block->p_buffer, p_block->i_buffer );
239 block_Release( p_block );
240 p_block = p_rea;
244 /* NOTE: p_start and p_end are corrupted from this point */
246 /* Third, expand payload */
248 /* Push payload start */
249 if( i_prebody > 0 )
251 p_block->p_buffer -= i_prebody;
252 p_block->i_buffer += i_prebody;
253 i_body += i_prebody;
254 i_prebody = 0;
257 /* Expand payload to requested size */
258 p_block->i_buffer = i_body;
260 return p_block;
264 static void block_heap_Release (block_t *block)
266 block_Invalidate (block);
267 free (block->p_start);
268 free (block);
272 * Creates a block from a heap allocation.
273 * This is provided by LibVLC so that manually heap-allocated blocks can safely
274 * be deallocated even after the origin plugin has been unloaded from memory.
276 * When block_Release() is called, VLC will free() the specified pointer.
278 * @param ptr base address of the heap allocation (will be free()'d)
279 * @param length bytes length of the heap allocation
280 * @return NULL in case of error (ptr free()'d in that case), or a valid
281 * block_t pointer.
283 block_t *block_heap_Alloc (void *addr, size_t length)
285 block_t *block = malloc (sizeof (*block));
286 if (block == NULL)
288 free (addr);
289 return NULL;
292 block_Init (block, addr, length);
293 block->pf_release = block_heap_Release;
294 return block;
297 #ifdef HAVE_MMAP
298 # include <sys/mman.h>
300 static void block_mmap_Release (block_t *block)
302 block_Invalidate (block);
303 munmap (block->p_start, block->i_size);
304 free (block);
308 * Creates a block from a virtual address memory mapping (mmap).
309 * This is provided by LibVLC so that mmap blocks can safely be deallocated
310 * even after the allocating plugin has been unloaded from memory.
312 * @param addr base address of the mapping (as returned by mmap)
313 * @param length length (bytes) of the mapping (as passed to mmap)
314 * @return NULL if addr is MAP_FAILED, or an error occurred (in the later
315 * case, munmap(addr, length) is invoked before returning).
317 block_t *block_mmap_Alloc (void *addr, size_t length)
319 if (addr == MAP_FAILED)
320 return NULL;
322 block_t *block = malloc (sizeof (*block));
323 if (block == NULL)
325 munmap (addr, length);
326 return NULL;
329 block_Init (block, addr, length);
330 block->pf_release = block_mmap_Release;
331 return block;
333 #else
334 block_t *block_mmap_Alloc (void *addr, size_t length)
336 (void)addr; (void)length; return NULL;
338 #endif
340 #ifdef HAVE_SYS_SHM_H
341 # include <sys/shm.h>
343 typedef struct block_shm_t
345 block_t self;
346 void *base_addr;
347 } block_shm_t;
349 static void block_shm_Release (block_t *block)
351 block_shm_t *p_sys = (block_shm_t *)block;
353 shmdt (p_sys->base_addr);
354 free (p_sys);
358 * Creates a block from a System V shared memory segment (shmget()).
359 * This is provided by LibVLC so that segments can safely be deallocated
360 * even after the allocating plugin has been unloaded from memory.
362 * @param addr base address of the segment (as returned by shmat())
363 * @param length length (bytes) of the segment (as passed to shmget())
364 * @return NULL if an error occurred (in that case, shmdt(addr) is invoked
365 * before returning NULL).
367 block_t *block_shm_Alloc (void *addr, size_t length)
369 block_shm_t *block = malloc (sizeof (*block));
370 if (unlikely(block == NULL))
372 shmdt (addr);
373 return NULL;
376 block_Init (&block->self, (uint8_t *)addr, length);
377 block->self.pf_release = block_shm_Release;
378 block->base_addr = addr;
379 return &block->self;
381 #else
382 block_t *block_shm_Alloc (void *addr, size_t length)
384 (void) addr; (void) length;
385 abort ();
387 #endif
390 #ifdef _WIN32
391 # include <io.h>
393 static
394 ssize_t pread (int fd, void *buf, size_t count, off_t offset)
396 HANDLE handle = (HANDLE)(intptr_t)_get_osfhandle (fd);
397 if (handle == INVALID_HANDLE_VALUE)
398 return -1;
400 OVERLAPPED olap; olap.Offset = offset; olap.OffsetHigh = (offset >> 32);
401 DWORD written;
402 /* This braindead API will override the file pointer even if we specify
403 * an explicit read offset... So do not expect this to mix well with
404 * regular read() calls. */
405 if (ReadFile (handle, buf, count, &written, &olap))
406 return written;
407 return -1;
409 #endif
412 * Loads a file into a block of memory through a file descriptor.
413 * If possible a private file mapping is created. Otherwise, the file is read
414 * normally. This function is a cancellation point.
416 * @note On 32-bits platforms,
417 * this function will not work for very large files,
418 * due to memory space constraints.
420 * @param fd file descriptor to load from
421 * @return a new block with the file content at p_buffer, and file length at
422 * i_buffer (release it with block_Release()), or NULL upon error (see errno).
424 block_t *block_File (int fd)
426 size_t length;
427 struct stat st;
429 /* First, get the file size */
430 if (fstat (fd, &st))
431 return NULL;
433 /* st_size is meaningful for regular files, shared memory and typed memory.
434 * It's also meaning for symlinks, but that's not possible with fstat().
435 * In other cases, it's undefined, and we should really not go further. */
436 #ifndef S_TYPEISSHM
437 # define S_TYPEISSHM( buf ) (0)
438 #endif
439 if (S_ISDIR (st.st_mode))
441 errno = EISDIR;
442 return NULL;
444 if (!S_ISREG (st.st_mode) && !S_TYPEISSHM (&st))
446 errno = ESPIPE;
447 return NULL;
450 /* Prevent an integer overflow in mmap() and malloc() */
451 if ((uintmax_t)st.st_size >= SIZE_MAX)
453 errno = ENOMEM;
454 return NULL;
456 length = (size_t)st.st_size;
458 #ifdef HAVE_MMAP
459 if (length > 0)
461 void *addr;
463 addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
464 if (addr != MAP_FAILED)
465 return block_mmap_Alloc (addr, length);
467 #endif
469 /* If mmap() is not implemented by the OS _or_ the filesystem... */
470 block_t *block = block_Alloc (length);
471 if (block == NULL)
472 return NULL;
473 block_cleanup_push (block);
475 for (size_t i = 0; i < length;)
477 ssize_t len = pread (fd, block->p_buffer + i, length - i, i);
478 if (len == -1)
480 block_Release (block);
481 block = NULL;
482 break;
484 i += len;
486 vlc_cleanup_pop ();
487 return block;
491 * Loads a file into a block of memory from the file path.
492 * See also block_File().
494 block_t *block_FilePath (const char *path)
496 int fd = vlc_open (path, O_RDONLY);
497 if (fd == -1)
498 return NULL;
500 block_t *block = block_File (fd);
501 close (fd);
502 return block;
506 * @section Thread-safe block queue functions
510 * Internal state for block queues
512 struct block_fifo_t
514 vlc_mutex_t lock; /* fifo data lock */
515 vlc_cond_t wait; /**< Wait for data */
516 vlc_cond_t wait_room; /**< Wait for queue depth to shrink */
518 block_t *p_first;
519 block_t **pp_last;
520 size_t i_depth;
521 size_t i_size;
522 bool b_force_wake;
525 block_fifo_t *block_FifoNew( void )
527 block_fifo_t *p_fifo = malloc( sizeof( block_fifo_t ) );
528 if( !p_fifo )
529 return NULL;
531 vlc_mutex_init( &p_fifo->lock );
532 vlc_cond_init( &p_fifo->wait );
533 vlc_cond_init( &p_fifo->wait_room );
534 p_fifo->p_first = NULL;
535 p_fifo->pp_last = &p_fifo->p_first;
536 p_fifo->i_depth = p_fifo->i_size = 0;
537 p_fifo->b_force_wake = false;
539 return p_fifo;
542 void block_FifoRelease( block_fifo_t *p_fifo )
544 block_FifoEmpty( p_fifo );
545 vlc_cond_destroy( &p_fifo->wait_room );
546 vlc_cond_destroy( &p_fifo->wait );
547 vlc_mutex_destroy( &p_fifo->lock );
548 free( p_fifo );
551 void block_FifoEmpty( block_fifo_t *p_fifo )
553 block_t *block;
555 vlc_mutex_lock( &p_fifo->lock );
556 block = p_fifo->p_first;
557 if (block != NULL)
559 p_fifo->i_depth = p_fifo->i_size = 0;
560 p_fifo->p_first = NULL;
561 p_fifo->pp_last = &p_fifo->p_first;
563 vlc_cond_broadcast( &p_fifo->wait_room );
564 vlc_mutex_unlock( &p_fifo->lock );
566 while (block != NULL)
568 block_t *buf;
570 buf = block->p_next;
571 block_Release (block);
572 block = buf;
577 * Wait until the FIFO gets below a certain size (if needed).
579 * Note that if more than one thread writes to the FIFO, you cannot assume that
580 * the FIFO is actually below the requested size upon return (since another
581 * thread could have refilled it already). This is typically not an issue, as
582 * this function is meant for (relaxed) congestion control.
584 * This function may be a cancellation point and it is cancel-safe.
586 * @param fifo queue to wait on
587 * @param max_depth wait until the queue has no more than this many blocks
588 * (use SIZE_MAX to ignore this constraint)
589 * @param max_size wait until the queue has no more than this many bytes
590 * (use SIZE_MAX to ignore this constraint)
591 * @return nothing.
593 void block_FifoPace (block_fifo_t *fifo, size_t max_depth, size_t max_size)
595 vlc_testcancel ();
597 vlc_mutex_lock (&fifo->lock);
598 while ((fifo->i_depth > max_depth) || (fifo->i_size > max_size))
600 mutex_cleanup_push (&fifo->lock);
601 vlc_cond_wait (&fifo->wait_room, &fifo->lock);
602 vlc_cleanup_pop ();
604 vlc_mutex_unlock (&fifo->lock);
608 * Immediately queue one block at the end of a FIFO.
609 * @param fifo queue
610 * @param block head of a block list to queue (may be NULL)
611 * @return total number of bytes appended to the queue
613 size_t block_FifoPut( block_fifo_t *p_fifo, block_t *p_block )
615 size_t i_size = 0, i_depth = 0;
616 block_t *p_last;
618 if (p_block == NULL)
619 return 0;
620 for (p_last = p_block; ; p_last = p_last->p_next)
622 i_size += p_last->i_buffer;
623 i_depth++;
624 if (!p_last->p_next)
625 break;
628 vlc_mutex_lock (&p_fifo->lock);
629 *p_fifo->pp_last = p_block;
630 p_fifo->pp_last = &p_last->p_next;
631 p_fifo->i_depth += i_depth;
632 p_fifo->i_size += i_size;
633 /* We queued at least one block: wake up one read-waiting thread */
634 vlc_cond_signal( &p_fifo->wait );
635 vlc_mutex_unlock( &p_fifo->lock );
637 return i_size;
640 void block_FifoWake( block_fifo_t *p_fifo )
642 vlc_mutex_lock( &p_fifo->lock );
643 if( p_fifo->p_first == NULL )
644 p_fifo->b_force_wake = true;
645 vlc_cond_broadcast( &p_fifo->wait );
646 vlc_mutex_unlock( &p_fifo->lock );
650 * Dequeue the first block from the FIFO. If necessary, wait until there is
651 * one block in the queue. This function is (always) cancellation point.
653 * @return a valid block, or NULL if block_FifoWake() was called.
655 block_t *block_FifoGet( block_fifo_t *p_fifo )
657 block_t *b;
659 vlc_testcancel( );
661 vlc_mutex_lock( &p_fifo->lock );
662 mutex_cleanup_push( &p_fifo->lock );
664 /* Remember vlc_cond_wait() may cause spurious wakeups
665 * (on both Win32 and POSIX) */
666 while( ( p_fifo->p_first == NULL ) && !p_fifo->b_force_wake )
667 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
669 vlc_cleanup_pop();
670 b = p_fifo->p_first;
672 p_fifo->b_force_wake = false;
673 if( b == NULL )
675 /* Forced wakeup */
676 vlc_mutex_unlock( &p_fifo->lock );
677 return NULL;
680 p_fifo->p_first = b->p_next;
681 p_fifo->i_depth--;
682 p_fifo->i_size -= b->i_buffer;
684 if( p_fifo->p_first == NULL )
686 p_fifo->pp_last = &p_fifo->p_first;
689 /* We don't know how many threads can queue new packets now. */
690 vlc_cond_broadcast( &p_fifo->wait_room );
691 vlc_mutex_unlock( &p_fifo->lock );
693 b->p_next = NULL;
694 return b;
698 * Peeks the first block in the FIFO.
699 * If necessary, wait until there is one block.
700 * This function is (always) a cancellation point.
702 * @warning This function leaves the block in the FIFO.
703 * You need to protect against concurrent threads who could dequeue the block.
704 * Preferrably, there should be only one thread reading from the FIFO.
706 * @return a valid block.
708 block_t *block_FifoShow( block_fifo_t *p_fifo )
710 block_t *b;
712 vlc_testcancel( );
714 vlc_mutex_lock( &p_fifo->lock );
715 mutex_cleanup_push( &p_fifo->lock );
717 while( p_fifo->p_first == NULL )
718 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
720 b = p_fifo->p_first;
722 vlc_cleanup_run ();
723 return b;
726 /* FIXME: not thread-safe */
727 size_t block_FifoSize( const block_fifo_t *p_fifo )
729 return p_fifo->i_size;
732 /* FIXME: not thread-safe */
733 size_t block_FifoCount( const block_fifo_t *p_fifo )
735 return p_fifo->i_depth;