]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTDataBuffer.cxx
Include the new AliAODPid object (and don't create it by default).
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTDataBuffer.cxx
1 // $Id$
2
3 /**************************************************************************
4  * This file is property of and copyright by the ALICE HLT Project        * 
5  * ALICE Experiment at CERN, All rights reserved.                         *
6  *                                                                        *
7  * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8  *                  for The ALICE HLT Project.                            *
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
19 /** @file   AliHLTDataBuffer.cxx
20     @author Matthias Richter
21     @date   
22     @brief  Handling of Data Buffers for HLT components.
23 */
24
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
31 #if __GNUC__>= 3
32 using namespace std;
33 #endif
34
35 #include "AliHLTDataBuffer.h"
36 #include "AliHLTConsumerDescriptor.h"
37 #include "AliHLTComponent.h"
38 #include "AliHLTTask.h"
39 #include <cerrno>
40 #include <cassert>
41 //#include <string>
42 //#include "AliHLTSystem.h"
43
44 typedef vector<AliHLTDataBuffer::AliHLTDataSegment> AliHLTDataSegmentList;
45 typedef vector<AliHLTDataBuffer::AliHLTRawBuffer*>  AliHLTRawBufferPList;
46
47 /** ROOT macro for the implementation of ROOT specific class methods */
48 ClassImp(AliHLTDataBuffer)
49
50 AliHLTDataBuffer::AliHLTDataBuffer()
51   :
52   fSegments(),
53   fConsumers(),
54   fActiveConsumers(),
55   fReleasedConsumers(),
56   fpBuffer(NULL),
57   fFlags(0),
58   fForwardedSegmentSources(),
59   fForwardedSegments()
60 {
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
66   fSegments.empty();
67   fConsumers.empty();
68   fActiveConsumers.empty();
69   fReleasedConsumers.empty();
70   fgNofInstances++;
71 }
72
73 int AliHLTDataBuffer::fgNofInstances=0;
74 AliHLTRawBufferPList AliHLTDataBuffer::fgFreeBuffers;
75 AliHLTRawBufferPList AliHLTDataBuffer::fgActiveBuffers;
76 AliHLTUInt32_t AliHLTDataBuffer::fgMargin=1024;
77 AliHLTLogging AliHLTDataBuffer::fgLogging;
78 const Int_t AliHLTDataBuffer::fgkSafetyPatternSize=16;
79 const char AliHLTDataBuffer::fgkSafetyPattern[]={0x28, 0x63, 0x29, 0x4d, 0x52, 0x49, 0x43, 0x48, 0x54, 0x45, 0x52, 0x20, 0x32, 0x30, 0x30, 0x37};
80
81 AliHLTDataBuffer::~AliHLTDataBuffer()
82 {
83   // see header file for function documentation
84   if (--fgNofInstances<=0) {
85     DeleteRawBuffers();
86   }
87   CleanupConsumerList();
88 }
89
90 int AliHLTDataBuffer::SetConsumer(AliHLTComponent* pConsumer)
91 {
92   // see header file for function documentation
93   int iResult=0;
94   if (pConsumer) {
95     if (FindConsumer(pConsumer)) {
96       HLTWarning("consumer %s (%p) already set to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
97     }
98     AliHLTConsumerDescriptor* pDesc=new AliHLTConsumerDescriptor(pConsumer);
99     if (pDesc) {
100       fConsumers.push_back(pDesc);
101       HLTDebug("set consumer %s (%p) to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
102     } else {
103       HLTError("memory allocation failed");
104       iResult=-ENOMEM;
105     }
106   } else {
107     HLTError("invalid parameter: consumer component (nil)");
108     iResult=-EINVAL;
109   }
110   return iResult;
111 }
112
113 int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList)
114 {
115   // see header file for function documentation
116   int iResult=0;
117   if (pConsumer) {
118     AliHLTDataSegmentList segments;
119     if ((iResult=FindMatchingDataSegments(pConsumer, segments))>=0) {
120       if (tgtList) {
121         AliHLTDataSegmentList::iterator segment=segments.begin();
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
135 int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataBuffer::AliHLTDataSegment>& tgtList)
136 {
137   // see header file for function documentation
138   int iResult=0;
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());
144
145   // add all forwarded blocks
146   tgtList.insert(tgtList.begin(), fForwardedSegments.begin(), fForwardedSegments.end());
147   iResult=tgtList.size();
148   return iResult;
149   
150   if (pConsumer) {
151     AliHLTComponentDataTypeList dtlist;
152     ((AliHLTComponent*)pConsumer)->GetInputDataTypes(dtlist);
153     AliHLTDataSegmentList::iterator segment=fSegments.begin();
154     while (segment!=fSegments.end()) {
155       AliHLTComponentDataTypeList::iterator type=dtlist.begin();
156       while (type!=dtlist.end()) {
157         if ((*segment).fDataType==(*type) ||
158             (*type)==kAliHLTAnyDataType) {
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
173 int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponentBlockDataList& blockDescList)
174 {
175   // see header file for function documentation
176   int iResult=0;
177   if (pConsumer) {
178     if (1/*fpBuffer*/) {
179       AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fConsumers);
180       if (pDesc) {
181         AliHLTDataSegmentList tgtList;
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) {
186           AliHLTDataSegmentList::iterator segment=tgtList.begin();
187           while (segment!=tgtList.end()) {
188             // fill the block data descriptor
189             AliHLTComponentBlockData bd;
190             AliHLTComponent::FillBlockData(bd);
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.
195             bd.fOffset=0;
196             AliHLTUInt8_t* pTgt=*segment;
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);
202             pDesc->SetActiveDataSegment(*segment);
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);
207             segment++;
208           }
209           // move this consumer to the active list
210           if (tgtList.size()==0) {
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) {
214             HLTDebug("component %p (%s) subscribed to data buffer %p", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), this);
215           } else {
216             // TODO: cleanup the consumer descriptor correctly
217             segment=tgtList.begin();
218             while (segment!=tgtList.end()) {
219               blockDescList.pop_back();
220               segment++;
221             }
222             HLTError("can not activate consumer %p for data buffer %p", pConsumer, this);
223             iResult=-EACCES;
224           }
225         } else {
226           HLTError("unresolved data segment(s) for component %p (%s)", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
227           iResult=-EBADF;
228         }
229       } else {
230         HLTError("component %p is not a data consumer of data buffer %s", pConsumer, this);
231         iResult=-ENOENT;
232       }
233     } else {
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;
239     }
240   } else {
241     HLTError("invalid parameter");
242     iResult=-EINVAL;
243   }
244   return iResult;
245 }
246
247 int AliHLTDataBuffer::Release(AliHLTComponentBlockData* pBlockDesc,
248                               const AliHLTComponent* pConsumer,
249                               const AliHLTTask* pOwnerTask)
250 {
251   // see header file for function documentation
252   int iResult=0;
253   if (pBlockDesc && pConsumer) {
254     AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fActiveConsumers);
255     if (pDesc) {
256       if ((iResult=pDesc->CheckActiveDataSegment(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize)))!=1) {
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 {
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         }
291       }
292       pBlockDesc->fOffset=0;
293       pBlockDesc->fPtr=NULL;
294       pBlockDesc->fSize=0;
295       if (pDesc->GetNofActiveSegments()==0) {
296         if ((iResult=ChangeConsumerState(pDesc, fActiveConsumers, fReleasedConsumers))>=0) {
297           if (GetNofActiveConsumers()==0 && GetNofPendingConsumers()==0) {
298             // this is the last consumer, reset the consumer list and release the raw buffer
299             ResetDataBuffer();
300           }
301         } else {
302           HLTError("can not deactivate consumer %p for data buffer %p", pConsumer, this);
303           iResult=-EACCES;
304         }
305       }
306     } else {
307       HLTWarning("component %p has currently not subscribed to the data buffer %p", pConsumer, this);
308       iResult=-ENOENT;
309     }
310   } else {
311     HLTError("inavalid parameter: pBlockDesc=%p pConsumer=%p", pBlockDesc, pConsumer);
312     iResult=-EINVAL;
313   }
314   return iResult;
315 }
316
317 int 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);
324   fForwardedSegments.push_back(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize, pBlockDesc->fDataType, pBlockDesc->fSpecification));
325   return 0;
326 }
327
328 AliHLTUInt8_t* AliHLTDataBuffer::GetTargetBuffer(int iMinSize)
329 {
330   // see header file for function documentation
331   AliHLTUInt8_t* pTargetBuffer=NULL;
332   if (fpBuffer!=NULL) {
333     HLTWarning("data buffer not properly reset, possible memory leak\n");
334   }
335   fpBuffer=CreateRawBuffer(iMinSize);
336   if (fpBuffer) {
337     pTargetBuffer=*fpBuffer;
338   } else {
339     HLTError("can not create raw buffer");
340   }
341   return pTargetBuffer;
342 }
343
344 int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData* arrayBlockData, int iSize)
345 {
346   // see header file for function documentation
347   int iResult=0;
348   if (pTgt && arrayBlockData && iSize>=0) {
349     if (fpBuffer) {
350       if (*fpBuffer==pTgt) {
351         AliHLTDataBuffer::AliHLTDataSegment segment;
352         for (int i=0; i<iSize; i++) {
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
356           // pointer. In either case it is 'ignored' and set to the beginning of the
357           // data buffer
358           if (arrayBlockData[i].fPtr==NULL ||
359               arrayBlockData[i].fPtr==*fpBuffer) {
360             arrayBlockData[i].fPtr=*fpBuffer;
361             if (arrayBlockData[i].fOffset+arrayBlockData[i].fSize<=fpBuffer->GetUsedSize()) {
362               segment.fSegmentOffset=arrayBlockData[i].fOffset;
363               segment.fPtr=(AliHLTUInt8_t*)arrayBlockData[i].fPtr;
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());
371               HLTError("block offset=%d, block size=%d, buffer size=%d", arrayBlockData[i].fOffset, arrayBlockData[i].fSize, fpBuffer->GetUsedSize());
372               iResult=-E2BIG;
373             }
374           } else {
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 "
377                      "be relative to the output buffer", arrayBlockData[i].fPtr, fpBuffer->GetPointer(), fpBuffer->GetUsedSize());
378             iResult=-ERANGE;
379           }
380         }
381       } else {
382         HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->GetPointer(), fpBuffer);
383         iResult=-EINVAL;
384       }
385     } else {
386       HLTFatal("internal data structur missmatch");
387       iResult=-EFAULT;
388     }
389   } else {
390     HLTError("invalid parameter: pTgtBuffer=%p arrayBlockData=%p", pTgt, arrayBlockData);
391     iResult=-EINVAL;
392   }
393   return iResult;
394 }
395
396 int AliHLTDataBuffer::IsEmpty()
397 {
398   // see header file for function documentation
399   int iResult=fpBuffer==NULL || GetNofSegments()==0;
400   return iResult;
401 }
402
403 int AliHLTDataBuffer::GetNofSegments()
404 {
405   // see header file for function documentation
406   int iResult=fSegments.size();
407   return iResult;
408 }
409
410 int AliHLTDataBuffer::GetNofConsumers()
411 {
412   // see header file for function documentation
413   int iResult=fConsumers.size() + GetNofActiveConsumers() + fReleasedConsumers.size();
414   return iResult;
415 }
416
417 int AliHLTDataBuffer::GetNofPendingConsumers()
418 {
419   // see header file for function documentation
420   int iResult=fConsumers.size();
421   return iResult;
422 }
423
424 int AliHLTDataBuffer::GetNofActiveConsumers()
425 {
426   // see header file for function documentation
427   int iResult=fActiveConsumers.size();
428   return iResult;
429 }
430
431 AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
432 {
433   // see header file for function documentation
434   AliHLTRawBuffer* pRawBuffer=NULL;
435   unsigned int reqSize=size+fgkSafetyPatternSize;
436   AliHLTRawBufferPList::iterator buffer=fgFreeBuffers.begin();
437   while (buffer!=fgFreeBuffers.end() && pRawBuffer==NULL) {
438     if ((*buffer)->CheckSize(reqSize)) {
439       // assign this element
440       pRawBuffer=*buffer;
441       pRawBuffer->UseBuffer(size);
442       fgFreeBuffers.erase(buffer);
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());
444       fgActiveBuffers.push_back(pRawBuffer);
445       break;
446     }
447     buffer++;
448   }
449   if (pRawBuffer==NULL) {
450     // no buffer found, create a new one
451     pRawBuffer=new AliHLTRawBuffer(reqSize);
452     if (pRawBuffer) {
453       if (pRawBuffer->GetPointer()) {
454         pRawBuffer->UseBuffer(size);
455         fgActiveBuffers.push_back(pRawBuffer);
456         fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->GetPointer(), pRawBuffer->GetTotalSize(), pRawBuffer);
457       } else {
458         delete pRawBuffer;
459         pRawBuffer=NULL;
460         fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
461       } 
462     } else {
463       fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
464     }
465   }
466   if (pRawBuffer!=NULL && fgkSafetyPatternSize>0) {
467     //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "writing safety pattern to %p offset %d", (*buffer)->GetPointer(), (*buffer)->GetUsedSize());
468     pRawBuffer->WritePattern(fgkSafetyPattern, fgkSafetyPatternSize);
469   }
470   return pRawBuffer;
471 }
472
473 int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
474 {
475   // see header file for function documentation
476   int iResult=0;
477   if (pBuffer) {
478     AliHLTRawBufferPList::iterator buffer=fgActiveBuffers.begin();
479     while (buffer!=fgActiveBuffers.end() && (*buffer)!=pBuffer) {
480       buffer++;
481     }
482     if (buffer!=fgActiveBuffers.end()) {
483       if (fgkSafetyPatternSize>0) {
484         //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "comparing safety pattern at %p offset %d", (*buffer)->GetPointer(), reinterpret_cast<AliHLTUInt32_t>(*buffer));
485         if ((*buffer)->CheckPattern(fgkSafetyPattern, fgkSafetyPatternSize)) {
486           fgLogging.Logging(kHLTLogFatal, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "component has written beyond end of data buffer %p size %d", (*buffer)->GetPointer(), (*buffer)->GetUsedSize());
487         }
488       }
489       (*buffer)->Reset();
490       fgFreeBuffers.push_back(*buffer);
491       fgActiveBuffers.erase(buffer);
492     } else {
493       fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
494       iResult=-ENOENT;
495     }
496   } else {
497     fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "invalid parameter");
498     iResult=-EINVAL;
499   }
500   return iResult;
501 }
502
503
504 int AliHLTDataBuffer::DeleteRawBuffers() 
505 {
506   // see header file for function documentation
507   int iResult=0;
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();
513     delete *buffer;
514     fgFreeBuffers.erase(buffer);
515   }
516   while ((buffer=fgActiveBuffers.begin())!=fgActiveBuffers.end()) {
517 //     iTotalSize+=(*buffer)->GetTotalSize();
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());
519     delete *buffer;
520     fgActiveBuffers.erase(buffer);
521   }
522 //   fgLogging.Logging(kHLTLogInfo, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "Total memory allocation: %d byte in %d buffers", iTotalSize, iCount);
523   return iResult;
524 }
525
526 AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, AliHLTConsumerDescriptorPList &list) const
527 {
528   // see header file for function documentation
529   AliHLTConsumerDescriptor* pDesc=NULL;
530   AliHLTConsumerDescriptorPList::iterator desc=list.begin();
531   while (desc!=list.end() && pDesc==NULL) {
532     if ((pConsumer==NULL || (*desc)->GetComponent()==pConsumer)) {
533       pDesc=*desc;
534     }
535     desc++;
536   }
537   return pDesc;
538 }
539
540 int AliHLTDataBuffer::ResetDataBuffer() 
541 {
542   // see header file for function documentation
543   int iResult=0;
544   AliHLTRawBuffer* pBuffer=fpBuffer;
545   fpBuffer=NULL;
546
547   // cleanup forwarded segment lists
548   assert(fForwardedSegments.size()==0);
549   fForwardedSegments.clear();
550   fForwardedSegmentSources.clear();
551
552   // cleanup consumer states
553   AliHLTConsumerDescriptorPList::iterator desc;
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();
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   }
577
578   // cleanup segments
579   AliHLTDataSegmentList::iterator segment=fSegments.begin();
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   }
589   return iResult;
590 }
591
592 int AliHLTDataBuffer::Reset()
593 {
594   // see header file for function documentation
595   return ResetDataBuffer();
596 }
597
598 // this is the version which works on lists of components instead of consumer descriptors
599 // int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, AliHLTComponentPList &srcList, AliHLTComponentPList &tgtList)
600 // {
601 //   int iResult=0;
602 //   if (pDesc) {
603 //     AliHLTComponentPList::iterator desc=srcList.begin();
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
623 int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, AliHLTConsumerDescriptorPList &srcList, AliHLTConsumerDescriptorPList &tgtList)
624 {
625   // see header file for function documentation
626   int iResult=-ENOENT;
627   if (pDesc) {
628     AliHLTConsumerDescriptorPList::iterator desc=srcList.begin();
629     while (desc!=srcList.end()) {
630       if ((*desc)==pDesc) {
631         srcList.erase(desc);
632         tgtList.push_back(pDesc);
633         iResult=0;
634         break;
635       }
636       desc++;
637     }
638     if (iResult<0) {
639       HLTError("can not find consumer descriptor %p in list", pDesc);
640     }
641   } else {
642     HLTError("invalid parameter");
643     iResult=-EINVAL;
644   }
645   return iResult;
646 }
647
648 int AliHLTDataBuffer::CleanupConsumerList() 
649 {
650   // see header file for function documentation
651   int iResult=0;
652   ResetDataBuffer();
653   AliHLTConsumerDescriptorPList::iterator desc=fConsumers.begin();
654   while (desc!=fConsumers.end()) {
655     delete *desc;
656     fConsumers.erase(desc);
657     desc=fConsumers.begin();
658   }
659   return iResult;
660 }
661
662 int AliHLTDataBuffer::FindConsumer(AliHLTComponent* pConsumer, int bAllLists)
663 {
664   // see header file for function documentation
665   AliHLTConsumerDescriptorPList::iterator desc=fConsumers.begin();
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 }
687
688 AliHLTDataBuffer::AliHLTRawBuffer::AliHLTRawBuffer(AliHLTUInt32_t size)
689   :
690   fSize(0),
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
705 AliHLTDataBuffer::AliHLTRawBuffer::~AliHLTRawBuffer()
706 {
707   if (fPtr) {
708     free(fPtr);
709   }
710   fPtr=NULL;
711   fSize=0;
712   fTotalSize=0;
713 }
714
715 int AliHLTDataBuffer::AliHLTRawBuffer::operator==(void* ptr) const
716 {
717   // see header file for function documentation
718   return fPtr == static_cast<AliHLTUInt8_t*>(ptr);
719 }
720
721 int AliHLTDataBuffer::AliHLTRawBuffer::operator<=(void* ptr) const
722 {
723   // see header file for function documentation
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
729 int AliHLTDataBuffer::AliHLTRawBuffer::operator>(void* ptr) const
730 {
731   // see header file for function documentation
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
737 int AliHLTDataBuffer::AliHLTRawBuffer::operator-(void* ptr) const
738 {
739   // see header file for function documentation
740   return static_cast<int>(static_cast<AliHLTUInt8_t*>(ptr)-fPtr);
741 }
742
743 AliHLTUInt8_t* AliHLTDataBuffer::AliHLTRawBuffer::UseBuffer(AliHLTUInt32_t size)
744 {
745   // see header file for function documentation
746   if (size>0 && fTotalSize>=size) {
747     fSize=size;
748     return fPtr;
749   }
750   return NULL;
751 }
752
753 int AliHLTDataBuffer::AliHLTRawBuffer::CheckSize(AliHLTUInt32_t size) const
754 {
755   // see header file for function documentation
756   return fTotalSize>=size && ((fTotalSize-size)<fgMargin);
757 }
758
759 int AliHLTDataBuffer::AliHLTRawBuffer::Reset()
760 {
761   // see header file for function documentation
762   fSize=0;
763   return 0;
764 }
765
766 int 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
781 int 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 }