OSDN Git Service

HandBrake 0.1
[handbrake-jp/handbrake-jp-git.git] / HBFifo.cpp
1 /* $Id: HBFifo.cpp,v 1.10 2003/08/24 13:27:41 titer Exp $ */
2
3 #include "HBCommon.h"
4 #include "HBFifo.h"
5
6 #include <Locker.h>
7
8 HBBuffer::HBBuffer( int size )
9 {
10     fAllocSize = size;
11     fSize = size;
12     fKeyFrame = false;
13     fData = (uint8_t*) malloc( size );
14     
15     if( !fData )
16     {
17         Log( "HBBuffer::HBBuffer() : malloc() failed, gonna crash soon" );
18     }
19 }
20
21 HBBuffer::~HBBuffer()
22 {
23     free( fData );
24 }
25
26 void HBBuffer::ReAlloc( int size )
27 {
28     realloc( fData, size );
29
30     if( !fData )
31     {
32         Log( "HBBuffer::ReAlloc() : realloc() failed, gonna crash soon" );
33     }
34
35     fAllocSize = size;
36 }
37
38 /* Constructor */
39 HBFifo::HBFifo( int capacity )
40 {
41     fCapacity    = capacity;
42     
43     fWhereToPush = 0;
44     fWhereToPop  = 0;
45     fBuffers     = (HBBuffer**) malloc( ( fCapacity + 1 ) * sizeof( void* ) );
46     fLocker      = new BLocker();
47     fDie         = false;
48 }
49
50 void HBFifo::Die()
51 {
52     Lock();
53
54     /* Empty the fifo */
55     while( fWhereToPush != fWhereToPop )
56     {
57         HBBuffer * buffer = fBuffers[fWhereToPop];
58         fWhereToPop++;
59         fWhereToPop %= ( fCapacity + 1 );
60         delete buffer;
61     }
62     
63     fDie = true;
64     
65     Unlock();
66 }
67
68 HBFifo::~HBFifo()
69 {
70     /* Empty the fifo */
71     while( fWhereToPush != fWhereToPop )
72     {
73         HBBuffer * buffer = fBuffers[fWhereToPop];
74         fWhereToPop++;
75         fWhereToPop %= ( fCapacity + 1 );
76         delete buffer;
77     }
78     
79     /* Cleaning */
80     free( fBuffers );
81     delete fLocker;
82 }
83
84 /* Size() : returns how much the fifo is currently filled */
85 int HBFifo::Size()
86 {
87     return ( fCapacity + 1 + fWhereToPush - fWhereToPop ) %
88              ( fCapacity + 1 );
89 }
90
91 /* Capacity() : simply returns the fifo capacity... */
92 int HBFifo::Capacity()
93 {
94     return fCapacity;
95 }
96
97 /* Push() : add a packet to the fifo. If the fifo is full, it blocks
98    until the packet can be added. Returns true when it is successful,
99    or false if the fifo has been destroyed before we could add it */
100 bool HBFifo::Push( HBBuffer * buffer )
101 {
102     bool success = false;
103
104     while( !fDie )
105     {
106         Lock();
107         if( Size() < fCapacity )
108         {
109             fBuffers[fWhereToPush] = buffer;
110             fWhereToPush++;
111             fWhereToPush %= ( fCapacity + 1 );
112             Unlock();
113             success = true;
114             break;
115         }
116         Unlock();
117         snooze( 10000 );
118     }
119     
120     if( !success )
121     {
122         delete buffer;
123     }
124     
125     return success;
126 }
127
128 /* Pop() : get the first packet if the fifo. If the fifo is empty, it
129    blocks until a packet comes. Returns true when it is successful,
130    or false if the fifo has been destroyed before we could get a packet */
131 HBBuffer * HBFifo::Pop()
132 {
133     while( !fDie )
134     {
135         Lock();    
136         if( fWhereToPush != fWhereToPop )
137         {
138             HBBuffer * buffer = fBuffers[fWhereToPop];
139             fWhereToPop++;
140             fWhereToPop %= ( fCapacity + 1 );
141             Unlock();
142             return buffer;
143         }
144         Unlock();
145         snooze( 10000 );
146     }
147     
148     return NULL;
149 }
150
151 /* Lock() : private function */
152 void HBFifo::Lock()
153 {
154     fLocker->Lock();
155 }
156
157 /* Unlock() : private function */
158 void HBFifo::Unlock()
159 {
160     fLocker->Unlock();
161 }