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