1 /*****************************************************************************
2 * block.c: Data blocks management functions
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
33 #include "vlc_block.h"
36 * @section Block handling functions.
40 * Internal state for heap block.
45 size_t i_allocated_buffer;
46 uint8_t p_allocated_buffer[];
50 static void BlockNoRelease( block_t *b )
52 fprintf( stderr, "block %p has no release callback! This is a bug!\n", b );
57 void block_Init( block_t *restrict b, void *buf, size_t size )
59 /* Fill all fields to their default */
63 b->i_dts = VLC_TS_INVALID;
69 b->pf_release = BlockNoRelease;
73 static void BlockRelease( block_t *p_block )
78 /* Memory alignment */
79 #define BLOCK_ALIGN 16
80 /* Initial size of reserved header and footer */
81 #define BLOCK_PADDING_SIZE 32
82 /* Maximum size of reserved footer before we release with realloc() */
83 #define BLOCK_WASTE_SIZE 2048
85 block_t *block_Alloc( size_t i_size )
87 /* We do only one malloc
88 * TODO: bench if doing 2 malloc but keeping a pool of buffer is better
91 * 2 * BLOCK_PADDING_SIZE -> pre + post padding
93 const size_t i_alloc = i_size + 2 * BLOCK_PADDING_SIZE + BLOCK_ALIGN;
94 block_sys_t *p_sys = malloc( sizeof( *p_sys ) + i_alloc );
99 /* Fill opaque data */
100 p_sys->i_allocated_buffer = i_alloc;
102 block_Init( &p_sys->self, p_sys->p_allocated_buffer + BLOCK_PADDING_SIZE
104 - ((uintptr_t)p_sys->p_allocated_buffer % BLOCK_ALIGN),
106 p_sys->self.pf_release = BlockRelease;
111 block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
113 block_sys_t *p_sys = (block_sys_t *)p_block;
114 ssize_t i_buffer_size = i_prebody + i_body;
116 if( i_buffer_size <= 0 )
118 block_Release( p_block );
122 if( p_block->pf_release != BlockRelease )
124 /* Special case when pf_release if overloaded
125 * TODO if used one day, then implement it in a smarter way */
126 block_t *p_dup = block_Duplicate( p_block );
127 block_Release( p_block );
132 p_sys = (block_sys_t *)p_block;
135 /* Adjust reserved header if there is enough room */
136 if( p_block->p_buffer - i_prebody > p_sys->p_allocated_buffer &&
137 p_block->p_buffer - i_prebody < p_sys->p_allocated_buffer +
138 p_sys->i_allocated_buffer )
140 p_block->p_buffer -= i_prebody;
141 p_block->i_buffer += i_prebody;
145 /* Adjust payload size if there is enough room */
146 if( p_block->p_buffer + i_body < p_sys->p_allocated_buffer +
147 p_sys->i_allocated_buffer )
149 p_block->i_buffer = i_buffer_size;
153 /* Not enough room, reallocate the buffer */
154 if( i_body > 0 || i_prebody > 0 )
156 /* FIXME: this is really dumb, we should use realloc() */
157 block_t *p_rea = block_New( NULL, i_buffer_size );
161 p_rea->i_dts = p_block->i_dts;
162 p_rea->i_pts = p_block->i_pts;
163 p_rea->i_flags = p_block->i_flags;
164 p_rea->i_length = p_block->i_length;
165 p_rea->i_rate = p_block->i_rate;
166 p_rea->i_samples = p_block->i_samples;
168 memcpy( p_rea->p_buffer + i_prebody, p_block->p_buffer,
169 __MIN( p_block->i_buffer, p_rea->i_buffer - i_prebody ) );
172 block_Release( p_block );
177 /* We have a very large reserved footer now? Release some of it.
178 * XXX it may not keep the algniment of p_buffer */
179 if( (p_sys->p_allocated_buffer + p_sys->i_allocated_buffer) -
180 (p_block->p_buffer + p_block->i_buffer) > BLOCK_WASTE_SIZE )
182 const ptrdiff_t i_prebody = p_block->p_buffer - p_sys->p_allocated_buffer;
183 const size_t i_new = i_prebody + p_block->i_buffer + 1 * BLOCK_PADDING_SIZE;
184 block_sys_t *p_new = realloc( p_sys, sizeof (*p_sys) + i_new );
189 p_sys->i_allocated_buffer = i_new;
190 p_block = &p_sys->self;
191 p_block->p_buffer = &p_sys->p_allocated_buffer[i_prebody];
198 # include <sys/mman.h>
200 typedef struct block_mmap_t
207 static void block_mmap_Release (block_t *block)
209 block_mmap_t *p_sys = (block_mmap_t *)block;
211 munmap (p_sys->base_addr, p_sys->length);
216 * Creates a block from a virtual address memory mapping (mmap).
217 * This is provided by LibVLC so that mmap blocks can safely be deallocated
218 * even after the allocating plugin has been unloaded from memory.
220 * @param addr base address of the mapping (as returned by mmap)
221 * @param length length (bytes) of the mapping (as passed to mmap)
222 * @return NULL if addr is MAP_FAILED, or an error occurred (in the later
223 * case, munmap(addr, length) is invoked before returning).
225 block_t *block_mmap_Alloc (void *addr, size_t length)
227 if (addr == MAP_FAILED)
230 block_mmap_t *block = malloc (sizeof (*block));
233 munmap (addr, length);
237 block_Init (&block->self, (uint8_t *)addr, length);
238 block->self.pf_release = block_mmap_Release;
239 block->base_addr = addr;
240 block->length = length;
244 block_t *block_mmap_Alloc (void *addr, size_t length)
246 (void)addr; (void)length; return NULL;
253 #define _get_osfhandle(a) ((long) (a))
257 ssize_t pread (int fd, void *buf, size_t count, off_t offset)
259 HANDLE handle = (HANDLE)(intptr_t)_get_osfhandle (fd);
260 if (handle == INVALID_HANDLE_VALUE)
263 OVERLAPPED olap = { .Offset = offset, .OffsetHigh = (offset >> 32), };
265 /* This braindead API will override the file pointer even if we specify
266 * an explicit read offset... So do not expect this to mix well with
267 * regular read() calls. */
268 if (ReadFile (handle, buf, count, &written, &olap))
275 * Loads a file into a block of memory. If possible a private file mapping is
276 * created. Otherwise, the file is read normally. On 32-bits platforms, this
277 * function will not work for very large files, due to memory space
278 * constraints. Cancellation point.
280 * @param fd file descriptor to load from
281 * @return a new block with the file content at p_buffer, and file length at
282 * i_buffer (release it with block_Release()), or NULL upon error (see errno).
284 block_t *block_File (int fd)
289 /* First, get the file size */
293 /* st_size is meaningful for regular files, shared memory and typed memory.
294 * It's also meaning for symlinks, but that's not possible with fstat().
295 * In other cases, it's undefined, and we should really not go further. */
297 # define S_TYPEISSHM( buf ) (0)
299 if (S_ISDIR (st.st_mode))
304 if (!S_ISREG (st.st_mode) && !S_TYPEISSHM (&st))
310 /* Prevent an integer overflow in mmap() and malloc() */
311 if (st.st_size >= SIZE_MAX)
316 length = (size_t)st.st_size;
323 addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
324 if (addr != MAP_FAILED)
325 return block_mmap_Alloc (addr, length);
329 /* If mmap() is not implemented by the OS _or_ the filesystem... */
330 block_t *block = block_Alloc (length);
333 block_cleanup_push (block);
335 for (size_t i = 0; i < length;)
337 ssize_t len = pread (fd, block->p_buffer + i, length - i, i);
340 block_Release (block);
351 * @section Thread-safe block queue functions
355 * Internal state for block queues
359 vlc_mutex_t lock; /* fifo data lock */
360 vlc_cond_t wait; /**< Wait for data */
361 vlc_cond_t wait_room; /**< Wait for queue depth to shrink */
370 block_fifo_t *block_FifoNew( void )
372 block_fifo_t *p_fifo = malloc( sizeof( block_fifo_t ) );
376 vlc_mutex_init( &p_fifo->lock );
377 vlc_cond_init( &p_fifo->wait );
378 vlc_cond_init( &p_fifo->wait_room );
379 p_fifo->p_first = NULL;
380 p_fifo->pp_last = &p_fifo->p_first;
381 p_fifo->i_depth = p_fifo->i_size = 0;
382 p_fifo->b_force_wake = false;
387 void block_FifoRelease( block_fifo_t *p_fifo )
389 block_FifoEmpty( p_fifo );
390 vlc_cond_destroy( &p_fifo->wait_room );
391 vlc_cond_destroy( &p_fifo->wait );
392 vlc_mutex_destroy( &p_fifo->lock );
396 void block_FifoEmpty( block_fifo_t *p_fifo )
400 vlc_mutex_lock( &p_fifo->lock );
401 for( b = p_fifo->p_first; b != NULL; )
410 p_fifo->i_depth = p_fifo->i_size = 0;
411 p_fifo->p_first = NULL;
412 p_fifo->pp_last = &p_fifo->p_first;
413 vlc_cond_broadcast( &p_fifo->wait_room );
414 vlc_mutex_unlock( &p_fifo->lock );
418 * Wait until the FIFO gets below a certain size (if needed).
420 * Note that if more than one thread writes to the FIFO, you cannot assume that
421 * the FIFO is actually below the requested size upon return (since another
422 * thread could have refilled it already). This is typically not an issue, as
423 * this function is meant for (relaxed) congestion control.
425 * This function may be a cancellation point and it is cancel-safe.
427 * @param fifo queue to wait on
428 * @param max_depth wait until the queue has no more than this many blocks
429 * (use SIZE_MAX to ignore this constraint)
430 * @param max_size wait until the queue has no more than this many bytes
431 * (use SIZE_MAX to ignore this constraint)
434 void block_FifoPace (block_fifo_t *fifo, size_t max_depth, size_t max_size)
438 vlc_mutex_lock (&fifo->lock);
439 while ((fifo->i_depth > max_depth) || (fifo->i_size > max_size))
441 mutex_cleanup_push (&fifo->lock);
442 vlc_cond_wait (&fifo->wait_room, &fifo->lock);
445 vlc_mutex_unlock (&fifo->lock);
449 * Immediately queue one block at the end of a FIFO.
451 * @param block head of a block list to queue (may be NULL)
453 size_t block_FifoPut( block_fifo_t *p_fifo, block_t *p_block )
456 vlc_mutex_lock( &p_fifo->lock );
458 while (p_block != NULL)
460 i_size += p_block->i_buffer;
462 *p_fifo->pp_last = p_block;
463 p_fifo->pp_last = &p_block->p_next;
465 p_fifo->i_size += p_block->i_buffer;
467 p_block = p_block->p_next;
470 /* We queued one block: wake up one read-waiting thread */
471 vlc_cond_signal( &p_fifo->wait );
472 vlc_mutex_unlock( &p_fifo->lock );
477 void block_FifoWake( block_fifo_t *p_fifo )
479 vlc_mutex_lock( &p_fifo->lock );
480 if( p_fifo->p_first == NULL )
481 p_fifo->b_force_wake = true;
482 vlc_cond_broadcast( &p_fifo->wait );
483 vlc_mutex_unlock( &p_fifo->lock );
486 block_t *block_FifoGet( block_fifo_t *p_fifo )
492 vlc_mutex_lock( &p_fifo->lock );
493 mutex_cleanup_push( &p_fifo->lock );
495 /* Remember vlc_cond_wait() may cause spurious wakeups
496 * (on both Win32 and POSIX) */
497 while( ( p_fifo->p_first == NULL ) && !p_fifo->b_force_wake )
498 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
503 p_fifo->b_force_wake = false;
507 vlc_mutex_unlock( &p_fifo->lock );
511 p_fifo->p_first = b->p_next;
513 p_fifo->i_size -= b->i_buffer;
515 if( p_fifo->p_first == NULL )
517 p_fifo->pp_last = &p_fifo->p_first;
520 /* We don't know how many threads can queue new packets now. */
521 vlc_cond_broadcast( &p_fifo->wait_room );
522 vlc_mutex_unlock( &p_fifo->lock );
528 block_t *block_FifoShow( block_fifo_t *p_fifo )
534 vlc_mutex_lock( &p_fifo->lock );
535 mutex_cleanup_push( &p_fifo->lock );
537 while( p_fifo->p_first == NULL )
538 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
546 /* FIXME: not thread-safe */
547 size_t block_FifoSize( const block_fifo_t *p_fifo )
549 return p_fifo->i_size;
552 /* FIXME: not thread-safe */
553 size_t block_FifoCount( const block_fifo_t *p_fifo )
555 return p_fifo->i_depth;