OSDN Git Service

added call to av_log_set_level in hb_init_real based on value of verbose param.
[handbrake-jp/handbrake-jp-git.git] / libmediafork / fifo.c
1 /* $Id: fifo.c,v 1.17 2005/10/15 18:05:03 titer Exp $
2
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. */
6
7 #include "mediafork.h"
8
9 #ifndef SYS_DARWIN
10 #include <malloc.h>
11 #endif
12
13 hb_buffer_t * hb_buffer_init( int size )
14 {
15     hb_buffer_t * b;
16     
17     if( !( b = calloc( sizeof( hb_buffer_t ), 1 ) ) )
18     {
19         hb_log( "out of memory" );
20         return NULL;
21     }
22
23     b->alloc = size;
24     b->size  = size;
25 #if defined( SYS_DARWIN ) || defined( SYS_FREEBSD )
26     b->data  = malloc( size );
27 #elif defined( SYS_CYGWIN )
28     /* FIXME */
29     b->data  = malloc( size + 17 );
30 #else
31     b->data  = memalign( 16, size );
32 #endif
33
34     if( !b->data )
35     {
36         hb_log( "out of memory" );
37         free( b );
38         return NULL;
39     }
40     return b;
41 }
42
43 void hb_buffer_realloc( hb_buffer_t * b, int size )
44 {
45     /* No more alignment, but we don't care */
46     b->data  = realloc( b->data, size );
47     b->alloc = size;
48 }
49
50 void hb_buffer_close( hb_buffer_t ** _b )
51 {
52     hb_buffer_t * b = *_b;
53
54     if( b->data )
55     {
56         free( b->data );
57     }
58     free( b );
59
60     *_b = NULL;
61 }
62
63 /* Fifo */
64 struct hb_fifo_s
65 {
66     hb_lock_t    * lock;
67     int            capacity;
68     int            size;
69     hb_buffer_t  * first;
70     hb_buffer_t  * last;
71 };
72
73 hb_fifo_t * hb_fifo_init( int capacity )
74 {
75     hb_fifo_t * f;
76     f           = calloc( sizeof( hb_fifo_t ), 1 );
77     f->lock     = hb_lock_init();
78     f->capacity = capacity;
79     return f;
80 }
81
82 int hb_fifo_size( hb_fifo_t * f )
83 {
84     int ret;
85
86     hb_lock( f->lock );
87     ret = f->size;
88     hb_unlock( f->lock );
89
90     return ret;
91 }
92
93 int hb_fifo_is_full( hb_fifo_t * f )
94 {
95     int ret;
96
97     hb_lock( f->lock );
98     ret = ( f->size >= f->capacity );
99     hb_unlock( f->lock );
100
101     return ret;
102 }
103
104 float hb_fifo_percent_full( hb_fifo_t * f )
105 {
106     float ret;
107
108     hb_lock( f->lock );
109     ret = f->size / f->capacity;
110     hb_unlock( f->lock );
111
112     return ret;
113 }
114
115 hb_buffer_t * hb_fifo_get( hb_fifo_t * f )
116 {
117     hb_buffer_t * b;
118
119     hb_lock( f->lock );
120     if( f->size < 1 )
121     {
122         hb_unlock( f->lock );
123         return NULL;
124     }
125     b         = f->first;
126     f->first  = b->next;
127     b->next   = NULL;
128     f->size  -= 1;
129     hb_unlock( f->lock );
130
131     return b;
132 }
133
134 hb_buffer_t * hb_fifo_see( hb_fifo_t * f )
135 {
136     hb_buffer_t * b;
137
138     hb_lock( f->lock );
139     if( f->size < 1 )
140     {
141         hb_unlock( f->lock );
142         return NULL;
143     }
144     b = f->first;
145     hb_unlock( f->lock );
146
147     return b;
148 }
149
150 hb_buffer_t * hb_fifo_see2( hb_fifo_t * f )
151 {
152     hb_buffer_t * b;
153
154     hb_lock( f->lock );
155     if( f->size < 2 )
156     {
157         hb_unlock( f->lock );
158         return NULL;
159     }
160     b = f->first->next;
161     hb_unlock( f->lock );
162
163     return b;
164 }
165
166 void hb_fifo_push( hb_fifo_t * f, hb_buffer_t * b )
167 {
168     if( !b )
169     {
170         return;
171     }
172
173     hb_lock( f->lock );
174     if( f->size > 0 )
175     {
176         f->last->next = b;
177     }
178     else
179     {
180         f->first = b;
181     }
182     f->last  = b;
183     f->size += 1;
184     while( f->last->next )
185     {
186         f->size += 1;
187         f->last  = f->last->next;
188     }
189     hb_unlock( f->lock );
190 }
191
192 void hb_fifo_close( hb_fifo_t ** _f )
193 {
194     hb_fifo_t   * f = *_f;
195     hb_buffer_t * b;
196     
197     hb_log( "fifo_close: trashing %d buffer(s)", hb_fifo_size( f ) );
198     while( ( b = hb_fifo_get( f ) ) )
199     {
200         hb_buffer_close( &b );
201     }
202
203     hb_lock_close( &f->lock );
204     free( f );
205
206     *_f = NULL;
207 }