OSDN Git Service

Add Bluray support
[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.fr/>.
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 #define FIFO_TIMEOUT 200
14
15 /* Fifo */
16 struct hb_fifo_s
17 {
18     hb_lock_t    * lock;
19     hb_cond_t    * cond_full;
20     int            wait_full;
21     hb_cond_t    * cond_empty;
22     int            wait_empty;
23     uint32_t       capacity;
24     uint32_t       thresh;
25     uint32_t       size;
26     uint32_t       buffer_size;
27     hb_buffer_t  * first;
28     hb_buffer_t  * last;
29 };
30
31 /* we round the requested buffer size up to the next power of 2 so there can
32  * be at most 32 possible pools when the size is a 32 bit int. To avoid a lot
33  * of slow & error-prone run-time checking we allow for all 32. */
34 #define MAX_BUFFER_POOLS  32
35 /* the buffer pool only exists to avoid the two malloc and two free calls that
36  * it would otherwise take to allocate & free a buffer. but we don't want to
37  * tie up a lot of memory in the pool because this allocator isn't as general
38  * as malloc so memory tied up here puts more pressure on the malloc pool.
39  * A pool of 16 elements will avoid 94% of the malloc/free calls without wasting
40  * too much memory. */
41 #define BUFFER_POOL_MAX_ELEMENTS 32
42
43 struct hb_buffer_pools_s
44 {
45     int64_t allocated;
46     hb_lock_t *lock;
47     hb_fifo_t *pool[MAX_BUFFER_POOLS];
48 } buffers;
49
50
51 void hb_buffer_pool_init( void )
52 {
53     buffers.lock = hb_lock_init();
54     buffers.allocated = 0;
55
56     /* we allocate pools for sizes 2^10 through 2^25. requests larger than
57      * 2^25 will get passed through to malloc. */
58     int i;
59     for ( i = 10; i < 26; ++i )
60     {
61         buffers.pool[i] = hb_fifo_init(BUFFER_POOL_MAX_ELEMENTS, 1);
62         buffers.pool[i]->buffer_size = 1 << i;
63     }
64     /* requests smaller than 2^10 are satisfied from the 2^10 pool. */
65     for ( i = 1; i < 10; ++i )
66     {
67         buffers.pool[i] = buffers.pool[10];
68     }
69 }
70
71 void hb_buffer_pool_free( void )
72 {
73     int i;
74     int count;
75     int64_t freed = 0;
76     hb_buffer_t *b;
77
78     hb_lock(buffers.lock);
79
80     for( i = 10; i < 26; ++i)
81     {
82         count = 0;
83         while( ( b = hb_fifo_get(buffers.pool[i]) ) )
84         {
85             freed += b->alloc;
86             if( b->data )
87             {
88                 free( b->data );
89             }
90             free( b );
91             count++;
92         }
93         if ( count )
94         {
95             hb_deep_log( 2, "Freed %d buffers of size %d", count,
96                     buffers.pool[i]->buffer_size);
97         }
98     }
99
100     hb_deep_log( 2, "Allocated %"PRId64" bytes of buffers on this pass and Freed %"PRId64" bytes, "
101            "%"PRId64" bytes leaked", buffers.allocated, freed, buffers.allocated - freed);
102     buffers.allocated = 0;
103     hb_unlock(buffers.lock);
104 }
105
106 static hb_fifo_t *size_to_pool( int size )
107 {
108     int i;
109     for ( i = 0; i < 30; ++i )
110     {
111         if ( size <= (1 << i) )
112         {
113             return buffers.pool[i];
114         }
115     }
116     return NULL;
117 }
118
119 hb_buffer_t * hb_buffer_init( int size )
120 {
121     hb_buffer_t * b;
122     hb_fifo_t *buffer_pool = size_to_pool( size );
123
124     if( buffer_pool )
125     {
126         b = hb_fifo_get( buffer_pool );
127
128         if( b )
129         {
130             /*
131              * Zero the contents of the buffer, would be nice if we
132              * didn't have to do this.
133              */
134             uint8_t *data = b->data;
135             memset( b, 0, sizeof(hb_buffer_t) );
136             b->alloc = buffer_pool->buffer_size;
137             b->size = size;
138             b->data = data;
139             return( b );
140         }
141     }
142
143     /*
144      * No existing buffers, create a new one
145      */
146     if( !( b = calloc( sizeof( hb_buffer_t ), 1 ) ) )
147     {
148         hb_log( "out of memory" );
149         return NULL;
150     }
151
152     b->size  = size;
153     b->alloc  = buffer_pool? buffer_pool->buffer_size : size;
154
155     if (size)
156     {
157 #if defined( SYS_DARWIN ) || defined( SYS_FREEBSD ) || defined( SYS_MINGW )
158         b->data  = malloc( b->alloc );
159 #elif defined( SYS_CYGWIN )
160         /* FIXME */
161         b->data  = malloc( b->alloc + 17 );
162 #else
163         b->data  = memalign( 16, b->alloc );
164 #endif
165         if( !b->data )
166         {
167             hb_log( "out of memory" );
168             free( b );
169             return NULL;
170         }
171         hb_lock(buffers.lock);
172         buffers.allocated += b->alloc;
173         hb_unlock(buffers.lock);
174     }
175     return b;
176 }
177
178 void hb_buffer_realloc( hb_buffer_t * b, int size )
179 {
180     if ( size > b->alloc )
181     {
182         uint32_t orig = b->alloc;
183         size = size_to_pool( size )->buffer_size;
184         b->data  = realloc( b->data, size );
185         b->alloc = size;
186
187         hb_lock(buffers.lock);
188         buffers.allocated += size - orig;
189         hb_unlock(buffers.lock);
190     }
191 }
192
193 void hb_buffer_close( hb_buffer_t ** _b )
194 {
195     hb_buffer_t * b = *_b;
196     hb_fifo_t *buffer_pool = size_to_pool( b->alloc );
197
198     if( buffer_pool && b->data && !hb_fifo_is_full( buffer_pool ) )
199     {
200         hb_fifo_push_head( buffer_pool, b );
201         *_b = NULL;
202         return;
203     }
204     /* either the pool is full or this size doesn't use a pool - free the buf */
205     while( b )
206     {
207         hb_buffer_t * next = b->next;
208         if( b->data )
209         {
210             free( b->data );
211             hb_lock(buffers.lock);
212             buffers.allocated -= b->alloc;
213             hb_unlock(buffers.lock);
214         }
215         free( b );
216         b = next;
217     }
218     *_b = NULL;
219 }
220
221 void hb_buffer_copy_settings( hb_buffer_t * dst, const hb_buffer_t * src )
222 {
223     dst->start     = src->start;
224     dst->stop      = src->stop;
225     dst->new_chap  = src->new_chap;
226     dst->frametype = src->frametype;
227     dst->flags     = src->flags;
228 }
229
230 hb_fifo_t * hb_fifo_init( int capacity, int thresh )
231 {
232     hb_fifo_t * f;
233     f             = calloc( sizeof( hb_fifo_t ), 1 );
234     f->lock       = hb_lock_init();
235     f->cond_full  = hb_cond_init();
236     f->cond_empty = hb_cond_init();
237     f->capacity   = capacity;
238     f->thresh     = thresh;
239     f->buffer_size = 0;
240     return f;
241 }
242
243 int hb_fifo_size_bytes( hb_fifo_t * f )
244 {
245     int ret = 0;
246     hb_buffer_t * link;
247
248     hb_lock( f->lock );
249     link = f->first;
250     while ( link )
251     {
252         ret += link->size;
253         link = link->next;
254     }
255     hb_unlock( f->lock );
256
257     return ret;
258 }
259
260 int hb_fifo_size( hb_fifo_t * f )
261 {
262     int ret;
263
264     hb_lock( f->lock );
265     ret = f->size;
266     hb_unlock( f->lock );
267
268     return ret;
269 }
270
271 int hb_fifo_is_full( hb_fifo_t * f )
272 {
273     int ret;
274
275     hb_lock( f->lock );
276     ret = ( f->size >= f->capacity );
277     hb_unlock( f->lock );
278
279     return ret;
280 }
281
282 float hb_fifo_percent_full( hb_fifo_t * f )
283 {
284     float ret;
285
286     hb_lock( f->lock );
287     ret = f->size / f->capacity;
288     hb_unlock( f->lock );
289
290     return ret;
291 }
292
293 hb_buffer_t * hb_fifo_get_wait( hb_fifo_t * f )
294 {
295     hb_buffer_t * b;
296
297     hb_lock( f->lock );
298     if( f->size < 1 )
299     {
300         f->wait_empty = 1;
301         hb_cond_timedwait( f->cond_empty, f->lock, FIFO_TIMEOUT );
302         if( f->size < 1 )
303         {
304             hb_unlock( f->lock );
305             return NULL;
306         }
307     }
308     b         = f->first;
309     f->first  = b->next;
310     b->next   = NULL;
311     f->size  -= 1;
312     if( f->wait_full && f->size == f->capacity - f->thresh )
313     {
314         f->wait_full = 0;
315         hb_cond_signal( f->cond_full );
316     }
317     hb_unlock( f->lock );
318
319     return b;
320 }
321
322 hb_buffer_t * hb_fifo_get( hb_fifo_t * f )
323 {
324     hb_buffer_t * b;
325
326     hb_lock( f->lock );
327     if( f->size < 1 )
328     {
329         hb_unlock( f->lock );
330         return NULL;
331     }
332     b         = f->first;
333     f->first  = b->next;
334     b->next   = NULL;
335     f->size  -= 1;
336     if( f->wait_full && f->size == f->capacity - f->thresh )
337     {
338         f->wait_full = 0;
339         hb_cond_signal( f->cond_full );
340     }
341     hb_unlock( f->lock );
342
343     return b;
344 }
345
346 hb_buffer_t * hb_fifo_see_wait( hb_fifo_t * f )
347 {
348     hb_buffer_t * b;
349
350     hb_lock( f->lock );
351     if( f->size < 1 )
352     {
353         f->wait_empty = 1;
354         hb_cond_timedwait( f->cond_empty, f->lock, FIFO_TIMEOUT );
355         if( f->size < 1 )
356         {
357             hb_unlock( f->lock );
358             return NULL;
359         }
360     }
361     b = f->first;
362     hb_unlock( f->lock );
363
364     return b;
365 }
366
367 hb_buffer_t * hb_fifo_see( hb_fifo_t * f )
368 {
369     hb_buffer_t * b;
370
371     hb_lock( f->lock );
372     if( f->size < 1 )
373     {
374         hb_unlock( f->lock );
375         return NULL;
376     }
377     b = f->first;
378     hb_unlock( f->lock );
379
380     return b;
381 }
382
383 hb_buffer_t * hb_fifo_see2( hb_fifo_t * f )
384 {
385     hb_buffer_t * b;
386
387     hb_lock( f->lock );
388     if( f->size < 2 )
389     {
390         hb_unlock( f->lock );
391         return NULL;
392     }
393     b = f->first->next;
394     hb_unlock( f->lock );
395
396     return b;
397 }
398
399 int hb_fifo_full_wait( hb_fifo_t * f )
400 {
401     int result;
402
403     hb_lock( f->lock );
404     if( f->size >= f->capacity )
405     {
406         f->wait_full = 1;
407         hb_cond_timedwait( f->cond_full, f->lock, FIFO_TIMEOUT );
408     }
409     result = ( f->size < f->capacity );
410     hb_unlock( f->lock );
411     return result;
412 }
413
414 void hb_fifo_push_wait( hb_fifo_t * f, hb_buffer_t * b )
415 {
416     if( !b )
417     {
418         return;
419     }
420
421     hb_lock( f->lock );
422     if( f->size >= f->capacity )
423     {
424         f->wait_full = 1;
425         hb_cond_timedwait( f->cond_full, f->lock, FIFO_TIMEOUT );
426     }
427     if( f->size > 0 )
428     {
429         f->last->next = b;
430     }
431     else
432     {
433         f->first = b;
434     }
435     f->last  = b;
436     f->size += 1;
437     while( f->last->next )
438     {
439         f->size += 1;
440         f->last  = f->last->next;
441     }
442     if( f->wait_empty && f->size >= f->thresh )
443     {
444         f->wait_empty = 0;
445         hb_cond_signal( f->cond_empty );
446     }
447     hb_unlock( f->lock );
448 }
449
450 void hb_fifo_push( hb_fifo_t * f, hb_buffer_t * b )
451 {
452     if( !b )
453     {
454         return;
455     }
456
457     hb_lock( f->lock );
458     if( f->size > 0 )
459     {
460         f->last->next = b;
461     }
462     else
463     {
464         f->first = b;
465     }
466     f->last  = b;
467     f->size += 1;
468     while( f->last->next )
469     {
470         f->size += 1;
471         f->last  = f->last->next;
472     }
473     if( f->wait_empty && f->size >= f->thresh )
474     {
475         f->wait_empty = 0;
476         hb_cond_signal( f->cond_empty );
477     }
478     hb_unlock( f->lock );
479 }
480
481 void hb_fifo_push_head( hb_fifo_t * f, hb_buffer_t * b )
482 {
483     hb_buffer_t * tmp;
484     uint32_t      size = 0;
485
486     if( !b )
487     {
488         return;
489     }
490
491     hb_lock( f->lock );
492
493     /*
494      * If there are a chain of buffers prepend the lot
495      */
496     tmp = b;
497     while( tmp->next )
498     {
499         tmp = tmp->next;
500         size += 1;
501     }
502
503     if( f->size > 0 )
504     {
505         tmp->next = f->first;
506     } 
507     else
508     {
509         f->last = tmp;
510     }
511
512     f->first = b;
513     f->size += ( size + 1 );
514
515     hb_unlock( f->lock );
516 }
517
518 void hb_fifo_close( hb_fifo_t ** _f )
519 {
520     hb_fifo_t   * f = *_f;
521     hb_buffer_t * b;
522
523     hb_deep_log( 2, "fifo_close: trashing %d buffer(s)", hb_fifo_size( f ) );
524     while( ( b = hb_fifo_get( f ) ) )
525     {
526         hb_buffer_close( &b );
527     }
528
529     hb_lock_close( &f->lock );
530     hb_cond_close( &f->cond_empty );
531     hb_cond_close( &f->cond_full );
532     free( f );
533
534     *_f = NULL;
535 }
536
537 void hb_fifo_flush( hb_fifo_t * f )
538 {
539     hb_buffer_t * b;
540
541     while( ( b = hb_fifo_get( f ) ) )
542     {
543         hb_buffer_close( &b );
544     }
545     hb_lock( f->lock );
546     hb_cond_signal( f->cond_empty );
547     hb_cond_signal( f->cond_full );
548     hb_unlock( f->lock );
549
550 }
551