]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/BASE/AliHLTDataBuffer.cxx
enhanced HOMER reader to work on normal buffer
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTDataBuffer.cxx
index 2866ae0ec9746d45c27b35c44617eee3ee9721d0..7c326dc5815d05c869c22749959a10012a84d960 100644 (file)
@@ -1,10 +1,11 @@
 // $Id$
 
 /**************************************************************************
- * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+ * This file is property of and copyright by the ALICE HLT Project        * 
+ * ALICE Experiment at CERN, All rights reserved.                         *
  *                                                                        *
- * Authors: Matthias Richter <Matthias.Richter@ift.uib.no>                *
- *          for The ALICE Off-line Project.                               *
+ * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
+ *                  for The ALICE HLT Project.                            *
  *                                                                        *
  * Permission to use, copy, modify and distribute this software and its   *
  * documentation strictly for non-commercial purposes is hereby granted   *
     @brief  Handling of Data Buffers for HLT components.
 */
 
+// see header file for class documentation
+// or
+// refer to README to build package
+// or
+// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
+
 #if __GNUC__>= 3
 using namespace std;
 #endif
 
 #include "AliHLTDataBuffer.h"
+#include "AliHLTConsumerDescriptor.h"
 #include "AliHLTComponent.h"
-#include <string>
-#include "AliHLTSystem.h"
-
-AliHLTConsumerDescriptor::AliHLTConsumerDescriptor()
-{
-  fpConsumer=NULL;
-  fSegments.clear();
-}
-
-AliHLTConsumerDescriptor::AliHLTConsumerDescriptor(AliHLTComponent* pConsumer)
-{
-  fpConsumer=pConsumer;
-  fSegments.clear();
-}
-
-AliHLTConsumerDescriptor::~AliHLTConsumerDescriptor()
-{
-  if (fSegments.size()>0) {
-    //HLTWarning("unreleased data segments found");
-  }
-}
-
-int AliHLTConsumerDescriptor::SetActiveDataSegment(AliHLTUInt32_t offset, AliHLTUInt32_t size)
-{
-  int iResult=0;
-  AliHLTDataSegment segment(offset, size);
-  fSegments.push_back(segment);
-  //HLTDebug("set active segment (%d:%d) for consumer %p", offset, size, this);
-  return iResult;
-}
-
-int AliHLTConsumerDescriptor::CheckActiveDataSegment(AliHLTUInt32_t offset, AliHLTUInt32_t size)
-{
-  int iResult=0;
-  if (fSegments.size()>0) {
-    vector<AliHLTDataSegment>::iterator segment=fSegments.begin();
-    while (segment!=fSegments.end()) {
-      if (iResult=((*segment).fSegmentOffset==offset && (*segment).fSegmentSize==size)) {
-       break;
-      }
-      segment++;
-    }
-  } else {
-    //HLTWarning("no data segment active for consumer %p", this);
-    iResult=-ENODATA;
-  }
-  return iResult;
-}
+#include <cerrno>
+#include <cassert>
+//#include <string>
+//#include "AliHLTSystem.h"
 
-int AliHLTConsumerDescriptor::ReleaseActiveDataSegment(AliHLTUInt32_t offset, AliHLTUInt32_t size)
-{
-  int iResult=0;
-  if (fSegments.size()>0) {
-    vector<AliHLTDataSegment>::iterator segment=fSegments.begin();
-    while (segment!=fSegments.end()) {
-      if (iResult=((*segment).fSegmentOffset==offset && (*segment).fSegmentSize==size)) {
-       fSegments.erase(segment);
-       break;
-      }
-      segment++;
-    }
-    if (iResult=0) {
-      //HLTWarning("no data segment (%d:%d) active for consumer %p", offset, size, this);
-      iResult=-ENOENT;
-    }
-  } else {
-    //HLTWarning("no data segment active for consumer %p", this);
-    iResult=-ENODATA;
-  }
-  return iResult;
-}
+typedef vector<AliHLTDataBuffer::AliHLTDataSegment> AliHLTDataSegmentList;
+typedef vector<AliHLTDataBuffer::AliHLTRawBuffer*>  AliHLTRawBufferPList;
 
 /** ROOT macro for the implementation of ROOT specific class methods */
 ClassImp(AliHLTDataBuffer)
 
 AliHLTDataBuffer::AliHLTDataBuffer()
