blob: 35bcb0449a2305f3a9ebbcd058e0977ede71bc50 (
plain)
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
|
/* $Id: Fifo.cpp,v 1.14 2003/10/02 15:44:52 titer Exp $
This file is part of the HandBrake source code.
Homepage: <http://beos.titer.org/handbrake/>.
It may be used under the terms of the GNU General Public License. */
#include "Fifo.h"
#include "Thread.h"
HBBuffer::HBBuffer( uint32_t size )
{
fAllocSize = size;
fSize = size;
fKeyFrame = false;
fData = (uint8_t*) malloc( size );
fPosition = 0;
if( !fData )
{
Log( "HBBuffer::HBBuffer() : malloc() failed, gonna crash soon" );
}
}
HBBuffer::~HBBuffer()
{
free( fData );
}
void HBBuffer::ReAlloc( uint32_t size )
{
fData = (uint8_t*) realloc( fData, size );
if( !fData )
{
Log( "HBBuffer::ReAlloc() : realloc() failed, gonna crash soon" );
}
fAllocSize = size;
}
/* Constructor */
HBFifo::HBFifo( int capacity )
{
fCapacity = capacity;
fWhereToPush = 0;
fWhereToPop = 0;
fBuffers = (HBBuffer**) malloc( ( fCapacity + 1 ) * sizeof( void* ) );
fLock = new HBLock();
}
HBFifo::~HBFifo()
{
Log( "HBFifo::~HBFifo : trashing %d buffers", Size() );
/* Empty the fifo */
while( fWhereToPush != fWhereToPop )
{
HBBuffer * buffer = fBuffers[fWhereToPop];
fWhereToPop++;
fWhereToPop %= ( fCapacity + 1 );
delete buffer;
}
/* Cleaning */
free( fBuffers );
delete fLock;
}
/* Size() : returns how much the fifo is currently filled */
int HBFifo::Size()
{
return ( fCapacity + 1 + fWhereToPush - fWhereToPop ) %
( fCapacity + 1 );
}
/* Capacity() : simply returns the fifo capacity... */
int HBFifo::Capacity()
{
return fCapacity;
}
/* Push() - returns immediatly (true if successful, false otherwise ) */
bool HBFifo::Push( HBBuffer * buffer )
{
fLock->Lock();
if( Size() < fCapacity )
{
fBuffers[fWhereToPush] = buffer;
fWhereToPush++;
fWhereToPush %= ( fCapacity + 1 );
fLock->Unlock();
return true;
}
fLock->Unlock();
return false;
}
/* Pop() - returns immediatly (a pointer to a buffer if successful,
NULL otherwise ) */
HBBuffer * HBFifo::Pop()
{
fLock->Lock();
if( fWhereToPush != fWhereToPop )
{
HBBuffer * buffer = fBuffers[fWhereToPop];
fWhereToPop++;
fWhereToPop %= ( fCapacity + 1 );
fLock->Unlock();
return buffer;
}
fLock->Unlock();
return NULL;
}
|