]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTDataBuffer.cxx
- The default value of useFastDecoder in ctor changed to kFALSE,
[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"
b46ca65e 38#include "AliHLTTask.h"
66043029 39#include <cerrno>
d6cbe999 40#include <cassert>
66043029 41//#include <string>
42//#include "AliHLTSystem.h"
3f2a1b1c 43
2be3f004 44typedef vector<AliHLTDataBuffer::AliHLTDataSegment> AliHLTDataSegmentList;
45typedef vector<AliHLTDataBuffer::AliHLTRawBuffer*> AliHLTRawBufferPList;
46
b22e91eb 47/** ROOT macro for the implementation of ROOT specific class methods */
3f2a1b1c 48ClassImp(AliHLTDataBuffer)
49
3f2a1b1c 50AliHLTDataBuffer::AliHLTDataBuffer()
85869391 51 :
52 fSegments(),
53 fConsumers(),
54 fActiveConsumers(),
55 fReleasedConsumers(),
56 fpBuffer(NULL),
b46ca65e 57 fFlags(0),
58 fForwardedSegmentSources(),
59 fForwardedSegments()
3f2a1b1c 60{
70ed7d01 61 // see header file for class documentation
62 // or
63 // refer to README to build package
64 // or
65 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
85869391 66 fSegments.empty();
67 fConsumers.empty();
68 fActiveConsumers.empty();
69 fReleasedConsumers.empty();
70ed7d01 70 fgNofInstances++;
3f2a1b1c 71}
72
70ed7d01 73int AliHLTDataBuffer::fgNofInstances=0;
2be3f004 74AliHLTRawBufferPList AliHLTDataBuffer::fgFreeBuffers;
75AliHLTRawBufferPList AliHLTDataBuffer::fgActiveBuffers;
70ed7d01 76AliHLTUInt32_t AliHLTDataBuffer::fgMargin=1024;
b22e91eb 77AliHLTLogging AliHLTDataBuffer::fgLogging;
8451168b 78const Int_t AliHLTDataBuffer::fgkSafetyPatternSize=16;
79const char AliHLTDataBuffer::fgkSafetyPattern[]={0x28, 0x63, 0x29, 0x4d, 0x52, 0x49, 0x43, 0x48, 0x54, 0x45, 0x52, 0x20, 0x32, 0x30, 0x30, 0x37};
b22e91eb 80
3f2a1b1c 81AliHLTDataBuffer::~AliHLTDataBuffer()
82{
70ed7d01 83 // see header file for function documentation
84 if (--fgNofInstances<=0) {
3f2a1b1c 85 DeleteRawBuffers();
86 }
87 CleanupConsumerList();
88}
89
0c0c9d99 90int AliHLTDataBuffer::SetConsumer(AliHLTComponent* pConsumer)
3f2a1b1c 91{
70ed7d01 92 // see header file for function documentation
3f2a1b1c 93 int iResult=0;
94 if (pConsumer) {
9ce4bf4a 95 if (FindConsumer(pConsumer)) {
96 HLTWarning("consumer %s (%p) already set to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
97 }
0c0c9d99 98 AliHLTConsumerDescriptor* pDesc=new AliHLTConsumerDescriptor(pConsumer);
3f2a1b1c 99 if (pDesc) {
100 fConsumers.push_back(pDesc);
9ce4bf4a 101 HLTDebug("set consumer %s (%p) to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
3f2a1b1c 102 } else {
103 HLTError("memory allocation failed");
104 iResult=-ENOMEM;
105 }
106 } else {
9ce4bf4a 107 HLTError("invalid parameter: consumer component (nil)");
3f2a1b1c 108 iResult=-EINVAL;
109 }
110 return iResult;
111}
112
2be3f004 113int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList)
0c0c9d99 114{
70ed7d01 115 // see header file for function documentation
0c0c9d99 116 int iResult=0;
117 if (pConsumer) {
2be3f004 118 AliHLTDataSegmentList segments;
0c0c9d99 119 if ((iResult=FindMatchingDataSegments(pConsumer, segments))>=0) {
120 if (tgtList) {
2be3f004 121 AliHLTDataSegmentList::iterator segment=segments.begin();
0c0c9d99 122 while (segment!=segments.end()) {
123 tgtList->push_back((*segment).fDataType);
124 segment++;
125 }
126 }
127 iResult=segments.size();
128 }
129 } else {
130 iResult=-EINVAL;
131 }
132 return iResult;
133}
134
6235cd38 135int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataBuffer::AliHLTDataSegment>& tgtList)
0c0c9d99 136{
70ed7d01 137 // see header file for function documentation
0c0c9d99 138 int iResult=0;
9b7fe12d 139
140 // Matthias 26.09.2007 relax the restriction to matching data blocks
141 // all blocks are passed to the consumer, which is the policy also in
142 // PubSub
143 tgtList.assign(fSegments.begin(), fSegments.end());
b46ca65e 144
145 // add all forwarded blocks
146 tgtList.insert(tgtList.begin(), fForwardedSegments.begin(), fForwardedSegments.end());
9b7fe12d 147 iResult=tgtList.size();
148 return iResult;
149
0c0c9d99 150 if (pConsumer) {
2be3f004 151 AliHLTComponentDataTypeList dtlist;
0c0c9d99 152 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(dtlist);
2be3f004 153 AliHLTDataSegmentList::iterator segment=fSegments.begin();
0c0c9d99 154 while (segment!=fSegments.end()) {
2be3f004 155 AliHLTComponentDataTypeList::iterator type=dtlist.begin();
0c0c9d99 156 while (type!=dtlist.end()) {
9ce4bf4a 157 if ((*segment).fDataType==(*type) ||
158 (*type)==kAliHLTAnyDataType) {
0c0c9d99 159 tgtList.push_back(*segment);
160 iResult++;
161 break;
162 }
163 type++;
164 }
165 segment++;
166 }
167 } else {
168 iResult=-EINVAL;
169 }
170 return iResult;
171}
172
457ec821 173int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponentBlockDataList& blockDescList)
3f2a1b1c 174{
70ed7d01 175 // see header file for function documentation
3f2a1b1c 176 int iResult=0;
457ec821 177 if (pConsumer) {
b46ca65e 178 if (1/*fpBuffer*/) {
0c0c9d99 179 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fConsumers);
3f2a1b1c 180 if (pDesc) {
2be3f004 181 AliHLTDataSegmentList tgtList;
9b7fe12d 182 // Matthias 26.07.2007 AliHLTSystem should behave the same way as PubSub
183 // so it does not matter if there are matching data types or not, unless
184 // we implement such a check in PubSub
185 if ((iResult=FindMatchingDataSegments(pConsumer, tgtList))>=0) {
2be3f004 186 AliHLTDataSegmentList::iterator segment=tgtList.begin();
457ec821 187 while (segment!=tgtList.end()) {
3f2a1b1c 188 // fill the block data descriptor
457ec821 189 AliHLTComponentBlockData bd;
190 AliHLTComponent::FillBlockData(bd);
3294f81a 191 // This models the behavior of PubSub.
192 // For incoming data blocks, fOffset must be ignored by the
193 // processing component. It is set for bookkeeping in the framework.
194 // fPtr always points to the beginning of the data.
457ec821 195 bd.fOffset=0;
b46ca65e 196 AliHLTUInt8_t* pTgt=*segment;
457ec821 197 bd.fPtr=reinterpret_cast<void*>(pTgt);
198 bd.fSize=(*segment).fSegmentSize;
199 bd.fDataType=(*segment).fDataType;
200 bd.fSpecification=(*segment).fSpecification;
201 blockDescList.push_back(bd);
b46ca65e 202 pDesc->SetActiveDataSegment(*segment);
457ec821 203 HLTDebug("component %p (%s) subscribed to segment offset %d size %d data type %s %#x",
204 pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), bd.fOffset,
205 bd.fSize, (AliHLTComponent::DataType2Text(bd.fDataType)).c_str(),
206 bd.fSpecification);
0c0c9d99 207 segment++;
208 }
209 // move this consumer to the active list
457ec821 210 if (tgtList.size()==0) {
b46ca65e 211 ChangeConsumerState(pDesc, fConsumers, fReleasedConsumers);
212 HLTDebug("no input data for component %p (%s) available", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
213 } else if (ChangeConsumerState(pDesc, fConsumers, fActiveConsumers)>=0) {
0c0c9d99 214 HLTDebug("component %p (%s) subscribed to data buffer %p", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), this);
3f2a1b1c 215 } else {
0c0c9d99 216 // TODO: cleanup the consumer descriptor correctly
457ec821 217 segment=tgtList.begin();
218 while (segment!=tgtList.end()) {
219 blockDescList.pop_back();
220 segment++;
221 }
3f2a1b1c 222 HLTError("can not activate consumer %p for data buffer %p", pConsumer, this);
223 iResult=-EACCES;
224 }
225 } else {
0c0c9d99 226 HLTError("unresolved data segment(s) for component %p (%s)", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
3f2a1b1c 227 iResult=-EBADF;
228 }
229 } else {
0c0c9d99 230 HLTError("component %p is not a data consumer of data buffer %s", pConsumer, this);
3f2a1b1c 231 iResult=-ENOENT;
232 }
233 } else {
9b7fe12d 234 // Matthias 26.07.2007 until now, data had to be present for successful subscription
235 // in order to be consistent with the PubSub framework, this restiction has been
236 // removed
237 //HLTError("data buffer %p is empty", this);
238 //iResult=-ENODATA;
3f2a1b1c 239 }
240 } else {
241 HLTError("invalid parameter");
242 iResult=-EINVAL;
243 }
244 return iResult;
245}
246
b46ca65e 247int AliHLTDataBuffer::Release(AliHLTComponentBlockData* pBlockDesc,
248 const AliHLTComponent* pConsumer,
249 const AliHLTTask* pOwnerTask)
3f2a1b1c 250{
70ed7d01 251 // see header file for function documentation
3f2a1b1c 252 int iResult=0;
253 if (pBlockDesc && pConsumer) {
0c0c9d99 254 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fActiveConsumers);
255 if (pDesc) {
b46ca65e 256 if ((iResult=pDesc->CheckActiveDataSegment(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize)))!=1) {
0c0c9d99 257 HLTWarning("data segment missmatch, component %p has not subscribed to a segment with offset %#x and size %d", pConsumer, pBlockDesc->fOffset, pBlockDesc->fSize);
258 // TODO: appropriate error handling, but so far optional
259 iResult=0;
260 } else {
b46ca65e 261 pDesc->ReleaseActiveDataSegment(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize));
262 }
263 if (GetNofPendingConsumers()==0 && fForwardedSegments.size()>0) {
264 // last consumer, release forwarded segments
265 assert(fForwardedSegments.size()==fForwardedSegmentSources.size());
266 AliHLTDataSegmentList::iterator segment=fForwardedSegments.begin();
267 AliHLTTaskPList::iterator src=fForwardedSegmentSources.begin();
268 //HLTDebug("%p checking forwarded segments", this);
269 for (; segment!=fForwardedSegments.end(); segment++, src++) {
270 //HLTDebug("segment ptr=%p offset=%d size=%d\n"
271 // "block ptr=%p offset=%d size=%d", (*segment).fPtr, (*segment).fSegmentOffset, (*segment).fSegmentSize, pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize);
272 if ((*segment)==AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize)) {
273 //HLTDebug("release segment of task %p", *src);
274 assert((*src)!=NULL);
275 if ((*src)!=NULL) {
276 if ((*src)->Release(pBlockDesc, pOwnerTask)>=0) {
277 HLTDebug("task %s (%p) released forwarded segment %p size %d of task %s (%p)",
278 pOwnerTask->GetName(), pOwnerTask, (*segment).GetPtr(), (*segment).GetSize(),
279 (*src)->GetName(), *src);
280 } else {
281 HLTError("task %s (%p) failed releasing forwarded segment %p size %d of task %s (%p)",
282 pOwnerTask->GetName(), pOwnerTask, (*segment).GetPtr(), (*segment).GetSize(),
283 (*src)->GetName(), *src);
284 }
285 }
286 fForwardedSegments.erase(segment);
287 fForwardedSegmentSources.erase(src);
288 break;
289 }
290 }
0c0c9d99 291 }
b46ca65e 292 pBlockDesc->fOffset=0;
293 pBlockDesc->fPtr=NULL;
294 pBlockDesc->fSize=0;
0c0c9d99 295 if (pDesc->GetNofActiveSegments()==0) {
3f2a1b1c 296 if ((iResult=ChangeConsumerState(pDesc, fActiveConsumers, fReleasedConsumers))>=0) {
b426991e 297 if (GetNofActiveConsumers()==0 && GetNofPendingConsumers()==0) {
0c0c9d99 298 // this is the last consumer, reset the consumer list and release the raw buffer
3f2a1b1c 299 ResetDataBuffer();
3f2a1b1c 300 }
301 } else {
302 HLTError("can not deactivate consumer %p for data buffer %p", pConsumer, this);
303 iResult=-EACCES;
304 }
3f2a1b1c 305 }
0c0c9d99 306 } else {
307 HLTWarning("component %p has currently not subscribed to the data buffer %p", pConsumer, this);
308 iResult=-ENOENT;
309 }
3f2a1b1c 310 } else {
311 HLTError("inavalid parameter: pBlockDesc=%p pConsumer=%p", pBlockDesc, pConsumer);
312 iResult=-EINVAL;
313 }
314 return iResult;
315}
316
b46ca65e 317int AliHLTDataBuffer::Forward(AliHLTTask* pSrcTask, AliHLTComponentBlockData* pBlockDesc)
318{
319 // see header file for function documentation
320 if (pSrcTask==NULL || pBlockDesc==NULL) return -EINVAL;
321 assert(fForwardedSegments.size()==fForwardedSegmentSources.size());
322 if (fForwardedSegments.size()!=fForwardedSegmentSources.size()) return -EFAULT;
323 fForwardedSegmentSources.push_back(pSrcTask);
457ec821 324 fForwardedSegments.push_back(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize, pBlockDesc->fDataType, pBlockDesc->fSpecification));
b46ca65e 325 return 0;
326}
327
3f2a1b1c 328AliHLTUInt8_t* AliHLTDataBuffer::GetTargetBuffer(int iMinSize)
329{
70ed7d01 330 // see header file for function documentation
3f2a1b1c 331 AliHLTUInt8_t* pTargetBuffer=NULL;
b6800be0 332 if (fpBuffer!=NULL) {
333 HLTWarning("data buffer not properly reset, possible memory leak\n");
334 }
3f2a1b1c 335 fpBuffer=CreateRawBuffer(iMinSize);
9ce4bf4a 336 if (fpBuffer) {
1e6e67ec 337 pTargetBuffer=*fpBuffer;
9ce4bf4a 338 } else {
339 HLTError("can not create raw buffer");
340 }
3f2a1b1c 341 return pTargetBuffer;
342}
343
8ede8717 344int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData* arrayBlockData, int iSize)
3f2a1b1c 345{
70ed7d01 346 // see header file for function documentation
3f2a1b1c 347 int iResult=0;
348 if (pTgt && arrayBlockData && iSize>=0) {
0c0c9d99 349 if (fpBuffer) {
1e6e67ec 350 if (*fpBuffer==pTgt) {
6235cd38 351 AliHLTDataBuffer::AliHLTDataSegment segment;
0c0c9d99 352 for (int i=0; i<iSize; i++) {
3294f81a 353 // This function has to model the behavior of PubSub
354 // For output blocks only the fOffset value is used, this must be the offset
355 // relative to the output pointer. fPtr must be either NULL or the output
b46ca65e 356 // pointer. In either case it is 'ignored' and set to the beginning of the
357 // data buffer
6434d28a 358 if (arrayBlockData[i].fPtr==NULL ||
3294f81a 359 arrayBlockData[i].fPtr==*fpBuffer) {
b46ca65e 360 arrayBlockData[i].fPtr=*fpBuffer;
c0a2bfc2 361 if (arrayBlockData[i].fOffset+arrayBlockData[i].fSize<=fpBuffer->GetUsedSize()) {
3294f81a 362 segment.fSegmentOffset=arrayBlockData[i].fOffset;
b46ca65e 363 segment.fPtr=(AliHLTUInt8_t*)arrayBlockData[i].fPtr;
1e6e67ec 364 segment.fSegmentSize=arrayBlockData[i].fSize;
365 segment.fDataType=arrayBlockData[i].fDataType;
366 segment.fSpecification=arrayBlockData[i].fSpecification;
367 fSegments.push_back(segment);
368 HLTDebug("set segment %s with size %d at offset %d", AliHLTComponent::DataType2Text(segment.fDataType).data(), segment.fSegmentSize, segment.fSegmentOffset);
369 } else {
370 HLTError("block data specification %#d (%s) exceeds size of data buffer", i, AliHLTComponent::DataType2Text(arrayBlockData[i].fDataType).data());
c0a2bfc2 371 HLTError("block offset=%d, block size=%d, buffer size=%d", arrayBlockData[i].fOffset, arrayBlockData[i].fSize, fpBuffer->GetUsedSize());
1e6e67ec 372 iResult=-E2BIG;
373 }
0c0c9d99 374 } else {
3294f81a 375 HLTError("invalid pointer (%p) in block data specification (buffer %p size %d)."
376 "please note: for output blocks only the fOffset value is valid and must "
c0a2bfc2 377 "be relative to the output buffer", arrayBlockData[i].fPtr, fpBuffer->GetPointer(), fpBuffer->GetUsedSize());
1e6e67ec 378 iResult=-ERANGE;
0c0c9d99 379 }
380 }
3f2a1b1c 381 } else {
c0a2bfc2 382 HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->GetPointer(), fpBuffer);
8451168b 383 iResult=-EINVAL;
3f2a1b1c 384 }
0c0c9d99 385 } else {
386 HLTFatal("internal data structur missmatch");
387 iResult=-EFAULT;
3f2a1b1c 388 }
389 } else {
0c0c9d99 390 HLTError("invalid parameter: pTgtBuffer=%p arrayBlockData=%p", pTgt, arrayBlockData);
3f2a1b1c 391 iResult=-EINVAL;
392 }
393 return iResult;
394}
395
396int AliHLTDataBuffer::IsEmpty()
397{
70ed7d01 398 // see header file for function documentation
3f2a1b1c 399 int iResult=fpBuffer==NULL || GetNofSegments()==0;
400 return iResult;
401}
402
403int AliHLTDataBuffer::GetNofSegments()
404{
70ed7d01 405 // see header file for function documentation
3f2a1b1c 406 int iResult=fSegments.size();
407 return iResult;
408}
409
410int AliHLTDataBuffer::GetNofConsumers()
411{
70ed7d01 412 // see header file for function documentation
3f2a1b1c 413 int iResult=fConsumers.size() + GetNofActiveConsumers() + fReleasedConsumers.size();
414 return iResult;
415}
416
b426991e 417int AliHLTDataBuffer::GetNofPendingConsumers()
418{
419 // see header file for function documentation
420 int iResult=fConsumers.size();
421 return iResult;
422}
423
3f2a1b1c 424int AliHLTDataBuffer::GetNofActiveConsumers()
425{
70ed7d01 426 // see header file for function documentation
3f2a1b1c 427 int iResult=fActiveConsumers.size();
428 return iResult;
429}
430
6235cd38 431AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
3f2a1b1c 432{
70ed7d01 433 // see header file for function documentation
3f2a1b1c 434 AliHLTRawBuffer* pRawBuffer=NULL;
7a5ccd96 435 unsigned int reqSize=size+fgkSafetyPatternSize;
2be3f004 436 AliHLTRawBufferPList::iterator buffer=fgFreeBuffers.begin();
70ed7d01 437 while (buffer!=fgFreeBuffers.end() && pRawBuffer==NULL) {
d6cbe999 438 if ((*buffer)->CheckSize(reqSize)) {
3f2a1b1c 439 // assign this element
440 pRawBuffer=*buffer;
d6cbe999 441 pRawBuffer->UseBuffer(size);
70ed7d01 442 fgFreeBuffers.erase(buffer);
c0a2bfc2 443 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->GetTotalSize(), pRawBuffer->GetPointer());
70ed7d01 444 fgActiveBuffers.push_back(pRawBuffer);
0c0c9d99 445 break;
3f2a1b1c 446 }
0c0c9d99 447 buffer++;
3f2a1b1c 448 }
449 if (pRawBuffer==NULL) {
0c0c9d99 450 // no buffer found, create a new one
d6cbe999 451 pRawBuffer=new AliHLTRawBuffer(reqSize);
3f2a1b1c 452 if (pRawBuffer) {
c0a2bfc2 453 if (pRawBuffer->GetPointer()) {
d6cbe999 454 pRawBuffer->UseBuffer(size);
70ed7d01 455 fgActiveBuffers.push_back(pRawBuffer);
c0a2bfc2 456 fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->GetPointer(), pRawBuffer->GetTotalSize(), pRawBuffer);
3f2a1b1c 457 } else {
458 delete pRawBuffer;
459 pRawBuffer=NULL;
0c0c9d99 460 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
3f2a1b1c 461 }
462 } else {
0c0c9d99 463 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
3f2a1b1c 464 }
465 }
73ede1d3 466 if (pRawBuffer!=NULL && fgkSafetyPatternSize>0) {
c0a2bfc2 467 //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "writing safety pattern to %p offset %d", (*buffer)->GetPointer(), (*buffer)->GetUsedSize());
49b01d04 468 pRawBuffer->WritePattern(fgkSafetyPattern, fgkSafetyPatternSize);
73ede1d3 469 }
3f2a1b1c 470 return pRawBuffer;
471}
472
473int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
474{
70ed7d01 475 // see header file for function documentation
3f2a1b1c 476 int iResult=0;
477 if (pBuffer) {
2be3f004 478 AliHLTRawBufferPList::iterator buffer=fgActiveBuffers.begin();
70ed7d01 479 while (buffer!=fgActiveBuffers.end() && (*buffer)!=pBuffer) {
3f2a1b1c 480 buffer++;
481 }
70ed7d01 482 if (buffer!=fgActiveBuffers.end()) {
8451168b 483 if (fgkSafetyPatternSize>0) {
c0a2bfc2 484 //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "comparing safety pattern at %p offset %d", (*buffer)->GetPointer(), reinterpret_cast<AliHLTUInt32_t>(*buffer));
d6cbe999 485 if ((*buffer)->CheckPattern(fgkSafetyPattern, fgkSafetyPatternSize)) {
c0a2bfc2 486 fgLogging.Logging(kHLTLogFatal, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "component has written beyond end of data buffer %p size %d", (*buffer)->GetPointer(), (*buffer)->GetUsedSize());
8451168b 487 }
488 }
d6cbe999 489 (*buffer)->Reset();
70ed7d01 490 fgFreeBuffers.push_back(*buffer);
491 fgActiveBuffers.erase(buffer);
3f2a1b1c 492 } else {
0c0c9d99 493 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
3f2a1b1c 494 iResult=-ENOENT;
495 }
496 } else {
0c0c9d99 497 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "invalid parameter");
3f2a1b1c 498 iResult=-EINVAL;
499 }
500 return iResult;
501}
502
503
504int AliHLTDataBuffer::DeleteRawBuffers()
505{
70ed7d01 506 // see header file for function documentation
3f2a1b1c 507 int iResult=0;
dba03d72 508// int iTotalSize=0;
509// int iCount=fgFreeBuffers.size()+fgActiveBuffers.size();
510 AliHLTRawBufferPList::iterator buffer;;
511 while ((buffer=fgFreeBuffers.begin())!=fgFreeBuffers.end()) {
512// iTotalSize+=(*buffer)->GetTotalSize();
3f2a1b1c 513 delete *buffer;
70ed7d01 514 fgFreeBuffers.erase(buffer);
3f2a1b1c 515 }
dba03d72 516 while ((buffer=fgActiveBuffers.begin())!=fgActiveBuffers.end()) {
517// iTotalSize+=(*buffer)->GetTotalSize();
c0a2bfc2 518 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "request to delete active raw buffer container (raw buffer %p, size %d)", (*buffer)->GetPointer(), (*buffer)->GetTotalSize());
3f2a1b1c 519 delete *buffer;
70ed7d01 520 fgActiveBuffers.erase(buffer);
3f2a1b1c 521 }
dba03d72 522// fgLogging.Logging(kHLTLogInfo, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "Total memory allocation: %d byte in %d buffers", iTotalSize, iCount);
3f2a1b1c 523 return iResult;
524}
525
2be3f004 526AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, AliHLTConsumerDescriptorPList &list) const
3f2a1b1c 527{
70ed7d01 528 // see header file for function documentation
3f2a1b1c 529 AliHLTConsumerDescriptor* pDesc=NULL;
2be3f004 530 AliHLTConsumerDescriptorPList::iterator desc=list.begin();
3f2a1b1c 531 while (desc!=list.end() && pDesc==NULL) {
0c0c9d99 532 if ((pConsumer==NULL || (*desc)->GetComponent()==pConsumer)) {
3f2a1b1c 533 pDesc=*desc;
534 }
0c0c9d99 535 desc++;
3f2a1b1c 536 }
537 return pDesc;
538}
539
0c0c9d99 540int AliHLTDataBuffer::ResetDataBuffer()
541{
70ed7d01 542 // see header file for function documentation
3f2a1b1c 543 int iResult=0;
0c0c9d99 544 AliHLTRawBuffer* pBuffer=fpBuffer;
3f2a1b1c 545 fpBuffer=NULL;
9ce4bf4a 546
b46ca65e 547 // cleanup forwarded segment lists
548 assert(fForwardedSegments.size()==0);
549 fForwardedSegments.clear();
550 fForwardedSegmentSources.clear();
551
9ce4bf4a 552 // cleanup consumer states
2be3f004 553 AliHLTConsumerDescriptorPList::iterator desc;
b426991e 554// if (GetNofPendingConsumers()>0) {
555// desc=fConsumers.begin();
556// while (desc!=fConsumers.end()) {
557// AliHLTComponent* pComp=(*desc)->GetComponent();
558// HLTError("internal error: consumer %p (%s %p) did not get data from data buffer %p", *desc, pComp?pComp->GetComponentID():"", pComp, this);
559// desc++;
560// }
561// }
562 desc=fReleasedConsumers.begin();
3f2a1b1c 563 while (desc!=fReleasedConsumers.end()) {
564 AliHLTConsumerDescriptor* pDesc=*desc;
565 fReleasedConsumers.erase(desc);
566 desc=fReleasedConsumers.begin();
567 fConsumers.push_back(pDesc);
568 }
569 desc=fActiveConsumers.begin();
570 while (desc!=fActiveConsumers.end()) {
571 AliHLTConsumerDescriptor* pDesc=*desc;
572 HLTWarning("consumer %p was not released", pDesc);
573 fActiveConsumers.erase(desc);
574 desc=fActiveConsumers.begin();
575 fConsumers.push_back(pDesc);
576 }
9ce4bf4a 577
578 // cleanup segments
2be3f004 579 AliHLTDataSegmentList::iterator segment=fSegments.begin();
9ce4bf4a 580 while (segment!=fSegments.end()) {
581 fSegments.erase(segment);
582 segment=fSegments.begin();
583 }
584
585 // cleanup raw buffer
586 if (pBuffer) {
587 ReleaseRawBuffer(pBuffer);
588 }
3f2a1b1c 589 return iResult;
590}
591
9ce4bf4a 592int AliHLTDataBuffer::Reset()
593{
70ed7d01 594 // see header file for function documentation
9ce4bf4a 595 return ResetDataBuffer();
596}
597
0c0c9d99 598// this is the version which works on lists of components instead of consumer descriptors
2be3f004 599// int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, AliHLTComponentPList &srcList, AliHLTComponentPList &tgtList)
0c0c9d99 600// {
601// int iResult=0;
602// if (pDesc) {
2be3f004 603// AliHLTComponentPList::iterator desc=srcList.begin();
0c0c9d99 604// while (desc!=srcList.end()) {
605// if ((*desc)==pConsumer) {
606// srcList.erase(desc);
607// tgtList.push_back(pConsumer);
608// break;
609// }
610// desc++;
611// }
612// if (desc==srcList.end()) {
613// HLTError("can not find consumer component %p in list", pConsumer);
614// iResult=-ENOENT;
615// }
616// } else {
617// HLTError("invalid parameter");
618// iResult=-EINVAL;
619// }
620// return iResult;
621// }
622
2be3f004 623int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, AliHLTConsumerDescriptorPList &srcList, AliHLTConsumerDescriptorPList &tgtList)
3f2a1b1c 624{
70ed7d01 625 // see header file for function documentation
9ce4bf4a 626 int iResult=-ENOENT;
3f2a1b1c 627 if (pDesc) {
2be3f004 628 AliHLTConsumerDescriptorPList::iterator desc=srcList.begin();
3f2a1b1c 629 while (desc!=srcList.end()) {
630 if ((*desc)==pDesc) {
631 srcList.erase(desc);
632 tgtList.push_back(pDesc);
9ce4bf4a 633 iResult=0;
3f2a1b1c 634 break;
635 }
0c0c9d99 636 desc++;
3f2a1b1c 637 }
9ce4bf4a 638 if (iResult<0) {
3f2a1b1c 639 HLTError("can not find consumer descriptor %p in list", pDesc);
3f2a1b1c 640 }
641 } else {
642 HLTError("invalid parameter");
643 iResult=-EINVAL;
644 }
645 return iResult;
646}
647
70ed7d01 648int AliHLTDataBuffer::CleanupConsumerList()
649{
650 // see header file for function documentation
3f2a1b1c 651 int iResult=0;
652 ResetDataBuffer();
2be3f004 653 AliHLTConsumerDescriptorPList::iterator desc=fConsumers.begin();
3f2a1b1c 654 while (desc!=fConsumers.end()) {
655 delete *desc;
656 fConsumers.erase(desc);
657 desc=fConsumers.begin();
658 }
659 return iResult;
660}
9ce4bf4a 661
70ed7d01 662int AliHLTDataBuffer::FindConsumer(AliHLTComponent* pConsumer, int bAllLists)
663{
664 // see header file for function documentation
2be3f004 665 AliHLTConsumerDescriptorPList::iterator desc=fConsumers.begin();
9ce4bf4a 666 while (desc!=fConsumers.end()) {
667 if ((*desc)->GetComponent()==pConsumer)
668 return 1;
669 desc++;
670 }
671 if (bAllLists==0) return 0;
672
673 desc=fActiveConsumers.begin();
674 while (desc!=fActiveConsumers.end()) {
675 if ((*desc)->GetComponent()==pConsumer)
676 return 1;
677 desc++;
678 }
679 desc=fReleasedConsumers.begin();
680 while (desc!=fReleasedConsumers.end()) {
681 if ((*desc)->GetComponent()==pConsumer)
682 return 1;
683 desc++;
684 }
685 return 0;
686}
1e6e67ec 687
d6cbe999 688AliHLTDataBuffer::AliHLTRawBuffer::AliHLTRawBuffer(AliHLTUInt32_t size)
689 :
c0a2bfc2 690 fSize(0),
d6cbe999 691 fTotalSize(size),
692 fPtr(static_cast<AliHLTUInt8_t*>(malloc(size)))
693{
694 // see header file for class documentation
695 // or
696 // refer to README to build package
697 // or
698 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
699 if (fPtr==NULL) {
700 fSize=0;
701 fTotalSize=0;
702 }
703}
704
705AliHLTDataBuffer::AliHLTRawBuffer::~AliHLTRawBuffer()
706{
707 if (fPtr) {
708 free(fPtr);
709 }
710 fPtr=NULL;
711 fSize=0;
712 fTotalSize=0;
713}
714
3a7c0444 715int AliHLTDataBuffer::AliHLTRawBuffer::operator==(void* ptr) const
1e6e67ec 716{
3a7c0444 717 // see header file for function documentation
1e6e67ec 718 return fPtr == static_cast<AliHLTUInt8_t*>(ptr);
719}
720
3a7c0444 721int AliHLTDataBuffer::AliHLTRawBuffer::operator<=(void* ptr) const
1e6e67ec 722{
3a7c0444 723 // see header file for function documentation
1e6e67ec 724 int iResult=fPtr <= static_cast<AliHLTUInt8_t*>(ptr);
725 //printf("%p: %p <= %p (%d)\n", this, fPtr, ptr, iResult);
726 return iResult;
727}
728
3a7c0444 729int AliHLTDataBuffer::AliHLTRawBuffer::operator>(void* ptr) const
1e6e67ec 730{
3a7c0444 731 // see header file for function documentation
1e6e67ec 732 int iResult=fPtr+fSize > static_cast<AliHLTUInt8_t*>(ptr);
733 //printf("%p: %p + %d > %p (%d)\n", this, fPtr, fSize, ptr, iResult);
734 return iResult;
735}
736
3a7c0444 737int AliHLTDataBuffer::AliHLTRawBuffer::operator-(void* ptr) const
1e6e67ec 738{
3a7c0444 739 // see header file for function documentation
1e6e67ec 740 return static_cast<int>(static_cast<AliHLTUInt8_t*>(ptr)-fPtr);
741}
d6cbe999 742
743AliHLTUInt8_t* AliHLTDataBuffer::AliHLTRawBuffer::UseBuffer(AliHLTUInt32_t size)
744{
745 // see header file for function documentation
dba03d72 746 if (size>0 && fTotalSize>=size) {
d6cbe999 747 fSize=size;
748 return fPtr;
749 }
750 return NULL;
751}
752
753int AliHLTDataBuffer::AliHLTRawBuffer::CheckSize(AliHLTUInt32_t size) const
754{
755 // see header file for function documentation
756 return fTotalSize>=size && ((fTotalSize-size)<fgMargin);
757}
758
759int AliHLTDataBuffer::AliHLTRawBuffer::Reset()
760{
761 // see header file for function documentation
762 fSize=0;
2be3f004 763 return 0;
d6cbe999 764}
765
766int AliHLTDataBuffer::AliHLTRawBuffer::WritePattern(const char* pattern, int size)
767{
768 // see header file for function documentation
769 int iResult=0;
770 if (pattern!=NULL && size>0) {
771 if (fSize+size<=fTotalSize) {
772 memcpy(((char*)fPtr)+fSize, pattern, size);
773 iResult=size;
774 } else {
775 iResult=-ENOSPC;
776 }
777 }
778 return iResult;
779}
780
781int AliHLTDataBuffer::AliHLTRawBuffer::CheckPattern(const char* pattern, int size) const
782{
783 // see header file for function documentation
784 int iResult=0;
785 if (pattern!=NULL && size>0) {
786 if (fSize+size<=fTotalSize) {
787 iResult=memcmp(((char*)fPtr)+fSize, pattern, size)!=0;
788 } else {
789 iResult=-ENOSPC;
790 }
791 }
792 return iResult;
793}