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