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"
35 /*****************************************************************************
37 *****************************************************************************/
42 size_t i_allocated_buffer;
43 uint8_t p_allocated_buffer[0];
47 static void BlockNoRelease( block_t *b )
49 fprintf( stderr, "block %p has no release callback! This is a bug!\n", b );
54 void block_Init( block_t *restrict b, void *buf, size_t size )
56 /* Fill all fields to their default */
57 b->p_next = b->p_prev = NULL;
59 b->i_pts = b->i_dts = b->i_length = 0;
64 b->pf_release = BlockNoRelease;
68 static void BlockRelease( block_t *p_block )
73 /* Memory alignment */
74 #define BLOCK_ALIGN 16
75 /* Initial size of reserved header and footer */
76 #define BLOCK_PADDING_SIZE 32
77 /* Maximum size of reserved footer before we release with realloc() */
78 #define BLOCK_WASTE_SIZE 2048
80 block_t *block_Alloc( size_t i_size )
82 /* We do only one malloc
83 * TODO: bench if doing 2 malloc but keeping a pool of buffer is better
86 * 2 * BLOCK_PADDING_SIZE -> pre + post padding
88 const size_t i_alloc = i_size + 2 * BLOCK_PADDING_SIZE + BLOCK_ALIGN;
89 block_sys_t *p_sys = malloc( sizeof( *p_sys ) + i_alloc );
94 /* Fill opaque data */
95 p_sys->i_allocated_buffer = i_alloc;
97 block_Init( &p_sys->self, p_sys->p_allocated_buffer + BLOCK_PADDING_SIZE
99 - ((uintptr_t)p_sys->p_allocated_buffer % BLOCK_ALIGN),
101 p_sys->self.pf_release = BlockRelease;
106 block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
108 block_sys_t *p_sys = (block_sys_t *)p_block;
109 ssize_t i_buffer_size = i_prebody + i_body;
111 if( i_buffer_size <= 0 )
113 block_Release( p_block );
117 if( p_block->pf_release != BlockRelease )
119 /* Special case when pf_release if overloaded
120 * TODO if used one day, then implement it in a smarter way */
121 block_t *p_dup = block_Duplicate( p_block );
122 block_Release( p_block );
129 /* Adjust reserved header if there is enough room */
130 if( p_block->p_buffer - i_prebody > p_sys->p_allocated_buffer &&
131 p_block->p_buffer - i_prebody < p_sys->p_allocated_buffer +
132 p_sys->i_allocated_buffer )
134 p_block->p_buffer -= i_prebody;
135 p_block->i_buffer += i_prebody;
139 /* Adjust payload size if there is enough room */
140 if( p_block->p_buffer + i_body < p_sys->p_allocated_buffer +
141 p_sys->i_allocated_buffer )
143 p_block->i_buffer = i_buffer_size;
147 /* Not enough room, reallocate the buffer */
148 if( i_body > 0 || i_prebody > 0 )
150 /* FIXME: this is really dumb, we should use realloc() */
151 block_t *p_rea = block_New( NULL, i_buffer_size );
155 p_rea->i_dts = p_block->i_dts;
156 p_rea->i_pts = p_block->i_pts;
157 p_rea->i_flags = p_block->i_flags;
158 p_rea->i_length = p_block->i_length;
159 p_rea->i_rate = p_block->i_rate;
160 p_rea->i_samples = p_block->i_samples;
162 memcpy( p_rea->p_buffer + i_prebody, p_block->p_buffer,
163 __MIN( p_block->i_buffer, p_rea->i_buffer - i_prebody ) );
166 block_Release( p_block );
172 /* Shrinking the buffer seems to cause performance problems, on Windows,
173 * at least. Also with demand paging, oversizing buffers is not an issue,
174 * as long as we don't write to the extraneous allocated space. */
175 /* We have a very large reserved footer now? Release some of it. */
176 if ((p_sys->p_allocated_buffer + p_sys->i_allocated_buffer) -
177 (p_block->p_buffer + p_block->i_buffer) > BLOCK_WASTE_SIZE)
179 const size_t news = p_block->i_buffer + 2 * BLOCK_PADDING_SIZE + 16;
180 block_sys_t *newb = realloc (p_sys, sizeof (*p_sys) + news);
185 p_sys->i_allocated_buffer = news;
186 p_block = &p_sys->self;
187 p_block->p_buffer = p_sys->p_allocated_buffer + BLOCK_PADDING_SIZE
189 - ((uintptr_t)p_sys->p_allocated_buffer % BLOCK_ALIGN);
197 # include <sys/mman.h>
199 typedef struct block_mmap_t
206 static void block_mmap_Release (block_t *block)
208 block_mmap_t *p_sys = (block_mmap_t *)block;
210 munmap (p_sys->base_addr, p_sys->length);
215 * Creates a block from a virtual address memory mapping (mmap).
216 * This is provided by LibVLC so that mmap blocks can safely be deallocated
217 * even after the allocating plugin has been unloaded from memory.
219 * @param addr base address of the mapping (as returned by mmap)
220 * @param length length (bytes) of the mapping (as passed to mmap)
221 * @return NULL if addr is MAP_FAILED, or an error occurred (in the later
222 * case, munmap(addr, length) is invoked before returning).
224 block_t *block_mmap_Alloc (void *addr, size_t length)
226 if (addr == MAP_FAILED)
229 block_mmap_t *block = malloc (sizeof (*block));
232 munmap (addr, length);
236 block_Init (&block->self, (uint8_t *)addr, length);
237 block->self.pf_release = block_mmap_Release;
238 block->base_addr = addr;
239 block->length = length;
243 block_t *block_mmap_Alloc (void *addr, size_t length)
245 (void)addr; (void)length; return NULL;
252 ssize_t pread (int fd, void *buf, size_t count, off_t offset)
254 HANDLE handle = (HANDLE)(intptr_t)_get_osfhandle (fd);
255 if (handle == INVALID_HANDLE_VALUE)
258 OVERLAPPED olap = { .Offset = offset, .OffsetHigh = (offset >> 32), };
260 /* This braindead API will override the file pointer even if we specify
261 * an explicit read offset... So do not expect this to mix well with
262 * regular read() calls. */
263 if (ReadFile (handle, buf, count, &written, &olap))
270 * Loads a file into a block of memory. If possible a private file mapping is
271 * created. Otherwise, the file is read normally. On 32-bits platforms, this
272 * function will not work for very large files, due to memory space
273 * constraints. Cancellation point.
275 * @param fd file descriptor to load from
276 * @return a new block with the file content at p_buffer, and file length at
277 * i_buffer (release it with block_Release()), or NULL upon error (see errno).
279 block_t *block_File (int fd)
284 /* First, get the file size */
288 /* st_size is meaningful for regular files, shared memory and typed memory.
289 * It's also meaning for symlinks, but that's not possible with fstat().
290 * In other cases, it's undefined, and we should really not go further. */
292 # define S_TYPEISSHM( buf ) (0)
294 if (S_ISDIR (st.st_mode))
299 if (!S_ISREG (st.st_mode) && !S_TYPEISSHM (&st))
305 /* Prevent an integer overflow in mmap() and malloc() */
306 if (st.st_size >= SIZE_MAX)
311 length = (size_t)st.st_size;
318 addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
319 if (addr != MAP_FAILED)
320 return block_mmap_Alloc (addr, length);
324 /* If mmap() is not implemented by the OS _or_ the filesystem... */
325 block_t *block = block_Alloc (length);
328 block_cleanup_push (block);
330 for (size_t i = 0; i < length;)
332 ssize_t len = pread (fd, block->p_buffer + i, length - i, i);
335 block_Release (block);
345 /*****************************************************************************
346 * block_fifo_t management
347 *****************************************************************************/
350 vlc_mutex_t lock; /* fifo data lock */
351 vlc_cond_t wait; /* fifo data conditional variable */
360 block_fifo_t *block_FifoNew( void )
362 block_fifo_t *p_fifo = malloc( sizeof( block_fifo_t ) );
366 vlc_mutex_init( &p_fifo->lock );
367 vlc_cond_init( NULL, &p_fifo->wait );
368 p_fifo->p_first = NULL;
369 p_fifo->pp_last = &p_fifo->p_first;
370 p_fifo->i_depth = p_fifo->i_size = 0;
371 p_fifo->b_force_wake = false;
376 void block_FifoRelease( block_fifo_t *p_fifo )
378 block_FifoEmpty( p_fifo );
379 vlc_cond_destroy( &p_fifo->wait );
380 vlc_mutex_destroy( &p_fifo->lock );
384 void block_FifoEmpty( block_fifo_t *p_fifo )
388 vlc_mutex_lock( &p_fifo->lock );
389 for( b = p_fifo->p_first; b != NULL; )
398 p_fifo->i_depth = p_fifo->i_size = 0;
399 p_fifo->p_first = NULL;
400 p_fifo->pp_last = &p_fifo->p_first;
401 vlc_mutex_unlock( &p_fifo->lock );
404 size_t block_FifoPut( block_fifo_t *p_fifo, block_t *p_block )
407 vlc_mutex_lock( &p_fifo->lock );
411 i_size += p_block->i_buffer;
413 *p_fifo->pp_last = p_block;
414 p_fifo->pp_last = &p_block->p_next;
416 p_fifo->i_size += p_block->i_buffer;
418 p_block = p_block->p_next;
422 /* warn there is data in this fifo */
423 vlc_cond_signal( &p_fifo->wait );
424 vlc_mutex_unlock( &p_fifo->lock );
429 void block_FifoWake( block_fifo_t *p_fifo )
431 vlc_mutex_lock( &p_fifo->lock );
432 if( p_fifo->p_first == NULL )
433 p_fifo->b_force_wake = true;
434 vlc_cond_signal( &p_fifo->wait );
435 vlc_mutex_unlock( &p_fifo->lock );
438 block_t *block_FifoGet( block_fifo_t *p_fifo )
442 vlc_mutex_lock( &p_fifo->lock );
443 mutex_cleanup_push( &p_fifo->lock );
445 /* Remember vlc_cond_wait() may cause spurious wakeups
446 * (on both Win32 and POSIX) */
447 while( ( p_fifo->p_first == NULL ) && !p_fifo->b_force_wake )
448 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
453 p_fifo->b_force_wake = false;
457 vlc_mutex_unlock( &p_fifo->lock );
461 p_fifo->p_first = b->p_next;
463 p_fifo->i_size -= b->i_buffer;
465 if( p_fifo->p_first == NULL )
467 p_fifo->pp_last = &p_fifo->p_first;
470 vlc_mutex_unlock( &p_fifo->lock );
476 block_t *block_FifoShow( block_fifo_t *p_fifo )
480 vlc_mutex_lock( &p_fifo->lock );
481 mutex_cleanup_push( &p_fifo->lock );
483 if( p_fifo->p_first == NULL )
484 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
492 /* FIXME: not thread-safe */
493 size_t block_FifoSize( const block_fifo_t *p_fifo )
495 return p_fifo->i_size;
498 /* FIXME: not thread-safe */
499 size_t block_FifoCount( const block_fifo_t *p_fifo )
501 return p_fifo->i_depth;