]> git.sesse.net Git - vlc/blob - src/misc/block.c
WinCE: more missing functions fixes
[vlc] / src / misc / block.c
1 /*****************************************************************************
2  * block.c: Data blocks management functions
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * $Id$
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 "vlc_block.h"
34
35 /*****************************************************************************
36  * Block functions.
37  *****************************************************************************/
38 /* private */
39 struct block_sys_t
40 {
41     block_t     self;
42     size_t      i_allocated_buffer;
43     uint8_t     p_allocated_buffer[];
44 };
45
46 #ifndef NDEBUG
47 static void BlockNoRelease( block_t *b )
48 {
49     fprintf( stderr, "block %p has no release callback! This is a bug!\n", b );
50     abort();
51 }
52 #endif
53
54 void block_Init( block_t *restrict b, void *buf, size_t size )
55 {
56     /* Fill all fields to their default */
57     b->p_next = b->p_prev = NULL;
58     b->i_flags = 0;
59     b->i_pts = b->i_dts = b->i_length = 0;
60     b->i_rate = 0;
61     b->p_buffer = buf;
62     b->i_buffer = size;
63 #ifndef NDEBUG
64     b->pf_release = BlockNoRelease;
65 #endif
66 }
67
68 static void BlockRelease( block_t *p_block )
69 {
70     free( p_block );
71 }
72
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
79
80 block_t *block_Alloc( size_t i_size )
81 {
82     /* We do only one malloc
83      * TODO: bench if doing 2 malloc but keeping a pool of buffer is better
84      * TODO: use memalign
85      * 16 -> align on 16
86      * 2 * BLOCK_PADDING_SIZE -> pre + post padding
87      */
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 );
90
91     if( p_sys == NULL )
92         return NULL;
93
94     /* Fill opaque data */
95     p_sys->i_allocated_buffer = i_alloc;
96
97     block_Init( &p_sys->self, p_sys->p_allocated_buffer + BLOCK_PADDING_SIZE
98                 + BLOCK_ALIGN
99                 - ((uintptr_t)p_sys->p_allocated_buffer % BLOCK_ALIGN),
100                 i_size );
101     p_sys->self.pf_release    = BlockRelease;
102
103     return &p_sys->self;
104 }
105
106 block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
107 {
108     block_sys_t *p_sys = (block_sys_t *)p_block;
109     ssize_t i_buffer_size = i_prebody + i_body;
110
111     if( i_buffer_size <= 0 )
112     {
113         block_Release( p_block );
114         return NULL;
115     }
116
117     if( p_block->pf_release != BlockRelease )
118     {
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 );
123         if( !p_dup )
124             return NULL;
125
126         p_block = p_dup;
127     }
128
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 )
133     {
134         p_block->p_buffer -= i_prebody;
135         p_block->i_buffer += i_prebody;
136         i_prebody = 0;
137     }
138
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 )
142     {
143         p_block->i_buffer = i_buffer_size;
144         i_body = 0;
145     }
146
147     /* Not enough room, reallocate the buffer */
148     if( i_body > 0 || i_prebody > 0 )
149     {
150         /* FIXME: this is really dumb, we should use realloc() */
151         block_t *p_rea = block_New( NULL, i_buffer_size );
152
153         if( p_rea )
154         {
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;
161
162             memcpy( p_rea->p_buffer + i_prebody, p_block->p_buffer,
163                     __MIN( p_block->i_buffer, p_rea->i_buffer - i_prebody ) );
164         }
165
166         block_Release( p_block );
167
168         return p_rea;
169     }
170
171     /* We have a very large reserved footer now? Release some of it. */
172     if ((p_sys->p_allocated_buffer + p_sys->i_allocated_buffer) -
173         (p_block->p_buffer + p_block->i_buffer) > BLOCK_WASTE_SIZE)
174     {
175         const size_t news = p_block->i_buffer + 2 * BLOCK_PADDING_SIZE + 16;
176         block_sys_t *newb = realloc (p_sys, sizeof (*p_sys) + news);
177
178         if (newb != NULL)
179         {
180             p_sys = newb;
181             p_sys->i_allocated_buffer = news;
182             p_block = &p_sys->self;
183             p_block->p_buffer = p_sys->p_allocated_buffer + BLOCK_PADDING_SIZE
184                 + BLOCK_ALIGN
185                 - ((uintptr_t)p_sys->p_allocated_buffer % BLOCK_ALIGN);
186         }
187     }
188     return p_block;
189 }
190
191 #ifdef HAVE_MMAP
192 # include <sys/mman.h>
193
194 typedef struct block_mmap_t
195 {
196     block_t     self;
197     void       *base_addr;
198     size_t      length;
199 } block_mmap_t;
200
201 static void block_mmap_Release (block_t *block)
202 {
203     block_mmap_t *p_sys = (block_mmap_t *)block;
204
205     munmap (p_sys->base_addr, p_sys->length);
206     free (p_sys);
207 }
208
209 /**
210  * Creates a block from a virtual address memory mapping (mmap).
211  * This is provided by LibVLC so that mmap blocks can safely be deallocated
212  * even after the allocating plugin has been unloaded from memory.
213  *
214  * @param addr base address of the mapping (as returned by mmap)
215  * @param length length (bytes) of the mapping (as passed to mmap)
216  * @return NULL if addr is MAP_FAILED, or an error occurred (in the later
217  * case, munmap(addr, length) is invoked before returning).
218  */
219 block_t *block_mmap_Alloc (void *addr, size_t length)
220 {
221     if (addr == MAP_FAILED)
222         return NULL;
223
224     block_mmap_t *block = malloc (sizeof (*block));
225     if (block == NULL)
226     {
227         munmap (addr, length);
228         return NULL;
229     }
230
231     block_Init (&block->self, (uint8_t *)addr, length);
232     block->self.pf_release = block_mmap_Release;
233     block->base_addr = addr;
234     block->length = length;
235     return &block->self;
236 }
237 #else
238 block_t *block_mmap_Alloc (void *addr, size_t length)
239 {
240     (void)addr; (void)length; return NULL;
241 }
242 #endif
243
244
245 #ifdef WIN32
246 #ifdef UNDER_CE
247 #define _get_osfhandle(a) ((long) (a))
248 #endif
249
250 static
251 ssize_t pread (int fd, void *buf, size_t count, off_t offset)
252 {
253     HANDLE handle = (HANDLE)(intptr_t)_get_osfhandle (fd);
254     if (handle == INVALID_HANDLE_VALUE)
255         return -1;
256
257     OVERLAPPED olap = { .Offset = offset, .OffsetHigh = (offset >> 32), };
258     DWORD written;
259     /* This braindead API will override the file pointer even if we specify
260      * an explicit read offset... So do not expect this to mix well with
261      * regular read() calls. */
262     if (ReadFile (handle, buf, count, &written, &olap))
263         return written;
264     return -1;
265 }
266 #endif
267
268 /**
269  * Loads a file into a block of memory. If possible a private file mapping is
270  * created. Otherwise, the file is read normally. On 32-bits platforms, this
271  * function will not work for very large files, due to memory space
272  * constraints. Cancellation point.
273  *
274  * @param fd file descriptor to load from
275  * @return a new block with the file content at p_buffer, and file length at
276  * i_buffer (release it with block_Release()), or NULL upon error (see errno).
277  */
278 block_t *block_File (int fd)
279 {
280     size_t length;
281     struct stat st;
282
283     /* First, get the file size */
284     if (fstat (fd, &st))
285         return NULL;
286
287     /* st_size is meaningful for regular files, shared memory and typed memory.
288      * It's also meaning for symlinks, but that's not possible with fstat().
289      * In other cases, it's undefined, and we should really not go further. */
290 #ifndef S_TYPEISSHM
291 # define S_TYPEISSHM( buf ) (0)
292 #endif
293     if (S_ISDIR (st.st_mode))
294     {
295         errno = EISDIR;
296         return NULL;
297     }
298     if (!S_ISREG (st.st_mode) && !S_TYPEISSHM (&st))
299     {
300         errno = ESPIPE;
301         return NULL;
302     }
303
304     /* Prevent an integer overflow in mmap() and malloc() */
305     if (st.st_size >= SIZE_MAX)
306     {
307         errno = ENOMEM;
308         return NULL;
309     }
310     length = (size_t)st.st_size;
311
312 #ifdef HAVE_MMAP
313     if (length > 0)
314     {
315         void *addr;
316
317         addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
318         if (addr != MAP_FAILED)
319             return block_mmap_Alloc (addr, length);
320     }
321 #endif
322
323     /* If mmap() is not implemented by the OS _or_ the filesystem... */
324     block_t *block = block_Alloc (length);
325     if (block == NULL)
326         return NULL;
327     block_cleanup_push (block);
328
329     for (size_t i = 0; i < length;)
330     {
331         ssize_t len = pread (fd, block->p_buffer + i, length - i, i);
332         if (len == -1)
333         {
334             block_Release (block);
335             block = NULL;
336             break;
337         }
338         i += len;
339     }
340     vlc_cleanup_pop ();
341     return block;
342 }
343
344 /*****************************************************************************
345  * block_fifo_t management
346  *****************************************************************************/
347 struct block_fifo_t
348 {
349     vlc_mutex_t         lock;                         /* fifo data lock */
350     vlc_cond_t          wait;         /* fifo data conditional variable */
351
352     block_t             *p_first;
353     block_t             **pp_last;
354     size_t              i_depth;
355     size_t              i_size;
356     bool          b_force_wake;
357 };
358
359 block_fifo_t *block_FifoNew( void )
360 {
361     block_fifo_t *p_fifo = malloc( sizeof( block_fifo_t ) );
362     if( !p_fifo )
363         return NULL;
364
365     vlc_mutex_init( &p_fifo->lock );
366     vlc_cond_init( &p_fifo->wait );
367     p_fifo->p_first = NULL;
368     p_fifo->pp_last = &p_fifo->p_first;
369     p_fifo->i_depth = p_fifo->i_size = 0;
370     p_fifo->b_force_wake = false;
371
372     return p_fifo;
373 }
374
375 void block_FifoRelease( block_fifo_t *p_fifo )
376 {
377     block_FifoEmpty( p_fifo );
378     vlc_cond_destroy( &p_fifo->wait );
379     vlc_mutex_destroy( &p_fifo->lock );
380     free( p_fifo );
381 }
382
383 void block_FifoEmpty( block_fifo_t *p_fifo )
384 {
385     block_t *b;
386
387     vlc_mutex_lock( &p_fifo->lock );
388     for( b = p_fifo->p_first; b != NULL; )
389     {
390         block_t *p_next;
391
392         p_next = b->p_next;
393         block_Release( b );
394         b = p_next;
395     }
396
397     p_fifo->i_depth = p_fifo->i_size = 0;
398     p_fifo->p_first = NULL;
399     p_fifo->pp_last = &p_fifo->p_first;
400     vlc_mutex_unlock( &p_fifo->lock );
401 }
402
403 size_t block_FifoPut( block_fifo_t *p_fifo, block_t *p_block )
404 {
405     size_t i_size = 0;
406     vlc_mutex_lock( &p_fifo->lock );
407
408     do
409     {
410         i_size += p_block->i_buffer;
411
412         *p_fifo->pp_last = p_block;
413         p_fifo->pp_last = &p_block->p_next;
414         p_fifo->i_depth++;
415         p_fifo->i_size += p_block->i_buffer;
416
417         p_block = p_block->p_next;
418
419     } while( p_block );
420
421     /* warn there is data in this fifo */
422     vlc_cond_signal( &p_fifo->wait );
423     vlc_mutex_unlock( &p_fifo->lock );
424
425     return i_size;
426 }
427
428 void block_FifoWake( block_fifo_t *p_fifo )
429 {
430     vlc_mutex_lock( &p_fifo->lock );
431     if( p_fifo->p_first == NULL )
432         p_fifo->b_force_wake = true;
433     vlc_cond_signal( &p_fifo->wait );
434     vlc_mutex_unlock( &p_fifo->lock );
435 }
436
437 block_t *block_FifoGet( block_fifo_t *p_fifo )
438 {
439     block_t *b;
440
441     vlc_mutex_lock( &p_fifo->lock );
442     mutex_cleanup_push( &p_fifo->lock );
443
444     /* Remember vlc_cond_wait() may cause spurious wakeups
445      * (on both Win32 and POSIX) */
446     while( ( p_fifo->p_first == NULL ) && !p_fifo->b_force_wake )
447         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
448
449     vlc_cleanup_pop();
450     b = p_fifo->p_first;
451
452     p_fifo->b_force_wake = false;
453     if( b == NULL )
454     {
455         /* Forced wakeup */
456         vlc_mutex_unlock( &p_fifo->lock );
457         return NULL;
458     }
459
460     p_fifo->p_first = b->p_next;
461     p_fifo->i_depth--;
462     p_fifo->i_size -= b->i_buffer;
463
464     if( p_fifo->p_first == NULL )
465     {
466         p_fifo->pp_last = &p_fifo->p_first;
467     }
468
469     vlc_mutex_unlock( &p_fifo->lock );
470
471     b->p_next = NULL;
472     return b;
473 }
474
475 block_t *block_FifoShow( block_fifo_t *p_fifo )
476 {
477     block_t *b;
478
479     vlc_mutex_lock( &p_fifo->lock );
480     mutex_cleanup_push( &p_fifo->lock );
481
482     if( p_fifo->p_first == NULL )
483         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
484
485     b = p_fifo->p_first;
486
487     vlc_cleanup_run ();
488     return b;
489 }
490
491 /* FIXME: not thread-safe */
492 size_t block_FifoSize( const block_fifo_t *p_fifo )
493 {
494     return p_fifo->i_size;
495 }
496
497 /* FIXME: not thread-safe */
498 size_t block_FifoCount( const block_fifo_t *p_fifo )
499 {
500     return p_fifo->i_depth;
501 }