]> git.sesse.net Git - vlc/blob - src/misc/block.c
Compat: relicense strcasestr to LGPL
[vlc] / src / misc / block.c
1 /*****************************************************************************
2  * block.c: Data blocks management functions
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * Copyright (C) 2007-2009 RĂ©mi Denis-Courmont
6  *
7  * Authors: Laurent Aimar <fenrir@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <sys/stat.h>
33 #include <assert.h>
34 #include <errno.h>
35 #include "vlc_block.h"
36
37 /**
38  * @section Block handling functions.
39  */
40
41 /**
42  * Internal state for heap block.
43   */
44 struct block_sys_t
45 {
46     block_t     self;
47     size_t      i_allocated_buffer;
48     uint8_t     p_allocated_buffer[];
49 };
50
51 #ifndef NDEBUG
52 static void BlockNoRelease( block_t *b )
53 {
54     fprintf( stderr, "block %p has no release callback! This is a bug!\n", b );
55     abort();
56 }
57 #endif
58
59 void block_Init( block_t *restrict b, void *buf, size_t size )
60 {
61     /* Fill all fields to their default */
62     b->p_next = NULL;
63     b->p_buffer = buf;
64     b->i_buffer = size;
65     b->i_flags = 0;
66     b->i_nb_samples = 0;
67     b->i_pts =
68     b->i_dts = VLC_TS_INVALID;
69     b->i_length = 0;
70 #ifndef NDEBUG
71     b->pf_release = BlockNoRelease;
72 #endif
73 }
74
75 static void BlockRelease( block_t *p_block )
76 {
77     free( p_block );
78 }
79
80 static void BlockMetaCopy( block_t *restrict out, const block_t *in )
81 {
82     out->p_next    = in->p_next;
83     out->i_nb_samples = in->i_nb_samples;
84     out->i_dts     = in->i_dts;
85     out->i_pts     = in->i_pts;
86     out->i_flags   = in->i_flags;
87     out->i_length  = in->i_length;
88 }
89
90 /* Memory alignment (must be a multiple of sizeof(void*) and a power of two) */
91 #define BLOCK_ALIGN        16
92 /* Initial reserved header and footer size (must be multiple of alignment) */
93 #define BLOCK_PADDING      32
94 /* Maximum size of reserved footer before we release with realloc() */
95 #define BLOCK_WASTE_SIZE   2048
96
97 block_t *block_Alloc( size_t i_size )
98 {
99     /* We do only one malloc
100      * TODO: bench if doing 2 malloc but keeping a pool of buffer is better
101      * 2 * BLOCK_PADDING -> pre + post padding
102      */
103     block_sys_t *p_sys;
104     uint8_t *buf;
105
106 #define ALIGN(x) (((x) + BLOCK_ALIGN - 1) & ~(BLOCK_ALIGN - 1))
107 #if 0 /*def HAVE_POSIX_MEMALIGN */
108     /* posix_memalign(,16,) is much slower than malloc() on glibc.
109      * -- Courmisch, September 2009, glibc 2.5 & 2.9 */
110     const size_t i_alloc = ALIGN(sizeof(*p_sys)) + (2 * BLOCK_PADDING)
111                          + ALIGN(i_size);
112     void *ptr;
113
114     if( posix_memalign( &ptr, BLOCK_ALIGN, i_alloc ) )
115         return NULL;
116
117     p_sys = ptr;
118     buf = p_sys->p_allocated_buffer + (-sizeof(*p_sys) & (BLOCK_ALIGN - 1));
119
120 #else
121     const size_t i_alloc = sizeof(*p_sys) + BLOCK_ALIGN + (2 * BLOCK_PADDING)
122                          + ALIGN(i_size);
123     p_sys = malloc( i_alloc );
124     if( p_sys == NULL )
125         return NULL;
126
127     buf = (void *)ALIGN((uintptr_t)p_sys->p_allocated_buffer);
128
129 #endif
130     buf += BLOCK_PADDING;
131
132     block_Init( &p_sys->self, buf, i_size );
133     p_sys->self.pf_release    = BlockRelease;
134     /* Fill opaque data */
135     p_sys->i_allocated_buffer = i_alloc - sizeof(*p_sys);
136
137     return &p_sys->self;
138 }
139
140 block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
141 {
142     size_t requested = i_prebody + i_body;
143
144     /* Corner case: empty block requested */
145     if( i_prebody <= 0 && i_body <= (size_t)(-i_prebody) )
146     {
147         block_Release( p_block );
148         return NULL;
149     }
150
151     if( p_block->pf_release != BlockRelease )
152     {
153         /* Special case when pf_release if overloaded
154          * TODO if used one day, then implement it in a smarter way */
155         block_t *p_dup = block_Duplicate( p_block );
156         block_Release( p_block );
157         if( !p_dup )
158             return NULL;
159
160         p_block = p_dup;
161     }
162
163     block_sys_t *p_sys = (block_sys_t *)p_block;
164     uint8_t *p_start = p_sys->p_allocated_buffer;
165     uint8_t *p_end = p_sys->p_allocated_buffer + p_sys->i_allocated_buffer;
166
167     assert( p_block->p_buffer + p_block->i_buffer <= p_end );
168     assert( p_block->p_buffer >= p_start );
169
170     /* Corner case: the current payload is discarded completely */
171     if( i_prebody <= 0 && p_block->i_buffer <= (size_t)-i_prebody )
172          p_block->i_buffer = 0; /* discard current payload */
173     if( p_block->i_buffer == 0 )
174     {
175         size_t available = p_end - p_start;
176
177         if( requested <= available )
178         {   /* Enough room: recycle buffer */
179             size_t extra = available - requested;
180
181             p_block->p_buffer = p_start + (extra / 2);
182             p_block->i_buffer = requested;
183             return p_block;
184         }
185         /* Not enough room: allocate a new buffer */
186         block_t *p_rea = block_Alloc( requested );
187         if( p_rea )
188             BlockMetaCopy( p_rea, p_block );
189         block_Release( p_block );
190         return p_rea;
191     }
192
193     /* First, shrink payload */
194
195     /* Pull payload start */
196     if( i_prebody < 0 )
197     {
198         assert( p_block->i_buffer >= (size_t)-i_prebody );
199         p_block->p_buffer -= i_prebody;
200         p_block->i_buffer += i_prebody;
201         i_body += i_prebody;
202         i_prebody = 0;
203     }
204
205     /* Trim payload end */
206     if( p_block->i_buffer > i_body )
207         p_block->i_buffer = i_body;
208
209     /* Second, reallocate the buffer if we lack space. This is done now to
210      * minimize the payload size for memory copy. */
211     assert( i_prebody >= 0 );
212     if( (size_t)(p_block->p_buffer - p_start) < (size_t)i_prebody
213      || (size_t)(p_end - p_block->p_buffer) < i_body )
214     {
215         block_t *p_rea = block_Alloc( requested );
216         if( p_rea )
217         {
218             BlockMetaCopy( p_rea, p_block );
219             p_rea->p_buffer += i_prebody;
220             p_rea->i_buffer -= i_prebody;
221             memcpy( p_rea->p_buffer, p_block->p_buffer, p_block->i_buffer );
222         }
223         block_Release( p_block );
224         if( p_rea == NULL )
225             return NULL;
226         p_block = p_rea;
227     }
228     else
229     /* We have a very large reserved footer now? Release some of it.
230      * XXX it might not preserve the alignment of p_buffer */
231     if( p_end - (p_block->p_buffer + i_body) > BLOCK_WASTE_SIZE )
232     {
233         block_t *p_rea = block_Alloc( requested );
234         if( p_rea )
235         {
236             BlockMetaCopy( p_rea, p_block );
237             p_rea->p_buffer += i_prebody;
238             p_rea->i_buffer -= i_prebody;
239             memcpy( p_rea->p_buffer, p_block->p_buffer, p_block->i_buffer );
240             block_Release( p_block );
241             p_block = p_rea;
242         }
243     }
244
245     /* NOTE: p_start and p_end are corrupted from this point */
246
247     /* Third, expand payload */
248
249     /* Push payload start */
250     if( i_prebody > 0 )
251     {
252         p_block->p_buffer -= i_prebody;
253         p_block->i_buffer += i_prebody;
254         i_body += i_prebody;
255         i_prebody = 0;
256     }
257
258     /* Expand payload to requested size */
259     p_block->i_buffer = i_body;
260
261     return p_block;
262 }
263
264
265 typedef struct
266 {
267     block_t  self;
268     void    *mem;
269 } block_heap_t;
270
271 static void block_heap_Release (block_t *self)
272 {
273     block_heap_t *block = (block_heap_t *)self;
274
275     free (block->mem);
276     free (block);
277 }
278
279 /**
280  * Creates a block from a heap allocation.
281  * This is provided by LibVLC so that manually heap-allocated blocks can safely
282  * be deallocated even after the origin plugin has been unloaded from memory.
283  *
284  * When block_Release() is called, VLC will free() the specified pointer.
285  *
286  * @param ptr base address of the heap allocation (will be free()'d)
287  * @param addr base address of the useful buffer data
288  * @param length bytes length of the useful buffer data
289  * @return NULL in case of error (ptr free()'d in that case), or a valid
290  * block_t pointer.
291  */
292 block_t *block_heap_Alloc (void *ptr, void *addr, size_t length)
293 {
294     block_heap_t *block = malloc (sizeof (*block));
295     if (block == NULL)
296     {
297         free (addr);
298         return NULL;
299     }
300
301     block_Init (&block->self, (uint8_t *)addr, length);
302     block->self.pf_release = block_heap_Release;
303     block->mem = ptr;
304     return &block->self;
305 }
306
307 #ifdef HAVE_MMAP
308 # include <sys/mman.h>
309
310 typedef struct block_mmap_t
311 {
312     block_t     self;
313     void       *base_addr;
314     size_t      length;
315 } block_mmap_t;
316
317 static void block_mmap_Release (block_t *block)
318 {
319     block_mmap_t *p_sys = (block_mmap_t *)block;
320
321     munmap (p_sys->base_addr, p_sys->length);
322     free (p_sys);
323 }
324
325 /**
326  * Creates a block from a virtual address memory mapping (mmap).
327  * This is provided by LibVLC so that mmap blocks can safely be deallocated
328  * even after the allocating plugin has been unloaded from memory.
329  *
330  * @param addr base address of the mapping (as returned by mmap)
331  * @param length length (bytes) of the mapping (as passed to mmap)
332  * @return NULL if addr is MAP_FAILED, or an error occurred (in the later
333  * case, munmap(addr, length) is invoked before returning).
334  */
335 block_t *block_mmap_Alloc (void *addr, size_t length)
336 {
337     if (addr == MAP_FAILED)
338         return NULL;
339
340     block_mmap_t *block = malloc (sizeof (*block));
341     if (block == NULL)
342     {
343         munmap (addr, length);
344         return NULL;
345     }
346
347     block_Init (&block->self, (uint8_t *)addr, length);
348     block->self.pf_release = block_mmap_Release;
349     block->base_addr = addr;
350     block->length = length;
351     return &block->self;
352 }
353 #else
354 block_t *block_mmap_Alloc (void *addr, size_t length)
355 {
356     (void)addr; (void)length; return NULL;
357 }
358 #endif
359
360
361 #ifdef WIN32
362 # include <io.h>
363 # ifdef UNDER_CE
364 #  define _get_osfhandle(a) ((long) (a))
365 # endif
366
367 static
368 ssize_t pread (int fd, void *buf, size_t count, off_t offset)
369 {
370     HANDLE handle = (HANDLE)(intptr_t)_get_osfhandle (fd);
371     if (handle == INVALID_HANDLE_VALUE)
372         return -1;
373
374     OVERLAPPED olap; olap.Offset = offset; olap.OffsetHigh = (offset >> 32);
375     DWORD written;
376     /* This braindead API will override the file pointer even if we specify
377      * an explicit read offset... So do not expect this to mix well with
378      * regular read() calls. */
379     if (ReadFile (handle, buf, count, &written, &olap))
380         return written;
381     return -1;
382 }
383 #elif !defined( HAVE_PREAD )
384 static
385 ssize_t pread(int fd, const void * buf, size_t size, off_t offset) {
386     off_t offs0;
387     ssize_t rd;
388     if ((offs0 = lseek(fd, 0, SEEK_CUR)) == (off_t)-1) return -1;
389     if (lseek(fd, offset, SEEK_SET) == (off_t)-1) return -1;
390     rd = read(fd, (void *)buf, size);
391     if (lseek(fd, offs0, SEEK_SET) == (off_t)-1) return -1;
392     return rd;
393 }
394
395 static
396 ssize_t pwrite(int fd, const void * buf, size_t size, off_t offset) {
397     off_t offs0;
398     ssize_t wr;
399     if ((offs0 = lseek(fd, 0, SEEK_CUR)) == (off_t)-1) return -1;
400     if (lseek(fd, offset, SEEK_SET) == (off_t)-1) return -1;
401     wr = write(fd, (void *)buf, size);
402     if (lseek(fd, offs0, SEEK_SET) == (off_t)-1) return -1;
403     return wr;
404 }
405 #endif
406
407 /**
408  * Loads a file into a block of memory. If possible a private file mapping is
409  * created. Otherwise, the file is read normally. On 32-bits platforms, this
410  * function will not work for very large files, due to memory space
411  * constraints. Cancellation point.
412  *
413  * @param fd file descriptor to load from
414  * @return a new block with the file content at p_buffer, and file length at
415  * i_buffer (release it with block_Release()), or NULL upon error (see errno).
416  */
417 block_t *block_File (int fd)
418 {
419     size_t length;
420     struct stat st;
421
422     /* First, get the file size */
423     if (fstat (fd, &st))
424         return NULL;
425
426     /* st_size is meaningful for regular files, shared memory and typed memory.
427      * It's also meaning for symlinks, but that's not possible with fstat().
428      * In other cases, it's undefined, and we should really not go further. */
429 #ifndef S_TYPEISSHM
430 # define S_TYPEISSHM( buf ) (0)
431 #endif
432     if (S_ISDIR (st.st_mode))
433     {
434         errno = EISDIR;
435         return NULL;
436     }
437     if (!S_ISREG (st.st_mode) && !S_TYPEISSHM (&st))
438     {
439         errno = ESPIPE;
440         return NULL;
441     }
442
443     /* Prevent an integer overflow in mmap() and malloc() */
444     if (st.st_size >= SIZE_MAX)
445     {
446         errno = ENOMEM;
447         return NULL;
448     }
449     length = (size_t)st.st_size;
450
451 #ifdef HAVE_MMAP
452     if (length > 0)
453     {
454         void *addr;
455
456         addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
457         if (addr != MAP_FAILED)
458             return block_mmap_Alloc (addr, length);
459     }
460 #endif
461
462     /* If mmap() is not implemented by the OS _or_ the filesystem... */
463     block_t *block = block_Alloc (length);
464     if (block == NULL)
465         return NULL;
466     block_cleanup_push (block);
467
468     for (size_t i = 0; i < length;)
469     {
470         ssize_t len = pread (fd, block->p_buffer + i, length - i, i);
471         if (len == -1)
472         {
473             block_Release (block);
474             block = NULL;
475             break;
476         }
477         i += len;
478     }
479     vlc_cleanup_pop ();
480     return block;
481 }
482
483 /**
484  * @section Thread-safe block queue functions
485  */
486
487 /**
488  * Internal state for block queues
489  */
490 struct block_fifo_t
491 {
492     vlc_mutex_t         lock;                         /* fifo data lock */
493     vlc_cond_t          wait;      /**< Wait for data */
494     vlc_cond_t          wait_room; /**< Wait for queue depth to shrink */
495
496     block_t             *p_first;
497     block_t             **pp_last;
498     size_t              i_depth;
499     size_t              i_size;
500     bool          b_force_wake;
501 };
502
503 block_fifo_t *block_FifoNew( void )
504 {
505     block_fifo_t *p_fifo = malloc( sizeof( block_fifo_t ) );
506     if( !p_fifo )
507         return NULL;
508
509     vlc_mutex_init( &p_fifo->lock );
510     vlc_cond_init( &p_fifo->wait );
511     vlc_cond_init( &p_fifo->wait_room );
512     p_fifo->p_first = NULL;
513     p_fifo->pp_last = &p_fifo->p_first;
514     p_fifo->i_depth = p_fifo->i_size = 0;
515     p_fifo->b_force_wake = false;
516
517     return p_fifo;
518 }
519
520 void block_FifoRelease( block_fifo_t *p_fifo )
521 {
522     block_FifoEmpty( p_fifo );
523     vlc_cond_destroy( &p_fifo->wait_room );
524     vlc_cond_destroy( &p_fifo->wait );
525     vlc_mutex_destroy( &p_fifo->lock );
526     free( p_fifo );
527 }
528
529 void block_FifoEmpty( block_fifo_t *p_fifo )
530 {
531     block_t *block;
532
533     vlc_mutex_lock( &p_fifo->lock );
534     block = p_fifo->p_first;
535     if (block != NULL)
536     {
537         p_fifo->i_depth = p_fifo->i_size = 0;
538         p_fifo->p_first = NULL;
539         p_fifo->pp_last = &p_fifo->p_first;
540     }
541     vlc_cond_broadcast( &p_fifo->wait_room );
542     vlc_mutex_unlock( &p_fifo->lock );
543
544     while (block != NULL)
545     {
546         block_t *buf;
547
548         buf = block->p_next;
549         block_Release (block);
550         block = buf;
551     }
552 }
553
554 /**
555  * Wait until the FIFO gets below a certain size (if needed).
556  *
557  * Note that if more than one thread writes to the FIFO, you cannot assume that
558  * the FIFO is actually below the requested size upon return (since another
559  * thread could have refilled it already). This is typically not an issue, as
560  * this function is meant for (relaxed) congestion control.
561  *
562  * This function may be a cancellation point and it is cancel-safe.
563  *
564  * @param fifo queue to wait on
565  * @param max_depth wait until the queue has no more than this many blocks
566  *                  (use SIZE_MAX to ignore this constraint)
567  * @param max_size wait until the queue has no more than this many bytes
568  *                  (use SIZE_MAX to ignore this constraint)
569  * @return nothing.
570  */
571 void block_FifoPace (block_fifo_t *fifo, size_t max_depth, size_t max_size)
572 {
573     vlc_testcancel ();
574
575     vlc_mutex_lock (&fifo->lock);
576     while ((fifo->i_depth > max_depth) || (fifo->i_size > max_size))
577     {
578          mutex_cleanup_push (&fifo->lock);
579          vlc_cond_wait (&fifo->wait_room, &fifo->lock);
580          vlc_cleanup_pop ();
581     }
582     vlc_mutex_unlock (&fifo->lock);
583 }
584
585 /**
586  * Immediately queue one block at the end of a FIFO.
587  * @param fifo queue
588  * @param block head of a block list to queue (may be NULL)
589  * @return total number of bytes appended to the queue
590  */
591 size_t block_FifoPut( block_fifo_t *p_fifo, block_t *p_block )
592 {
593     size_t i_size = 0, i_depth = 0;
594     block_t *p_last;
595
596     if (p_block == NULL)
597         return 0;
598     for (p_last = p_block; ; p_last = p_last->p_next)
599     {
600         i_size += p_last->i_buffer;
601         i_depth++;
602         if (!p_last->p_next)
603             break;
604     }
605
606     vlc_mutex_lock (&p_fifo->lock);
607     *p_fifo->pp_last = p_block;
608     p_fifo->pp_last = &p_last->p_next;
609     p_fifo->i_depth += i_depth;
610     p_fifo->i_size += i_size;
611     /* We queued at least one block: wake up one read-waiting thread */
612     vlc_cond_signal( &p_fifo->wait );
613     vlc_mutex_unlock( &p_fifo->lock );
614
615     return i_size;
616 }
617
618 void block_FifoWake( block_fifo_t *p_fifo )
619 {
620     vlc_mutex_lock( &p_fifo->lock );
621     if( p_fifo->p_first == NULL )
622         p_fifo->b_force_wake = true;
623     vlc_cond_broadcast( &p_fifo->wait );
624     vlc_mutex_unlock( &p_fifo->lock );
625 }
626
627 /**
628  * Dequeue the first block from the FIFO. If necessary, wait until there is
629  * one block in the queue. This function is (always) cancellation point.
630  *
631  * @return a valid block, or NULL if block_FifoWake() was called.
632  */
633 block_t *block_FifoGet( block_fifo_t *p_fifo )
634 {
635     block_t *b;
636
637     vlc_testcancel( );
638
639     vlc_mutex_lock( &p_fifo->lock );
640     mutex_cleanup_push( &p_fifo->lock );
641
642     /* Remember vlc_cond_wait() may cause spurious wakeups
643      * (on both Win32 and POSIX) */
644     while( ( p_fifo->p_first == NULL ) && !p_fifo->b_force_wake )
645         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
646
647     vlc_cleanup_pop();
648     b = p_fifo->p_first;
649
650     p_fifo->b_force_wake = false;
651     if( b == NULL )
652     {
653         /* Forced wakeup */
654         vlc_mutex_unlock( &p_fifo->lock );
655         return NULL;
656     }
657
658     p_fifo->p_first = b->p_next;
659     p_fifo->i_depth--;
660     p_fifo->i_size -= b->i_buffer;
661
662     if( p_fifo->p_first == NULL )
663     {
664         p_fifo->pp_last = &p_fifo->p_first;
665     }
666
667     /* We don't know how many threads can queue new packets now. */
668     vlc_cond_broadcast( &p_fifo->wait_room );
669     vlc_mutex_unlock( &p_fifo->lock );
670
671     b->p_next = NULL;
672     return b;
673 }
674
675 /**
676  * Peeks the first block in the FIFO.
677  * If necessary, wait until there is one block.
678  * This function is (always) a cancellation point.
679  *
680  * @warning This function leaves the block in the FIFO.
681  * You need to protect against concurrent threads who could dequeue the block.
682  * Preferrably, there should be only one thread reading from the FIFO.
683  *
684  * @return a valid block.
685  */
686 block_t *block_FifoShow( block_fifo_t *p_fifo )
687 {
688     block_t *b;
689
690     vlc_testcancel( );
691
692     vlc_mutex_lock( &p_fifo->lock );
693     mutex_cleanup_push( &p_fifo->lock );
694
695     while( p_fifo->p_first == NULL )
696         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
697
698     b = p_fifo->p_first;
699
700     vlc_cleanup_run ();
701     return b;
702 }
703
704 /* FIXME: not thread-safe */
705 size_t block_FifoSize( const block_fifo_t *p_fifo )
706 {
707     return p_fifo->i_size;
708 }
709
710 /* FIXME: not thread-safe */
711 size_t block_FifoCount( const block_fifo_t *p_fifo )
712 {
713     return p_fifo->i_depth;
714 }