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