OSDN Git Service

Fix for missing subtitles
[handbrake-jp/handbrake-jp-git.git] / libhb / 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 "hb.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 hb_buffer_t * hb_fifo_get( hb_fifo_t * f )
105 {
106     hb_buffer_t * b;
107
108     hb_lock( f->lock );
109     if( f->size < 1 )
110     {
111         hb_unlock( f->lock );
112         return NULL;
113     }
114     b         = f->first;
115     f->first  = b->next;
116     b->next   = NULL;
117     f->size  -= 1;
118     hb_unlock( f->lock );
119
120     return b;
121 }
122
123 hb_buffer_t * hb_fifo_see( hb_fifo_t * f )
124 {
125     hb_buffer_t * b;
126
127     hb_lock( f->lock );
128     if( f->size < 1 )
129     {
130         hb_unlock( f->lock );
131         return NULL;
132     }
133     b = f->first;
134     hb_unlock( f->lock );
135
136     return b;
137 }
138
139 hb_buffer_t * hb_fifo_see2( hb_fifo_t * f )
140 {
141     hb_buffer_t * b;
142
143     hb_lock( f->lock );
144     if( f->size < 2 )
145     {
146         hb_unlock( f->lock );
147         return NULL;
148     }
149     b = f->first->next;
150     hb_unlock( f->lock );
151
152     return b;
153 }
154
155 void hb_fifo_push( hb_fifo_t * f, hb_buffer_t * b )
156 {
157     if( !b )
158     {
159         return;
160     }
161
162     hb_lock( f->lock );
163     if( f->size > 0 )
164     {
165         f->last->next = b;
166     }
167     else
168     {
169         f->first = b;
170     }
171     f->last  = b;
172     f->size += 1;
173     while( f->last->next )
174     {
175         f->size += 1;
176         f->last  = f->last->next;
177     }
178     hb_unlock( f->lock );
179 }
180
181 void hb_fifo_close( hb_fifo_t ** _f )
182 {
183     hb_fifo_t   * f = *_f;
184     hb_buffer_t * b;
185     
186     hb_log( "fifo_close: trashing %d buffer(s)", hb_fifo_size( f ) );
187     while( ( b = hb_fifo_get( f ) ) )
188     {
189         hb_buffer_close( &b );
190     }
191
192     hb_lock_close( &f->lock );
193     free( f );
194
195     *_f = NULL;
196 }