]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTDataBuffer.cxx
missing static variable added
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTDataBuffer.cxx
CommitLineData
3f2a1b1c 1// $Id$
2
3/**************************************************************************
4 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5 * *
6 * Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
7 * for The ALICE Off-line Project. *
8 * *
9 * Permission to use, copy, modify and distribute this software and its *
10 * documentation strictly for non-commercial purposes is hereby granted *
11 * without fee, provided that the above copyright notice appears in all *
12 * copies and that both the copyright notice and this permission notice *
13 * appear in the supporting documentation. The authors make no claims *
14 * about the suitability of this software for any purpose. It is *
15 * provided "as is" without express or implied warranty. *
16 **************************************************************************/
17
18///////////////////////////////////////////////////////////////////////////////
19// //
20// handling of HLT data buffers //
21// //
22///////////////////////////////////////////////////////////////////////////////
23
0c0c9d99 24#if __GNUC__>= 3
3f2a1b1c 25using namespace std;
26#endif
27
28#include "AliHLTDataBuffer.h"
29#include <string>
30#include "AliHLTSystem.h"
31
32AliHLTConsumerDescriptor::AliHLTConsumerDescriptor()
33{
34 fpConsumer=NULL;
0c0c9d99 35 fSegments.clear();
3f2a1b1c 36}
37
0c0c9d99 38AliHLTConsumerDescriptor::AliHLTConsumerDescriptor(AliHLTComponent* pConsumer)
3f2a1b1c 39{
40 fpConsumer=pConsumer;
0c0c9d99 41 fSegments.clear();
3f2a1b1c 42}
43
44AliHLTConsumerDescriptor::~AliHLTConsumerDescriptor()
45{
0c0c9d99 46 if (fSegments.size()>0) {
47 //HLTWarning("unreleased data segments found");
48 }
3f2a1b1c 49}
50
0c0c9d99 51int AliHLTConsumerDescriptor::SetActiveDataSegment(AliHLTUInt32_t offset, AliHLTUInt32_t size)
3f2a1b1c 52{
53 int iResult=0;
0c0c9d99 54 AliHLTDataSegment segment(offset, size);
55 fSegments.push_back(segment);
56 //HLTDebug("set active segment (%d:%d) for consumer %p", offset, size, this);
3f2a1b1c 57 return iResult;
58}
59
60int AliHLTConsumerDescriptor::CheckActiveDataSegment(AliHLTUInt32_t offset, AliHLTUInt32_t size)
61{
62 int iResult=0;
0c0c9d99 63 if (fSegments.size()>0) {
64 vector<AliHLTDataSegment>::iterator segment=fSegments.begin();
65 while (segment!=fSegments.end()) {
66 if (iResult=((*segment).fSegmentOffset==offset && (*segment).fSegmentSize==size)) {
67 break;
68 }
69 segment++;
70 }
71 } else {
72 //HLTWarning("no data segment active for consumer %p", this);
73 iResult=-ENODATA;
74 }
3f2a1b1c 75 return iResult;
76}
77
0c0c9d99 78int AliHLTConsumerDescriptor::ReleaseActiveDataSegment(AliHLTUInt32_t offset, AliHLTUInt32_t size)
3f2a1b1c 79{
80 int iResult=0;
0c0c9d99 81 if (fSegments.size()>0) {
82 vector<AliHLTDataSegment>::iterator segment=fSegments.begin();
83 while (segment!=fSegments.end()) {
84 if (iResult=((*segment).fSegmentOffset==offset && (*segment).fSegmentSize==size)) {
85 fSegments.erase(segment);
86 break;
87 }
88 segment++;
89 }
90 if (iResult=0) {
91 //HLTWarning("no data segment (%d:%d) active for consumer %p", offset, size, this);
92 iResult=-ENOENT;
93 }
94 } else {
95 //HLTWarning("no data segment active for consumer %p", this);
96 iResult=-ENODATA;
97 }
3f2a1b1c 98 return iResult;
99}
100
101ClassImp(AliHLTDataBuffer)
102
103int AliHLTDataBuffer::fNofInstances=0;
104vector<AliHLTRawBuffer*> AliHLTDataBuffer::fFreeBuffers;
105vector<AliHLTRawBuffer*> AliHLTDataBuffer::fActiveBuffers;
106AliHLTUInt32_t AliHLTDataBuffer::fMargin=1024;
9fd68563 107AliHLTLogging AliHLTDataBuffer::fgLogging;
3f2a1b1c 108
109
110AliHLTDataBuffer::AliHLTDataBuffer()
111{
112 // TODO: do the right initialization
113 //fSegments.empty();
0c0c9d99 114 //fConsumers.empty;
115 //fActiveConsumers.empty;
116 //fReleasedConsumers.empty;
3f2a1b1c 117 fpBuffer=NULL;
118 fFlags=0;
119 fNofInstances++;
120}
121
122AliHLTDataBuffer::~AliHLTDataBuffer()
123{
124 if (--fNofInstances<=0) {
125 DeleteRawBuffers();
126 }
127 CleanupConsumerList();
128}
129
0c0c9d99 130int AliHLTDataBuffer::SetConsumer(AliHLTComponent* pConsumer)
3f2a1b1c 131{
132 int iResult=0;
133 if (pConsumer) {
0c0c9d99 134 AliHLTConsumerDescriptor* pDesc=new AliHLTConsumerDescriptor(pConsumer);
3f2a1b1c 135 if (pDesc) {
136 fConsumers.push_back(pDesc);
137 } else {
138 HLTError("memory allocation failed");
139 iResult=-ENOMEM;
140 }
141 } else {
142 HLTError("invalid parameter");
143 iResult=-EINVAL;
144 }
145 return iResult;
146}
147
0c0c9d99 148int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, vector<AliHLTComponent_DataType>* tgtList)
149{
150 int iResult=0;
151 if (pConsumer) {
152 vector<AliHLTDataSegment> segments;
153 if ((iResult=FindMatchingDataSegments(pConsumer, segments))>=0) {
154 if (tgtList) {
155 vector<AliHLTDataSegment>::iterator segment=segments.begin();
156 while (segment!=segments.end()) {
157 tgtList->push_back((*segment).fDataType);
158 segment++;
159 }
160 }
161 iResult=segments.size();
162 }
163 } else {
164 iResult=-EINVAL;
165 }
166 return iResult;
167}
168
169int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataSegment>& tgtList)
170{
171 int iResult=0;
172 if (pConsumer) {
173 vector<AliHLTComponent_DataType> dtlist;
174 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(dtlist);
175 vector<AliHLTDataSegment>::iterator segment=fSegments.begin();
176 while (segment!=fSegments.end()) {
177 vector<AliHLTComponent_DataType>::iterator type=dtlist.begin();
178 while (type!=dtlist.end()) {
179 if ((*segment).fDataType==(*type)) {
180 tgtList.push_back(*segment);
181 iResult++;
182 break;
183 }
184 type++;
185 }
186 segment++;
187 }
188 } else {
189 iResult=-EINVAL;
190 }
191 return iResult;
192}
193
194int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponent_BlockData* arrayBlockDesc, int iArraySize)
3f2a1b1c 195{
196 int iResult=0;
0c0c9d99 197 if (pConsumer && arrayBlockDesc) {
3f2a1b1c 198 if (fpBuffer) {
0c0c9d99 199 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fConsumers);
3f2a1b1c 200 if (pDesc) {
0c0c9d99 201 vector<AliHLTDataSegment> tgtList;
202 /* TODO: think about a good policy for this check
203 * is it enough that at least one segment is available, or have all to be available?
204 * or is it possible to have optional segments?
205 */
206 if ((iResult=FindMatchingDataSegments(pConsumer, tgtList))>0) {
207 int i =0;
208 vector<AliHLTDataSegment>::iterator segment=tgtList.begin();
209 while (segment!=tgtList.end() && i<iArraySize) {
3f2a1b1c 210 // fill the block data descriptor
0c0c9d99 211 arrayBlockDesc[i].fStructSize=sizeof(AliHLTComponent_BlockData);
3f2a1b1c 212 // the shared memory key is not used in AliRoot
0c0c9d99 213 arrayBlockDesc[i].fShmKey.fStructSize=sizeof(AliHLTComponent_ShmData);
214 arrayBlockDesc[i].fShmKey.fShmType=gkAliHLTComponent_InvalidShmType;
215 arrayBlockDesc[i].fShmKey.fShmID=gkAliHLTComponent_InvalidShmID;
216 arrayBlockDesc[i].fOffset=(*segment).fSegmentOffset;
217 arrayBlockDesc[i].fPtr=fpBuffer->fPtr;
218 arrayBlockDesc[i].fSize=(*segment).fSegmentSize;
219 arrayBlockDesc[i].fDataType=(*segment).fDataType;
220 arrayBlockDesc[i].fSpecification=(*segment).fSpecification;
221 pDesc->SetActiveDataSegment(arrayBlockDesc[i].fOffset, arrayBlockDesc[i].fSize);
222 HLTDebug("component %p (%s) subscribed to segment #%d offset %d", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), i, arrayBlockDesc[i].fOffset);
223 i++;
224 segment++;
225 }
226 // move this consumer to the active list
227 if (ChangeConsumerState(pDesc, fConsumers, fActiveConsumers)>=0) {
228 HLTDebug("component %p (%s) subscribed to data buffer %p", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), this);
3f2a1b1c 229 } else {
0c0c9d99 230 // TODO: cleanup the consumer descriptor correctly
231 memset(arrayBlockDesc, 0, iArraySize*sizeof(AliHLTComponent_BlockData));
3f2a1b1c 232 HLTError("can not activate consumer %p for data buffer %p", pConsumer, this);
233 iResult=-EACCES;
234 }
235 } else {
0c0c9d99 236 HLTError("unresolved data segment(s) for component %p (%s)", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
3f2a1b1c 237 iResult=-EBADF;
238 }
239 } else {
0c0c9d99 240 HLTError("component %p is not a data consumer of data buffer %s", pConsumer, this);
3f2a1b1c 241 iResult=-ENOENT;
242 }
243 } else {
244 HLTError("data buffer %p is empty", this);
245 iResult=-ENODATA;
246 }
247 } else {
248 HLTError("invalid parameter");
249 iResult=-EINVAL;
250 }
251 return iResult;
252}
253
254int AliHLTDataBuffer::Release(AliHLTComponent_BlockData* pBlockDesc, const AliHLTComponent* pConsumer)
255{
256 int iResult=0;
257 if (pBlockDesc && pConsumer) {
0c0c9d99 258 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fActiveConsumers);
259 if (pDesc) {
260 if ((iResult=pDesc->CheckActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize))!=1) {
261 HLTWarning("data segment missmatch, component %p has not subscribed to a segment with offset %#x and size %d", pConsumer, pBlockDesc->fOffset, pBlockDesc->fSize);
262 // TODO: appropriate error handling, but so far optional
263 iResult=0;
264 } else {
265 pDesc->ReleaseActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize);
3f2a1b1c 266 pBlockDesc->fOffset=0;
267 pBlockDesc->fPtr=NULL;
268 pBlockDesc->fSize=0;
0c0c9d99 269 }
270 if (pDesc->GetNofActiveSegments()==0) {
3f2a1b1c 271 if ((iResult=ChangeConsumerState(pDesc, fActiveConsumers, fReleasedConsumers))>=0) {
272 if (GetNofActiveConsumers()==0) {
0c0c9d99 273 // this is the last consumer, reset the consumer list and release the raw buffer
3f2a1b1c 274 ResetDataBuffer();
3f2a1b1c 275 }
276 } else {
277 HLTError("can not deactivate consumer %p for data buffer %p", pConsumer, this);
278 iResult=-EACCES;
279 }
3f2a1b1c 280 }
0c0c9d99 281 } else {
282 HLTWarning("component %p has currently not subscribed to the data buffer %p", pConsumer, this);
283 iResult=-ENOENT;
284 }
3f2a1b1c 285 } else {
286 HLTError("inavalid parameter: pBlockDesc=%p pConsumer=%p", pBlockDesc, pConsumer);
287 iResult=-EINVAL;
288 }
289 return iResult;
290}
291
292AliHLTUInt8_t* AliHLTDataBuffer::GetTargetBuffer(int iMinSize)
293{
294 AliHLTUInt8_t* pTargetBuffer=NULL;
295 fpBuffer=CreateRawBuffer(iMinSize);
296 pTargetBuffer=(AliHLTUInt8_t*)fpBuffer;
297 return pTargetBuffer;
298}
299
300int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponent_BlockData* arrayBlockData, int iSize)
301{
302 int iResult=0;
303 if (pTgt && arrayBlockData && iSize>=0) {
0c0c9d99 304 if (fpBuffer) {
305 if (fpBuffer->fPtr==(void*)pTgt) {
306 AliHLTDataSegment segment;
307 memset(&segment, 0, sizeof(AliHLTDataSegment));
308 for (int i=0; i<iSize; i++) {
309 if (arrayBlockData[i].fOffset+arrayBlockData[i].fSize<fpBuffer->fSize) {
310 segment.fSegmentOffset=arrayBlockData[i].fOffset;
311 segment.fSegmentSize=arrayBlockData[i].fSize;
312 segment.fDataType=arrayBlockData[i].fDataType;
313 segment.fSpecification=arrayBlockData[i].fSpecification;
314 fSegments.push_back(segment);
315 } else {
316 HLTError("block data specification #%d (%s@%s) exceeds size of data buffer", i, arrayBlockData[i].fDataType.fOrigin, arrayBlockData[i].fDataType.fID);
317 }
318 }
3f2a1b1c 319 } else {
0c0c9d99 320 HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->fPtr, fpBuffer);
3f2a1b1c 321 }
0c0c9d99 322 } else {
323 HLTFatal("internal data structur missmatch");
324 iResult=-EFAULT;
3f2a1b1c 325 }
326 } else {
0c0c9d99 327 HLTError("invalid parameter: pTgtBuffer=%p arrayBlockData=%p", pTgt, arrayBlockData);
3f2a1b1c 328 iResult=-EINVAL;
329 }
330 return iResult;
331}
332
333int AliHLTDataBuffer::IsEmpty()
334{
335 int iResult=fpBuffer==NULL || GetNofSegments()==0;
336 return iResult;
337}
338
339int AliHLTDataBuffer::GetNofSegments()
340{
341 int iResult=fSegments.size();
342 return iResult;
343}
344
345int AliHLTDataBuffer::GetNofConsumers()
346{
347 int iResult=fConsumers.size() + GetNofActiveConsumers() + fReleasedConsumers.size();
348 return iResult;
349}
350
351int AliHLTDataBuffer::GetNofActiveConsumers()
352{
353 int iResult=fActiveConsumers.size();
354 return iResult;
355}
356
357AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
358{
359 AliHLTRawBuffer* pRawBuffer=NULL;
360 vector<AliHLTRawBuffer*>::iterator buffer=fFreeBuffers.begin();
361 while (buffer!=fFreeBuffers.end() && pRawBuffer==NULL) {
362 if ((*buffer)->fTotalSize>=size && ((*buffer)->fTotalSize-size)<fMargin) {
363 // assign this element
364 pRawBuffer=*buffer;
365 pRawBuffer->fSize=size;
366 fFreeBuffers.erase(buffer);
0c0c9d99 367 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->fTotalSize, pRawBuffer->fPtr);
3f2a1b1c 368 fActiveBuffers.push_back(pRawBuffer);
0c0c9d99 369 break;
3f2a1b1c 370 }
0c0c9d99 371 buffer++;
3f2a1b1c 372 }
373 if (pRawBuffer==NULL) {
0c0c9d99 374 // no buffer found, create a new one
3f2a1b1c 375 pRawBuffer=new AliHLTRawBuffer;
376 if (pRawBuffer) {
377 memset(pRawBuffer, 0, sizeof(AliHLTRawBuffer));
378 pRawBuffer->fPtr=malloc(size);
379 if (pRawBuffer->fPtr) {
380 pRawBuffer->fSize=size;
381 pRawBuffer->fTotalSize=size;
382 fActiveBuffers.push_back(pRawBuffer);
0c0c9d99 383 fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->fPtr, pRawBuffer->fTotalSize, pRawBuffer);
3f2a1b1c 384 } else {
385 delete pRawBuffer;
386 pRawBuffer=NULL;
0c0c9d99 387 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
3f2a1b1c 388 }
389 } else {
0c0c9d99 390 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
3f2a1b1c 391 }
392 }
393 return pRawBuffer;
394}
395
396int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
397{
398 int iResult=0;
399 if (pBuffer) {
400 vector<AliHLTRawBuffer*>::iterator buffer=fActiveBuffers.begin();
401 while (buffer!=fActiveBuffers.end() && (*buffer)!=pBuffer) {
402 buffer++;
403 }
404 if (buffer!=fActiveBuffers.end()) {
405 (*buffer)->fSize=0;
406 fFreeBuffers.push_back(*buffer);
407 fActiveBuffers.erase(buffer);
408 } else {
0c0c9d99 409 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
3f2a1b1c 410 iResult=-ENOENT;
411 }
412 } else {
0c0c9d99 413 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "invalid parameter");
3f2a1b1c 414 iResult=-EINVAL;
415 }
416 return iResult;
417}
418
419
420int AliHLTDataBuffer::DeleteRawBuffers()
421{
422 int iResult=0;
423 vector<AliHLTRawBuffer*>::iterator buffer=fFreeBuffers.begin();
424 while (buffer!=fFreeBuffers.end()) {
425 free((*buffer)->fPtr);
426 delete *buffer;
427 fFreeBuffers.erase(buffer);
428 buffer=fFreeBuffers.begin();
429 }
430 buffer=fActiveBuffers.begin();
431 while (buffer!=fFreeBuffers.end()) {
0c0c9d99 432 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "request to delete active raw buffer container (raw buffer %p, size %d)", (*buffer)->fPtr, (*buffer)->fTotalSize);
3f2a1b1c 433 free((*buffer)->fPtr);
434 delete *buffer;
435 fActiveBuffers.erase(buffer);
436 buffer=fActiveBuffers.begin();
437 }
438 return iResult;
439}
440
0c0c9d99 441AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, vector<AliHLTConsumerDescriptor*> &list)
3f2a1b1c 442{
443 AliHLTConsumerDescriptor* pDesc=NULL;
444 vector<AliHLTConsumerDescriptor*>::iterator desc=list.begin();
445 while (desc!=list.end() && pDesc==NULL) {
0c0c9d99 446 if ((pConsumer==NULL || (*desc)->GetComponent()==pConsumer)) {
3f2a1b1c 447 pDesc=*desc;
448 }
0c0c9d99 449 desc++;
3f2a1b1c 450 }
451 return pDesc;
452}
453
0c0c9d99 454int AliHLTDataBuffer::ResetDataBuffer()
455{
3f2a1b1c 456 int iResult=0;
0c0c9d99 457 AliHLTRawBuffer* pBuffer=fpBuffer;
3f2a1b1c 458 fpBuffer=NULL;
459 vector<AliHLTConsumerDescriptor*>::iterator desc=fReleasedConsumers.begin();
460 while (desc!=fReleasedConsumers.end()) {
461 AliHLTConsumerDescriptor* pDesc=*desc;
462 fReleasedConsumers.erase(desc);
463 desc=fReleasedConsumers.begin();
464 fConsumers.push_back(pDesc);
465 }
466 desc=fActiveConsumers.begin();
467 while (desc!=fActiveConsumers.end()) {
468 AliHLTConsumerDescriptor* pDesc=*desc;
469 HLTWarning("consumer %p was not released", pDesc);
470 fActiveConsumers.erase(desc);
471 desc=fActiveConsumers.begin();
472 fConsumers.push_back(pDesc);
473 }
0c0c9d99 474 ReleaseRawBuffer(pBuffer);
3f2a1b1c 475 return iResult;
476}
477
0c0c9d99 478// this is the version which works on lists of components instead of consumer descriptors
479// int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, vector<AliHLTComponent*> &srcList, vector<AliHLTComponent*> &tgtList)
480// {
481// int iResult=0;
482// if (pDesc) {
483// vector<AliHLTComponent*>::iterator desc=srcList.begin();
484// while (desc!=srcList.end()) {
485// if ((*desc)==pConsumer) {
486// srcList.erase(desc);
487// tgtList.push_back(pConsumer);
488// break;
489// }
490// desc++;
491// }
492// if (desc==srcList.end()) {
493// HLTError("can not find consumer component %p in list", pConsumer);
494// iResult=-ENOENT;
495// }
496// } else {
497// HLTError("invalid parameter");
498// iResult=-EINVAL;
499// }
500// return iResult;
501// }
502
3f2a1b1c 503int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, vector<AliHLTConsumerDescriptor*> &srcList, vector<AliHLTConsumerDescriptor*> &tgtList)
504{
505 int iResult=0;
506 if (pDesc) {
507 vector<AliHLTConsumerDescriptor*>::iterator desc=srcList.begin();
508 while (desc!=srcList.end()) {
509 if ((*desc)==pDesc) {
510 srcList.erase(desc);
511 tgtList.push_back(pDesc);
512 break;
513 }
0c0c9d99 514 desc++;
3f2a1b1c 515 }
516 if (desc==srcList.end()) {
517 HLTError("can not find consumer descriptor %p in list", pDesc);
518 iResult=-ENOENT;
519 }
520 } else {
521 HLTError("invalid parameter");
522 iResult=-EINVAL;
523 }
524 return iResult;
525}
526
527int AliHLTDataBuffer::CleanupConsumerList() {
528 int iResult=0;
529 ResetDataBuffer();
530 vector<AliHLTConsumerDescriptor*>::iterator desc=fConsumers.begin();
531 while (desc!=fConsumers.end()) {
532 delete *desc;
533 fConsumers.erase(desc);
534 desc=fConsumers.begin();
535 }
536 return iResult;
537}