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