1b6539cbd18b1c95ce13e444e77c848e9008c51d
[vlc.git] / 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 #include <vlc/vlc.h>
28 #include "vlc_block.h"
29
30 /*****************************************************************************
31  * Block functions.
32  *****************************************************************************/
33 /* private */
34 struct block_sys_t
35 {
36     block_t     self;
37     size_t      i_allocated_buffer;
38     uint8_t     p_allocated_buffer[0];
39 };
40
41 #define BLOCK_PADDING_SIZE 32
42 static void BlockRelease( block_t * );
43
44 block_t *__block_New( vlc_object_t *p_obj, size_t i_size )
45 {
46     /* We do only one malloc
47      * TODO bench if doing 2 malloc but keeping a pool of buffer is better
48      * 16 -> align on 16
49      * 2 * BLOCK_PADDING_SIZE -> pre + post padding
50      */
51     const size_t i_alloc = i_size + 2 * BLOCK_PADDING_SIZE + 16;
52     block_sys_t *p_sys = malloc( sizeof( *p_sys ) + i_alloc );
53
54     if( p_sys == NULL )
55         return NULL;
56
57     /* Fill opaque data */
58     p_sys->i_allocated_buffer = i_alloc;
59
60     /* Fill all fields */
61     p_sys->self.p_next      = NULL;
62     p_sys->self.p_prev      = NULL;
63     p_sys->self.i_flags     = 0;
64     p_sys->self.i_pts       = 0;
65     p_sys->self.i_dts       = 0;
66     p_sys->self.i_length    = 0;
67     p_sys->self.i_rate      = 0;
68     p_sys->self.i_buffer    = i_size;
69     p_sys->self.p_buffer    =
70         &p_sys->p_allocated_buffer[BLOCK_PADDING_SIZE +
71             16 - ((uintptr_t)p_sys->p_allocated_buffer % 16 )];
72     p_sys->self.pf_release     = BlockRelease;
73
74     return &p_sys->self;
75 }
76
77 block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
78 {
79     block_sys_t *p_sys = (block_sys_t *)p_block;
80     ssize_t i_buffer_size;
81
82     if( p_block->pf_release != BlockRelease )
83     {
84         /* Special case when pf_release if overloaded
85          * TODO if used one day, them implement it in a smarter way */
86         block_t *p_dup = block_Duplicate( p_block );
87         block_Release( p_block );
88         if( !p_dup )
89             return NULL;
90
91         p_block = p_dup;
92     }
93
94     i_buffer_size = i_prebody + i_body;
95
96     if( i_buffer_size <= 0 )
97     {
98         block_Release( p_block );
99         return NULL;
100     }
101
102     if( p_block->p_buffer - i_prebody > p_sys->p_allocated_buffer &&
103         p_block->p_buffer - i_prebody < p_sys->p_allocated_buffer +
104         p_sys->i_allocated_buffer )
105     {
106         p_block->p_buffer -= i_prebody;
107         p_block->i_buffer += i_prebody;
108         i_prebody = 0;
109     }
110     if( p_block->p_buffer + i_body < p_sys->p_allocated_buffer +
111         p_sys->i_allocated_buffer )
112     {
113         p_block->i_buffer = i_buffer_size;
114         i_body = 0;
115     }
116
117     if( i_body > 0 || i_prebody > 0 )
118     {
119         block_t *p_rea = block_New( NULL, i_buffer_size );
120
121         if( p_rea )
122         {
123             p_rea->i_dts     = p_block->i_dts;
124             p_rea->i_pts     = p_block->i_pts;
125             p_rea->i_flags   = p_block->i_flags;
126             p_rea->i_length  = p_block->i_length;
127             p_rea->i_rate    = p_block->i_rate;
128             p_rea->i_samples = p_block->i_samples;
129
130             memcpy( p_rea->p_buffer + i_prebody, p_block->p_buffer,
131                     __MIN( p_block->i_buffer, p_rea->i_buffer - i_prebody ) );
132         }
133
134         block_Release( p_block );
135
136         return p_rea;
137     }
138
139     return p_block;
140 }
141
142 static void BlockRelease( block_t *p_block )
143 {
144     free( p_block );
145 }
146
147
148 /*****************************************************************************
149  * block_fifo_t management
150  *****************************************************************************/
151 struct block_fifo_t
152 {
153     vlc_mutex_t         lock;                         /* fifo data lock */
154     vlc_cond_t          wait;         /* fifo data conditional variable */
155
156     block_t             *p_first;
157     block_t             **pp_last;
158     size_t              i_depth;
159     size_t              i_size;
160     vlc_bool_t          b_force_wake;
161 };
162
163 block_fifo_t *__block_FifoNew( vlc_object_t *p_obj )
164 {
165     block_fifo_t *p_fifo;
166
167     p_fifo = malloc( sizeof( block_fifo_t ) );
168     if( !p_fifo ) return NULL;
169     vlc_mutex_init( p_obj, &p_fifo->lock );
170     vlc_cond_init( p_obj, &p_fifo->wait );
171     p_fifo->p_first = NULL;
172     p_fifo->pp_last = &p_fifo->p_first;
173     p_fifo->i_depth = p_fifo->i_size = 0;
174     p_fifo->b_force_wake = VLC_FALSE;
175
176     return p_fifo;
177 }
178
179 void block_FifoRelease( block_fifo_t *p_fifo )
180 {
181     block_FifoEmpty( p_fifo );
182     vlc_cond_destroy( &p_fifo->wait );
183     vlc_mutex_destroy( &p_fifo->lock );
184     free( p_fifo );
185 }
186
187 void block_FifoEmpty( block_fifo_t *p_fifo )
188 {
189     block_t *b;
190
191     vlc_mutex_lock( &p_fifo->lock );
192     for( b = p_fifo->p_first; b != NULL; )
193     {
194         block_t *p_next;
195
196         p_next = b->p_next;
197         block_Release( b );
198         b = p_next;
199     }
200
201     p_fifo->i_depth = p_fifo->i_size = 0;
202     p_fifo->p_first = NULL;
203     p_fifo->pp_last = &p_fifo->p_first;
204     vlc_mutex_unlock( &p_fifo->lock );
205 }
206
207 size_t block_FifoPut( block_fifo_t *p_fifo, block_t *p_block )
208 {
209     size_t i_size = 0;
210     vlc_mutex_lock( &p_fifo->lock );
211
212     do
213     {
214         i_size += p_block->i_buffer;
215
216         *p_fifo->pp_last = p_block;
217         p_fifo->pp_last = &p_block->p_next;
218         p_fifo->i_depth++;
219         p_fifo->i_size += p_block->i_buffer;
220
221         p_block = p_block->p_next;
222
223     } while( p_block );
224
225     /* warn there is data in this fifo */
226     vlc_cond_signal( &p_fifo->wait );
227     vlc_mutex_unlock( &p_fifo->lock );
228
229     return i_size;
230 }
231
232 void block_FifoWake( block_fifo_t *p_fifo )
233 {
234     vlc_mutex_lock( &p_fifo->lock );
235     if( p_fifo->p_first == NULL )
236         p_fifo->b_force_wake = VLC_TRUE;
237     vlc_cond_signal( &p_fifo->wait );
238     vlc_mutex_unlock( &p_fifo->lock );
239 }
240
241 block_t *block_FifoGet( block_fifo_t *p_fifo )
242 {
243     block_t *b;
244
245     vlc_mutex_lock( &p_fifo->lock );
246
247     /* Remember vlc_cond_wait() may cause spurious wakeups
248      * (on both Win32 and POSIX) */
249     while( ( p_fifo->p_first == NULL ) && !p_fifo->b_force_wake )
250     {
251         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
252     }
253
254     b = p_fifo->p_first;
255
256     p_fifo->b_force_wake = VLC_FALSE;
257     if( b == NULL )
258     {
259         /* Forced wakeup */
260         vlc_mutex_unlock( &p_fifo->lock );
261         return NULL;
262     }
263
264     p_fifo->p_first = b->p_next;
265     p_fifo->i_depth--;
266     p_fifo->i_size -= b->i_buffer;
267
268     if( p_fifo->p_first == NULL )
269     {
270         p_fifo->pp_last = &p_fifo->p_first;
271     }
272
273     vlc_mutex_unlock( &p_fifo->lock );
274
275     b->p_next = NULL;
276     return b;
277 }
278
279 block_t *block_FifoShow( block_fifo_t *p_fifo )
280 {
281     block_t *b;
282
283     vlc_mutex_lock( &p_fifo->lock );
284
285     if( p_fifo->p_first == NULL )
286     {
287         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
288     }
289
290     b = p_fifo->p_first;
291
292     vlc_mutex_unlock( &p_fifo->lock );
293
294     return( b );
295 }
296
297 size_t block_FifoSize( const block_fifo_t *p_fifo )
298 {
299     return p_fifo->i_size;
300 }
301
302 size_t block_FifoCount( const block_fifo_t *p_fifo )
303 {
304     return p_fifo->i_depth;
305 }