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