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