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