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