+  :
+  fSegments(),
+  fConsumers(),
+  fActiveConsumers(),
+  fReleasedConsumers(),
+  fpBuffer(NULL),
+  fFlags(0)
 {
-  // TODO: do the right initialization 
-  //fSegments.empty();
-  //fConsumers.empty;
-  //fActiveConsumers.empty;
-  //fReleasedConsumers.empty;
-  fpBuffer=NULL;
-  fFlags=0;
-  fNofInstances++;
+  // see header file for class documentation
+  // or
+  // refer to README to build package
+  // or
+  // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
+  fSegments.empty();
+  fConsumers.empty();
+  fActiveConsumers.empty();
+  fReleasedConsumers.empty();
+  fgNofInstances++;
 }
 
-int AliHLTDataBuffer::fNofInstances=0;
-vector<AliHLTRawBuffer*> AliHLTDataBuffer::fFreeBuffers;
-vector<AliHLTRawBuffer*> AliHLTDataBuffer::fActiveBuffers;
-AliHLTUInt32_t AliHLTDataBuffer::fMargin=1024;
+int AliHLTDataBuffer::fgNofInstances=0;
+AliHLTRawBufferPList AliHLTDataBuffer::fgFreeBuffers;
+AliHLTRawBufferPList AliHLTDataBuffer::fgActiveBuffers;
+AliHLTUInt32_t AliHLTDataBuffer::fgMargin=1024;
 AliHLTLogging AliHLTDataBuffer::fgLogging;
