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