blob: fd79f734af9d6583e0291ed89ad62879fe3866f2 (
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
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
|
/* $Id: HBFifo.cpp,v 1.10 2003/08/24 13:27:41 titer Exp $ */
#include "HBCommon.h"
#include "HBFifo.h"
#include <Locker.h>
HBBuffer::HBBuffer( int size )
{
fAllocSize = size;
fSize = size;
fKeyFrame = false;
fData = (uint8_t*) malloc( size );
if( !fData )
{
Log( "HBBuffer::HBBuffer() : malloc() failed, gonna crash soon" );
}
}
HBBuffer::~HBBuffer()
{
free( fData );
}
void HBBuffer::ReAlloc( int size )
{
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* ) );
fLocker = new BLocker();
fDie = false;
}
void HBFifo::Die()
{
Lock();
/* Empty the fifo */
while( fWhereToPush != fWhereToPop )
{
HBBuffer * buffer = fBuffers[fWhereToPop];
fWhereToPop++;
fWhereToPop %= ( fCapacity + 1 );
delete buffer;
}
fDie = true;
Unlock();
}
HBFifo::~HBFifo()
{
/* Empty the fifo */
while( fWhereToPush != fWhereToPop )
{
HBBuffer * buffer = fBuffers[fWhereToPop];
fWhereToPop++;
fWhereToPop %= ( fCapacity + 1 );
delete buffer;
}
/* Cleaning */
free( fBuffers );
delete fLocker;
}
/* 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() : add a packet to the fifo. If the fifo is full, it blocks
until the packet can be added. Returns true when it is successful,
or false if the fifo has been destroyed before we could add it */
bool HBFifo::Push( HBBuffer * buffer )
{
bool success = false;
while( !fDie )
{
Lock();
if( Size() < fCapacity )
{
fBuffers[fWhereToPush] = buffer;
fWhereToPush++;
fWhereToPush %= ( fCapacity + 1 );
Unlock();
success = true;
break;
}
Unlock();
snooze( 10000 );
}
if( !success )
{
delete buffer;
}
return success;
}
/* Pop() : get the first packet if the fifo. If the fifo is empty, it
blocks until a packet comes. Returns true when it is successful,
or false if the fifo has been destroyed before we could get a packet */
HBBuffer * HBFifo::Pop()
{
while( !fDie )
{
Lock();
if( fWhereToPush != fWhereToPop )
{
HBBuffer * buffer = fBuffers[fWhereToPop];
fWhereToPop++;
fWhereToPop %= ( fCapacity + 1 );
Unlock();
return buffer;
}
Unlock();
snooze( 10000 );
}
return NULL;
}
/* Lock() : private function */
void HBFifo::Lock()
{
fLocker->Lock();
}
/* Unlock() : private function */
void HBFifo::Unlock()
{
fLocker->Unlock();
}
|