+const Int_t AliHLTDataBuffer::fgkSafetyPatternSize=16;
+const char AliHLTDataBuffer::fgkSafetyPattern[]={0x28, 0x63, 0x29, 0x4d, 0x52, 0x49, 0x43, 0x48, 0x54, 0x45, 0x52, 0x20, 0x32, 0x30, 0x30, 0x37};
 
 AliHLTDataBuffer::~AliHLTDataBuffer()
 {
-  if (--fNofInstances<=0) {
+  // see header file for function documentation
+  if (--fgNofInstances<=0) {
     DeleteRawBuffers();
   }
   CleanupConsumerList();
@@ -130,30 +86,36 @@ AliHLTDataBuffer::~AliHLTDataBuffer()
 
 int AliHLTDataBuffer::SetConsumer(AliHLTComponent* pConsumer)
 {
+  // see header file for function documentation
   int iResult=0;
   if (pConsumer) {
+    if (FindConsumer(pConsumer)) {
+      HLTWarning("consumer %s (%p) already set to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
+    }
     AliHLTConsumerDescriptor* pDesc=new AliHLTConsumerDescriptor(pConsumer);
     if (pDesc) {
       fConsumers.push_back(pDesc);
+      HLTDebug("set consumer %s (%p) to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
     } else {
       HLTError("memory allocation failed");
       iResult=-ENOMEM;
     }
   } else {
-    HLTError("invalid parameter");
+    HLTError("invalid parameter: consumer component (nil)");
     iResult=-EINVAL;
   }
   return iResult;
 }
 
-int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, vector<AliHLTComponent_DataType>* tgtList)
+int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList)
 {
+  // see header file for function documentation
   int iResult=0;
   if (pConsumer) {
-    vector<AliHLTDataSegment> segments;
+    AliHLTDataSegmentList segments;
     if ((iResult=FindMatchingDataSegments(pConsumer, segments))>=0) {
       if (tgtList) {
-       vector<AliHLTDataSegment>::iterator segment=segments.begin();
+       AliHLTDataSegmentList::iterator segment=segments.begin();
        while (segment!=segments.end()) {
          tgtList->push_back((*segment).fDataType);
          segment++;
@@ -167,17 +129,27 @@ int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, v
   return iResult;
 }
 
-int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataSegment>& tgtList)
+int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataBuffer::AliHLTDataSegment>& tgtList)
 {
+  // see header file for function documentation
   int iResult=0;
+
+  // Matthias 26.09.2007 relax the restriction to matching data blocks
+  // all blocks are passed to the consumer, which is the policy also in
+  // PubSub
+  tgtList.assign(fSegments.begin(), fSegments.end());
+  iResult=tgtList.size();
+  return iResult;
+  
   if (pConsumer) {
-    vector<AliHLTComponent_DataType> dtlist;
+    AliHLTComponentDataTypeList dtlist;
     ((AliHLTComponent*)pConsumer)->GetInputDataTypes(dtlist);
-    vector<AliHLTDataSegment>::iterator segment=fSegments.begin();
+    AliHLTDataSegmentList::iterator segment=fSegments.begin();
     while (segment!=fSegments.end()) {
-      vector<AliHLTComponent_DataType>::iterator type=dtlist.begin();
+      AliHLTComponentDataTypeList::iterator type=dtlist.begin();
       while (type!=dtlist.end()) {
-       if ((*segment).fDataType==(*type)) {
+       if ((*segment).fDataType==(*type) ||
+           (*type)==kAliHLTAnyDataType) {
          tgtList.push_back(*segment);
          iResult++;
          break;
@@ -192,47 +164,62 @@ int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer,
   return iResult;
 }
 
-int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponent_BlockData* arrayBlockDesc, int iArraySize)
+int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponentBlockData* arrayBlockDesc, int iArraySize)
 {
+  // see header file for function documentation
   int iResult=0;
   if (pConsumer && arrayBlockDesc) {
     if (fpBuffer) {
       AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fConsumers);
       if (pDesc) {
-       vector<AliHLTDataSegment> tgtList;
-       /* TODO: think about a good policy for this check
-        * is it enough that at least one segment is available, or have all to be available?
-        * or is it possible to have optional segments?
-        */
-       if ((iResult=FindMatchingDataSegments(pConsumer, tgtList))>0) {
+       AliHLTDataSegmentList tgtList;
+       // Matthias 26.07.2007 AliHLTSystem should behave the same way as PubSub
+       // so it does not matter if there are matching data types or not, unless
+       // we implement such a check in PubSub
+       if ((iResult=FindMatchingDataSegments(pConsumer, tgtList))>=0) {
          int i =0;
-         vector<AliHLTDataSegment>::iterator segment=tgtList.begin();
+         AliHLTDataSegmentList::iterator segment=tgtList.begin();
          while (segment!=tgtList.end() && i<iArraySize) {
            // fill the block data descriptor
-           arrayBlockDesc[i].fStructSize=sizeof(AliHLTComponent_BlockData);
+           arrayBlockDesc[i].fStructSize=sizeof(AliHLTComponentBlockData);
            // the shared memory key is not used in AliRoot
-           arrayBlockDesc[i].fShmKey.fStructSize=sizeof(AliHLTComponent_ShmData);
-           arrayBlockDesc[i].fShmKey.fShmType=gkAliHLTComponent_InvalidShmType;
-           arrayBlockDesc[i].fShmKey.fShmID=gkAliHLTComponent_InvalidShmID;
+           arrayBlockDesc[i].fShmKey.fStructSize=sizeof(AliHLTComponentShmData);
+           arrayBlockDesc[i].fShmKey.fShmType=gkAliHLTComponentInvalidShmType;
+           arrayBlockDesc[i].fShmKey.fShmID=gkAliHLTComponentInvalidShmID;
+           // This models the behavior of PubSub.
+           // For incoming data blocks, fOffset must be ignored by the
+           // processing component. It is set for bookkeeping in the framework.
+           // fPtr always points to the beginning of the data.
            arrayBlockDesc[i].fOffset=(*segment).fSegmentOffset;
-           arrayBlockDesc[i].fPtr=fpBuffer->fPtr;
+           AliHLTUInt8_t* pTgt=*fpBuffer;
+           pTgt+=(*segment).fSegmentOffset;
+           arrayBlockDesc[i].fPtr=reinterpret_cast<void*>(pTgt);
            arrayBlockDesc[i].fSize=(*segment).fSegmentSize;
            arrayBlockDesc[i].fDataType=(*segment).fDataType;
            arrayBlockDesc[i].fSpecification=(*segment).fSpecification;
            pDesc->SetActiveDataSegment(arrayBlockDesc[i].fOffset, arrayBlockDesc[i].fSize);
-           HLTDebug("component %p (%s) subscribed to segment #%d offset %d", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), i, arrayBlockDesc[i].fOffset);
+           HLTDebug("component %p (%s) subscribed to segment #%d offset %d size %d data type %s %#x", 
+                    pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), i, arrayBlockDesc[i].fOffset,
+                    arrayBlockDesc[i].fSize, (AliHLTComponent::DataType2Text(arrayBlockDesc[i].fDataType)).c_str(), 
+                    arrayBlockDesc[i].fSpecification);
            i++;
            segment++;
          }
+         // check whether there was enough space for the segments
+         if (i!=(int)tgtList.size()) {
+           HLTError("too little space in block descriptor array: required %d, available %d", tgtList.size(), iArraySize);
+           iResult=-ENOSPC;
+         } else {
          // move this consumer to the active list
          if (ChangeConsumerState(pDesc, fConsumers, fActiveConsumers)>=0) {
            HLTDebug("component %p (%s) subscribed to data buffer %p", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), this);
          } else {
            // TODO: cleanup the consumer descriptor correctly
-           memset(arrayBlockDesc, 0, iArraySize*sizeof(AliHLTComponent_BlockData));
+           memset(arrayBlockDesc, 0, iArraySize*sizeof(AliHLTComponentBlockData));
            HLTError("can not activate consumer %p for data buffer %p", pConsumer, this);
            iResult=-EACCES;
          }
+         }
        } else {
          HLTError("unresolved data segment(s) for component %p (%s)", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
          iResult=-EBADF;
@@ -242,8 +229,11 @@ int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponen
        iResult=-ENOENT;
       }
     } else {
-      HLTError("data buffer %p is empty", this);
-      iResult=-ENODATA;
+      // Matthias 26.07.2007 until now, data had to be present for successful subscription
+      // in order to be consistent with the PubSub framework, this restiction has been
+      // removed
+      //HLTError("data buffer %p is empty", this);
+      //iResult=-ENODATA;
     }
   } else {
     HLTError("invalid parameter");
@@ -252,8 +242,9 @@ int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponen
   return iResult;
 }
 
-int AliHLTDataBuffer::Release(AliHLTComponent_BlockData* pBlockDesc, const AliHLTComponent* pConsumer)
+int AliHLTDataBuffer::Release(AliHLTComponentBlockData* pBlockDesc, const AliHLTComponent* pConsumer)
 {
+  // see header file for function documentation
   int iResult=0;
   if (pBlockDesc && pConsumer) {
     AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fActiveConsumers);
@@ -270,7 +261,7 @@ int AliHLTDataBuffer::Release(AliHLTComponent_BlockData* pBlockDesc, const AliHL
       }
       if (pDesc->GetNofActiveSegments()==0) {
        if ((iResult=ChangeConsumerState(pDesc, fActiveConsumers, fReleasedConsumers))>=0) {
-         if (GetNofActiveConsumers()==0) {
+         if (GetNofActiveConsumers()==0 && GetNofPendingConsumers()==0) {
            // this is the last consumer, reset the consumer list and release the raw buffer
            ResetDataBuffer();
          }
@@ -292,33 +283,57 @@ int AliHLTDataBuffer::Release(AliHLTComponent_BlockData* pBlockDesc, const AliHL
 
 AliHLTUInt8_t* AliHLTDataBuffer::GetTargetBuffer(int iMinSize)
 {
+  // see header file for function documentation
   AliHLTUInt8_t* pTargetBuffer=NULL;
+  if (fpBuffer!=NULL) {
+    HLTWarning("data buffer not properly reset, possible memory leak\n");
+  }
   fpBuffer=CreateRawBuffer(iMinSize);
-  pTargetBuffer=(AliHLTUInt8_t*)fpBuffer;
+  if (fpBuffer) {
+    pTargetBuffer=*fpBuffer;
+  } else {
+    HLTError("can not create raw buffer");
+  }
   return pTargetBuffer;
 }
 
-int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponent_BlockData* arrayBlockData, int iSize)
+int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData* arrayBlockData, int iSize)
 {
+  // see header file for function documentation
   int iResult=0;
   if (pTgt && arrayBlockData && iSize>=0) {
     if (fpBuffer) {
-      if (fpBuffer->fPtr==(void*)pTgt) {
-       AliHLTDataSegment segment;
-       memset(&segment, 0, sizeof(AliHLTDataSegment));
+      if (*fpBuffer==pTgt) {
+       AliHLTDataBuffer::AliHLTDataSegment segment;
        for (int i=0; i<iSize; i++) {
-         if (arrayBlockData[i].fOffset+arrayBlockData[i].fSize<fpBuffer->fSize) {
-           segment.fSegmentOffset=arrayBlockData[i].fOffset;
-           segment.fSegmentSize=arrayBlockData[i].fSize;
-           segment.fDataType=arrayBlockData[i].fDataType;
-           segment.fSpecification=arrayBlockData[i].fSpecification;
-           fSegments.push_back(segment);
+         // This function has to model the behavior of PubSub
+         // For output blocks only the fOffset value is used, this must be the offset
+         // relative to the output pointer. fPtr must be either NULL or the output
+         // pointer
+         if (arrayBlockData[i].fPtr==NULL ||
+             arrayBlockData[i].fPtr==*fpBuffer) {
+           if (arrayBlockData[i].fOffset+arrayBlockData[i].fSize<=fpBuffer->GetUsedSize()) {
+             segment.fSegmentOffset=arrayBlockData[i].fOffset;
+             segment.fSegmentSize=arrayBlockData[i].fSize;
+             segment.fDataType=arrayBlockData[i].fDataType;
+             segment.fSpecification=arrayBlockData[i].fSpecification;
+             fSegments.push_back(segment);
+             HLTDebug("set segment %s with size %d at offset %d", AliHLTComponent::DataType2Text(segment.fDataType).data(), segment.fSegmentSize, segment.fSegmentOffset);
+           } else {
+             HLTError("block data specification %#d (%s) exceeds size of data buffer", i, AliHLTComponent::DataType2Text(arrayBlockData[i].fDataType).data());
+             HLTError("block offset=%d, block size=%d, buffer size=%d", arrayBlockData[i].fOffset, arrayBlockData[i].fSize, fpBuffer->GetUsedSize());
+             iResult=-E2BIG;
+           }
          } else {
-           HLTError("block data specification #%d (%s@%s) exceeds size of data buffer", i, arrayBlockData[i].fDataType.fOrigin, arrayBlockData[i].fDataType.fID);
+           HLTError("invalid pointer (%p) in block data specification (buffer %p size %d)."
+                    "please note: for output blocks only the fOffset value is valid and must "
+                    "be relative to the output buffer", arrayBlockData[i].fPtr, fpBuffer->GetPointer(), fpBuffer->GetUsedSize());
+           iResult=-ERANGE;
          }
        }
       } else {
-       HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->fPtr, fpBuffer);
+       HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->GetPointer(), fpBuffer);
+       iResult=-EINVAL;
       }
     } else {
       HLTFatal("internal data structur missmatch");
@@ -333,55 +348,65 @@ int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponent_BlockData
 
 int AliHLTDataBuffer::IsEmpty()
 {
+  // see header file for function documentation
   int iResult=fpBuffer==NULL || GetNofSegments()==0;
   return iResult;
 }
 
 int AliHLTDataBuffer::GetNofSegments()
 {
+  // see header file for function documentation
   int iResult=fSegments.size();
   return iResult;
 }
 
 int AliHLTDataBuffer::GetNofConsumers()
 {
+  // see header file for function documentation
   int iResult=fConsumers.size() + GetNofActiveConsumers() + fReleasedConsumers.size();
   return iResult;
 }
 
+int AliHLTDataBuffer::GetNofPendingConsumers()
+{
+  // see header file for function documentation
+  int iResult=fConsumers.size();
+  return iResult;
+}
+
 int AliHLTDataBuffer::GetNofActiveConsumers()
 {
+  // see header file for function documentation
   int iResult=fActiveConsumers.size();
   return iResult;
 }
 
-AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
+AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
 {
+  // see header file for function documentation
   AliHLTRawBuffer* pRawBuffer=NULL;
-  vector<AliHLTRawBuffer*>::iterator buffer=fFreeBuffers.begin();
-  while (buffer!=fFreeBuffers.end() && pRawBuffer==NULL) {
-    if ((*buffer)->fTotalSize>=size && ((*buffer)->fTotalSize-size)<fMargin) {
+  unsigned int reqSize=size+fgkSafetyPatternSize;
+  AliHLTRawBufferPList::iterator buffer=fgFreeBuffers.begin();
+  while (buffer!=fgFreeBuffers.end() && pRawBuffer==NULL) {
+    if ((*buffer)->CheckSize(reqSize)) {
       // assign this element
       pRawBuffer=*buffer;
-      pRawBuffer->fSize=size;
-      fFreeBuffers.erase(buffer);
-      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->fTotalSize, pRawBuffer->fPtr);
-      fActiveBuffers.push_back(pRawBuffer);
+      pRawBuffer->UseBuffer(size);
+      fgFreeBuffers.erase(buffer);
+      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());
+      fgActiveBuffers.push_back(pRawBuffer);
       break;
     }
     buffer++;
   }
   if (pRawBuffer==NULL) {
     // no buffer found, create a new one
-    pRawBuffer=new AliHLTRawBuffer;
+    pRawBuffer=new AliHLTRawBuffer(reqSize);
     if (pRawBuffer) {
-      memset(pRawBuffer, 0, sizeof(AliHLTRawBuffer));
-      pRawBuffer->fPtr=malloc(size);
-      if (pRawBuffer->fPtr) {
-       pRawBuffer->fSize=size;
-       pRawBuffer->fTotalSize=size;
-       fActiveBuffers.push_back(pRawBuffer);
-       fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->fPtr, pRawBuffer->fTotalSize, pRawBuffer);
+      if (pRawBuffer->GetPointer()) {
+       pRawBuffer->UseBuffer(size);
+       fgActiveBuffers.push_back(pRawBuffer);
+       fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->GetPointer(), pRawBuffer->GetTotalSize(), pRawBuffer);
       } else {
        delete pRawBuffer;
        pRawBuffer=NULL;
@@ -391,21 +416,33 @@ AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
       fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
     }
   }
+  if (pRawBuffer!=NULL && fgkSafetyPatternSize>0) {
+    //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "writing safety pattern to %p offset %d", (*buffer)->GetPointer(), (*buffer)->GetUsedSize());
+    int res=pRawBuffer->WritePattern(fgkSafetyPattern, fgkSafetyPatternSize);
+    assert(res>=0);
+  }
   return pRawBuffer;
 }
 
 int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
 {
+  // see header file for function documentation
   int iResult=0;
   if (pBuffer) {
-    vector<AliHLTRawBuffer*>::iterator buffer=fActiveBuffers.begin();
-    while (buffer!=fActiveBuffers.end() && (*buffer)!=pBuffer) {
+    AliHLTRawBufferPList::iterator buffer=fgActiveBuffers.begin();
+    while (buffer!=fgActiveBuffers.end() && (*buffer)!=pBuffer) {
       buffer++;
     }
-    if (buffer!=fActiveBuffers.end()) {
-      (*buffer)->fSize=0;
-      fFreeBuffers.push_back(*buffer);
-      fActiveBuffers.erase(buffer);
+    if (buffer!=fgActiveBuffers.end()) {
+      if (fgkSafetyPatternSize>0) {
+       //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "comparing safety pattern at %p offset %d", (*buffer)->GetPointer(), reinterpret_cast<AliHLTUInt32_t>(*buffer));
+       if ((*buffer)->CheckPattern(fgkSafetyPattern, fgkSafetyPatternSize)) {
+         fgLogging.Logging(kHLTLogFatal, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "component has written beyond end of data buffer %p size %d", (*buffer)->GetPointer(), (*buffer)->GetUsedSize());
+       }
+      }
+      (*buffer)->Reset();
+      fgFreeBuffers.push_back(*buffer);
+      fgActiveBuffers.erase(buffer);
     } else {
       fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
       iResult=-ENOENT;
@@ -420,29 +457,29 @@ int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
 
 int AliHLTDataBuffer::DeleteRawBuffers() 
 {
+  // see header file for function documentation
   int iResult=0;
-  vector<AliHLTRawBuffer*>::iterator buffer=fFreeBuffers.begin();
-  while (buffer!=fFreeBuffers.end()) {
-    free((*buffer)->fPtr);
+  AliHLTRawBufferPList::iterator buffer=fgFreeBuffers.begin();
+  while (buffer!=fgFreeBuffers.end()) {
     delete *buffer;
-    fFreeBuffers.erase(buffer);
-    buffer=fFreeBuffers.begin();
+    fgFreeBuffers.erase(buffer);
+    buffer=fgFreeBuffers.begin();
   }
-  buffer=fActiveBuffers.begin();
-  while (buffer!=fFreeBuffers.end()) {
-    fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "request to delete active raw buffer container (raw buffer %p, size %d)", (*buffer)->fPtr, (*buffer)->fTotalSize);
-    free((*buffer)->fPtr);
+  buffer=fgActiveBuffers.begin();
+  while (buffer!=fgActiveBuffers.end()) {
+    fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "request to delete active raw buffer container (raw buffer %p, size %d)", (*buffer)->GetPointer(), (*buffer)->GetTotalSize());
     delete *buffer;
-    fActiveBuffers.erase(buffer);
-    buffer=fActiveBuffers.begin();
+    fgActiveBuffers.erase(buffer);
+    buffer=fgActiveBuffers.begin();
   }
   return iResult;
 }
 
-AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, vector<AliHLTConsumerDescriptor*> &list)
+AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, AliHLTConsumerDescriptorPList &list) const
 {
+  // see header file for function documentation
   AliHLTConsumerDescriptor* pDesc=NULL;
-  vector<AliHLTConsumerDescriptor*>::iterator desc=list.begin();
+  AliHLTConsumerDescriptorPList::iterator desc=list.begin();
   while (desc!=list.end() && pDesc==NULL) {
     if ((pConsumer==NULL || (*desc)->GetComponent()==pConsumer)) {
       pDesc=*desc;
@@ -454,10 +491,22 @@ AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent*
 
 int AliHLTDataBuffer::ResetDataBuffer() 
 {
+  // see header file for function documentation
   int iResult=0;
   AliHLTRawBuffer* pBuffer=fpBuffer;
   fpBuffer=NULL;
-  vector<AliHLTConsumerDescriptor*>::iterator desc=fReleasedConsumers.begin();
+
+  // cleanup consumer states
+  AliHLTConsumerDescriptorPList::iterator desc;
+//   if (GetNofPendingConsumers()>0) {
+//     desc=fConsumers.begin();
+//     while (desc!=fConsumers.end()) {
+//       AliHLTComponent* pComp=(*desc)->GetComponent();
+//       HLTError("internal error: consumer %p (%s %p) did not get data from data buffer %p", *desc, pComp?pComp->GetComponentID():"", pComp, this);
+//       desc++;
+//     }
+//   }
+  desc=fReleasedConsumers.begin();
   while (desc!=fReleasedConsumers.end()) {
     AliHLTConsumerDescriptor* pDesc=*desc;
     fReleasedConsumers.erase(desc);
@@ -472,16 +521,33 @@ int AliHLTDataBuffer::ResetDataBuffer()
     desc=fActiveConsumers.begin();
     fConsumers.push_back(pDesc);
   }
-  ReleaseRawBuffer(pBuffer);
+
+  // cleanup segments
+  AliHLTDataSegmentList::iterator segment=fSegments.begin();
+  while (segment!=fSegments.end()) {
+    fSegments.erase(segment);
+    segment=fSegments.begin();
+  }
+
+  // cleanup raw buffer
+  if (pBuffer) {
+    ReleaseRawBuffer(pBuffer);
+  }
   return iResult;
 }
 
+int AliHLTDataBuffer::Reset()
+{
+  // see header file for function documentation
+  return ResetDataBuffer();
+}
+
 // this is the version which works on lists of components instead of consumer descriptors
-// int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, vector<AliHLTComponent*> &srcList, vector<AliHLTComponent*> &tgtList)
+// int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, AliHLTComponentPList &srcList, AliHLTComponentPList &tgtList)
 // {
 //   int iResult=0;
 //   if (pDesc) {
-//     vector<AliHLTComponent*>::iterator desc=srcList.begin();
+//     AliHLTComponentPList::iterator desc=srcList.begin();
 //     while (desc!=srcList.end()) {
 //       if ((*desc)==pConsumer) {
 //     srcList.erase(desc);
@@ -501,22 +567,23 @@ int AliHLTDataBuffer::ResetDataBuffer()
 //   return iResult;
 // }
 
-int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, vector<AliHLTConsumerDescriptor*> &srcList, vector<AliHLTConsumerDescriptor*> &tgtList)
+int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, AliHLTConsumerDescriptorPList &srcList, AliHLTConsumerDescriptorPList &tgtList)
 {
-  int iResult=0;
+  // see header file for function documentation
+  int iResult=-ENOENT;
   if (pDesc) {
-    vector<AliHLTConsumerDescriptor*>::iterator desc=srcList.begin();
+    AliHLTConsumerDescriptorPList::iterator desc=srcList.begin();
     while (desc!=srcList.end()) {
       if ((*desc)==pDesc) {
        srcList.erase(desc);
        tgtList.push_back(pDesc);
+       iResult=0;
        break;
       }
       desc++;
     }
-    if (desc==srcList.end()) {
+    if (iResult<0) {
       HLTError("can not find consumer descriptor %p in list", pDesc);
-      iResult=-ENOENT;
     }
   } else {
     HLTError("invalid parameter");
@@ -525,10 +592,12 @@ int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, vecto
   return iResult;
 }
 
-int AliHLTDataBuffer::CleanupConsumerList() {
+int AliHLTDataBuffer::CleanupConsumerList() 
+{
+  // see header file for function documentation
   int iResult=0;
   ResetDataBuffer();
-  vector<AliHLTConsumerDescriptor*>::iterator desc=fConsumers.begin();
+  AliHLTConsumerDescriptorPList::iterator desc=fConsumers.begin();
   while (desc!=fConsumers.end()) {
     delete *desc;
     fConsumers.erase(desc);
@@ -536,3 +605,136 @@ int AliHLTDataBuffer::CleanupConsumerList() {
   }
   return iResult;
 }
+
+int AliHLTDataBuffer::FindConsumer(AliHLTComponent* pConsumer, int bAllLists)
+{
+  // see header file for function documentation
+  AliHLTConsumerDescriptorPList::iterator desc=fConsumers.begin();
+  while (desc!=fConsumers.end()) {
+    if ((*desc)->GetComponent()==pConsumer)
+      return 1;
+    desc++;
+  }
+  if (bAllLists==0) return 0;
+
+  desc=fActiveConsumers.begin();
+  while (desc!=fActiveConsumers.end()) {
+    if ((*desc)->GetComponent()==pConsumer)
+      return 1;
+    desc++;
+  }
+  desc=fReleasedConsumers.begin();
+  while (desc!=fReleasedConsumers.end()) {
+    if ((*desc)->GetComponent()==pConsumer)
+      return 1;
+    desc++;
+  }
+  return 0;
+}
+
+AliHLTDataBuffer::AliHLTRawBuffer::AliHLTRawBuffer(AliHLTUInt32_t size)
+  :
+  fSize(0),
+  fTotalSize(size),
+  fPtr(static_cast<AliHLTUInt8_t*>(malloc(size)))
+{
+  // see header file for class documentation
+  // or
+  // refer to README to build package
+  // or
+  // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
+  if (fPtr==NULL) {
+    fSize=0;
+    fTotalSize=0;
+  }
+}
+
+AliHLTDataBuffer::AliHLTRawBuffer::~AliHLTRawBuffer()
+{
+  if (fPtr) {
+    free(fPtr);
+  }
+  fPtr=NULL;
+  fSize=0;
+  fTotalSize=0;
+}
+
+int AliHLTDataBuffer::AliHLTRawBuffer::operator==(void* ptr) const
+{
+  // see header file for function documentation
+  return fPtr == static_cast<AliHLTUInt8_t*>(ptr);
+}
+
+int AliHLTDataBuffer::AliHLTRawBuffer::operator<=(void* ptr) const
+{
+  // see header file for function documentation
+  int iResult=fPtr <= static_cast<AliHLTUInt8_t*>(ptr);
+  //printf("%p: %p <= %p (%d)\n", this, fPtr, ptr, iResult);
+  return iResult;
+}
+
+int AliHLTDataBuffer::AliHLTRawBuffer::operator>(void* ptr) const
+{
+  // see header file for function documentation
+  int iResult=fPtr+fSize > static_cast<AliHLTUInt8_t*>(ptr);
+  //printf("%p: %p + %d > %p (%d)\n", this, fPtr, fSize, ptr, iResult);
+  return iResult;
+}
+
+int AliHLTDataBuffer::AliHLTRawBuffer::operator-(void* ptr) const
+{
+  // see header file for function documentation
+  return static_cast<int>(static_cast<AliHLTUInt8_t*>(ptr)-fPtr);
+}
+
+AliHLTUInt8_t* AliHLTDataBuffer::AliHLTRawBuffer::UseBuffer(AliHLTUInt32_t size)
+{
+  // see header file for function documentation
+  if (size>0 && CheckSize(size)) {
+    fSize=size;
+    return fPtr;
+  }
+  return NULL;
+}
+
+int AliHLTDataBuffer::AliHLTRawBuffer::CheckSize(AliHLTUInt32_t size) const
+{
+  // see header file for function documentation
+  return fTotalSize>=size && ((fTotalSize-size)<fgMargin);
+}
+
+int AliHLTDataBuffer::AliHLTRawBuffer::Reset()
+{
+  // see header file for function documentation
+  fSize=0;
+  return 0;
+}
+
+int AliHLTDataBuffer::AliHLTRawBuffer::WritePattern(const char* pattern, int size)
+{
+  // see header file for function documentation
+  int iResult=0;
+  if (pattern!=NULL && size>0) {
+    if (fSize+size<=fTotalSize) {
+      memcpy(((char*)fPtr)+fSize, pattern, size);
+      iResult=size;
+    } else {
+      iResult=-ENOSPC;
+    }
+  }
+  return iResult;
+}
+
+int AliHLTDataBuffer::AliHLTRawBuffer::CheckPattern(const char* pattern, int size) const
+{
+  // see header file for function documentation
+  int iResult=0;
+  if (pattern!=NULL && size>0) {
+    if (fSize+size<=fTotalSize) {
+      iResult=memcmp(((char*)fPtr)+fSize, pattern, size)!=0;
+    } else {
+      iResult=-ENOSPC;
+    }
+  }
+  return iResult;
+}