]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTDataBuffer.cxx
Minor cleanup of code.
[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 {
0c0c9d99 223 HLTError("component %p is not a data consumer of data buffer %s", pConsumer, this);
3f2a1b1c 224 iResult=-ENOENT;
225 }
226 } else {
9b7fe12d 227 // Matthias 26.07.2007 until now, data had to be present for successful subscription
228 // in order to be consistent with the PubSub framework, this restiction has been
229 // removed
230 //HLTError("data buffer %p is empty", this);
231 //iResult=-ENODATA;
3f2a1b1c 232 }
233 } else {
234 HLTError("invalid parameter");
235 iResult=-EINVAL;
236 }
237 return iResult;
238}
239
b46ca65e 240int AliHLTDataBuffer::Release(AliHLTComponentBlockData* pBlockDesc,
241 const AliHLTComponent* pConsumer,
242 const AliHLTTask* pOwnerTask)
3f2a1b1c 243{
70ed7d01 244 // see header file for function documentation
3f2a1b1c 245 int iResult=0;
246 if (pBlockDesc && pConsumer) {
0c0c9d99 247 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fActiveConsumers);
248 if (pDesc) {
b46ca65e 249 if ((iResult=pDesc->CheckActiveDataSegment(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize)))!=1) {
c5123824 250 HLTWarning("data segment mismatch, component %p has not subscribed to a segment with offset %#x and size %d", pConsumer, pBlockDesc->fOffset, pBlockDesc->fSize);
0c0c9d99 251 // TODO: appropriate error handling, but so far optional
252 iResult=0;
253 } else {
b46ca65e 254 pDesc->ReleaseActiveDataSegment(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize));
255 }
256 if (GetNofPendingConsumers()==0 && fForwardedSegments.size()>0) {
257 // last consumer, release forwarded segments
258 assert(fForwardedSegments.size()==fForwardedSegmentSources.size());
259 AliHLTDataSegmentList::iterator segment=fForwardedSegments.begin();
260 AliHLTTaskPList::iterator src=fForwardedSegmentSources.begin();
261 //HLTDebug("%p checking forwarded segments", this);
262 for (; segment!=fForwardedSegments.end(); segment++, src++) {
263 //HLTDebug("segment ptr=%p offset=%d size=%d\n"
264 // "block ptr=%p offset=%d size=%d", (*segment).fPtr, (*segment).fSegmentOffset, (*segment).fSegmentSize, pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize);
265 if ((*segment)==AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize)) {
266 //HLTDebug("release segment of task %p", *src);
267 assert((*src)!=NULL);
268 if ((*src)!=NULL) {
269 if ((*src)->Release(pBlockDesc, pOwnerTask)>=0) {
270 HLTDebug("task %s (%p) released forwarded segment %p size %d of task %s (%p)",
271 pOwnerTask->GetName(), pOwnerTask, (*segment).GetPtr(), (*segment).GetSize(),
272 (*src)->GetName(), *src);
273 } else {
274 HLTError("task %s (%p) failed releasing forwarded segment %p size %d of task %s (%p)",
275 pOwnerTask->GetName(), pOwnerTask, (*segment).GetPtr(), (*segment).GetSize(),
276 (*src)->GetName(), *src);
277 }
278 }
279 fForwardedSegments.erase(segment);
280 fForwardedSegmentSources.erase(src);
281 break;
282 }
283 }
0c0c9d99 284 }
b46ca65e 285 pBlockDesc->fOffset=0;
286 pBlockDesc->fPtr=NULL;
287 pBlockDesc->fSize=0;
0c0c9d99 288 if (pDesc->GetNofActiveSegments()==0) {
3f2a1b1c 289 if ((iResult=ChangeConsumerState(pDesc, fActiveConsumers, fReleasedConsumers))>=0) {
b426991e 290 if (GetNofActiveConsumers()==0 && GetNofPendingConsumers()==0) {
0c0c9d99 291 // this is the last consumer, reset the consumer list and release the raw buffer
3f2a1b1c 292 ResetDataBuffer();
3f2a1b1c 293 }
294 } else {
295 HLTError("can not deactivate consumer %p for data buffer %p", pConsumer, this);
296 iResult=-EACCES;
297 }
3f2a1b1c 298 }
0c0c9d99 299 } else {
300 HLTWarning("component %p has currently not subscribed to the data buffer %p", pConsumer, this);
301 iResult=-ENOENT;
302 }
3f2a1b1c 303 } else {
304 HLTError("inavalid parameter: pBlockDesc=%p pConsumer=%p", pBlockDesc, pConsumer);
305 iResult=-EINVAL;
306 }
307 return iResult;
308}
309
b46ca65e 310int AliHLTDataBuffer::Forward(AliHLTTask* pSrcTask, AliHLTComponentBlockData* pBlockDesc)
311{
312 // see header file for function documentation
313 if (pSrcTask==NULL || pBlockDesc==NULL) return -EINVAL;
314 assert(fForwardedSegments.size()==fForwardedSegmentSources.size());
315 if (fForwardedSegments.size()!=fForwardedSegmentSources.size()) return -EFAULT;
316 fForwardedSegmentSources.push_back(pSrcTask);
457ec821 317 fForwardedSegments.push_back(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize, pBlockDesc->fDataType, pBlockDesc->fSpecification));
b46ca65e 318 return 0;
319}
320
3f2a1b1c 321AliHLTUInt8_t* AliHLTDataBuffer::GetTargetBuffer(int iMinSize)
322{
70ed7d01 323 // see header file for function documentation
3f2a1b1c 324 AliHLTUInt8_t* pTargetBuffer=NULL;
b6800be0 325 if (fpBuffer!=NULL) {
326 HLTWarning("data buffer not properly reset, possible memory leak\n");
327 }
3f2a1b1c 328 fpBuffer=CreateRawBuffer(iMinSize);
9ce4bf4a 329 if (fpBuffer) {
1e6e67ec 330 pTargetBuffer=*fpBuffer;
9ce4bf4a 331 } else {
332 HLTError("can not create raw buffer");
333 }
3f2a1b1c 334 return pTargetBuffer;
335}
336
8ede8717 337int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData* arrayBlockData, int iSize)
3f2a1b1c 338{
70ed7d01 339 // see header file for function documentation
3f2a1b1c 340 int iResult=0;
341 if (pTgt && arrayBlockData && iSize>=0) {
0c0c9d99 342 if (fpBuffer) {
1e6e67ec 343 if (*fpBuffer==pTgt) {
6235cd38 344 AliHLTDataBuffer::AliHLTDataSegment segment;
0c0c9d99 345 for (int i=0; i<iSize; i++) {
3294f81a 346 // This function has to model the behavior of PubSub
347 // For output blocks only the fOffset value is used, this must be the offset
348 // relative to the output pointer. fPtr must be either NULL or the output
b46ca65e 349 // pointer. In either case it is 'ignored' and set to the beginning of the
350 // data buffer
6434d28a 351 if (arrayBlockData[i].fPtr==NULL ||
3294f81a 352 arrayBlockData[i].fPtr==*fpBuffer) {
b46ca65e 353 arrayBlockData[i].fPtr=*fpBuffer;
c0a2bfc2 354 if (arrayBlockData[i].fOffset+arrayBlockData[i].fSize<=fpBuffer->GetUsedSize()) {
3294f81a 355 segment.fSegmentOffset=arrayBlockData[i].fOffset;
b46ca65e 356 segment.fPtr=(AliHLTUInt8_t*)arrayBlockData[i].fPtr;
1e6e67ec 357 segment.fSegmentSize=arrayBlockData[i].fSize;
358 segment.fDataType=arrayBlockData[i].fDataType;
359 segment.fSpecification=arrayBlockData[i].fSpecification;
360 fSegments.push_back(segment);
361 HLTDebug("set segment %s with size %d at offset %d", AliHLTComponent::DataType2Text(segment.fDataType).data(), segment.fSegmentSize, segment.fSegmentOffset);
362 } else {
363 HLTError("block data specification %#d (%s) exceeds size of data buffer", i, AliHLTComponent::DataType2Text(arrayBlockData[i].fDataType).data());
c0a2bfc2 364 HLTError("block offset=%d, block size=%d, buffer size=%d", arrayBlockData[i].fOffset, arrayBlockData[i].fSize, fpBuffer->GetUsedSize());
1e6e67ec 365 iResult=-E2BIG;
366 }
0c0c9d99 367 } else {
3294f81a 368 HLTError("invalid pointer (%p) in block data specification (buffer %p size %d)."
369 "please note: for output blocks only the fOffset value is valid and must "
c0a2bfc2 370 "be relative to the output buffer", arrayBlockData[i].fPtr, fpBuffer->GetPointer(), fpBuffer->GetUsedSize());
1e6e67ec 371 iResult=-ERANGE;
0c0c9d99 372 }
373 }
3f2a1b1c 374 } else {
c0a2bfc2 375 HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->GetPointer(), fpBuffer);
8451168b 376 iResult=-EINVAL;
3f2a1b1c 377 }
0c0c9d99 378 } else {
c5123824 379 HLTFatal("internal data structur mismatch");
0c0c9d99 380 iResult=-EFAULT;
3f2a1b1c 381 }
382 } else {
0c0c9d99 383 HLTError("invalid parameter: pTgtBuffer=%p arrayBlockData=%p", pTgt, arrayBlockData);
3f2a1b1c 384 iResult=-EINVAL;
385 }
386 return iResult;
387}
388
389int AliHLTDataBuffer::IsEmpty()
390{
70ed7d01 391 // see header file for function documentation
3f2a1b1c 392 int iResult=fpBuffer==NULL || GetNofSegments()==0;
393 return iResult;
394}
395
396int AliHLTDataBuffer::GetNofSegments()
397{
70ed7d01 398 // see header file for function documentation
3f2a1b1c 399 int iResult=fSegments.size();
400 return iResult;
401}
402
403int AliHLTDataBuffer::GetNofConsumers()
404{
70ed7d01 405 // see header file for function documentation
3f2a1b1c 406 int iResult=fConsumers.size() + GetNofActiveConsumers() + fReleasedConsumers.size();
407 return iResult;
408}
409
b426991e 410int AliHLTDataBuffer::GetNofPendingConsumers()
411{
412 // see header file for function documentation
413 int iResult=fConsumers.size();
414 return iResult;
415}
416
3f2a1b1c 417int AliHLTDataBuffer::GetNofActiveConsumers()
418{
70ed7d01 419 // see header file for function documentation
3f2a1b1c 420 int iResult=fActiveConsumers.size();
421 return iResult;
422}
423
6235cd38 424AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
3f2a1b1c 425{
70ed7d01 426 // see header file for function documentation
3f2a1b1c 427 AliHLTRawBuffer* pRawBuffer=NULL;
7a5ccd96 428 unsigned int reqSize=size+fgkSafetyPatternSize;
2be3f004 429 AliHLTRawBufferPList::iterator buffer=fgFreeBuffers.begin();
70ed7d01 430 while (buffer!=fgFreeBuffers.end() && pRawBuffer==NULL) {
d6cbe999 431 if ((*buffer)->CheckSize(reqSize)) {
3f2a1b1c 432 // assign this element
433 pRawBuffer=*buffer;
d6cbe999 434 pRawBuffer->UseBuffer(size);
70ed7d01 435 fgFreeBuffers.erase(buffer);
c0a2bfc2 436 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 437 fgActiveBuffers.push_back(pRawBuffer);
0c0c9d99 438 break;
3f2a1b1c 439 }
0c0c9d99 440 buffer++;
3f2a1b1c 441 }
442 if (pRawBuffer==NULL) {
0c0c9d99 443 // no buffer found, create a new one
d6cbe999 444 pRawBuffer=new AliHLTRawBuffer(reqSize);
3f2a1b1c 445 if (pRawBuffer) {
c0a2bfc2 446 if (pRawBuffer->GetPointer()) {
d6cbe999 447 pRawBuffer->UseBuffer(size);
70ed7d01 448 fgActiveBuffers.push_back(pRawBuffer);
c0a2bfc2 449 fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->GetPointer(), pRawBuffer->GetTotalSize(), pRawBuffer);
3f2a1b1c 450 } else {
451 delete pRawBuffer;
452 pRawBuffer=NULL;
0c0c9d99 453 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
3f2a1b1c 454 }
455 } else {
0c0c9d99 456 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
3f2a1b1c 457 }
458 }
73ede1d3 459 if (pRawBuffer!=NULL && fgkSafetyPatternSize>0) {
c0a2bfc2 460 //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "writing safety pattern to %p offset %d", (*buffer)->GetPointer(), (*buffer)->GetUsedSize());
49b01d04 461 pRawBuffer->WritePattern(fgkSafetyPattern, fgkSafetyPatternSize);
73ede1d3 462 }
3f2a1b1c 463 return pRawBuffer;
464}
465
466int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
467{
70ed7d01 468 // see header file for function documentation
3f2a1b1c 469 int iResult=0;
470 if (pBuffer) {
2be3f004 471 AliHLTRawBufferPList::iterator buffer=fgActiveBuffers.begin();
70ed7d01 472 while (buffer!=fgActiveBuffers.end() && (*buffer)!=pBuffer) {
3f2a1b1c 473 buffer++;
474 }
70ed7d01 475 if (buffer!=fgActiveBuffers.end()) {
8451168b 476 if (fgkSafetyPatternSize>0) {
c0a2bfc2 477 //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "comparing safety pattern at %p offset %d", (*buffer)->GetPointer(), reinterpret_cast<AliHLTUInt32_t>(*buffer));
d6cbe999 478 if ((*buffer)->CheckPattern(fgkSafetyPattern, fgkSafetyPatternSize)) {
c0a2bfc2 479 fgLogging.Logging(kHLTLogFatal, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "component has written beyond end of data buffer %p size %d", (*buffer)->GetPointer(), (*buffer)->GetUsedSize());
8451168b 480 }
481 }
d6cbe999 482 (*buffer)->Reset();
70ed7d01 483 fgFreeBuffers.push_back(*buffer);
484 fgActiveBuffers.erase(buffer);
3f2a1b1c 485 } else {
0c0c9d99 486 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
3f2a1b1c 487 iResult=-ENOENT;
488 }
489 } else {
0c0c9d99 490 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "invalid parameter");
3f2a1b1c 491 iResult=-EINVAL;
492 }
493 return iResult;
494}
495
496
497int AliHLTDataBuffer::DeleteRawBuffers()
498{
70ed7d01 499 // see header file for function documentation
3f2a1b1c 500 int iResult=0;
dba03d72 501// int iTotalSize=0;
502// int iCount=fgFreeBuffers.size()+fgActiveBuffers.size();
503 AliHLTRawBufferPList::iterator buffer;;
504 while ((buffer=fgFreeBuffers.begin())!=fgFreeBuffers.end()) {
505// iTotalSize+=(*buffer)->GetTotalSize();
3f2a1b1c 506 delete *buffer;
70ed7d01 507 fgFreeBuffers.erase(buffer);
3f2a1b1c 508 }
dba03d72 509 while ((buffer=fgActiveBuffers.begin())!=fgActiveBuffers.end()) {
510// iTotalSize+=(*buffer)->GetTotalSize();
c0a2bfc2 511 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 512 delete *buffer;
70ed7d01 513 fgActiveBuffers.erase(buffer);
3f2a1b1c 514 }
dba03d72 515// fgLogging.Logging(kHLTLogInfo, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "Total memory allocation: %d byte in %d buffers", iTotalSize, iCount);
3f2a1b1c 516 return iResult;
517}
518
2be3f004 519AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, AliHLTConsumerDescriptorPList &list) const
3f2a1b1c 520{
70ed7d01 521 // see header file for function documentation
3f2a1b1c 522 AliHLTConsumerDescriptor* pDesc=NULL;
2be3f004 523 AliHLTConsumerDescriptorPList::iterator desc=list.begin();
3f2a1b1c 524 while (desc!=list.end() && pDesc==NULL) {
0c0c9d99 525 if ((pConsumer==NULL || (*desc)->GetComponent()==pConsumer)) {
3f2a1b1c 526 pDesc=*desc;
527 }
0c0c9d99 528 desc++;
3f2a1b1c 529 }
530 return pDesc;
531}
532
0c0c9d99 533int AliHLTDataBuffer::ResetDataBuffer()
534{
70ed7d01 535 // see header file for function documentation
3f2a1b1c 536 int iResult=0;
0c0c9d99 537 AliHLTRawBuffer* pBuffer=fpBuffer;
3f2a1b1c 538 fpBuffer=NULL;
9ce4bf4a 539
b46ca65e 540 // cleanup forwarded segment lists
541 assert(fForwardedSegments.size()==0);
542 fForwardedSegments.clear();
543 fForwardedSegmentSources.clear();
544
9ce4bf4a 545 // cleanup consumer states
2be3f004 546 AliHLTConsumerDescriptorPList::iterator desc;
b426991e 547// if (GetNofPendingConsumers()>0) {
548// desc=fConsumers.begin();
549// while (desc!=fConsumers.end()) {
550// AliHLTComponent* pComp=(*desc)->GetComponent();
551// HLTError("internal error: consumer %p (%s %p) did not get data from data buffer %p", *desc, pComp?pComp->GetComponentID():"", pComp, this);
552// desc++;
553// }
554// }
555 desc=fReleasedConsumers.begin();
3f2a1b1c 556 while (desc!=fReleasedConsumers.end()) {
557 AliHLTConsumerDescriptor* pDesc=*desc;
558 fReleasedConsumers.erase(desc);
559 desc=fReleasedConsumers.begin();
560 fConsumers.push_back(pDesc);
561 }
562 desc=fActiveConsumers.begin();
563 while (desc!=fActiveConsumers.end()) {
564 AliHLTConsumerDescriptor* pDesc=*desc;
565 HLTWarning("consumer %p was not released", pDesc);
566 fActiveConsumers.erase(desc);
567 desc=fActiveConsumers.begin();
568 fConsumers.push_back(pDesc);
569 }
9ce4bf4a 570
571 // cleanup segments
2be3f004 572 AliHLTDataSegmentList::iterator segment=fSegments.begin();
9ce4bf4a 573 while (segment!=fSegments.end()) {
574 fSegments.erase(segment);
575 segment=fSegments.begin();
576 }
577
578 // cleanup raw buffer
579 if (pBuffer) {
580 ReleaseRawBuffer(pBuffer);
581 }
3f2a1b1c 582 return iResult;
583}
584
9ce4bf4a 585int AliHLTDataBuffer::Reset()
586{
70ed7d01 587 // see header file for function documentation
9ce4bf4a 588 return ResetDataBuffer();
589}
590
0c0c9d99 591// this is the version which works on lists of components instead of consumer descriptors
2be3f004 592// int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, AliHLTComponentPList &srcList, AliHLTComponentPList &tgtList)
0c0c9d99 593// {
594// int iResult=0;
595// if (pDesc) {
2be3f004 596// AliHLTComponentPList::iterator desc=srcList.begin();
0c0c9d99 597// while (desc!=srcList.end()) {
598// if ((*desc)==pConsumer) {
599// srcList.erase(desc);
600// tgtList.push_back(pConsumer);
601// break;
602// }
603// desc++;
604// }
605// if (desc==srcList.end()) {
606// HLTError("can not find consumer component %p in list", pConsumer);
607// iResult=-ENOENT;
608// }
609// } else {
610// HLTError("invalid parameter");
611// iResult=-EINVAL;
612// }
613// return iResult;
614// }
615
2be3f004 616int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, AliHLTConsumerDescriptorPList &srcList, AliHLTConsumerDescriptorPList &tgtList)
3f2a1b1c 617{
70ed7d01 618 // see header file for function documentation
9ce4bf4a 619 int iResult=-ENOENT;
3f2a1b1c 620 if (pDesc) {
2be3f004 621 AliHLTConsumerDescriptorPList::iterator desc=srcList.begin();
3f2a1b1c 622 while (desc!=srcList.end()) {
623 if ((*desc)==pDesc) {
624 srcList.erase(desc);
625 tgtList.push_back(pDesc);
9ce4bf4a 626 iResult=0;
3f2a1b1c 627 break;
628 }
0c0c9d99 629 desc++;
3f2a1b1c 630 }
9ce4bf4a 631 if (iResult<0) {
3f2a1b1c 632 HLTError("can not find consumer descriptor %p in list", pDesc);
3f2a1b1c 633 }
634 } else {
635 HLTError("invalid parameter");
636 iResult=-EINVAL;
637 }
638 return iResult;
639}
640
70ed7d01 641int AliHLTDataBuffer::CleanupConsumerList()
642{
643 // see header file for function documentation
3f2a1b1c 644 int iResult=0;
645 ResetDataBuffer();
2be3f004 646 AliHLTConsumerDescriptorPList::iterator desc=fConsumers.begin();
3f2a1b1c 647 while (desc!=fConsumers.end()) {
648 delete *desc;
649 fConsumers.erase(desc);
650 desc=fConsumers.begin();
651 }
652 return iResult;
653}
9ce4bf4a 654
70ed7d01 655int AliHLTDataBuffer::FindConsumer(AliHLTComponent* pConsumer, int bAllLists)
656{
657 // see header file for function documentation
2be3f004 658 AliHLTConsumerDescriptorPList::iterator desc=fConsumers.begin();
9ce4bf4a 659 while (desc!=fConsumers.end()) {
660 if ((*desc)->GetComponent()==pConsumer)
661 return 1;
662 desc++;
663 }
664 if (bAllLists==0) return 0;
665
666 desc=fActiveConsumers.begin();
667 while (desc!=fActiveConsumers.end()) {
668 if ((*desc)->GetComponent()==pConsumer)
669 return 1;
670 desc++;
671 }
672 desc=fReleasedConsumers.begin();
673 while (desc!=fReleasedConsumers.end()) {
674 if ((*desc)->GetComponent()==pConsumer)
675 return 1;
676 desc++;
677 }
678 return 0;
679}
1e6e67ec 680
d6cbe999 681AliHLTDataBuffer::AliHLTRawBuffer::AliHLTRawBuffer(AliHLTUInt32_t size)
682 :
c0a2bfc2 683 fSize(0),
d6cbe999 684 fTotalSize(size),
685 fPtr(static_cast<AliHLTUInt8_t*>(malloc(size)))
686{
687 // see header file for class documentation
688 // or
689 // refer to README to build package
690 // or
691 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
692 if (fPtr==NULL) {
693 fSize=0;
694 fTotalSize=0;
695 }
696}
697
698AliHLTDataBuffer::AliHLTRawBuffer::~AliHLTRawBuffer()
699{
700 if (fPtr) {
701 free(fPtr);
702 }
703 fPtr=NULL;
704 fSize=0;
705 fTotalSize=0;
706}
707
3a7c0444 708int AliHLTDataBuffer::AliHLTRawBuffer::operator==(void* ptr) const
1e6e67ec 709{
3a7c0444 710 // see header file for function documentation
1e6e67ec 711 return fPtr == static_cast<AliHLTUInt8_t*>(ptr);
712}
713
3a7c0444 714int AliHLTDataBuffer::AliHLTRawBuffer::operator<=(void* ptr) const
1e6e67ec 715{
3a7c0444 716 // see header file for function documentation
1e6e67ec 717 int iResult=fPtr <= static_cast<AliHLTUInt8_t*>(ptr);
718 //printf("%p: %p <= %p (%d)\n", this, fPtr, ptr, iResult);
719 return iResult;
720}
721
3a7c0444 722int AliHLTDataBuffer::AliHLTRawBuffer::operator>(void* ptr) const
1e6e67ec 723{
3a7c0444 724 // see header file for function documentation
1e6e67ec 725 int iResult=fPtr+fSize > static_cast<AliHLTUInt8_t*>(ptr);
726 //printf("%p: %p + %d > %p (%d)\n", this, fPtr, fSize, ptr, iResult);
727 return iResult;
728}
729
3a7c0444 730int AliHLTDataBuffer::AliHLTRawBuffer::operator-(void* ptr) const
1e6e67ec 731{
3a7c0444 732 // see header file for function documentation
1e6e67ec 733 return static_cast<int>(static_cast<AliHLTUInt8_t*>(ptr)-fPtr);
734}
d6cbe999 735
736AliHLTUInt8_t* AliHLTDataBuffer::AliHLTRawBuffer::UseBuffer(AliHLTUInt32_t size)
737{
738 // see header file for function documentation
dba03d72 739 if (size>0 && fTotalSize>=size) {
d6cbe999 740 fSize=size;
741 return fPtr;
742 }
743 return NULL;
744}
745
746int AliHLTDataBuffer::AliHLTRawBuffer::CheckSize(AliHLTUInt32_t size) const
747{
748 // see header file for function documentation
749 return fTotalSize>=size && ((fTotalSize-size)<fgMargin);
750}
751
752int AliHLTDataBuffer::AliHLTRawBuffer::Reset()
753{
754 // see header file for function documentation
755 fSize=0;
2be3f004 756 return 0;
d6cbe999 757}
758
759int AliHLTDataBuffer::AliHLTRawBuffer::WritePattern(const char* pattern, int size)
760{
761 // see header file for function documentation
762 int iResult=0;
763 if (pattern!=NULL && size>0) {
764 if (fSize+size<=fTotalSize) {
765 memcpy(((char*)fPtr)+fSize, pattern, size);
766 iResult=size;
767 } else {
768 iResult=-ENOSPC;
769 }
770 }
771 return iResult;
772}
773
774int AliHLTDataBuffer::AliHLTRawBuffer::CheckPattern(const char* pattern, int size) const
775{
776 // see header file for function documentation
777 int iResult=0;
778 if (pattern!=NULL && size>0) {
779 if (fSize+size<=fTotalSize) {
780 iResult=memcmp(((char*)fPtr)+fSize, pattern, size)!=0;
781 } else {
782 iResult=-ENOSPC;
783 }
784 }
785 return iResult;
786}