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