1 /* $Id: fifo.c,v 1.17 2005/10/15 18:05:03 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.m0k.org/>.
5 It may be used under the terms of the GNU General Public License. */
13 hb_buffer_t * hb_buffer_init( int size )
17 if( !( b = calloc( sizeof( hb_buffer_t ), 1 ) ) )
19 hb_log( "out of memory" );
25 #if defined( SYS_DARWIN ) || defined( SYS_FREEBSD )
26 b->data = malloc( size );
27 #elif defined( SYS_CYGWIN )
29 b->data = malloc( size + 17 );
31 b->data = memalign( 16, size );
36 hb_log( "out of memory" );
43 void hb_buffer_realloc( hb_buffer_t * b, int size )
45 /* No more alignment, but we don't care */
46 b->data = realloc( b->data, size );
50 void hb_buffer_close( hb_buffer_t ** _b )
52 hb_buffer_t * b = *_b;
73 hb_fifo_t * hb_fifo_init( int capacity )
76 f = calloc( sizeof( hb_fifo_t ), 1 );
77 f->lock = hb_lock_init();
78 f->capacity = capacity;
82 int hb_fifo_size( hb_fifo_t * f )
93 int hb_fifo_is_full( hb_fifo_t * f )
98 ret = ( f->size >= f->capacity );
104 float hb_fifo_percent_full( hb_fifo_t * f )
109 ret = f->size / f->capacity;
110 hb_unlock( f->lock );
115 hb_buffer_t * hb_fifo_get( hb_fifo_t * f )
122 hb_unlock( f->lock );
129 hb_unlock( f->lock );
134 hb_buffer_t * hb_fifo_see( hb_fifo_t * f )
141 hb_unlock( f->lock );
145 hb_unlock( f->lock );
150 hb_buffer_t * hb_fifo_see2( hb_fifo_t * f )
157 hb_unlock( f->lock );
161 hb_unlock( f->lock );
166 void hb_fifo_push( hb_fifo_t * f, hb_buffer_t * b )
184 while( f->last->next )
187 f->last = f->last->next;
189 hb_unlock( f->lock );
192 void hb_fifo_close( hb_fifo_t ** _f )
197 hb_log( "fifo_close: trashing %d buffer(s)", hb_fifo_size( f ) );
198 while( ( b = hb_fifo_get( f ) ) )
200 hb_buffer_close( &b );
203 hb_lock_close( &f->lock );