]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTDataBuffer.cxx
avoid repeated scanning of digit via a static instance
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTDataBuffer.cxx
CommitLineData
3f2a1b1c 1// $Id$
2
3/**************************************************************************
9be2600f 4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
3f2a1b1c 6 * *
9be2600f 7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * for The ALICE HLT Project. *
3f2a1b1c 9 * *
10 * Permission to use, copy, modify and distribute this software and its *
11 * documentation strictly for non-commercial purposes is hereby granted *
12 * without fee, provided that the above copyright notice appears in all *
13 * copies and that both the copyright notice and this permission notice *
14 * appear in the supporting documentation. The authors make no claims *
15 * about the suitability of this software for any purpose. It is *
16 * provided "as is" without express or implied warranty. *
17 **************************************************************************/
18
b22e91eb 19/** @file AliHLTDataBuffer.cxx
20 @author Matthias Richter
21 @date
22 @brief Handling of Data Buffers for HLT components.
23*/
3f2a1b1c 24
30338a30 25// see header file for class documentation
26// or
27// refer to README to build package
28// or
29// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
30
0c0c9d99 31#if __GNUC__>= 3
3f2a1b1c 32using namespace std;
33#endif
34
35#include "AliHLTDataBuffer.h"
6235cd38 36#include "AliHLTConsumerDescriptor.h"
b22e91eb 37#include "AliHLTComponent.h"
66043029 38#include <cerrno>
39//#include <string>
40//#include "AliHLTSystem.h"
3f2a1b1c 41
b22e91eb 42/** ROOT macro for the implementation of ROOT specific class methods */
3f2a1b1c 43ClassImp(AliHLTDataBuffer)
44
3f2a1b1c 45AliHLTDataBuffer::AliHLTDataBuffer()
85869391 46 :
47 fSegments(),
48 fConsumers(),
49 fActiveConsumers(),
50 fReleasedConsumers(),
51 fpBuffer(NULL),
52 fFlags(0)
3f2a1b1c 53{
70ed7d01 54 // see header file for class documentation
55 // or
56 // refer to README to build package
57 // or
58 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
85869391 59 fSegments.empty();
60 fConsumers.empty();
61 fActiveConsumers.empty();
62 fReleasedConsumers.empty();
70ed7d01 63 fgNofInstances++;
3f2a1b1c 64}
65
85869391 66AliHLTDataBuffer::AliHLTDataBuffer(const AliHLTDataBuffer&)
67 :
53feaef5 68 TObject(),
69 AliHLTLogging(),
85869391 70 fSegments(),
71 fConsumers(),
72 fActiveConsumers(),
73 fReleasedConsumers(),
74 fpBuffer(NULL),
75 fFlags(0)
76{
70ed7d01 77 // see header file for function documentation
85869391 78 HLTFatal("copy constructor untested");
79}
80
81AliHLTDataBuffer& AliHLTDataBuffer::operator=(const AliHLTDataBuffer&)
82{
70ed7d01 83 // see header file for function documentation
85869391 84 HLTFatal("assignment operator untested");
85 return *this;
86}
87
70ed7d01 88int AliHLTDataBuffer::fgNofInstances=0;
6235cd38 89vector<AliHLTDataBuffer::AliHLTRawBuffer*> AliHLTDataBuffer::fgFreeBuffers;
90vector<AliHLTDataBuffer::AliHLTRawBuffer*> AliHLTDataBuffer::fgActiveBuffers;
70ed7d01 91AliHLTUInt32_t AliHLTDataBuffer::fgMargin=1024;
b22e91eb 92AliHLTLogging AliHLTDataBuffer::fgLogging;
8451168b 93const Int_t AliHLTDataBuffer::fgkSafetyPatternSize=16;
94const char AliHLTDataBuffer::fgkSafetyPattern[]={0x28, 0x63, 0x29, 0x4d, 0x52, 0x49, 0x43, 0x48, 0x54, 0x45, 0x52, 0x20, 0x32, 0x30, 0x30, 0x37};
b22e91eb 95
3f2a1b1c 96AliHLTDataBuffer::~AliHLTDataBuffer()
97{
70ed7d01 98 // see header file for function documentation
99 if (--fgNofInstances<=0) {
3f2a1b1c 100 DeleteRawBuffers();
101 }
102 CleanupConsumerList();
103}
104
0c0c9d99 105int AliHLTDataBuffer::SetConsumer(AliHLTComponent* pConsumer)
3f2a1b1c 106{
70ed7d01 107 // see header file for function documentation
3f2a1b1c 108 int iResult=0;
109 if (pConsumer) {
9ce4bf4a 110 if (FindConsumer(pConsumer)) {
111 HLTWarning("consumer %s (%p) already set to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
112 }
0c0c9d99 113 AliHLTConsumerDescriptor* pDesc=new AliHLTConsumerDescriptor(pConsumer);
3f2a1b1c 114 if (pDesc) {
115 fConsumers.push_back(pDesc);
9ce4bf4a 116 HLTDebug("set consumer %s (%p) to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
3f2a1b1c 117 } else {
118 HLTError("memory allocation failed");
119 iResult=-ENOMEM;
120 }
121 } else {
9ce4bf4a 122 HLTError("invalid parameter: consumer component (nil)");
3f2a1b1c 123 iResult=-EINVAL;
124 }
125 return iResult;
126}
127
8ede8717 128int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList)
0c0c9d99 129{
70ed7d01 130 // see header file for function documentation
0c0c9d99 131 int iResult=0;
132 if (pConsumer) {
6235cd38 133 vector<AliHLTDataBuffer::AliHLTDataSegment> segments;
0c0c9d99 134 if ((iResult=FindMatchingDataSegments(pConsumer, segments))>=0) {
135 if (tgtList) {
6235cd38 136 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=segments.begin();
0c0c9d99 137 while (segment!=segments.end()) {
138 tgtList->push_back((*segment).fDataType);
139 segment++;
140 }
141 }
142 iResult=segments.size();
143 }
144 } else {
145 iResult=-EINVAL;
146 }
147 return iResult;
148}
149
6235cd38 150int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataBuffer::AliHLTDataSegment>& tgtList)
0c0c9d99 151{
70ed7d01 152 // see header file for function documentation
0c0c9d99 153 int iResult=0;
154 if (pConsumer) {
8ede8717 155 vector<AliHLTComponentDataType> dtlist;
0c0c9d99 156 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(dtlist);
6235cd38 157 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=fSegments.begin();
0c0c9d99 158 while (segment!=fSegments.end()) {
8ede8717 159 vector<AliHLTComponentDataType>::iterator type=dtlist.begin();
0c0c9d99 160 while (type!=dtlist.end()) {
9ce4bf4a 161 if ((*segment).fDataType==(*type) ||
162 (*type)==kAliHLTAnyDataType) {
0c0c9d99 163 tgtList.push_back(*segment);
164 iResult++;
165 break;
166 }
167 type++;
168 }
169 segment++;
170 }
171 } else {
172 iResult=-EINVAL;
173 }
174 return iResult;
175}
176
8ede8717 177int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponentBlockData* arrayBlockDesc, int iArraySize)
3f2a1b1c 178{
70ed7d01 179 // see header file for function documentation
3f2a1b1c 180 int iResult=0;
0c0c9d99 181 if (pConsumer && arrayBlockDesc) {
3f2a1b1c 182 if (fpBuffer) {
0c0c9d99 183 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fConsumers);
3f2a1b1c 184 if (pDesc) {
6235cd38 185 vector<AliHLTDataBuffer::AliHLTDataSegment> tgtList;
0c0c9d99 186 /* TODO: think about a good policy for this check
187 * is it enough that at least one segment is available, or have all to be available?
188 * or is it possible to have optional segments?
189 */
190 if ((iResult=FindMatchingDataSegments(pConsumer, tgtList))>0) {
191 int i =0;
6235cd38 192 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=tgtList.begin();
0c0c9d99 193 while (segment!=tgtList.end() && i<iArraySize) {
3f2a1b1c 194 // fill the block data descriptor
8ede8717 195 arrayBlockDesc[i].fStructSize=sizeof(AliHLTComponentBlockData);
3f2a1b1c 196 // the shared memory key is not used in AliRoot
8ede8717 197 arrayBlockDesc[i].fShmKey.fStructSize=sizeof(AliHLTComponentShmData);
198 arrayBlockDesc[i].fShmKey.fShmType=gkAliHLTComponentInvalidShmType;
199 arrayBlockDesc[i].fShmKey.fShmID=gkAliHLTComponentInvalidShmID;
0c0c9d99 200 arrayBlockDesc[i].fOffset=(*segment).fSegmentOffset;
1e6e67ec 201 arrayBlockDesc[i].fPtr=*fpBuffer;
0c0c9d99 202 arrayBlockDesc[i].fSize=(*segment).fSegmentSize;
203 arrayBlockDesc[i].fDataType=(*segment).fDataType;
204 arrayBlockDesc[i].fSpecification=(*segment).fSpecification;
205 pDesc->SetActiveDataSegment(arrayBlockDesc[i].fOffset, arrayBlockDesc[i].fSize);
1e6e67ec 206 HLTDebug("component %p (%s) subscribed to segment #%d offset %d size %d data type %s %#x",
207 pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), i, arrayBlockDesc[i].fOffset,
208 arrayBlockDesc[i].fSize, (AliHLTComponent::DataType2Text(arrayBlockDesc[i].fDataType)).c_str(),
209 arrayBlockDesc[i].fSpecification);
0c0c9d99 210 i++;
211 segment++;
212 }
213 // move this consumer to the active list
214 if (ChangeConsumerState(pDesc, fConsumers, fActiveConsumers)>=0) {
215 HLTDebug("component %p (%s) subscribed to data buffer %p", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), this);
3f2a1b1c 216 } else {
0c0c9d99 217 // TODO: cleanup the consumer descriptor correctly
8ede8717 218 memset(arrayBlockDesc, 0, iArraySize*sizeof(AliHLTComponentBlockData));
3f2a1b1c 219 HLTError("can not activate consumer %p for data buffer %p", pConsumer, this);
220 iResult=-EACCES;
221 }
222 } else {
0c0c9d99 223 HLTError("unresolved data segment(s) for component %p (%s)", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
3f2a1b1c 224 iResult=-EBADF;
225 }
226 } else {
0c0c9d99 227 HLTError("component %p is not a data consumer of data buffer %s", pConsumer, this);
3f2a1b1c 228 iResult=-ENOENT;
229 }
230 } else {
231 HLTError("data buffer %p is empty", this);
232 iResult=-ENODATA;
233 }
234 } else {
235 HLTError("invalid parameter");
236 iResult=-EINVAL;
237 }
238 return iResult;
239}
240
8ede8717 241int AliHLTDataBuffer::Release(AliHLTComponentBlockData* pBlockDesc, const AliHLTComponent* pConsumer)
3f2a1b1c 242{
70ed7d01 243 // see header file for function documentation
3f2a1b1c 244 int iResult=0;
245 if (pBlockDesc && pConsumer) {
0c0c9d99 246 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fActiveConsumers);
247 if (pDesc) {
248 if ((iResult=pDesc->CheckActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize))!=1) {
249 HLTWarning("data segment missmatch, component %p has not subscribed to a segment with offset %#x and size %d", pConsumer, pBlockDesc->fOffset, pBlockDesc->fSize);
250 // TODO: appropriate error handling, but so far optional
251 iResult=0;
252 } else {
253 pDesc->ReleaseActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize);
3f2a1b1c 254 pBlockDesc->fOffset=0;
255 pBlockDesc->fPtr=NULL;
256 pBlockDesc->fSize=0;
0c0c9d99 257 }
258 if (pDesc->GetNofActiveSegments()==0) {
3f2a1b1c 259 if ((iResult=ChangeConsumerState(pDesc, fActiveConsumers, fReleasedConsumers))>=0) {
260 if (GetNofActiveConsumers()==0) {
0c0c9d99 261 // this is the last consumer, reset the consumer list and release the raw buffer
3f2a1b1c 262 ResetDataBuffer();
3f2a1b1c 263 }
264 } else {
265 HLTError("can not deactivate consumer %p for data buffer %p", pConsumer, this);
266 iResult=-EACCES;
267 }
3f2a1b1c 268 }
0c0c9d99 269 } else {
270 HLTWarning("component %p has currently not subscribed to the data buffer %p", pConsumer, this);
271 iResult=-ENOENT;
272 }
3f2a1b1c 273 } else {
274 HLTError("inavalid parameter: pBlockDesc=%p pConsumer=%p", pBlockDesc, pConsumer);
275 iResult=-EINVAL;
276 }
277 return iResult;
278}
279
280AliHLTUInt8_t* AliHLTDataBuffer::GetTargetBuffer(int iMinSize)
281{
70ed7d01 282 // see header file for function documentation
3f2a1b1c 283 AliHLTUInt8_t* pTargetBuffer=NULL;
b6800be0 284 if (fpBuffer!=NULL) {
285 HLTWarning("data buffer not properly reset, possible memory leak\n");
286 }
3f2a1b1c 287 fpBuffer=CreateRawBuffer(iMinSize);
9ce4bf4a 288 if (fpBuffer) {
1e6e67ec 289 pTargetBuffer=*fpBuffer;
9ce4bf4a 290 } else {
291 HLTError("can not create raw buffer");
292 }
3f2a1b1c 293 return pTargetBuffer;
294}
295
8ede8717 296int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData* arrayBlockData, int iSize)
3f2a1b1c 297{
70ed7d01 298 // see header file for function documentation
3f2a1b1c 299 int iResult=0;
300 if (pTgt && arrayBlockData && iSize>=0) {
0c0c9d99 301 if (fpBuffer) {
1e6e67ec 302 if (*fpBuffer==pTgt) {
6235cd38 303 AliHLTDataBuffer::AliHLTDataSegment segment;
0c0c9d99 304 for (int i=0; i<iSize; i++) {
6434d28a 305 // the pointer can be either NULL, than only the offset is considered, or a valid
306 // pointer in the range of the buffer
307 // The operator '>' includes the size of the buffer
308 if (arrayBlockData[i].fPtr==NULL ||
309 ((*fpBuffer)<=arrayBlockData[i].fPtr && (*fpBuffer)>arrayBlockData[i].fPtr)) {
310 int ptrOffset=0;
311 if (arrayBlockData[i].fPtr!=NULL) {
312 ptrOffset=(*fpBuffer)-arrayBlockData[i].fPtr;
313 }
1e6e67ec 314 if (arrayBlockData[i].fOffset+ptrOffset+arrayBlockData[i].fSize<=fpBuffer->fSize) {
315 segment.fSegmentOffset=arrayBlockData[i].fOffset+ptrOffset;
316 segment.fSegmentSize=arrayBlockData[i].fSize;
317 segment.fDataType=arrayBlockData[i].fDataType;
318 segment.fSpecification=arrayBlockData[i].fSpecification;
319 fSegments.push_back(segment);
320 HLTDebug("set segment %s with size %d at offset %d", AliHLTComponent::DataType2Text(segment.fDataType).data(), segment.fSegmentSize, segment.fSegmentOffset);
321 } else {
322 HLTError("block data specification %#d (%s) exceeds size of data buffer", i, AliHLTComponent::DataType2Text(arrayBlockData[i].fDataType).data());
323 HLTError("block offset=%d, block size=%d, buffer size=%d", arrayBlockData[i].fOffset, arrayBlockData[i].fSize, fpBuffer->fSize);
324 iResult=-E2BIG;
325 }
0c0c9d99 326 } else {
1e6e67ec 327 HLTError("invalid pointer (%p) in block data specification (buffer %p size %d)", arrayBlockData[i].fPtr, fpBuffer->fPtr, fpBuffer->fSize);
328 iResult=-ERANGE;
0c0c9d99 329 }
330 }
3f2a1b1c 331 } else {
0c0c9d99 332 HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->fPtr, fpBuffer);
8451168b 333 iResult=-EINVAL;
3f2a1b1c 334 }
0c0c9d99 335 } else {
336 HLTFatal("internal data structur missmatch");
337 iResult=-EFAULT;
3f2a1b1c 338 }
339 } else {
0c0c9d99 340 HLTError("invalid parameter: pTgtBuffer=%p arrayBlockData=%p", pTgt, arrayBlockData);
3f2a1b1c 341 iResult=-EINVAL;
342 }
343 return iResult;
344}
345
346int AliHLTDataBuffer::IsEmpty()
347{
70ed7d01 348 // see header file for function documentation
3f2a1b1c 349 int iResult=fpBuffer==NULL || GetNofSegments()==0;
350 return iResult;
351}
352
353int AliHLTDataBuffer::GetNofSegments()
354{
70ed7d01 355 // see header file for function documentation
3f2a1b1c 356 int iResult=fSegments.size();
357 return iResult;
358}
359
360int AliHLTDataBuffer::GetNofConsumers()
361{
70ed7d01 362 // see header file for function documentation
3f2a1b1c 363 int iResult=fConsumers.size() + GetNofActiveConsumers() + fReleasedConsumers.size();
364 return iResult;
365}
366
367int AliHLTDataBuffer::GetNofActiveConsumers()
368{
70ed7d01 369 // see header file for function documentation
3f2a1b1c 370 int iResult=fActiveConsumers.size();
371 return iResult;
372}
373
6235cd38 374AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
3f2a1b1c 375{
70ed7d01 376 // see header file for function documentation
3f2a1b1c 377 AliHLTRawBuffer* pRawBuffer=NULL;
7a5ccd96 378 unsigned int reqSize=size+fgkSafetyPatternSize;
70ed7d01 379 vector<AliHLTRawBuffer*>::iterator buffer=fgFreeBuffers.begin();
380 while (buffer!=fgFreeBuffers.end() && pRawBuffer==NULL) {
8451168b 381 if ((*buffer)->fTotalSize>=reqSize && ((*buffer)->fTotalSize-reqSize)<fgMargin) {
3f2a1b1c 382 // assign this element
383 pRawBuffer=*buffer;
384 pRawBuffer->fSize=size;
70ed7d01 385 fgFreeBuffers.erase(buffer);
0c0c9d99 386 fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "raw buffer container %p provided for request of %d bytes (total %d available in buffer %p)", pRawBuffer, size, pRawBuffer->fTotalSize, pRawBuffer->fPtr);
70ed7d01 387 fgActiveBuffers.push_back(pRawBuffer);
0c0c9d99 388 break;
3f2a1b1c 389 }
0c0c9d99 390 buffer++;
3f2a1b1c 391 }
392 if (pRawBuffer==NULL) {
0c0c9d99 393 // no buffer found, create a new one
3f2a1b1c 394 pRawBuffer=new AliHLTRawBuffer;
395 if (pRawBuffer) {
1e6e67ec 396 pRawBuffer->fPtr=static_cast<AliHLTUInt8_t*>(malloc(reqSize));
3f2a1b1c 397 if (pRawBuffer->fPtr) {
398 pRawBuffer->fSize=size;
8451168b 399 pRawBuffer->fTotalSize=reqSize;
70ed7d01 400 fgActiveBuffers.push_back(pRawBuffer);
0c0c9d99 401 fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->fPtr, pRawBuffer->fTotalSize, pRawBuffer);
3f2a1b1c 402 } else {
403 delete pRawBuffer;
404 pRawBuffer=NULL;
0c0c9d99 405 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
3f2a1b1c 406 }
407 } else {
0c0c9d99 408 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
3f2a1b1c 409 }
410 }
73ede1d3 411 if (pRawBuffer!=NULL && fgkSafetyPatternSize>0) {
412 //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "writing safety pattern to %p offset %d", pRawBuffer->fPtr, pRawBuffer->fSize);
413 memcpy(((char*)pRawBuffer->fPtr)+pRawBuffer->fSize, fgkSafetyPattern, fgkSafetyPatternSize);
414 }
3f2a1b1c 415 return pRawBuffer;
416}
417
418int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
419{
70ed7d01 420 // see header file for function documentation
3f2a1b1c 421 int iResult=0;
422 if (pBuffer) {
70ed7d01 423 vector<AliHLTRawBuffer*>::iterator buffer=fgActiveBuffers.begin();
424 while (buffer!=fgActiveBuffers.end() && (*buffer)!=pBuffer) {
3f2a1b1c 425 buffer++;
426 }
70ed7d01 427 if (buffer!=fgActiveBuffers.end()) {
8451168b 428 if (fgkSafetyPatternSize>0) {
73ede1d3 429 //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "comparing safety pattern at %p offset %d", (*buffer)->fPtr, (*buffer)->fSize);
8451168b 430 if (memcmp(((char*)(*buffer)->fPtr)+(*buffer)->fSize, fgkSafetyPattern, fgkSafetyPatternSize)!=0) {
431 fgLogging.Logging(kHLTLogFatal, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "component has written beyond end of data buffer %p size %d", (*buffer)->fPtr, (*buffer)->fSize);
432 }
433 }
3f2a1b1c 434 (*buffer)->fSize=0;
70ed7d01 435 fgFreeBuffers.push_back(*buffer);
436 fgActiveBuffers.erase(buffer);
3f2a1b1c 437 } else {
0c0c9d99 438 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
3f2a1b1c 439 iResult=-ENOENT;
440 }
441 } else {
0c0c9d99 442 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "invalid parameter");
3f2a1b1c 443 iResult=-EINVAL;
444 }
445 return iResult;
446}
447
448
449int AliHLTDataBuffer::DeleteRawBuffers()
450{
70ed7d01 451 // see header file for function documentation
3f2a1b1c 452 int iResult=0;
70ed7d01 453 vector<AliHLTRawBuffer*>::iterator buffer=fgFreeBuffers.begin();
454 while (buffer!=fgFreeBuffers.end()) {
3f2a1b1c 455 free((*buffer)->fPtr);
456 delete *buffer;
70ed7d01 457 fgFreeBuffers.erase(buffer);
458 buffer=fgFreeBuffers.begin();
3f2a1b1c 459 }
70ed7d01 460 buffer=fgActiveBuffers.begin();
461 while (buffer!=fgActiveBuffers.end()) {
0c0c9d99 462 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "request to delete active raw buffer container (raw buffer %p, size %d)", (*buffer)->fPtr, (*buffer)->fTotalSize);
3f2a1b1c 463 free((*buffer)->fPtr);
464 delete *buffer;
70ed7d01 465 fgActiveBuffers.erase(buffer);
466 buffer=fgActiveBuffers.begin();
3f2a1b1c 467 }
468 return iResult;
469}
470
70ed7d01 471AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, vector<AliHLTConsumerDescriptor*> &list) const
3f2a1b1c 472{
70ed7d01 473 // see header file for function documentation
3f2a1b1c 474 AliHLTConsumerDescriptor* pDesc=NULL;
475 vector<AliHLTConsumerDescriptor*>::iterator desc=list.begin();
476 while (desc!=list.end() && pDesc==NULL) {
0c0c9d99 477 if ((pConsumer==NULL || (*desc)->GetComponent()==pConsumer)) {
3f2a1b1c 478 pDesc=*desc;
479 }
0c0c9d99 480 desc++;
3f2a1b1c 481 }
482 return pDesc;
483}
484
0c0c9d99 485int AliHLTDataBuffer::ResetDataBuffer()
486{
70ed7d01 487 // see header file for function documentation
3f2a1b1c 488 int iResult=0;
0c0c9d99 489 AliHLTRawBuffer* pBuffer=fpBuffer;
3f2a1b1c 490 fpBuffer=NULL;
9ce4bf4a 491
492 // cleanup consumer states
3f2a1b1c 493 vector<AliHLTConsumerDescriptor*>::iterator desc=fReleasedConsumers.begin();
494 while (desc!=fReleasedConsumers.end()) {
495 AliHLTConsumerDescriptor* pDesc=*desc;
496 fReleasedConsumers.erase(desc);
497 desc=fReleasedConsumers.begin();
498 fConsumers.push_back(pDesc);
499 }
500 desc=fActiveConsumers.begin();
501 while (desc!=fActiveConsumers.end()) {
502 AliHLTConsumerDescriptor* pDesc=*desc;
503 HLTWarning("consumer %p was not released", pDesc);
504 fActiveConsumers.erase(desc);
505 desc=fActiveConsumers.begin();
506 fConsumers.push_back(pDesc);
507 }
9ce4bf4a 508
509 // cleanup segments
6235cd38 510 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=fSegments.begin();
9ce4bf4a 511 while (segment!=fSegments.end()) {
512 fSegments.erase(segment);
513 segment=fSegments.begin();
514 }
515
516 // cleanup raw buffer
517 if (pBuffer) {
518 ReleaseRawBuffer(pBuffer);
519 }
3f2a1b1c 520 return iResult;
521}
522
9ce4bf4a 523int AliHLTDataBuffer::Reset()
524{
70ed7d01 525 // see header file for function documentation
9ce4bf4a 526 return ResetDataBuffer();
527}
528
0c0c9d99 529// this is the version which works on lists of components instead of consumer descriptors
530// int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, vector<AliHLTComponent*> &srcList, vector<AliHLTComponent*> &tgtList)
531// {
532// int iResult=0;
533// if (pDesc) {
534// vector<AliHLTComponent*>::iterator desc=srcList.begin();
535// while (desc!=srcList.end()) {
536// if ((*desc)==pConsumer) {
537// srcList.erase(desc);
538// tgtList.push_back(pConsumer);
539// break;
540// }
541// desc++;
542// }
543// if (desc==srcList.end()) {
544// HLTError("can not find consumer component %p in list", pConsumer);
545// iResult=-ENOENT;
546// }
547// } else {
548// HLTError("invalid parameter");
549// iResult=-EINVAL;
550// }
551// return iResult;
552// }
553
3f2a1b1c 554int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, vector<AliHLTConsumerDescriptor*> &srcList, vector<AliHLTConsumerDescriptor*> &tgtList)
555{
70ed7d01 556 // see header file for function documentation
9ce4bf4a 557 int iResult=-ENOENT;
3f2a1b1c 558 if (pDesc) {
559 vector<AliHLTConsumerDescriptor*>::iterator desc=srcList.begin();
560 while (desc!=srcList.end()) {
561 if ((*desc)==pDesc) {
562 srcList.erase(desc);
563 tgtList.push_back(pDesc);
9ce4bf4a 564 iResult=0;
3f2a1b1c 565 break;
566 }
0c0c9d99 567 desc++;
3f2a1b1c 568 }
9ce4bf4a 569 if (iResult<0) {
3f2a1b1c 570 HLTError("can not find consumer descriptor %p in list", pDesc);
3f2a1b1c 571 }
572 } else {
573 HLTError("invalid parameter");
574 iResult=-EINVAL;
575 }
576 return iResult;
577}
578
70ed7d01 579int AliHLTDataBuffer::CleanupConsumerList()
580{
581 // see header file for function documentation
3f2a1b1c 582 int iResult=0;
583 ResetDataBuffer();
584 vector<AliHLTConsumerDescriptor*>::iterator desc=fConsumers.begin();
585 while (desc!=fConsumers.end()) {
586 delete *desc;
587 fConsumers.erase(desc);
588 desc=fConsumers.begin();
589 }
590 return iResult;
591}
9ce4bf4a 592
70ed7d01 593int AliHLTDataBuffer::FindConsumer(AliHLTComponent* pConsumer, int bAllLists)
594{
595 // see header file for function documentation
9ce4bf4a 596 vector<AliHLTConsumerDescriptor*>::iterator desc=fConsumers.begin();
597 while (desc!=fConsumers.end()) {
598 if ((*desc)->GetComponent()==pConsumer)
599 return 1;
600 desc++;
601 }
602 if (bAllLists==0) return 0;
603
604 desc=fActiveConsumers.begin();
605 while (desc!=fActiveConsumers.end()) {
606 if ((*desc)->GetComponent()==pConsumer)
607 return 1;
608 desc++;
609 }
610 desc=fReleasedConsumers.begin();
611 while (desc!=fReleasedConsumers.end()) {
612 if ((*desc)->GetComponent()==pConsumer)
613 return 1;
614 desc++;
615 }
616 return 0;
617}
1e6e67ec 618
619int AliHLTDataBuffer::AliHLTRawBuffer::operator==(void* ptr)
620{
621 return fPtr == static_cast<AliHLTUInt8_t*>(ptr);
622}
623
624int AliHLTDataBuffer::AliHLTRawBuffer::operator<=(void* ptr)
625{
626 int iResult=fPtr <= static_cast<AliHLTUInt8_t*>(ptr);
627 //printf("%p: %p <= %p (%d)\n", this, fPtr, ptr, iResult);
628 return iResult;
629}
630
631int AliHLTDataBuffer::AliHLTRawBuffer::operator>(void* ptr)
632{
633 int iResult=fPtr+fSize > static_cast<AliHLTUInt8_t*>(ptr);
634 //printf("%p: %p + %d > %p (%d)\n", this, fPtr, fSize, ptr, iResult);
635 return iResult;
636}
637
638int AliHLTDataBuffer::AliHLTRawBuffer::operator-(void* ptr)
639{
640 return static_cast<int>(static_cast<AliHLTUInt8_t*>(ptr)-fPtr);
641}