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