|
1 // Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 |
|
15 #ifndef REQUEST_BUFFER_H_ |
|
16 #define REQUEST_BUFFER_H_ |
|
17 |
|
18 #include "serializerplatform.h" |
|
19 |
|
20 /* |
|
21 * Base for ring buffer users |
|
22 */ |
|
23 class MRequestBufferBookKeepingBase |
|
24 { |
|
25 public: |
|
26 virtual TUint32 GetWriteCount() = 0; |
|
27 virtual TUint32 GetReadCount() = 0; |
|
28 virtual TUint32 BufferTail() = 0; |
|
29 }; |
|
30 |
|
31 /* |
|
32 * Ring buffer writer. Data source for ring buffer. |
|
33 */ |
|
34 class MRequestBufferBookKeepingWriter: public MRequestBufferBookKeepingBase |
|
35 { |
|
36 public: |
|
37 virtual TUint32 BufferHead() = 0; |
|
38 virtual void SetBufferHead( TUint32 aIndex ) = 0; |
|
39 virtual void IncrementWriteCount( TUint32 aWriteCount ) = 0; |
|
40 virtual void SetMaxTailIndex( TUint32 aIndex ) = 0; |
|
41 }; |
|
42 |
|
43 /* |
|
44 * Ring buffer reader. Data consumer for ring buffer. |
|
45 */ |
|
46 class MRequestBufferBookKeepingReader: public MRequestBufferBookKeepingBase |
|
47 { |
|
48 public: |
|
49 virtual void SetBufferTail( TUint32 aIndex ) = 0; |
|
50 virtual void IncrementReadCount( TUint32 aReadCount ) = 0; |
|
51 virtual TUint32 MaxTailIndex() = 0; |
|
52 }; |
|
53 |
|
54 class RequestBufferBase |
|
55 { |
|
56 public: |
|
57 |
|
58 /* |
|
59 * |
|
60 */ |
|
61 static TUint32 AdjustAlignment( TUint32 aIndex, const TUint32 aAlignment ) |
|
62 { |
|
63 const TUint32 remainder = aIndex % aAlignment; |
|
64 if ( remainder ) |
|
65 { |
|
66 aIndex += aAlignment - remainder; |
|
67 } |
|
68 return aIndex; |
|
69 } |
|
70 |
|
71 protected: |
|
72 |
|
73 /* |
|
74 * |
|
75 */ |
|
76 RequestBufferBase( TUint32 aSize ): |
|
77 iSize( aSize ) |
|
78 {} |
|
79 |
|
80 protected: |
|
81 const TUint32 iSize; |
|
82 }; |
|
83 |
|
84 |
|
85 |
|
86 class RequestBufferWriter: RequestBufferBase |
|
87 { |
|
88 public: |
|
89 |
|
90 /* |
|
91 * |
|
92 */ |
|
93 RequestBufferWriter( MRequestBufferBookKeepingWriter& aBookKeeper, TUint32 aSize ): |
|
94 RequestBufferBase( aSize ), |
|
95 iBookKeeper( aBookKeeper ) |
|
96 {} |
|
97 |
|
98 /* |
|
99 * |
|
100 */ |
|
101 void InitBuffer() |
|
102 { |
|
103 iBookKeeper.SetBufferHead( 0 ); |
|
104 iBookKeeper.SetMaxTailIndex( iSize ); |
|
105 } |
|
106 |
|
107 /* |
|
108 * Does not check for free space, assure free space by using CheckForSpace |
|
109 */ |
|
110 TUint32 AllocateBytes( const TUint32 aBytes ) |
|
111 { |
|
112 TUint32 base = iBookKeeper.BufferHead(); |
|
113 if ( base + aBytes > iSize ) |
|
114 { |
|
115 iBookKeeper.SetMaxTailIndex( base ); |
|
116 base = 0; |
|
117 } |
|
118 else if ( iBookKeeper.BufferTail() <= base ) |
|
119 { |
|
120 iBookKeeper.SetMaxTailIndex( iSize ); |
|
121 } |
|
122 return base; |
|
123 } |
|
124 |
|
125 /* |
|
126 * Does not check for free space, assure free space by using CheckForSpace |
|
127 */ |
|
128 void CommitBytes( const TUint32 aBase, const TUint32 aBytes ) |
|
129 { |
|
130 //TUint32 base = CheckIndexForWrapAround( AdjustAlignment( aBase + aBytes, 4 ) ); |
|
131 TUint32 base = AdjustAlignment( aBase + aBytes, 4 ); |
|
132 const TUint32 inc( base - aBase ); |
|
133 iBookKeeper.SetBufferHead( base ); |
|
134 iBookKeeper.IncrementWriteCount( inc ); |
|
135 } |
|
136 |
|
137 /* |
|
138 * |
|
139 */ |
|
140 TBool CheckForSpace( const TUint32 aSpaceNeeded ) |
|
141 { |
|
142 const TUint32 inputBufferHead( iBookKeeper.BufferHead() ); |
|
143 const TUint32 inputBufferTail( iBookKeeper.BufferTail() ); |
|
144 //Notice that tail might grow during the execution of this function |
|
145 // but it would only cause false negative as a result |
|
146 |
|
147 //Buffer can be empty or full |
|
148 if ( inputBufferHead == inputBufferTail ) |
|
149 { |
|
150 //Check if the buffer is full or empty |
|
151 if ( iBookKeeper.GetWriteCount() - iBookKeeper.GetReadCount() ) |
|
152 { |
|
153 //Buffer is full |
|
154 return EFalse; |
|
155 } |
|
156 } |
|
157 |
|
158 //Let's check if the SFC fits to the buffer |
|
159 const TUint32 newHeadIndex = inputBufferHead + aSpaceNeeded;// + alignmentAdjust; |
|
160 |
|
161 if ( inputBufferHead < inputBufferTail && newHeadIndex > inputBufferTail ) |
|
162 { |
|
163 //Buffer does not have enough space |
|
164 return EFalse; |
|
165 } |
|
166 else if ( inputBufferHead >= inputBufferTail ) |
|
167 { |
|
168 if ( newHeadIndex > iSize && aSpaceNeeded > inputBufferTail ) |
|
169 { |
|
170 //Buffer does not have enough space |
|
171 return EFalse; |
|
172 } |
|
173 } |
|
174 return ETrue; |
|
175 } |
|
176 |
|
177 private: |
|
178 MRequestBufferBookKeepingWriter& iBookKeeper; |
|
179 }; |
|
180 |
|
181 class RequestBufferReader: public RequestBufferBase |
|
182 { |
|
183 public: |
|
184 /* |
|
185 * |
|
186 */ |
|
187 RequestBufferReader( MRequestBufferBookKeepingReader& aBookKeeper, TUint32 aSize ): |
|
188 RequestBufferBase( aSize ), |
|
189 iBookKeeper( aBookKeeper ) |
|
190 {} |
|
191 |
|
192 /* |
|
193 * |
|
194 */ |
|
195 void InitBuffer() |
|
196 { |
|
197 iBookKeeper.SetBufferTail( 0 ); |
|
198 } |
|
199 |
|
200 /* |
|
201 * |
|
202 */ |
|
203 TUint32 GetReadIndex() |
|
204 { |
|
205 const TUint32 bufTail( iBookKeeper.BufferTail() ); |
|
206 const TUint32 bufTail2( CheckIndexForWrapAround( bufTail ) ); |
|
207 if ( bufTail != bufTail2 ) |
|
208 { |
|
209 iBookKeeper.SetBufferTail( bufTail2 ); |
|
210 } |
|
211 return bufTail2; |
|
212 } |
|
213 |
|
214 /* |
|
215 * |
|
216 */ |
|
217 void FreeBytes( TUint32 aBytes ) |
|
218 { |
|
219 const TUint32 oldTail(iBookKeeper.BufferTail()); |
|
220 TUint32 newBufferTail = AdjustAlignment( aBytes + oldTail, 4 ); |
|
221 const TUint32 inc( newBufferTail - oldTail ); |
|
222 newBufferTail = CheckIndexForWrapAround( newBufferTail ); |
|
223 iBookKeeper.IncrementReadCount( inc ); |
|
224 iBookKeeper.SetBufferTail( newBufferTail ); |
|
225 } |
|
226 |
|
227 /* |
|
228 * |
|
229 */ |
|
230 TBool IsDataAvailable() |
|
231 { |
|
232 const TUint32 readc( iBookKeeper.GetReadCount() ); |
|
233 const TUint32 writec( iBookKeeper.GetWriteCount() ); |
|
234 return readc != writec; |
|
235 } |
|
236 |
|
237 /* |
|
238 * |
|
239 */ |
|
240 TUint32 CheckIndexForWrapAround( TUint32 aIndex ) |
|
241 { |
|
242 //Head is behind of tail when MaxTailIndex is applied so |
|
243 // this routine works for head, too |
|
244 if ( aIndex >= iBookKeeper.MaxTailIndex() ) |
|
245 { |
|
246 return 0; |
|
247 } |
|
248 return aIndex; |
|
249 } |
|
250 |
|
251 private: |
|
252 MRequestBufferBookKeepingReader& iBookKeeper; |
|
253 }; |
|
254 |
|
255 #endif |