]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTDataBuffer.cxx
bugfix: correct range of DDL for specified detector
[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 //  @note   Only used in the AliRoot framework
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 #define USE_ALIHLTRAWPAGE
45
46 typedef vector<AliHLTDataBuffer::AliHLTDataSegment> AliHLTDataSegmentList;
47
48 /** ROOT macro for the implementation of ROOT specific class methods */
49 ClassImp(AliHLTDataBuffer)
50
51 AliHLTDataBuffer::AliHLTDataBuffer()
52   :
53   fSegments(),
54   fConsumers(),
55   fActiveConsumers(),
56   fReleasedConsumers(),
57   fpBuffer(NULL),
58   fFlags(0),
59   fForwardedSegmentSources(),
60   fForwardedSegments()
61 {
62   // see header file for class documentation
63   // or
64   // refer to README to build package
65   // or
66   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
67   fSegments.empty();
68   fConsumers.empty();
69   fActiveConsumers.empty();
70   fReleasedConsumers.empty();
71   fgNofInstances++;
72 }
73
74 int AliHLTDataBuffer::fgNofInstances=0;
75 AliHLTDataBuffer::AliHLTRawBufferPList AliHLTDataBuffer::fgFreeBuffers;
76 AliHLTDataBuffer::AliHLTRawBufferPList AliHLTDataBuffer::fgActiveBuffers;
77 AliHLTUInt32_t AliHLTDataBuffer::fgMargin=1024;
78 AliHLTLogging AliHLTDataBuffer::fgLogging;
79 const Int_t AliHLTDataBuffer::fgkSafetyPatternSize=16;
80 const char AliHLTDataBuffer::fgkSafetyPattern[]={0x28, 0x63, 0x29, 0x4d, 0x52, 0x49, 0x43, 0x48, 0x54, 0x45, 0x52, 0x20, 0x32, 0x30, 0x30, 0x37};
81 AliHLTUInt32_t AliHLTDataBuffer::fgEventCount=0;
82
83 AliHLTDataBuffer::~AliHLTDataBuffer()
84 {
85   // see header file for function documentation
86   CleanupConsumerList();
87
88   if (--fgNofInstances<=0) {
89     DeleteRawBuffers();
90   }
91 }
92
93 int AliHLTDataBuffer::SetConsumer(AliHLTComponent* pConsumer)
94 {
95   // see header file for function documentation
96   int iResult=0;
97   if (pConsumer) {
98     if (FindConsumer(pConsumer)) {
99       HLTWarning("consumer %s (%p) already set to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
100     }
101     AliHLTConsumerDescriptor* pDesc=new AliHLTConsumerDescriptor(pConsumer);
102     if (pDesc) {
103       fConsumers.push_back(pDesc);
104       HLTDebug("set consumer %s (%p) to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
105     } else {
106       HLTError("memory allocation failed");
107       iResult=-ENOMEM;
108     }
109   } else {
110     HLTError("invalid parameter: consumer component (nil)");
111     iResult=-EINVAL;
112   }
113   return iResult;
114 }
115
116 int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList)
117 {
118   // see header file for function documentation
119   int iResult=0;
120   if (pConsumer) {
121     AliHLTDataSegmentList segments;
122     if ((iResult=FindMatchingDataSegments(pConsumer, segments))>=0) {
123       if (tgtList) {
124         AliHLTDataSegmentList::iterator segment=segments.begin();
125         while (segment!=segments.end()) {
126           tgtList->push_back((*segment).fDataType);
127           segment++;
128         }
129       }
130       iResult=segments.size();
131     }
132   } else {
133     iResult=-EINVAL;
134   }
135   return iResult;
136 }
137
138 int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* /*pConsumer*/,
139                                                vector<AliHLTDataBuffer::AliHLTDataSegment>& tgtList)
140 {
141   // see header file for function documentation
142   int iResult=0;
143
144   // Matthias 26.09.2007 relax the restriction to matching data blocks
145   // all blocks are passed to the consumer, which is the policy also in
146   // PubSub
147   tgtList.assign(fSegments.begin(), fSegments.end());
148
149   // add all forwarded blocks
150   tgtList.insert(tgtList.begin(), fForwardedSegments.begin(), fForwardedSegments.end());
151   iResult=tgtList.size();
152   return iResult;
153 }
154
155 int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponentBlockDataList& blockDescList)
156 {
157   // see header file for function documentation
158   int iResult=0;
159   if (pConsumer) {
160     if (1/*fpBuffer*/) {
161       AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fConsumers);
162       if (pDesc) {
163         AliHLTDataSegmentList tgtList;
164         // Matthias 26.07.2007 AliHLTSystem should behave the same way as PubSub
165         // so it does not matter if there are matching data types or not, unless
166         // we implement such a check in PubSub
167         if ((iResult=FindMatchingDataSegments(pConsumer, tgtList))>=0) {
168           AliHLTDataSegmentList::iterator segment=tgtList.begin();
169           while (segment!=tgtList.end()) {
170             // fill the block data descriptor
171             AliHLTComponentBlockData bd;
172             AliHLTComponent::FillBlockData(bd);
173             // This models the behavior of PubSub.
174             // For incoming data blocks, fOffset must be ignored by the
175             // processing component. It is set for bookkeeping in the framework.
176             // fPtr always points to the beginning of the data.
177             bd.fOffset=0;
178             AliHLTUInt8_t* pTgt=*segment;
179             bd.fPtr=reinterpret_cast<void*>(pTgt);
180             bd.fSize=(*segment).fSegmentSize;
181             bd.fDataType=(*segment).fDataType;
182             bd.fSpecification=(*segment).fSpecification;
183             blockDescList.push_back(bd);
184             pDesc->SetActiveDataSegment(*segment);
185             HLTDebug("component %p (%s) subscribed to segment offset %d size %d data type %s %#x", 
186                      pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), bd.fOffset,
187                      bd.fSize, (AliHLTComponent::DataType2Text(bd.fDataType)).c_str(), 
188                      bd.fSpecification);
189             segment++;
190           }
191           // move this consumer to the active list
192           if (tgtList.size()==0) {
193             ChangeConsumerState(pDesc, fConsumers, fReleasedConsumers);
194             HLTDebug("no input data for component %p (%s) available", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
195           } else if (ChangeConsumerState(pDesc, fConsumers, fActiveConsumers)>=0) {
196             HLTDebug("component %p (%s) subscribed to data buffer %p", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), this);
197           } else {
198             // TODO: cleanup the consumer descriptor correctly
199             segment=tgtList.begin();
200             while (segment!=tgtList.end()) {
201               blockDescList.pop_back();
202               segment++;
203             }
204             HLTError("can not activate consumer %p for data buffer %p", pConsumer, this);
205             iResult=-EACCES;
206           }
207         } else {
208           HLTError("unresolved data segment(s) for component %p (%s)", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
209           iResult=-EBADF;
210         }
211       } else {
212         if (!FindConsumer(pConsumer)) {
213           HLTError("component %p is not a data consumer of data buffer %p", pConsumer, this);
214         } else {
215           HLTError("component %p is a valid data consumer of data buffer %p, but did not release it's buffer subscription", pConsumer, this);
216         }
217         iResult=-ENOENT;
218       }
219     } else {
220       // Matthias 26.07.2007 until now, data had to be present for successful subscription
221       // in order to be consistent with the PubSub framework, this restiction has been
222       // removed
223       //HLTError("data buffer %p is empty", this);
224       //iResult=-ENODATA;
225     }
226   } else {
227     HLTError("invalid parameter");
228     iResult=-EINVAL;
229   }
230   return iResult;
231 }
232
233 int AliHLTDataBuffer::Release(AliHLTComponentBlockData* pBlockDesc,
234                               const AliHLTComponent* pConsumer,
235                               const AliHLTTask* pOwnerTask)
236 {
237   // see header file for function documentation
238   int iResult=0;
239   if (pBlockDesc && pConsumer) {
240     AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fActiveConsumers);
241     if (pDesc) {
242       if ((iResult=pDesc->CheckActiveDataSegment(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize)))!=1) {
243         HLTWarning("data segment mismatch, component %p has not subscribed to a segment with offset %#x and size %d", pConsumer, pBlockDesc->fOffset, pBlockDesc->fSize);
244         // TODO: appropriate error handling, but so far optional
245         iResult=0;
246       } else {
247         pDesc->ReleaseActiveDataSegment(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize));
248       }
249       if (GetNofPendingConsumers()==0 && fForwardedSegments.size()>0) {
250         // last consumer, release forwarded segments
251         ReleaseForwardedBlock(pBlockDesc, pOwnerTask);
252       }
253       pBlockDesc->fOffset=0;
254       pBlockDesc->fPtr=NULL;
255       pBlockDesc->fSize=0;
256       if (pDesc->GetNofActiveSegments()==0) {
257         if ((iResult=ChangeConsumerState(pDesc, fActiveConsumers, fReleasedConsumers))>=0) {
258           if (GetNofActiveConsumers()==0 && GetNofPendingConsumers()==0) {
259             // this is the last consumer, reset the consumer list and release the raw buffer
260             ResetDataBuffer();
261           }
262         } else {
263           HLTError("can not deactivate consumer %p for data buffer %p", pConsumer, this);
264           iResult=-EACCES;
265         }
266       }
267     } else {
268       HLTWarning("component %p has currently not subscribed to the data buffer %p", pConsumer, this);
269       iResult=-ENOENT;
270     }
271   } else {
272     HLTError("inavalid parameter: pBlockDesc=%p pConsumer=%p", pBlockDesc, pConsumer);
273     iResult=-EINVAL;
274   }
275   return iResult;
276 }
277
278 int AliHLTDataBuffer::ReleaseForwardedBlock(AliHLTComponentBlockData* pBlockDesc,
279                                             const AliHLTTask* pOwnerTask)
280 {
281   // see header file for function documentation
282   int iResult=0;
283   if (pBlockDesc && pOwnerTask) {
284         assert(fForwardedSegments.size()==fForwardedSegmentSources.size());
285         AliHLTDataSegmentList::iterator segment=fForwardedSegments.begin();
286         AliHLTTaskPList::iterator src=fForwardedSegmentSources.begin();
287         //HLTDebug("%p checking forwarded segments", this);
288         for (; segment!=fForwardedSegments.end(); segment++, src++) {
289           //HLTDebug("segment ptr=%p offset=%d size=%d\n"
290           //   "block ptr=%p offset=%d size=%d", (*segment).fPtr, (*segment).fSegmentOffset, (*segment).fSegmentSize, pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize);
291           if ((*segment)==AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize)) {
292             //HLTDebug("release segment of task %p", *src);
293             assert((*src)!=NULL);
294             if ((*src)!=NULL) {
295               if ((*src)->Release(pBlockDesc, pOwnerTask)>=0) {
296                 HLTDebug("task %s (%p) released forwarded segment %p size %d of task %s (%p)",
297                          pOwnerTask->GetName(), pOwnerTask, (*segment).GetPtr(), (*segment).GetSize(),
298                          (*src)->GetName(), *src);
299               } else {
300                 HLTError("task %s (%p) failed releasing forwarded segment %p size %d of task %s (%p)",
301                          pOwnerTask->GetName(), pOwnerTask, (*segment).GetPtr(), (*segment).GetSize(),
302                          (*src)->GetName(), *src);
303               }
304             }
305             fForwardedSegments.erase(segment);
306             fForwardedSegmentSources.erase(src);
307             break;
308           }
309         }
310   } else {
311     HLTError("inavalid parameter: pBlockDesc=%p pOwnerTask=%p", pBlockDesc, pOwnerTask);
312     iResult=-EINVAL;
313   }
314   return iResult;
315 }
316
317 int AliHLTDataBuffer::Forward(AliHLTTask* pSrcTask, AliHLTComponentBlockData* pBlockDesc)
318 {
319   // see header file for function documentation
320   if (pSrcTask==NULL || pBlockDesc==NULL) return -EINVAL;
321   assert(fForwardedSegments.size()==fForwardedSegmentSources.size());
322   if (fForwardedSegments.size()!=fForwardedSegmentSources.size()) return -EFAULT;
323   fForwardedSegmentSources.push_back(pSrcTask);
324   fForwardedSegments.push_back(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize, pBlockDesc->fDataType, pBlockDesc->fSpecification));
325   return 0;
326 }
327
328 AliHLTUInt8_t* AliHLTDataBuffer::GetTargetBuffer(int iMinSize)
329 {
330   // see header file for function documentation
331   AliHLTUInt8_t* pTargetBuffer=NULL;
332   if (fpBuffer!=NULL) {
333     HLTWarning("data buffer not properly reset, possible memory leak\n");
334   }
335   fpBuffer=CreateRawBuffer(iMinSize);
336   if (fpBuffer) {
337     pTargetBuffer=*fpBuffer;
338   } else {
339     HLTError("can not create raw buffer");
340   }
341   return pTargetBuffer;
342 }
343
344 int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData* arrayBlockData, int iSize)
345 {
346   // see header file for function documentation
347   int iResult=0;
348   if (pTgt && arrayBlockData && iSize>=0) {
349     if (fpBuffer) {
350       if (*fpBuffer==pTgt) {
351         AliHLTDataBuffer::AliHLTDataSegment segment;
352         AliHLTUInt32_t maxSize=0;
353         for (int i=0; i<iSize; i++) {
354           // This function has to model the behavior of PubSub
355           // For output blocks only the fOffset value is used, this must be the offset
356           // relative to the output pointer. fPtr must be either NULL or the output
357           // pointer. In either case it is 'ignored' and set to the beginning of the
358           // data buffer
359           if (arrayBlockData[i].fPtr==NULL ||
360               arrayBlockData[i].fPtr==*fpBuffer) {
361             arrayBlockData[i].fPtr=*fpBuffer;
362             if ((arrayBlockData[i].fOffset+arrayBlockData[i].fSize<=fpBuffer->GetUsedSize()) ||
363                 ((arrayBlockData[i].fOffset==~(AliHLTUInt32_t)0) && arrayBlockData[i].fSize==0)) {
364               segment.fSegmentOffset=arrayBlockData[i].fOffset;
365               segment.fPtr=(AliHLTUInt8_t*)arrayBlockData[i].fPtr;
366               segment.fSegmentSize=arrayBlockData[i].fSize;
367               segment.fDataType=arrayBlockData[i].fDataType;
368               segment.fSpecification=arrayBlockData[i].fSpecification;
369               fSegments.push_back(segment);
370               HLTDebug("set segment %s with size %d at offset %d", AliHLTComponent::DataType2Text(segment.fDataType).data(), segment.fSegmentSize, segment.fSegmentOffset);
371
372               // find the actual size of the data
373               if ((arrayBlockData[i].fOffset!=~(AliHLTUInt32_t)0) &&
374                   arrayBlockData[i].fOffset+arrayBlockData[i].fSize>maxSize) {
375                 maxSize=arrayBlockData[i].fOffset+arrayBlockData[i].fSize;
376               }
377             } else {
378               HLTError("block data specification %#d (%s) exceeds size of data buffer", i, AliHLTComponent::DataType2Text(arrayBlockData[i].fDataType).data());
379               HLTError("block offset=%d, block size=%d, buffer size=%d", arrayBlockData[i].fOffset, arrayBlockData[i].fSize, fpBuffer->GetUsedSize());
380               iResult=-E2BIG;
381             }
382           } else {
383             HLTError("invalid pointer (%p) in block data specification (buffer %p size %d)."
384                      "please note: for output blocks only the fOffset value is valid and must "
385                      "be relative to the output buffer", arrayBlockData[i].fPtr, fpBuffer->GetPointer(), fpBuffer->GetUsedSize());
386             iResult=-ERANGE;
387           }
388         }
389         // to be enabled if unit test is ready
390 #ifdef USE_ALIHLTRAWPAGE
391         iResult=SetRawBufferDataSize(fpBuffer, maxSize);        
392 #endif //USE_ALIHLTRAWPAGE
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() const
416 {
417   // see header file for function documentation
418   int iResult=fSegments.size() + fForwardedSegments.size();
419   return iResult;
420 }
421
422 int AliHLTDataBuffer::GetNofConsumers() const
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() const
430 {
431   // see header file for function documentation
432   int iResult=fConsumers.size();
433   return iResult;
434 }
435
436 int AliHLTDataBuffer::GetNofActiveConsumers() const
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 #ifndef USE_ALIHLTRAWPAGE
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 #else
480   pRawBuffer=AliHLTDataBuffer::AliHLTRawPage::GlobalAlloc(reqSize);
481   if (pRawBuffer) {
482     pRawBuffer->UseBuffer(size);
483   }
484 #endif
485   if (pRawBuffer!=NULL && fgkSafetyPatternSize>0) {
486     //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "writing safety pattern to %p offset %d", (*buffer)->GetPointer(), (*buffer)->GetUsedSize());
487     pRawBuffer->WritePattern(fgkSafetyPattern, fgkSafetyPatternSize);
488   }
489   return pRawBuffer;
490 }
491
492 int AliHLTDataBuffer::SetRawBufferDataSize(AliHLTRawBuffer* pBuffer, AliHLTUInt32_t size) const
493 {
494   // see header file for function documentation
495   int iResult=0;
496   if (!pBuffer) return -EINVAL;
497   if (size>pBuffer->GetUsedSize()) {
498     HLTError("indicated data size %d exceeds data buffer %p (%d)", size, pBuffer->GetPointer(), pBuffer->GetUsedSize());
499     return -ENOSPC;
500   }
501   if (fgkSafetyPatternSize>0) {
502     if (pBuffer->CheckPattern(fgkSafetyPattern, fgkSafetyPatternSize)) {
503       HLTError("potential memory corruption: component has written beyond end of data buffer %p size %d", pBuffer->GetPointer(), pBuffer->GetUsedSize());
504     }
505   }
506   // shrink the buffer and write new pattern at the end
507 #ifdef USE_ALIHLTRAWPAGE
508   AliHLTDataBuffer::AliHLTRawPage* rawpage=AliHLTDataBuffer::AliHLTRawPage::FindPage(pBuffer);
509   if (rawpage) {
510     pBuffer->UseBuffer(size);
511     if (rawpage->SetSize(pBuffer, size+fgkSafetyPatternSize)==0) {
512       // nothing to do
513     } else {
514       fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::SetRawBufferDataSize", "data buffer handling", "failed to set size for raw buffer %p", pBuffer);
515       iResult=-EFAULT;
516     }
517   } else {
518     fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::SetRawBufferDataSize", "data buffer handling", "can not find raw page for buffer %p", pBuffer);
519     iResult=-ENOENT;
520   }
521 #else //!USE_ALIHLTRAWPAGE
522   pBuffer->UseBuffer(size);
523 #endif //USE_ALIHLTRAWPAGE
524   if (fgkSafetyPatternSize>0) {
525     pBuffer->WritePattern(fgkSafetyPattern, fgkSafetyPatternSize);
526   }
527   return iResult;
528 }
529
530 int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
531 {
532   // see header file for function documentation
533   int iResult=0;
534   if (pBuffer) {
535 #ifdef USE_ALIHLTRAWPAGE
536     AliHLTDataBuffer::AliHLTRawPage* rawpage=AliHLTDataBuffer::AliHLTRawPage::FindPage(pBuffer);
537     if (rawpage)
538 #else //!USE_ALIHLTRAWPAGE
539     AliHLTRawBufferPList::iterator buffer=fgActiveBuffers.begin();
540     while (buffer!=fgActiveBuffers.end() && (*buffer)!=pBuffer) {
541       buffer++;
542     }
543     if (buffer!=fgActiveBuffers.end())
544 #endif //USE_ALIHLTRAWPAGE
545     {
546       if (fgkSafetyPatternSize>0) {
547         //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "comparing safety pattern at %p offset %d", pBuffer->GetPointer(), reinterpret_cast<AliHLTUInt32_t>(pBuffer));
548         if ((pBuffer)->CheckPattern(fgkSafetyPattern, fgkSafetyPatternSize)) {
549           fgLogging.Logging(kHLTLogFatal, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "component has written beyond end of data buffer %p size %d", pBuffer->GetPointer(), pBuffer->GetUsedSize());
550         }
551       }
552       pBuffer->Reset();
553 #ifdef USE_ALIHLTRAWPAGE
554       if (rawpage->Free(pBuffer)==0) {
555       } else {
556         fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "failed to release raw buffer %p", pBuffer);
557       }
558 #else //!USE_ALIHLTRAWPAGE
559       fgFreeBuffers.push_back(pBuffer);
560       fgActiveBuffers.erase(buffer);
561 #endif //USE_ALIHLTRAWPAGE
562     } else {
563 #ifdef USE_ALIHLTRAWPAGE
564       fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw page for buffer %p", pBuffer);
565 #else //!USE_ALIHLTRAWPAGE
566       fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
567 #endif //USE_ALIHLTRAWPAGE
568       iResult=-ENOENT;
569     }
570   } else {
571     fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "invalid parameter");
572     iResult=-EINVAL;
573   }
574   return iResult;
575 }
576
577
578 int AliHLTDataBuffer::DeleteRawBuffers() 
579 {
580   // see header file for function documentation
581   int iResult=0;
582 #ifdef ALIHLTSYSTEM_PROFILING
583   int iTotalSize=0;
584   int iCount=fgFreeBuffers.size()+fgActiveBuffers.size();
585 #endif //ALIHLTSYSTEM_PROFILING
586   AliHLTRawBufferPList::iterator buffer;;
587   while ((buffer=fgFreeBuffers.begin())!=fgFreeBuffers.end()) {
588 #ifdef ALIHLTSYSTEM_PROFILING
589     iTotalSize+=(*buffer)->GetTotalSize();
590 #endif //ALIHLTSYSTEM_PROFILING
591     delete *buffer;
592     fgFreeBuffers.erase(buffer);
593   }
594   while ((buffer=fgActiveBuffers.begin())!=fgActiveBuffers.end()) {
595 #ifdef ALIHLTSYSTEM_PROFILING
596     iTotalSize+=(*buffer)->GetTotalSize();
597 #endif //ALIHLTSYSTEM_PROFILING
598     fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::DeleteRawBuffer", "data buffer handling", "request to delete active raw buffer container (raw buffer %p, size %d)", (*buffer)->GetPointer(), (*buffer)->GetTotalSize());
599     delete *buffer;
600     fgActiveBuffers.erase(buffer);
601   }
602 #ifdef ALIHLTSYSTEM_PROFILING
603   fgLogging.Logging(kHLTLogImportant, "AliHLTDataBuffer::DeleteRawBuffer", "data buffer handling", "Total memory allocation: %d byte in %d buffers", iTotalSize, iCount);
604 #endif //ALIHLTSYSTEM_PROFILING
605   return iResult;
606 }
607
608 int AliHLTDataBuffer::PrintStatistics() 
609 {
610   // see header file for function documentation
611   int iResult=0;
612 #ifdef USE_ALIHLTRAWPAGE
613   int nofPages=0;
614   AliHLTUInt32_t totalSize=0;
615   for (AliHLTDataBuffer::AliHLTRawPage* rawpage=AliHLTDataBuffer::AliHLTRawPage::NextPage(NULL);
616        rawpage!=NULL; 
617        rawpage=AliHLTDataBuffer::AliHLTRawPage::NextPage(rawpage)) {
618     nofPages++;
619     totalSize+=rawpage->Size();
620     if (fgLogging.CheckFilter(kHLTLogDebug)) rawpage->Print("");
621   }
622   //if (rawpage) rawpage->Print("global");
623   fgLogging.Logging(kHLTLogInfo, "AliHLTDataBuffer::PrintStatistics", "data buffer handling", "total number of memory pages: %d   total size %d", nofPages, totalSize);
624
625 #else //! USE_ALIHLTRAWPAGE
626   int iFree=0;
627   int iActive=0;
628   AliHLTRawBufferPList::iterator buffer;;
629   for (buffer=fgFreeBuffers.begin(); buffer!=fgFreeBuffers.end(); buffer++) {
630     iFree+=(*buffer)->GetTotalSize();
631   }
632   for (buffer=fgActiveBuffers.begin(); buffer!=fgActiveBuffers.end(); buffer++) {
633     iActive+=(*buffer)->GetTotalSize();
634   }
635   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);
636 #endif // USE_ALIHLTRAWPAGE
637   return iResult;
638 }
639
640 AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, AliHLTConsumerDescriptorPList &list) const
641 {
642   // see header file for function documentation
643   AliHLTConsumerDescriptor* pDesc=NULL;
644   AliHLTConsumerDescriptorPList::iterator desc=list.begin();
645   while (desc!=list.end() && pDesc==NULL) {
646     if ((pConsumer==NULL || (*desc)->GetComponent()==pConsumer)) {
647       pDesc=*desc;
648     }
649     desc++;
650   }
651   return pDesc;
652 }
653
654 int AliHLTDataBuffer::ResetDataBuffer() 
655 {
656   // see header file for function documentation
657   int iResult=0;
658   AliHLTRawBuffer* pBuffer=fpBuffer;
659   fpBuffer=NULL;
660
661   // cleanup forwarded segment lists
662   assert(fForwardedSegments.size()==0);
663   fForwardedSegments.clear();
664   fForwardedSegmentSources.clear();
665
666   // cleanup consumer states
667   AliHLTConsumerDescriptorPList::iterator desc;
668 //   if (GetNofPendingConsumers()>0) {
669 //     desc=fConsumers.begin();
670 //     while (desc!=fConsumers.end()) {
671 //       AliHLTComponent* pComp=(*desc)->GetComponent();
672 //       HLTError("internal error: consumer %p (%s %p) did not get data from data buffer %p", *desc, pComp?pComp->GetComponentID():"", pComp, this);
673 //       desc++;
674 //     }
675 //   }
676   desc=fReleasedConsumers.begin();
677   while (desc!=fReleasedConsumers.end()) {
678     AliHLTConsumerDescriptor* pDesc=*desc;
679     fReleasedConsumers.erase(desc);
680     desc=fReleasedConsumers.begin();
681     fConsumers.push_back(pDesc);
682   }
683   desc=fActiveConsumers.begin();
684   while (desc!=fActiveConsumers.end()) {
685     AliHLTConsumerDescriptor* pDesc=*desc;
686     HLTWarning("consumer %p (%s) was not released", pDesc, pDesc->GetComponent()?pDesc->GetComponent()->GetComponentID():"### invalid component ###");
687     fActiveConsumers.erase(desc);
688     desc=fActiveConsumers.begin();
689     fConsumers.push_back(pDesc);
690   }
691
692   // cleanup segments
693   AliHLTDataSegmentList::iterator segment=fSegments.begin();
694   while (segment!=fSegments.end()) {
695     fSegments.erase(segment);
696     segment=fSegments.begin();
697   }
698
699   // cleanup raw buffer
700   if (pBuffer) {
701     ReleaseRawBuffer(pBuffer);
702   }
703   return iResult;
704 }
705
706 int AliHLTDataBuffer::Reset()
707 {
708   // see header file for function documentation
709   return ResetDataBuffer();
710 }
711
712 // this is the version which works on lists of components instead of consumer descriptors
713 // int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, AliHLTComponentPList &srcList, AliHLTComponentPList &tgtList)
714 // {
715 //   int iResult=0;
716 //   if (pDesc) {
717 //     AliHLTComponentPList::iterator desc=srcList.begin();
718 //     while (desc!=srcList.end()) {
719 //       if ((*desc)==pConsumer) {
720 //      srcList.erase(desc);
721 //      tgtList.push_back(pConsumer);
722 //      break;
723 //       }
724 //      desc++;
725 //     }
726 //     if (desc==srcList.end()) {
727 //       HLTError("can not find consumer component %p in list", pConsumer);
728 //       iResult=-ENOENT;
729 //     }
730 //   } else {
731 //     HLTError("invalid parameter");
732 //     iResult=-EINVAL;
733 //   }
734 //   return iResult;
735 // }
736
737 int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, AliHLTConsumerDescriptorPList &srcList, AliHLTConsumerDescriptorPList &tgtList)
738 {
739   // see header file for function documentation
740   int iResult=-ENOENT;
741   if (pDesc) {
742     AliHLTConsumerDescriptorPList::iterator desc=srcList.begin();
743     while (desc!=srcList.end()) {
744       if ((*desc)==pDesc) {
745         srcList.erase(desc);
746         tgtList.push_back(pDesc);
747         iResult=0;
748         break;
749       }
750       desc++;
751     }
752     if (iResult<0) {
753       HLTError("can not find consumer descriptor %p in list", pDesc);
754     }
755   } else {
756     HLTError("invalid parameter");
757     iResult=-EINVAL;
758   }
759   return iResult;
760 }
761
762 int AliHLTDataBuffer::CleanupConsumerList() 
763 {
764   // see header file for function documentation
765   int iResult=0;
766   ResetDataBuffer();
767   AliHLTConsumerDescriptorPList::iterator desc=fConsumers.begin();
768   while (desc!=fConsumers.end()) {
769     delete *desc;
770     fConsumers.erase(desc);
771     desc=fConsumers.begin();
772   }
773   return iResult;
774 }
775
776 int AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, int bAllLists)
777 {
778   // see header file for function documentation
779   AliHLTConsumerDescriptorPList::iterator desc=fConsumers.begin();
780   while (desc!=fConsumers.end()) {
781     if ((*desc)->GetComponent()==pConsumer)
782       return 1;
783     desc++;
784   }
785   if (bAllLists==0) return 0;
786
787   desc=fActiveConsumers.begin();
788   while (desc!=fActiveConsumers.end()) {
789     if ((*desc)->GetComponent()==pConsumer)
790       return 1;
791     desc++;
792   }
793   desc=fReleasedConsumers.begin();
794   while (desc!=fReleasedConsumers.end()) {
795     if ((*desc)->GetComponent()==pConsumer)
796       return 1;
797     desc++;
798   }
799   return 0;
800 }
801
802 AliHLTDataBuffer::AliHLTRawBuffer::AliHLTRawBuffer(AliHLTUInt32_t size)
803   : fSize(0)
804   , fTotalSize(size)
805   , fExternalPtr(NULL)
806   , fPtr(static_cast<AliHLTUInt8_t*>(malloc(size)))
807   , fLastEventCount(0)
808 {
809   // see header file for class documentation
810   // or
811   // refer to README to build package
812   // or
813   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
814   if (fPtr==NULL) {
815     fSize=0;
816     fTotalSize=0;
817   }
818 }
819
820 AliHLTDataBuffer::AliHLTRawBuffer::AliHLTRawBuffer(AliHLTUInt32_t size, AliHLTUInt8_t* buffer)
821   : fSize(0)
822   , fTotalSize(size)
823   , fExternalPtr(buffer)
824   , fPtr(fExternalPtr)
825   , fLastEventCount(0)
826 {
827   // see header file for class documentation
828 }
829
830 AliHLTDataBuffer::AliHLTRawBuffer::~AliHLTRawBuffer()
831 {
832   // see header file for class documentation
833   if (fExternalPtr==NULL && fPtr) {
834     free(fPtr);
835   }
836   fPtr=NULL;
837   fSize=0;
838   fTotalSize=0;
839 }
840
841 int AliHLTDataBuffer::AliHLTRawBuffer::operator==(void* ptr) const
842 {
843   // see header file for function documentation
844   return fPtr == static_cast<AliHLTUInt8_t*>(ptr);
845 }
846
847 int AliHLTDataBuffer::AliHLTRawBuffer::operator<(void* ptr) const
848 {
849   // see header file for function documentation
850   int iResult=fPtr < static_cast<AliHLTUInt8_t*>(ptr);
851   //printf("%p: %p <= %p (%d)\n", this, fPtr, ptr, iResult);
852   return iResult;
853 }
854
855 int AliHLTDataBuffer::AliHLTRawBuffer::operator<=(void* ptr) const
856 {
857   // see header file for function documentation
858   int iResult=fPtr <= static_cast<AliHLTUInt8_t*>(ptr);
859   //printf("%p: %p <= %p (%d)\n", this, fPtr, ptr, iResult);
860   return iResult;
861 }
862
863 int AliHLTDataBuffer::AliHLTRawBuffer::operator>(void* ptr) const
864 {
865   // see header file for function documentation
866   int iResult=fPtr+fSize > static_cast<AliHLTUInt8_t*>(ptr);
867   //printf("%p: %p + %d > %p (%d)\n", this, fPtr, fSize, ptr, iResult);
868   return iResult;
869 }
870
871 int AliHLTDataBuffer::AliHLTRawBuffer::operator-(void* ptr) const
872 {
873   // see header file for function documentation
874   return static_cast<int>(static_cast<AliHLTUInt8_t*>(ptr)-fPtr);
875 }
876
877 int AliHLTDataBuffer::AliHLTRawBuffer::operator<(const AliHLTRawBuffer& op) const
878 {
879   // see header file for function documentation
880   return (fPtr+fSize < op.fPtr);
881 }
882
883 int AliHLTDataBuffer::AliHLTRawBuffer::operator<=(const AliHLTRawBuffer& op) const
884 {
885   // see header file for function documentation
886   return (fPtr+fSize <= op.fPtr);
887 }
888
889 int AliHLTDataBuffer::AliHLTRawBuffer::operator>(const AliHLTRawBuffer& op) const
890 {
891   // see header file for function documentation
892   return (fPtr >= op.fPtr+op.fSize);
893 }
894
895 AliHLTUInt8_t* AliHLTDataBuffer::AliHLTRawBuffer::UseBuffer(AliHLTUInt32_t size)
896 {
897   // mark a portion of the buffer as used
898   if (fTotalSize>=size) {
899     fSize=size;
900     fLastEventCount=AliHLTDataBuffer::fgEventCount;
901     // only return pointer if there is a portion of the buffer used
902     if (size>0) return fPtr;
903   }
904   return NULL;
905 }
906
907 AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::AliHLTRawBuffer::Split(AliHLTUInt32_t size)
908 {
909   // split a buffer at specified size
910   // only possible for buffers with external memory
911   if (fTotalSize>size && 
912       (fSize==0 || fSize<=size) && // used size must fit into the first part
913       fExternalPtr!=NULL) {
914     AliHLTRawBuffer* part2=new AliHLTRawBuffer(fTotalSize-size, fPtr+size);
915     if (part2) {
916       fTotalSize=size;
917     }
918     return part2;
919   } else {
920     //cout << "can not split fTotalSize=" << fTotalSize << "  fSize=" << fSize << "  at size=" << size << endl; 
921   }
922   return NULL;
923 }
924
925 int AliHLTDataBuffer::AliHLTRawBuffer::CheckSize(AliHLTUInt32_t size) const
926 {
927   // see header file for function documentation
928   if (fTotalSize<size) return 0;
929   unsigned adjust=0;
930   if (fLastEventCount+1<AliHLTDataBuffer::fgEventCount) {
931     adjust=AliHLTDataBuffer::fgEventCount-fLastEventCount;
932   }
933   return (adjust>2) || ((fTotalSize-size)<(fgMargin<<adjust));
934 }
935
936 int AliHLTDataBuffer::AliHLTRawBuffer::Reset()
937 {
938   // see header file for function documentation
939   fSize=0;
940   return 0;
941 }
942
943 int AliHLTDataBuffer::AliHLTRawBuffer::WritePattern(const char* pattern, int size)
944 {
945   // see header file for function documentation
946   int iResult=0;
947   if (pattern!=NULL && size>0) {
948     if (fSize+size<=fTotalSize) {
949       memcpy(((char*)fPtr)+fSize, pattern, size);
950       iResult=size;
951     } else {
952       iResult=-ENOSPC;
953     }
954   }
955   return iResult;
956 }
957
958 int AliHLTDataBuffer::AliHLTRawBuffer::CheckPattern(const char* pattern, int size) const
959 {
960   // see header file for function documentation
961   int iResult=0;
962   if (pattern!=NULL && size>0) {
963     if (fSize+size<=fTotalSize) {
964       iResult=memcmp(((char*)fPtr)+fSize, pattern, size)!=0;
965     } else {
966       iResult=-ENOSPC;
967     }
968   }
969   return iResult;
970 }
971
972 int AliHLTDataBuffer::AliHLTRawBuffer::Merge(const AliHLTDataBuffer::AliHLTRawBuffer& neighbor)
973 {
974   // Merge buffer with neighboring buffer.
975   // Only possible if the buffers are consecutive with out any gap.
976
977   if (!fExternalPtr || !neighbor.fExternalPtr) return -EPERM;
978
979   if (neighbor.fTotalSize==0 &&
980       fPtr < neighbor.fPtr &&
981       fPtr+fTotalSize > neighbor.fPtr) {
982     // special case for a buffer of zero size embedded into this buffer
983     // nothing to do
984     return 0;
985   }
986   if (fTotalSize==0 &&
987       neighbor.fPtr < fPtr &&
988       neighbor.fPtr+neighbor.fTotalSize > fPtr) {
989     // special case for this buffer of size zero embedded into another buffer
990     fPtr=neighbor.fPtr;
991     fExternalPtr=fPtr;
992     fTotalSize+=neighbor.fTotalSize;
993     fSize=0;
994     return 0;
995   }
996   if (fPtr+fTotalSize == neighbor.fPtr) {
997     fTotalSize+=neighbor.fTotalSize;
998     fSize=0;
999     return 0;
1000   }
1001   if (fPtr == neighbor.fPtr+neighbor.fTotalSize) {
1002     fPtr=neighbor.fPtr;
1003     fExternalPtr=fPtr;
1004     fTotalSize+=neighbor.fTotalSize;
1005     fSize=0;
1006     return 0;
1007   }
1008   return -EINVAL;
1009 }
1010
1011 void AliHLTDataBuffer::AliHLTRawBuffer::Print(const char* option) const
1012 {
1013   /// print buffer information
1014   if (strcmp(option, "min")!=0) {
1015     cout << "************* AliHLTRawBuffer status ***********" << endl;
1016   }
1017   printf("  %p: buffer %p%s size %d used %d\n", this, fPtr, fExternalPtr?" (external)":"", fTotalSize, fSize); fflush(stdout);
1018 }
1019
1020 AliHLTDataBuffer::AliHLTRawPage::AliHLTRawPage(AliHLTUInt32_t pagesize)
1021   : fSize(pagesize)
1022   , fPtr(static_cast<AliHLTUInt8_t*>(malloc(pagesize)))
1023   , fFreeBuffers()
1024   , fUsedBuffers()
1025 {
1026   // constructor
1027   if (fPtr) {
1028     fFreeBuffers.push_back(new AliHLTRawBuffer(fSize, fPtr));
1029   } else {
1030     fSize=0;
1031   }
1032 }
1033
1034 AliHLTDataBuffer::AliHLTRawPage::~AliHLTRawPage()
1035 {
1036   // destructor
1037   if (IsUsed()) {
1038     // do not free if the resources have not been completely freed
1039     HLTError("memory mismatch: not all allocated intances have been released");
1040   } else {
1041     if (IsFragmented()) {
1042       HLTWarning("page still fragmented");
1043     }
1044     AliHLTRawBufferPList::iterator element=fFreeBuffers.begin();
1045     while (element!=fFreeBuffers.end()) {
1046       if (*element) delete *element;
1047       element=fFreeBuffers.erase(element);
1048     }
1049     if (fPtr) {
1050       free(fPtr);
1051     }
1052     fPtr=NULL;
1053     fSize=0;
1054   }
1055 }
1056
1057 AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::AliHLTRawPage::Alloc(AliHLTUInt32_t size)
1058 {
1059   /// alloc a buffer of specified size
1060   if (fFreeBuffers.size()==0) return NULL;
1061   
1062   for (AliHLTRawBufferPList::iterator iter=fFreeBuffers.begin();
1063        iter!=fFreeBuffers.end();
1064        iter++) {
1065     if ((*iter)->GetTotalSize()==size) {
1066       AliHLTRawBuffer* thisbuffer=*iter;
1067       fFreeBuffers.erase(iter);
1068       fUsedBuffers.push_back(thisbuffer);
1069       return thisbuffer;
1070     } else if ((*iter)->GetTotalSize()>size) {
1071       AliHLTRawBuffer* thisbuffer=*iter;
1072       AliHLTRawBuffer* newbuffer=thisbuffer->Split(size);
1073       if (newbuffer) {
1074         *iter=newbuffer;
1075         fUsedBuffers.push_back(thisbuffer);
1076         return thisbuffer;
1077       } else {
1078         HLTWarning("failed to alloc raw buffer: cannot split raw buffer %p of size %d (used %d) at size %d", *iter, (*iter)->GetTotalSize(), (*iter)->GetUsedSize(), size);
1079       }
1080     }
1081   }
1082   return NULL;
1083 }
1084
1085 int AliHLTDataBuffer::AliHLTRawPage::Free(AliHLTRawBuffer* pBuffer)
1086 {
1087   /// free a buffer and merge consecutive free buffers
1088   int iResult=0;
1089   for (AliHLTRawBufferPList::iterator iter=fUsedBuffers.begin();
1090        iter!=fUsedBuffers.end() && iResult>=0;
1091        iter++) {
1092     if ((*iter)==pBuffer) {
1093       fUsedBuffers.erase(iter);
1094       AliHLTRawBufferPList::iterator prev=fFreeBuffers.begin();
1095       for (; prev!=fFreeBuffers.end() && iResult>=0; prev++) {
1096         if ((*pBuffer)<(*(*prev)) ||
1097             ((*prev)->GetTotalSize()==0 && pBuffer->GetPointer()<=(*prev)->GetPointer() && (*prev)->GetPointer()<=pBuffer->GetPointer()+pBuffer->GetTotalSize())) {
1098           // check consecutive buffers
1099           if ((*(*prev)) == (pBuffer->GetPointer()+pBuffer->GetTotalSize()) ||
1100               ((*prev)->GetTotalSize()==0 && pBuffer->GetPointer()<=(*prev)->GetPointer() && (*prev)->GetPointer()<=pBuffer->GetPointer()+pBuffer->GetTotalSize())) {
1101             // the buffer to be released has a consecutive free buffer -> merge them
1102             if ((iResult=pBuffer->Merge(*(*prev)))>=0) {
1103               delete *prev;
1104               *prev=pBuffer;
1105             } else {
1106               HLTError("failed to merge consecutive/overlapping buffers %p and %p", pBuffer, (*prev));
1107               pBuffer->Print("");
1108               (*prev)->Print("");
1109             }
1110             break;
1111           }
1112           fFreeBuffers.insert(prev, pBuffer);
1113           break;
1114         }
1115         if ((*pBuffer)>(*(*prev)) ||
1116             (pBuffer->GetTotalSize()==0 && (*prev)->GetPointer()<=pBuffer->GetPointer() && pBuffer->GetPointer()<=(*prev)->GetPointer()+(*prev)->GetTotalSize())) {
1117           // check consecutive buffers
1118           if ((*pBuffer) == ((*prev)->GetPointer()+(*prev)->GetTotalSize())||
1119               (pBuffer->GetTotalSize()==0 && (*prev)->GetPointer()<=pBuffer->GetPointer() && pBuffer->GetPointer()<=(*prev)->GetPointer()+(*prev)->GetTotalSize())) {
1120             // the buffer to be released is consecutive to a free buffer -> merge them
1121             if ((iResult=pBuffer->Merge(*(*prev)))>=0) {
1122               AliHLTRawBufferPList::iterator succ=prev+1;
1123               delete *prev;
1124               *prev=pBuffer;
1125               // check if the buffer and the following one are consecutive
1126               if (succ!=fFreeBuffers.end() &&
1127                   (*(*succ)) == (pBuffer->GetPointer()+pBuffer->GetTotalSize())) {
1128                 if ((iResult=pBuffer->Merge(*(*succ)))>=0) {
1129                   delete *succ;
1130                   fFreeBuffers.erase(succ);
1131                 }
1132               }
1133             }
1134             break;
1135           }
1136         }
1137       }
1138       if (prev==fFreeBuffers.end()) {
1139         fFreeBuffers.push_back(pBuffer);
1140       }
1141
1142       // merge consecutive free buffers
1143       prev=fFreeBuffers.begin();
1144       for (AliHLTRawBufferPList::iterator current=prev+1; current!=fFreeBuffers.end() && iResult>=0; ) {
1145         // check if the buffer is embedded into the previous one
1146         if ((*current)->GetTotalSize()==0 && (*prev)->GetPointer()<=(*current)->GetPointer() && (*current)->GetPointer()<(*prev)->GetPointer()+(*prev)->GetTotalSize())  {
1147           if ((iResult=(*prev)->Merge(*(*current)))>=0) {
1148             current=fFreeBuffers.erase(current);
1149             continue;
1150           } else {
1151             HLTError("failed to merge embedded zero length buffer into preceeding buffer");
1152             Print("");
1153           }
1154         }
1155         // check if the buffer is consecutive to the previous one
1156         if ((*(*current)) == ((*prev)->GetPointer()+(*prev)->GetTotalSize())) {
1157           if ((iResult=(*prev)->Merge(*(*current)))>=0) {
1158             current=fFreeBuffers.erase(current);
1159             continue;
1160           } else {
1161             HLTError("failed to merge consecutive free buffers");
1162             Print("");
1163           }
1164         }
1165         prev=current++;
1166       }
1167
1168       // buffer was part of this page
1169       return 0;
1170     }
1171   }
1172   // buffer not found in this page
1173   return 1;
1174 }
1175
1176 int AliHLTDataBuffer::AliHLTRawPage::SetSize(const AliHLTDataBuffer::AliHLTRawBuffer* pBuffer, AliHLTUInt32_t size)
1177 {
1178   /// set the size of a raw buffer and release the remaining part
1179   int iResult=0;
1180   for (AliHLTRawBufferPList::iterator iter=fUsedBuffers.begin();
1181        iter!=fUsedBuffers.end() && iResult>=0;
1182        iter++) {
1183     if ((*iter)==pBuffer) {      // buffer was part of this page
1184       if ((*iter)->GetTotalSize()==size) return 0;
1185       if ((*iter)->GetTotalSize()<size) {
1186         HLTError("%d exceeds total size of buffer %p (%d used %d)\n", size, *iter, (*iter)->GetTotalSize(), (*iter)->GetUsedSize());
1187         return -ENOSPC;
1188       }
1189       AliHLTDataBuffer::AliHLTRawBuffer* freespace=(*iter)->Split(size);
1190       if (freespace) {
1191         fUsedBuffers.push_back(freespace);
1192         Free(freespace);
1193       } else {
1194         HLTWarning("failed to relase unused memory: cannot split raw buffer %p of size %d (used %d) at size %d", *iter, (*iter)->GetTotalSize(), (*iter)->GetUsedSize(), size);
1195       }
1196       return 0;
1197     }
1198   }
1199   // buffer not found in this page
1200   return 1;
1201 }
1202
1203 bool AliHLTDataBuffer::AliHLTRawPage::HasBuffer(const AliHLTDataBuffer::AliHLTRawBuffer* pBuffer)
1204 {
1205   /// check if the buffer is in this page
1206   for (AliHLTRawBufferPList::iterator iter=fUsedBuffers.begin();
1207        iter!=fUsedBuffers.end();
1208        iter++) {
1209     if ((*iter)==pBuffer) {      // buffer was part of this page
1210       return true;
1211     }
1212   }
1213   // buffer not found in this page
1214   return false;
1215 }
1216
1217 AliHLTUInt32_t AliHLTDataBuffer::AliHLTRawPage::Capacity() const 
1218 {
1219   /// get max available contiguous buffer
1220   AliHLTUInt32_t capacity=0;
1221   for (unsigned i=0; i<fFreeBuffers.size(); i++) {
1222     if (fFreeBuffers[i]->GetTotalSize()>capacity) 
1223       capacity=fFreeBuffers[i]->GetTotalSize();
1224   }
1225   return capacity;
1226 }
1227
1228 void AliHLTDataBuffer::AliHLTRawPage::Print(const char* option)
1229 {
1230   /// print page information
1231   if (strcmp(option, "global")==0) {
1232     cout << "number of global pages: " << fgGlobalPages.size() << endl;
1233     for (AliHLTRawPage* rawpage=NextPage(NULL);
1234          rawpage!=NULL; 
1235          rawpage=NextPage(rawpage)) {
1236       rawpage->Print("");
1237     }
1238     return;
1239   }
1240   cout << "************* AliHLTRawPage status ***********" << endl;
1241   cout << "  instance " << this << endl;
1242   printf("  buffer %p  size %d", fPtr, fSize);
1243   cout << "  used buffers: " << fUsedBuffers.size() << endl;
1244   AliHLTRawBufferPList::iterator iter=fUsedBuffers.begin();
1245   for (; iter!=fUsedBuffers.end(); iter++) {
1246     cout << "  "; (*iter)->Print("min");
1247   }
1248   cout << "  free buffers: " << fFreeBuffers.size() << endl;
1249   iter=fFreeBuffers.begin();
1250   for (; iter!=fFreeBuffers.end(); iter++) {
1251     cout << "  "; (*iter)->Print("min");
1252   }
1253 }
1254
1255
1256 vector<AliHLTDataBuffer::AliHLTRawPage*> AliHLTDataBuffer::AliHLTRawPage::fgGlobalPages;
1257
1258 AliHLTUInt32_t AliHLTDataBuffer::AliHLTRawPage::fgGlobalPageSize=30*1024*1024;
1259
1260 AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::AliHLTRawPage::GlobalAlloc(AliHLTUInt32_t size, int verbosity)
1261 {
1262   // alloc a buffer of specified size from the global pages
1263   AliHLTDataBuffer::AliHLTRawBuffer* rawbuffer=NULL;
1264   vector<AliHLTDataBuffer::AliHLTRawPage*>::iterator page=fgGlobalPages.begin();
1265   AliHLTLogging log;
1266   for (page=fgGlobalPages.begin();page!=fgGlobalPages.end(); page++) {
1267     if ((rawbuffer=(*page)->Alloc(size))!=NULL) {
1268       if (verbosity>1) {
1269         log.Logging(kHLTLogInfo, "AliHLTDataBuffer::AliHLTRawPage::GlobalAlloc", "data buffer handling", "allocated raw buffer %p from page %p\n", rawbuffer, *page);
1270         rawbuffer->Print("min");
1271       }
1272       break;
1273     }
1274   }
1275   if (!rawbuffer) {
1276     AliHLTUInt32_t rawPageSize=fgGlobalPageSize;
1277     if (rawPageSize<size) {
1278       if (rawPageSize*10<size) {
1279         log.Logging(kHLTLogError, "AliHLTDataBuffer::AliHLTRawPage::GlobalAlloc", "data buffer handling", "refusing to allocate buffer of size %d", size);
1280         return NULL;
1281       }
1282       rawPageSize=size;
1283     }
1284     AliHLTDataBuffer::AliHLTRawPage* rawpage=new AliHLTDataBuffer::AliHLTRawPage(rawPageSize);
1285     if (!rawpage) {
1286       log.Logging(kHLTLogError, "AliHLTDataBuffer::AliHLTRawPage::GlobalAlloc", "data buffer handling", "can not create raw page");
1287       return NULL;
1288     }
1289
1290     // check is there is at least one unused page which can be replaced by the newly created one
1291     for (page=fgGlobalPages.begin(); page!=fgGlobalPages.end(); page++) {
1292       if ((*page)->IsUsed()) continue;
1293       delete *page;
1294       fgGlobalPages.erase(page);
1295       break; // delete only one page to be replaced by the new page
1296     }
1297     fgGlobalPages.push_back(rawpage);
1298     if ((rawbuffer=rawpage->Alloc(size))!=NULL) {
1299       if (verbosity>1) {
1300         log.Logging(kHLTLogInfo, "AliHLTDataBuffer::AliHLTRawPage::GlobalAlloc", "data buffer handling", "allocated raw buffer %p from page %p\n", rawbuffer, rawpage);
1301         rawbuffer->Print("min");
1302       }
1303     }
1304   }
1305
1306   return rawbuffer;
1307 }
1308
1309 AliHLTDataBuffer::AliHLTRawPage* AliHLTDataBuffer::AliHLTRawPage::FindPage(AliHLTDataBuffer::AliHLTRawBuffer* buffer)
1310 {
1311   // find buffer in the global pages
1312   vector<AliHLTDataBuffer::AliHLTRawPage*>::iterator page=fgGlobalPages.begin();
1313   for (; page!=fgGlobalPages.end(); page++) {
1314     if ((*page)->HasBuffer(buffer)) {
1315       return *page;
1316     }
1317   }
1318
1319   return NULL;
1320 }
1321
1322 int AliHLTDataBuffer::AliHLTRawPage::GlobalClean()
1323 {
1324   // cleanup the global pages */
1325   vector<AliHLTDataBuffer::AliHLTRawPage*>::iterator page=fgGlobalPages.begin();
1326   while (page!=fgGlobalPages.end()) {
1327     if (!(*page)->IsUsed()) {
1328       delete *page;
1329       page=fgGlobalPages.erase(page);
1330       continue;
1331     }
1332     AliHLTLogging log;
1333     log.Logging(kHLTLogError, "AliHLTDataBuffer::AliHLTRawPage::GlobalClean", "data buffer handling", "HLT memory page still in use, skipping cleanup, potential memory leak");
1334     
1335     page++;
1336   }
1337   
1338   return 0;
1339 }
1340
1341 AliHLTDataBuffer::AliHLTRawPage* AliHLTDataBuffer::AliHLTRawPage::NextPage(const AliHLTDataBuffer::AliHLTRawPage* prev)
1342 {
1343   // get next global page
1344   vector<AliHLTDataBuffer::AliHLTRawPage*>::iterator page=fgGlobalPages.begin();
1345   for (; page!=fgGlobalPages.end(); page++) {
1346     if (prev==NULL) return *page;
1347     if (*page!=prev) continue;
1348     if (++page!=fgGlobalPages.end()) return *page;
1349     break;
1350   }
1351   return NULL;
1352 }
1353
1354 void AliHLTDataBuffer::AliHLTDataSegment::Print(const char* /*option*/) const
1355 {
1356   // print info for data segment
1357   cout << "AliHLTDataSegment " << this 
1358        << " " << AliHLTComponent::DataType2Text(fDataType)
1359        << " " << hex << fSpecification << dec
1360        << " Ptr " << (void*)fPtr
1361        << " offset " << fSegmentOffset
1362        << " size " << fSegmentSize
1363        << endl;
1364 }
1365
1366 void AliHLTDataBuffer::AliHLTForwardedDataSegment::Print(const char* option) const
1367 {
1368   // print info for data segment
1369   cout << "AliHLTForwardeDataSegment " << this << endl;
1370   cout << "    my    : "; AliHLTDataSegment::Print(option);
1371   cout << "    parent: "; fParentSegment.Print(option);
1372   cout << "    task  : "; 
1373   if (fParentTask) fParentTask->Print("");
1374   else cout << "nil" << endl;
1375 }
1376
1377 void AliHLTDataBuffer::Print(const char* option) const
1378 {
1379   // print info for data buffer
1380   unsigned i=0;
1381   cout << "AliHLTDataBuffer " << this << endl;
1382   cout << " raw buffer " << fpBuffer << endl;
1383   if (fpBuffer) {
1384     cout << " ";
1385     fpBuffer->Print(option);
1386   }
1387
1388   cout << " total segments: " << GetNofSegments() << endl;
1389   cout << "   data segments: " << fSegments.size() << endl;
1390   for (i=0; i<fSegments.size(); i++) {
1391     cout << "     ";
1392     fSegments[i].Print(option);
1393   }
1394
1395   cout << "   forwarded segments: " << fForwardedSegments.size() << endl;
1396   for (i=0; i<fForwardedSegments.size(); i++) {
1397     cout << "     ";
1398     fForwardedSegments[i].Print(option);
1399   }
1400
1401   cout << " consumers: " << GetNofConsumers() << endl;
1402   for (i=0; i<fConsumers.size(); i++) {
1403     cout << "   ";
1404     fConsumers[i]->Print(option);
1405   }
1406
1407   cout << " active consumers: " << GetNofActiveConsumers() << endl;
1408   for (i=0; i<fActiveConsumers.size(); i++) {
1409     cout << "   ";
1410     fActiveConsumers[i]->Print(option);
1411   }
1412
1413   cout << " released consumers: " << fReleasedConsumers.size() << endl;
1414   for (i=0; i<fReleasedConsumers.size(); i++) {
1415     cout << "   ";
1416     fReleasedConsumers[i]->Print(option);
1417   }
1418
1419 }