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