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