]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTDataBuffer.cxx
adding unit test for AliHLTDataBuffer class
[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
40 /** ROOT macro for the implementation of ROOT specific class methods */
41 ClassImp(AliHLTDataBuffer)
42
43 AliHLTDataBuffer::AliHLTDataBuffer()
44   :
45   fSegments(),
46   fConsumers(),
47   fActiveConsumers(),
48   fReleasedConsumers(),
49   fpBuffer(NULL),
50   fFlags(0),
51   fForwardedSegmentSources(),
52   fForwardedSegments()
53 {
54   // see header file for class documentation
55   // or
56   // refer to README to build package
57   // or
58   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
59   fSegments.empty();
60   fConsumers.empty();
61   fActiveConsumers.empty();
62   fReleasedConsumers.empty();
63   fgNofInstances++;
64 }
65
66 int AliHLTDataBuffer::fgNofInstances=0;
67 AliHLTDataBuffer::AliHLTRawBufferPList AliHLTDataBuffer::fgFreeBuffers;
68 AliHLTDataBuffer::AliHLTRawBufferPList AliHLTDataBuffer::fgActiveBuffers;
69 AliHLTUInt32_t AliHLTDataBuffer::fgMargin=1024;
70 AliHLTLogging AliHLTDataBuffer::fgLogging;
71 const Int_t AliHLTDataBuffer::fgkSafetyPatternSize=16;
72 const char AliHLTDataBuffer::fgkSafetyPattern[]={0x28, 0x63, 0x29, 0x4d, 0x52, 0x49, 0x43, 0x48, 0x54, 0x45, 0x52, 0x20, 0x32, 0x30, 0x30, 0x37};
73 AliHLTUInt32_t AliHLTDataBuffer::fgEventCount=0;
74
75 AliHLTDataBuffer::~AliHLTDataBuffer()
76 {
77   // see header file for function documentation
78   if (--fgNofInstances<=0) {
79     DeleteRawBuffers();
80   }
81   CleanupConsumerList();
82 }
83
84 int AliHLTDataBuffer::SetConsumer(AliHLTComponent* pConsumer)
85 {
86   // see header file for function documentation
87   int iResult=0;
88   if (pConsumer) {
89     if (FindConsumer(pConsumer)) {
90       HLTWarning("consumer %s (%p) already set to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
91     }
92     AliHLTConsumerDescriptor* pDesc=new AliHLTConsumerDescriptor(pConsumer);
93     if (pDesc) {
94       fConsumers.push_back(pDesc);
95       HLTDebug("set consumer %s (%p) to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
96     } else {
97       HLTError("memory allocation failed");
98       iResult=-ENOMEM;
99     }
100   } else {
101     HLTError("invalid parameter: consumer component (nil)");
102     iResult=-EINVAL;
103   }
104   return iResult;
105 }
106
107 int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList)
108 {
109   // see header file for function documentation
110   int iResult=0;
111   if (pConsumer) {
112     AliHLTDataSegmentList segments;
113     if ((iResult=FindMatchingDataSegments(pConsumer, segments))>=0) {
114       if (tgtList) {
115         AliHLTDataSegmentList::iterator segment=segments.begin();
116         while (segment!=segments.end()) {
117           tgtList->push_back((*segment).fDataType);
118           segment++;
119         }
120       }
121       iResult=segments.size();
122     }
123   } else {
124     iResult=-EINVAL;
125   }
126   return iResult;
127 }
128
129 int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataBuffer::AliHLTDataSegment>& tgtList)
130 {
131   // see header file for function documentation
132   int iResult=0;
133
134   // Matthias 26.09.2007 relax the restriction to matching data blocks
135   // all blocks are passed to the consumer, which is the policy also in
136   // PubSub
137   tgtList.assign(fSegments.begin(), fSegments.end());
138
139   // add all forwarded blocks
140   tgtList.insert(tgtList.begin(), fForwardedSegments.begin(), fForwardedSegments.end());
141   iResult=tgtList.size();
142   return iResult;
143   
144   if (pConsumer) {
145     AliHLTComponentDataTypeList dtlist;
146     ((AliHLTComponent*)pConsumer)->GetInputDataTypes(dtlist);
147     AliHLTDataSegmentList::iterator segment=fSegments.begin();
148     while (segment!=fSegments.end()) {
149       AliHLTComponentDataTypeList::iterator type=dtlist.begin();
150       while (type!=dtlist.end()) {
151         if ((*segment).fDataType==(*type)) {
152           tgtList.push_back(*segment);
153           iResult++;
154           break;
155         }
156         type++;
157       }
158       segment++;
159     }
160   } else {
161     iResult=-EINVAL;
162   }
163   return iResult;
164 }
165
166 int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponentBlockDataList& blockDescList)
167 {
168   // see header file for function documentation
169   int iResult=0;
170   if (pConsumer) {
171     if (1/*fpBuffer*/) {
172       AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fConsumers);
173       if (pDesc) {
174         AliHLTDataSegmentList tgtList;
175         // Matthias 26.07.2007 AliHLTSystem should behave the same way as PubSub
176         // so it does not matter if there are matching data types or not, unless
177         // we implement such a check in PubSub
178         if ((iResult=FindMatchingDataSegments(pConsumer, tgtList))>=0) {
179           AliHLTDataSegmentList::iterator segment=tgtList.begin();
180           while (segment!=tgtList.end()) {
181             // fill the block data descriptor
182             AliHLTComponentBlockData bd;
183             AliHLTComponent::FillBlockData(bd);
184             // This models the behavior of PubSub.
185             // For incoming data blocks, fOffset must be ignored by the
186             // processing component. It is set for bookkeeping in the framework.
187             // fPtr always points to the beginning of the data.
188             bd.fOffset=0;
189             AliHLTUInt8_t* pTgt=*segment;
190             bd.fPtr=reinterpret_cast<void*>(pTgt);
191             bd.fSize=(*segment).fSegmentSize;
192             bd.fDataType=(*segment).fDataType;
193             bd.fSpecification=(*segment).fSpecification;
194             blockDescList.push_back(bd);
195             pDesc->SetActiveDataSegment(*segment);
196             HLTDebug("component %p (%s) subscribed to segment offset %d size %d data type %s %#x", 
197                      pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), bd.fOffset,
198                      bd.fSize, (AliHLTComponent::DataType2Text(bd.fDataType)).c_str(), 
199                      bd.fSpecification);
200             segment++;
201           }
202           // move this consumer to the active list
203           if (tgtList.size()==0) {
204             ChangeConsumerState(pDesc, fConsumers, fReleasedConsumers);
205             HLTDebug("no input data for component %p (%s) available", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
206           } else if (ChangeConsumerState(pDesc, fConsumers, fActiveConsumers)>=0) {
207             HLTDebug("component %p (%s) subscribed to data buffer %p", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), this);
208           } else {
209             // TODO: cleanup the consumer descriptor correctly
210             segment=tgtList.begin();
211             while (segment!=tgtList.end()) {
212               blockDescList.pop_back();
213               segment++;
214             }
215             HLTError("can not activate consumer %p for data buffer %p", pConsumer, this);
216             iResult=-EACCES;
217           }
218         } else {
219           HLTError("unresolved data segment(s) for component %p (%s)", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
220           iResult=-EBADF;
221         }
222       } else {
223         if (!FindConsumer(pConsumer)) {
224           HLTError("component %p is not a data consumer of data buffer %p", pConsumer, this);
225         } else {
226           HLTError("component %p is a valid data consumer of data buffer %p, but did not release it's buffer subscription", pConsumer, this);
227         }
228         iResult=-ENOENT;
229       }
230     } else {
231       // Matthias 26.07.2007 until now, data had to be present for successful subscription
232       // in order to be consistent with the PubSub framework, this restiction has been
233       // removed
234       //HLTError("data buffer %p is empty", this);
235       //iResult=-ENODATA;
236     }
237   } else {
238     HLTError("invalid parameter");
239     iResult=-EINVAL;
240   }
241   return iResult;
242 }
243
244 int AliHLTDataBuffer::Release(AliHLTComponentBlockData* pBlockDesc,
245                               const AliHLTComponent* pConsumer,
246                               const AliHLTTask* pOwnerTask)
247 {
248   // see header file for function documentation
249   int iResult=0;
250   if (pBlockDesc && pConsumer) {
251     AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fActiveConsumers);
252     if (pDesc) {
253       if ((iResult=pDesc->CheckActiveDataSegment(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize)))!=1) {
254         HLTWarning("data segment mismatch, component %p has not subscribed to a segment with offset %#x and size %d", pConsumer, pBlockDesc->fOffset, pBlockDesc->fSize);
255         // TODO: appropriate error handling, but so far optional
256         iResult=0;
257       } else {
258         pDesc->ReleaseActiveDataSegment(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize));
259       }
260       if (GetNofPendingConsumers()==0 && fForwardedSegments.size()>0) {
261         // last consumer, release forwarded segments
262         ReleaseForwardedBlock(pBlockDesc, pOwnerTask);
263       }
264       pBlockDesc->fOffset=0;
265       pBlockDesc->fPtr=NULL;
266       pBlockDesc->fSize=0;
267       if (pDesc->GetNofActiveSegments()==0) {
268         if ((iResult=ChangeConsumerState(pDesc, fActiveConsumers, fReleasedConsumers))>=0) {
269           if (GetNofActiveConsumers()==0 && GetNofPendingConsumers()==0) {
270             // this is the last consumer, reset the consumer list and release the raw buffer
271             ResetDataBuffer();
272           }
273         } else {
274           HLTError("can not deactivate consumer %p for data buffer %p", pConsumer, this);
275           iResult=-EACCES;
276         }
277       }
278     } else {
279       HLTWarning("component %p has currently not subscribed to the data buffer %p", pConsumer, this);
280       iResult=-ENOENT;
281     }
282   } else {
283     HLTError("inavalid parameter: pBlockDesc=%p pConsumer=%p", pBlockDesc, pConsumer);
284     iResult=-EINVAL;
285   }
286   return iResult;
287 }
288
289 int AliHLTDataBuffer::ReleaseForwardedBlock(AliHLTComponentBlockData* pBlockDesc,
290                                             const AliHLTTask* pOwnerTask)
291 {
292   // see header file for function documentation
293   int iResult=0;
294   if (pBlockDesc && pOwnerTask) {
295         assert(fForwardedSegments.size()==fForwardedSegmentSources.size());
296         AliHLTDataSegmentList::iterator segment=fForwardedSegments.begin();
297         AliHLTTaskPList::iterator src=fForwardedSegmentSources.begin();
298         //HLTDebug("%p checking forwarded segments", this);
299         for (; segment!=fForwardedSegments.end(); segment++, src++) {
300           //HLTDebug("segment ptr=%p offset=%d size=%d\n"
301           //   "block ptr=%p offset=%d size=%d", (*segment).fPtr, (*segment).fSegmentOffset, (*segment).fSegmentSize, pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize);
302           if ((*segment)==AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize)) {
303             //HLTDebug("release segment of task %p", *src);
304             assert((*src)!=NULL);
305             if ((*src)!=NULL) {
306               if ((*src)->Release(pBlockDesc, pOwnerTask)>=0) {
307                 HLTDebug("task %s (%p) released forwarded segment %p size %d of task %s (%p)",
308                          pOwnerTask->GetName(), pOwnerTask, (*segment).GetPtr(), (*segment).GetSize(),
309                          (*src)->GetName(), *src);
310               } else {
311                 HLTError("task %s (%p) failed releasing forwarded segment %p size %d of task %s (%p)",
312                          pOwnerTask->GetName(), pOwnerTask, (*segment).GetPtr(), (*segment).GetSize(),
313                          (*src)->GetName(), *src);
314               }
315             }
316             fForwardedSegments.erase(segment);
317             fForwardedSegmentSources.erase(src);
318             break;
319           }
320         }
321   } else {
322     HLTError("inavalid parameter: pBlockDesc=%p pOwnerTask=%p", pBlockDesc, pOwnerTask);
323     iResult=-EINVAL;
324   }
325   return iResult;
326 }
327
328 int AliHLTDataBuffer::Forward(AliHLTTask* pSrcTask, AliHLTComponentBlockData* pBlockDesc)
329 {
330   // see header file for function documentation
331   if (pSrcTask==NULL || pBlockDesc==NULL) return -EINVAL;
332   assert(fForwardedSegments.size()==fForwardedSegmentSources.size());
333   if (fForwardedSegments.size()!=fForwardedSegmentSources.size()) return -EFAULT;
334   fForwardedSegmentSources.push_back(pSrcTask);
335   fForwardedSegments.push_back(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize, pBlockDesc->fDataType, pBlockDesc->fSpecification));
336   return 0;
337 }
338
339 AliHLTUInt8_t* AliHLTDataBuffer::GetTargetBuffer(int iMinSize)
340 {
341   // see header file for function documentation
342   AliHLTUInt8_t* pTargetBuffer=NULL;
343   if (fpBuffer!=NULL) {
344     HLTWarning("data buffer not properly reset, possible memory leak\n");
345   }
346   fpBuffer=CreateRawBuffer(iMinSize);
347   if (fpBuffer) {
348     pTargetBuffer=*fpBuffer;
349   } else {
350     HLTError("can not create raw buffer");
351   }
352   return pTargetBuffer;
353 }
354
355 int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData* arrayBlockData, int iSize)
356 {
357   // see header file for function documentation
358   int iResult=0;
359   if (pTgt && arrayBlockData && iSize>=0) {
360     if (fpBuffer) {
361       if (*fpBuffer==pTgt) {
362         AliHLTDataBuffer::AliHLTDataSegment segment;
363         for (int i=0; i<iSize; i++) {
364           // This function has to model the behavior of PubSub
365           // For output blocks only the fOffset value is used, this must be the offset
366           // relative to the output pointer. fPtr must be either NULL or the output
367           // pointer. In either case it is 'ignored' and set to the beginning of the
368           // data buffer
369           if (arrayBlockData[i].fPtr==NULL ||
370               arrayBlockData[i].fPtr==*fpBuffer) {
371             arrayBlockData[i].fPtr=*fpBuffer;
372             if ((arrayBlockData[i].fOffset+arrayBlockData[i].fSize<=fpBuffer->GetUsedSize()) ||
373                 ((arrayBlockData[i].fOffset==~(AliHLTUInt32_t)0) && arrayBlockData[i].fSize==0)) {
374               segment.fSegmentOffset=arrayBlockData[i].fOffset;
375               segment.fPtr=(AliHLTUInt8_t*)arrayBlockData[i].fPtr;
376               segment.fSegmentSize=arrayBlockData[i].fSize;
377               segment.fDataType=arrayBlockData[i].fDataType;
378               segment.fSpecification=arrayBlockData[i].fSpecification;
379               fSegments.push_back(segment);
380               HLTDebug("set segment %s with size %d at offset %d", AliHLTComponent::DataType2Text(segment.fDataType).data(), segment.fSegmentSize, segment.fSegmentOffset);
381             } else {
382               HLTError("block data specification %#d (%s) exceeds size of data buffer", i, AliHLTComponent::DataType2Text(arrayBlockData[i].fDataType).data());
383               HLTError("block offset=%d, block size=%d, buffer size=%d", arrayBlockData[i].fOffset, arrayBlockData[i].fSize, fpBuffer->GetUsedSize());
384               iResult=-E2BIG;
385             }
386           } else {
387             HLTError("invalid pointer (%p) in block data specification (buffer %p size %d)."
388                      "please note: for output blocks only the fOffset value is valid and must "
389                      "be relative to the output buffer", arrayBlockData[i].fPtr, fpBuffer->GetPointer(), fpBuffer->GetUsedSize());
390             iResult=-ERANGE;
391           }
392         }
393       } else {
394         HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->GetPointer(), fpBuffer);
395         iResult=-EINVAL;
396       }
397     } else {
398       HLTFatal("internal data structur mismatch");
399       iResult=-EFAULT;
400     }
401   } else {
402     HLTError("invalid parameter: pTgtBuffer=%p arrayBlockData=%p", pTgt, arrayBlockData);
403     iResult=-EINVAL;
404   }
405   return iResult;
406 }
407
408 int AliHLTDataBuffer::IsEmpty()
409 {
410   // see header file for function documentation
411   int iResult=(fpBuffer==NULL && fForwardedSegments.size()==0) || GetNofSegments()==0;
412   return iResult;
413 }
414
415 int AliHLTDataBuffer::GetNofSegments()
416 {
417   // see header file for function documentation
418   int iResult=fSegments.size() + fForwardedSegments.size();
419   return iResult;
420 }
421
422 int AliHLTDataBuffer::GetNofConsumers()
423 {
424   // see header file for function documentation
425   int iResult=fConsumers.size() + GetNofActiveConsumers() + fReleasedConsumers.size();
426   return iResult;
427 }
428
429 int AliHLTDataBuffer::GetNofPendingConsumers()
430 {
431   // see header file for function documentation
432   int iResult=fConsumers.size();
433   return iResult;
434 }
435
436 int AliHLTDataBuffer::GetNofActiveConsumers()
437 {
438   // see header file for function documentation
439   int iResult=fActiveConsumers.size();
440   return iResult;
441 }
442
443 AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
444 {
445   // see header file for function documentation
446   AliHLTRawBuffer* pRawBuffer=NULL;
447   unsigned int reqSize=size+fgkSafetyPatternSize;
448   AliHLTRawBufferPList::iterator buffer=fgFreeBuffers.begin();
449   while (buffer!=fgFreeBuffers.end() && pRawBuffer==NULL) {
450     if ((*buffer)->CheckSize(reqSize)) {
451       // assign this element
452       pRawBuffer=*buffer;
453       pRawBuffer->UseBuffer(size);
454       fgFreeBuffers.erase(buffer);
455       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());
456       fgActiveBuffers.push_back(pRawBuffer);
457       break;
458     }
459     buffer++;
460   }
461   if (pRawBuffer==NULL) {
462     // no buffer found, create a new one
463     pRawBuffer=new AliHLTRawBuffer(reqSize);
464     if (pRawBuffer) {
465       if (pRawBuffer->GetPointer()) {
466         pRawBuffer->UseBuffer(size);
467         fgActiveBuffers.push_back(pRawBuffer);
468         fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->GetPointer(), pRawBuffer->GetTotalSize(), pRawBuffer);
469       } else {
470         delete pRawBuffer;
471         pRawBuffer=NULL;
472         fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
473       } 
474     } else {
475       fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
476     }
477   }
478   if (pRawBuffer!=NULL && fgkSafetyPatternSize>0) {
479     //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "writing safety pattern to %p offset %d", (*buffer)->GetPointer(), (*buffer)->GetUsedSize());
480     pRawBuffer->WritePattern(fgkSafetyPattern, fgkSafetyPatternSize);
481   }
482   return pRawBuffer;
483 }
484
485 int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
486 {
487   // see header file for function documentation
488   int iResult=0;
489   if (pBuffer) {
490     AliHLTRawBufferPList::iterator buffer=fgActiveBuffers.begin();
491     while (buffer!=fgActiveBuffers.end() && (*buffer)!=pBuffer) {
492       buffer++;
493     }
494     if (buffer!=fgActiveBuffers.end()) {
495       if (fgkSafetyPatternSize>0) {
496         //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "comparing safety pattern at %p offset %d", (*buffer)->GetPointer(), reinterpret_cast<AliHLTUInt32_t>(*buffer));
497         if ((*buffer)->CheckPattern(fgkSafetyPattern, fgkSafetyPatternSize)) {
498           fgLogging.Logging(kHLTLogFatal, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "component has written beyond end of data buffer %p size %d", (*buffer)->GetPointer(), (*buffer)->GetUsedSize());
499         }
500       }
501       (*buffer)->Reset();
502       fgFreeBuffers.push_back(*buffer);
503       fgActiveBuffers.erase(buffer);
504     } else {
505       fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
506       iResult=-ENOENT;
507     }
508   } else {
509     fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "invalid parameter");
510     iResult=-EINVAL;
511   }
512   return iResult;
513 }
514
515
516 int AliHLTDataBuffer::DeleteRawBuffers() 
517 {
518   // see header file for function documentation
519   int iResult=0;
520 #ifdef ALIHLTSYSTEM_PROFILING
521   int iTotalSize=0;
522   int iCount=fgFreeBuffers.size()+fgActiveBuffers.size();
523 #endif //ALIHLTSYSTEM_PROFILING
524   AliHLTRawBufferPList::iterator buffer;;
525   while ((buffer=fgFreeBuffers.begin())!=fgFreeBuffers.end()) {
526 #ifdef ALIHLTSYSTEM_PROFILING
527     iTotalSize+=(*buffer)->GetTotalSize();
528 #endif //ALIHLTSYSTEM_PROFILING
529     delete *buffer;
530     fgFreeBuffers.erase(buffer);
531   }
532   while ((buffer=fgActiveBuffers.begin())!=fgActiveBuffers.end()) {
533 #ifdef ALIHLTSYSTEM_PROFILING
534     iTotalSize+=(*buffer)->GetTotalSize();
535 #endif //ALIHLTSYSTEM_PROFILING
536     fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "request to delete active raw buffer container (raw buffer %p, size %d)", (*buffer)->GetPointer(), (*buffer)->GetTotalSize());
537     delete *buffer;
538     fgActiveBuffers.erase(buffer);
539   }
540 #ifdef ALIHLTSYSTEM_PROFILING
541   fgLogging.Logging(kHLTLogImportant, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "Total memory allocation: %d byte in %d buffers", iTotalSize, iCount);
542 #endif //ALIHLTSYSTEM_PROFILING
543   return iResult;
544 }
545
546 int AliHLTDataBuffer::PrintStatistics() 
547 {
548   // see header file for function documentation
549   int iResult=0;
550   int iFree=0;
551   int iActive=0;
552   AliHLTRawBufferPList::iterator buffer;;
553   for (buffer=fgFreeBuffers.begin(); buffer!=fgFreeBuffers.end(); buffer++) {
554     iFree+=(*buffer)->GetTotalSize();
555   }
556   for (buffer=fgActiveBuffers.begin(); buffer!=fgActiveBuffers.end(); buffer++) {
557     iActive+=(*buffer)->GetTotalSize();
558   }
559   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);
560   return iResult;
561 }
562
563 AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, AliHLTConsumerDescriptorPList &list) const
564 {
565   // see header file for function documentation
566   AliHLTConsumerDescriptor* pDesc=NULL;
567   AliHLTConsumerDescriptorPList::iterator desc=list.begin();
568   while (desc!=list.end() && pDesc==NULL) {
569     if ((pConsumer==NULL || (*desc)->GetComponent()==pConsumer)) {
570       pDesc=*desc;
571     }
572     desc++;
573   }
574   return pDesc;
575 }
576
577 int AliHLTDataBuffer::ResetDataBuffer() 
578 {
579   // see header file for function documentation
580   int iResult=0;
581   AliHLTRawBuffer* pBuffer=fpBuffer;
582   fpBuffer=NULL;
583
584   // cleanup forwarded segment lists
585   assert(fForwardedSegments.size()==0);
586   fForwardedSegments.clear();
587   fForwardedSegmentSources.clear();
588
589   // cleanup consumer states
590   AliHLTConsumerDescriptorPList::iterator desc;
591 //   if (GetNofPendingConsumers()>0) {
592 //     desc=fConsumers.begin();
593 //     while (desc!=fConsumers.end()) {
594 //       AliHLTComponent* pComp=(*desc)->GetComponent();
595 //       HLTError("internal error: consumer %p (%s %p) did not get data from data buffer %p", *desc, pComp?pComp->GetComponentID():"", pComp, this);
596 //       desc++;
597 //     }
598 //   }
599   desc=fReleasedConsumers.begin();
600   while (desc!=fReleasedConsumers.end()) {
601     AliHLTConsumerDescriptor* pDesc=*desc;
602     fReleasedConsumers.erase(desc);
603     desc=fReleasedConsumers.begin();
604     fConsumers.push_back(pDesc);
605   }
606   desc=fActiveConsumers.begin();
607   while (desc!=fActiveConsumers.end()) {
608     AliHLTConsumerDescriptor* pDesc=*desc;
609     HLTWarning("consumer %p (%s) was not released", pDesc, pDesc->GetComponent()?pDesc->GetComponent()->GetComponentID():"### invalid component ###");
610     fActiveConsumers.erase(desc);
611     desc=fActiveConsumers.begin();
612     fConsumers.push_back(pDesc);
613   }
614
615   // cleanup segments
616   AliHLTDataSegmentList::iterator segment=fSegments.begin();
617   while (segment!=fSegments.end()) {
618     fSegments.erase(segment);
619     segment=fSegments.begin();
620   }
621
622   // cleanup raw buffer
623   if (pBuffer) {
624     ReleaseRawBuffer(pBuffer);
625   }
626   return iResult;
627 }
628
629 int AliHLTDataBuffer::Reset()
630 {
631   // see header file for function documentation
632   return ResetDataBuffer();
633 }
634
635 // this is the version which works on lists of components instead of consumer descriptors
636 // int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, AliHLTComponentPList &srcList, AliHLTComponentPList &tgtList)
637 // {
638 //   int iResult=0;
639 //   if (pDesc) {
640 //     AliHLTComponentPList::iterator desc=srcList.begin();
641 //     while (desc!=srcList.end()) {
642 //       if ((*desc)==pConsumer) {
643 //      srcList.erase(desc);
644 //      tgtList.push_back(pConsumer);
645 //      break;
646 //       }
647 //      desc++;
648 //     }
649 //     if (desc==srcList.end()) {
650 //       HLTError("can not find consumer component %p in list", pConsumer);
651 //       iResult=-ENOENT;
652 //     }
653 //   } else {
654 //     HLTError("invalid parameter");
655 //     iResult=-EINVAL;
656 //   }
657 //   return iResult;
658 // }
659
660 int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, AliHLTConsumerDescriptorPList &srcList, AliHLTConsumerDescriptorPList &tgtList)
661 {
662   // see header file for function documentation
663   int iResult=-ENOENT;
664   if (pDesc) {
665     AliHLTConsumerDescriptorPList::iterator desc=srcList.begin();
666     while (desc!=srcList.end()) {
667       if ((*desc)==pDesc) {
668         srcList.erase(desc);
669         tgtList.push_back(pDesc);
670         iResult=0;
671         break;
672       }
673       desc++;
674     }
675     if (iResult<0) {
676       HLTError("can not find consumer descriptor %p in list", pDesc);
677     }
678   } else {
679     HLTError("invalid parameter");
680     iResult=-EINVAL;
681   }
682   return iResult;
683 }
684
685 int AliHLTDataBuffer::CleanupConsumerList() 
686 {
687   // see header file for function documentation
688   int iResult=0;
689   ResetDataBuffer();
690   AliHLTConsumerDescriptorPList::iterator desc=fConsumers.begin();
691   while (desc!=fConsumers.end()) {
692     delete *desc;
693     fConsumers.erase(desc);
694     desc=fConsumers.begin();
695   }
696   return iResult;
697 }
698
699 int AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, int bAllLists)
700 {
701   // see header file for function documentation
702   AliHLTConsumerDescriptorPList::iterator desc=fConsumers.begin();
703   while (desc!=fConsumers.end()) {
704     if ((*desc)->GetComponent()==pConsumer)
705       return 1;
706     desc++;
707   }
708   if (bAllLists==0) return 0;
709
710   desc=fActiveConsumers.begin();
711   while (desc!=fActiveConsumers.end()) {
712     if ((*desc)->GetComponent()==pConsumer)
713       return 1;
714     desc++;
715   }
716   desc=fReleasedConsumers.begin();
717   while (desc!=fReleasedConsumers.end()) {
718     if ((*desc)->GetComponent()==pConsumer)
719       return 1;
720     desc++;
721   }
722   return 0;
723 }
724
725 AliHLTDataBuffer::AliHLTRawBuffer::AliHLTRawBuffer(AliHLTUInt32_t size)
726   : fSize(0)
727   , fTotalSize(size)
728   , fExternalPtr(NULL)
729   , fPtr(static_cast<AliHLTUInt8_t*>(malloc(size)))
730   , fLastEventCount(0)
731 {
732   // see header file for class documentation
733   // or
734   // refer to README to build package
735   // or
736   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
737   if (fPtr==NULL) {
738     fSize=0;
739     fTotalSize=0;
740   }
741 }
742
743 AliHLTDataBuffer::AliHLTRawBuffer::AliHLTRawBuffer(AliHLTUInt32_t size, AliHLTUInt8_t* buffer)
744   : fSize(0)
745   , fTotalSize(size)
746   , fExternalPtr(buffer)
747   , fPtr(fExternalPtr)
748   , fLastEventCount(0)
749 {
750   // see header file for class documentation
751 }
752
753 AliHLTDataBuffer::AliHLTRawBuffer::~AliHLTRawBuffer()
754 {
755   if (fExternalPtr==NULL && fPtr) {
756     free(fPtr);
757   }
758   fPtr=NULL;
759   fSize=0;
760   fTotalSize=0;
761 }
762
763 int AliHLTDataBuffer::AliHLTRawBuffer::operator==(void* ptr) const
764 {
765   // see header file for function documentation
766   return fPtr == static_cast<AliHLTUInt8_t*>(ptr);
767 }
768
769 int AliHLTDataBuffer::AliHLTRawBuffer::operator<(void* ptr) const
770 {
771   // see header file for function documentation
772   int iResult=fPtr < static_cast<AliHLTUInt8_t*>(ptr);
773   //printf("%p: %p <= %p (%d)\n", this, fPtr, ptr, iResult);
774   return iResult;
775 }
776
777 int AliHLTDataBuffer::AliHLTRawBuffer::operator<=(void* ptr) const
778 {
779   // see header file for function documentation
780   int iResult=fPtr <= static_cast<AliHLTUInt8_t*>(ptr);
781   //printf("%p: %p <= %p (%d)\n", this, fPtr, ptr, iResult);
782   return iResult;
783 }
784
785 int AliHLTDataBuffer::AliHLTRawBuffer::operator>(void* ptr) const
786 {
787   // see header file for function documentation
788   int iResult=fPtr+fSize > static_cast<AliHLTUInt8_t*>(ptr);
789   //printf("%p: %p + %d > %p (%d)\n", this, fPtr, fSize, ptr, iResult);
790   return iResult;
791 }
792
793 int AliHLTDataBuffer::AliHLTRawBuffer::operator-(void* ptr) const
794 {
795   // see header file for function documentation
796   return static_cast<int>(static_cast<AliHLTUInt8_t*>(ptr)-fPtr);
797 }
798
799 int AliHLTDataBuffer::AliHLTRawBuffer::operator<(const AliHLTRawBuffer& op) const
800 {
801   // see header file for function documentation
802   return (fPtr+fSize < op.fPtr);
803 }
804
805 int AliHLTDataBuffer::AliHLTRawBuffer::operator<=(const AliHLTRawBuffer& op) const
806 {
807   // see header file for function documentation
808   return (fPtr+fSize <= op.fPtr);
809 }
810
811 int AliHLTDataBuffer::AliHLTRawBuffer::operator>(const AliHLTRawBuffer& op) const
812 {
813   // see header file for function documentation
814   return (fPtr >= op.fPtr+op.fSize);
815 }
816
817 AliHLTUInt8_t* AliHLTDataBuffer::AliHLTRawBuffer::UseBuffer(AliHLTUInt32_t size)
818 {
819   // mark a portion of the buffer as used
820   if (size>0 && fTotalSize>=size) {
821     fSize=size;
822     fLastEventCount=AliHLTDataBuffer::fgEventCount;
823     return fPtr;
824   }
825   return NULL;
826 }
827
828 AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::AliHLTRawBuffer::Split(AliHLTUInt32_t size)
829 {
830   // split a buffer at specified size
831   // only possible for buffers with external memory
832   if (size>0 && fTotalSize>size && 
833       (fSize==0 || fSize<size) &&
834       fExternalPtr!=NULL) {
835     AliHLTRawBuffer* part2=new AliHLTRawBuffer(fTotalSize-size, fPtr+size);
836     if (part2) {
837       fTotalSize=size;
838     }
839     return part2;
840   }
841   return NULL;
842 }
843
844 int AliHLTDataBuffer::AliHLTRawBuffer::CheckSize(AliHLTUInt32_t size) const
845 {
846   // see header file for function documentation
847   if (fTotalSize<size) return 0;
848   unsigned adjust=0;
849   if (fLastEventCount+1<AliHLTDataBuffer::fgEventCount) {
850     adjust=AliHLTDataBuffer::fgEventCount-fLastEventCount;
851   }
852   return (adjust>2) || ((fTotalSize-size)<(fgMargin<<adjust));
853 }
854
855 int AliHLTDataBuffer::AliHLTRawBuffer::Reset()
856 {
857   // see header file for function documentation
858   fSize=0;
859   return 0;
860 }
861
862 int AliHLTDataBuffer::AliHLTRawBuffer::WritePattern(const char* pattern, int size)
863 {
864   // see header file for function documentation
865   int iResult=0;
866   if (pattern!=NULL && size>0) {
867     if (fSize+size<=fTotalSize) {
868       memcpy(((char*)fPtr)+fSize, pattern, size);
869       iResult=size;
870     } else {
871       iResult=-ENOSPC;
872     }
873   }
874   return iResult;
875 }
876
877 int AliHLTDataBuffer::AliHLTRawBuffer::CheckPattern(const char* pattern, int size) const
878 {
879   // see header file for function documentation
880   int iResult=0;
881   if (pattern!=NULL && size>0) {
882     if (fSize+size<=fTotalSize) {
883       iResult=memcmp(((char*)fPtr)+fSize, pattern, size)!=0;
884     } else {
885       iResult=-ENOSPC;
886     }
887   }
888   return iResult;
889 }
890
891 int AliHLTDataBuffer::AliHLTRawBuffer::Merge(const AliHLTDataBuffer::AliHLTRawBuffer& neighbor)
892 {
893   // Merge buffer with neighboring buffer.
894   // Only possible if the buffers are consecutive with out any gap.
895
896   if (!fExternalPtr || !neighbor.fExternalPtr) return -EPERM;
897
898   if (fPtr+fTotalSize == neighbor.fPtr) {
899     fTotalSize+=neighbor.fTotalSize;
900     fSize=0;
901     return 0;
902   }
903   if (fPtr == neighbor.fPtr+neighbor.fTotalSize) {
904     fPtr=neighbor.fPtr;
905     fExternalPtr=fPtr;
906     fTotalSize+=neighbor.fTotalSize;
907     fSize=0;
908     return 0;
909   }
910   return -EINVAL;
911 }
912
913 void AliHLTDataBuffer::AliHLTRawBuffer::Print(const char* option)
914 {
915   /// print buffer information
916   if (strcmp(option, "min")!=0) {
917     cout << "************* AliHLTRawBuffer status ***********" << endl;
918   }
919   printf("  %p: buffer %p%s size %d used %d\n", this, fPtr, fExternalPtr?" (external)":"", fTotalSize, fSize); fflush(stdout);
920 }
921
922 AliHLTDataBuffer::AliHLTRawPage::AliHLTRawPage(AliHLTUInt32_t pagesize)
923   : fSize(pagesize)
924   , fPtr(static_cast<AliHLTUInt8_t*>(malloc(pagesize)))
925   , fFreeBuffers()
926   , fUsedBuffers()
927 {
928   // constructor
929   if (fPtr) {
930     fFreeBuffers.push_back(new AliHLTRawBuffer(fSize, fPtr));
931   } else {
932     fSize=0;
933   }
934 }
935
936 AliHLTDataBuffer::AliHLTRawPage::~AliHLTRawPage()
937 {
938   // destructor
939   if (fUsedBuffers.size()>0) {
940     // do not free if the resources have not been completely freed
941     HLTError("memory mismatch: not all allocated intances have been released");
942   } else {
943     if (fFreeBuffers.size()>0) {
944       HLTWarning("page still fragmented");
945     }
946     AliHLTRawBufferPList::iterator element=fFreeBuffers.begin();
947     while (element!=fFreeBuffers.end()) {
948       if (*element) delete *element;
949       element=fFreeBuffers.erase(element);
950     }
951     if (fPtr) {
952       free(fPtr);
953     }
954     fPtr=NULL;
955     fSize=0;
956   }
957 }
958
959 AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::AliHLTRawPage::Alloc(AliHLTUInt32_t size)
960 {
961   /// alloc a buffer of specified size
962   if (fFreeBuffers.size()==0) return NULL;
963   
964   for (AliHLTRawBufferPList::iterator iter=fFreeBuffers.begin();
965        iter!=fFreeBuffers.end();
966        iter++) {
967     if ((*iter)->GetTotalSize()>=size) {
968       AliHLTRawBuffer* thisbuffer=*iter;
969       AliHLTRawBuffer* newbuffer=thisbuffer->Split(size);
970       if (newbuffer) {
971         *iter=newbuffer;
972         fUsedBuffers.push_back(thisbuffer);
973         return thisbuffer;
974       } else {
975         HLTWarning("failed to split raw buffer %p", *iter);
976       }
977     }
978   }
979   return NULL;
980 }
981
982 int AliHLTDataBuffer::AliHLTRawPage::Free(AliHLTRawBuffer* pBuffer)
983 {
984   /// free a buffer and merge consecutive free buffers
985   int iResult=0;
986   for (AliHLTRawBufferPList::iterator iter=fUsedBuffers.begin();
987        iter!=fUsedBuffers.end() && iResult>=0;
988        iter++) {
989     if ((*iter)==pBuffer) {
990       fUsedBuffers.erase(iter);
991       AliHLTRawBufferPList::iterator prev=fFreeBuffers.begin();
992       for (; prev!=fFreeBuffers.end() && iResult>=0; prev++) {
993         if ((*pBuffer)<(*(*prev))) {
994           // check consecutive buffers
995           if ((*(*prev)) == (pBuffer->GetPointer()+pBuffer->GetTotalSize())) {
996             // the buffer to be released has a consecutive free buffer -> merge them
997             if ((iResult=pBuffer->Merge(*(*prev)))>=0) {
998               delete *prev;
999               *prev=pBuffer;
1000             }
1001             break;
1002           }
1003           fFreeBuffers.insert(prev, pBuffer);
1004           break;
1005         }
1006         if ((*pBuffer)>(*(*prev))) {
1007           // check consecutive buffers
1008           if ((*pBuffer) == ((*prev)->GetPointer()+(*prev)->GetTotalSize())) {
1009             // the buffer to be released is consecutive to a free buffer -> merge them
1010             if ((iResult=pBuffer->Merge(*(*prev)))>=0) {
1011               AliHLTRawBufferPList::iterator succ=prev+1;
1012               delete *prev;
1013               *prev=pBuffer;
1014               // check if the buffer and the following one are consecutive
1015               if (succ!=fFreeBuffers.end() &&
1016                   (*(*succ)) == (pBuffer->GetPointer()+pBuffer->GetTotalSize())) {
1017                 if ((iResult=pBuffer->Merge(*(*succ)))>=0) {
1018                   delete *succ;
1019                   fFreeBuffers.erase(succ);
1020                 }
1021               }
1022             }
1023             break;
1024           }
1025         }
1026       }
1027       if (prev==fFreeBuffers.end()) {
1028         fFreeBuffers.push_back(pBuffer);
1029       }
1030       // buffer was part of this page
1031       return 0;
1032     }
1033   }
1034   // buffer not found in this page
1035   return 1;
1036 }
1037
1038 int AliHLTDataBuffer::AliHLTRawPage::SetSize(AliHLTDataBuffer::AliHLTRawBuffer* pBuffer, AliHLTUInt32_t size)
1039 {
1040   /// set the size of a raw buffer and release the remaining part
1041   int iResult=0;
1042   for (AliHLTRawBufferPList::iterator iter=fUsedBuffers.begin();
1043        iter!=fUsedBuffers.end() && iResult>=0;
1044        iter++) {
1045     if ((*iter)==pBuffer) {      // buffer was part of this page
1046       AliHLTDataBuffer::AliHLTRawBuffer* freespace=(*iter)->Split(size);
1047       if (freespace) {
1048         fUsedBuffers.push_back(freespace);
1049         Free(freespace);
1050       } else {
1051         HLTWarning("failed to split raw buffer %p in order to relase unused memory", *iter);    
1052       }
1053       return 0;
1054     }
1055   }
1056   // buffer not found in this page
1057   return 1;
1058 }
1059
1060 AliHLTUInt32_t AliHLTDataBuffer::AliHLTRawPage::Capacity() const 
1061 {
1062   /// get max available contiguous buffer
1063   AliHLTUInt32_t capacity=0;
1064   for (unsigned i=0; i<fFreeBuffers.size(); i++) {
1065     if (fFreeBuffers[i]->GetTotalSize()>capacity) 
1066       capacity=fFreeBuffers[i]->GetTotalSize();
1067   }
1068   return capacity;
1069 }
1070
1071 void AliHLTDataBuffer::AliHLTRawPage::Print(const char* /*option*/)
1072 {
1073   /// print page information
1074   cout << "************* AliHLTRawPage status ***********" << endl;
1075   printf("  buffer %p  size %d", fPtr, fSize);
1076   cout << "  used buffers: " << fUsedBuffers.size() << endl;
1077   AliHLTRawBufferPList::iterator iter=fUsedBuffers.begin();
1078   for (; iter!=fUsedBuffers.end(); iter++) {
1079     cout << "  "; (*iter)->Print("min");
1080   }
1081   cout << "  free buffers: " << fFreeBuffers.size() << endl;
1082   iter=fFreeBuffers.begin();
1083   for (; iter!=fFreeBuffers.end(); iter++) {
1084     cout << "  "; (*iter)->Print("min");
1085   }
1086 }