]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLT_C_Component_WrapperInterface.cxx
minor coverity defect: added protection for self-assignment
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLT_C_Component_WrapperInterface.cxx
1 // $Id$
2
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 //*                  Timm Steinbeck <timm@kip.uni-heidelberg.de>           *
9 //*                  for The ALICE HLT Project.                            *
10 //*                                                                        *
11 //* Permission to use, copy, modify and distribute this software and its   *
12 //* documentation strictly for non-commercial purposes is hereby granted   *
13 //* without fee, provided that the above copyright notice appears in all   *
14 //* copies and that both the copyright notice and this permission notice   *
15 //* appear in the supporting documentation. The authors make no claims     *
16 //* about the suitability of this software for any purpose. It is          *
17 //* provided "as is" without express or implied warranty.                  *
18 //**************************************************************************
19
20 //  @file   AliHLT_C_Component_WrapperInterface.cxx
21 //  @author Matthias Richter, Timm Steinbeck
22 //  @date   
23 //  @brief  Old C interface to the AliRoot HLT component handler
24 //  @note   This interface is deprecated, the new interface is defined
25 //          in HLT/BASE/AliHLTExternalInterface
26
27 #if __GNUC__>= 3
28 using namespace std;
29 #endif
30
31 #include "AliHLT_C_Component_WrapperInterface.h"
32 #include "AliHLTComponentHandler.h"
33 #include "AliHLTComponent.h"
34 #include "AliHLTMisc.h"
35 #include <errno.h>
36
37 static AliHLTComponentHandler *gComponentHandler_C = NULL;
38 static AliHLTRunDesc gRunDesc=kAliHLTVoidRunDesc;
39 static char* gRunType=NULL;
40
41 int AliHLT_C_Component_InitSystem( AliHLTComponentEnvironment* comenv )
42 {
43   // init the HLT system
44   if ( gComponentHandler_C )
45     {
46       return EINPROGRESS;
47     }
48
49   // July 2008  
50   // Due to a bug in the SimpleComponentWrapper and AliRootWrapperSubscriber
51   // the fStructSize member was never initialized and we can not use this
52   // method of synchronizing different versions.
53   // This interface is now deprecated, only kept for backward compatibility.
54   // All function pointers are explicitely mapped to the new structure.
55
56   AliHLTAnalysisEnvironment mappedEnv;
57   memset(&mappedEnv, 0, sizeof(mappedEnv));
58   mappedEnv.fStructSize=sizeof(mappedEnv);
59   if (comenv) {
60     mappedEnv.fParam               = comenv->fParam;
61     mappedEnv.fAllocMemoryFunc     = comenv->fAllocMemoryFunc;
62     mappedEnv.fGetEventDoneDataFunc= comenv->fGetEventDoneDataFunc;
63     mappedEnv.fLoggingFunc         = comenv->fLoggingFunc;
64   }
65
66   gComponentHandler_C = new AliHLTComponentHandler(&mappedEnv);
67   if ( !gComponentHandler_C )
68     return EFAULT;
69   gComponentHandler_C->InitAliLogTrap(gComponentHandler_C);
70   gComponentHandler_C->AnnounceVersion();
71   return 0;
72 }
73
74 int AliHLT_C_Component_DeinitSystem()
75 {
76   // De-init the HLT system and clean-up internal memory
77   if ( gComponentHandler_C )
78     {
79       delete gComponentHandler_C;
80       gComponentHandler_C = NULL;
81     }
82   return 0;
83 }
84
85 int AliHLT_C_Component_LoadLibrary( const char* libraryPath )
86 {
87   // load a component library
88   if ( !gComponentHandler_C )
89     return ENXIO;
90   return gComponentHandler_C->LoadLibrary( libraryPath );
91 }
92
93 int AliHLT_C_Component_UnloadLibrary( const char* /*libraryPath*/ )
94 {
95   // unload a component library
96   if ( !gComponentHandler_C )
97     return ENXIO;
98   // Matthias 26.10.2007
99   // Unloading of libraries has to be re-worked. It has been commented out here
100   // since the libraries will be unloaded at the destruction of the component
101   // handler instance anyway. So it has no effect to the operation in PubSub.
102   // With the introduction of the dynamic component registration via module
103   // agents we run into trouble when cleaning up the samples managed by the
104   // component handler. Destruction of the sample objects is done AFTER
105   // unloading of the library and thus the destructor is not present any 
106   // more.
107   //return gComponentHandler_C->UnloadLibrary( libraryPath );
108   return 0;
109 }
110
111 int AliHLT_C_CreateComponent( const char* componentType, void* environParam, int argc, const char** argv, AliHLTComponentHandle* handle )
112 {
113   // create a component
114   if ( !gComponentHandler_C )
115     return ENXIO;
116   if ( !handle ) return EINVAL;
117   AliHLTComponent* comp=NULL;
118   const char* cdbPath = getenv("ALIHLT_HCDBDIR");
119   if (!cdbPath) cdbPath = getenv("ALICE_ROOT");
120   int ret = gComponentHandler_C->CreateComponent( componentType, comp);
121   if (ret>=0 && comp) {
122     AliHLTMisc::Instance().InitCDB(cdbPath);
123     AliHLTMisc::Instance().SetCDBRunNo(gRunDesc.fRunNo);
124     comp->SetRunDescription(&gRunDesc, gRunType);
125     const AliHLTAnalysisEnvironment* comenv=gComponentHandler_C->GetEnvironment();
126     ret=comp->Init(comenv, environParam, argc, argv);
127   }
128   *handle = reinterpret_cast<AliHLTComponentHandle>( comp );
129
130   return ret;
131 }
132
133 void AliHLT_C_DestroyComponent( AliHLTComponentHandle handle )
134 {
135   // destroy a component
136   if ( !handle )
137     return;
138   
139   AliHLTComponent* pComp=reinterpret_cast<AliHLTComponent*>( handle );
140   pComp->Deinit();
141   delete pComp;
142 }
143
144 int AliHLT_C_SetRunDescription(const AliHLTRunDesc* desc, const char* runType)
145 {
146   // set run description
147   if (!desc) return -EINVAL;
148   if (desc->fStructSize<sizeof(AliHLTUInt32_t)) return -EINVAL;
149   if (!gComponentHandler_C) return ENXIO;
150
151   memcpy(&gRunDesc, desc, desc->fStructSize<sizeof(gRunDesc)?desc->fStructSize:sizeof(gRunDesc));
152   gRunDesc.fStructSize=sizeof(gRunDesc);
153   if (gRunType) delete [] gRunType;
154   gRunType=NULL;
155   if (runType) {
156     gRunType=new char[strlen(runType)+1];
157     if (gRunType) strcpy(gRunType, runType);
158   }
159   return 0;
160 }
161
162 int AliHLT_C_ProcessEvent( AliHLTComponentHandle handle, const AliHLTComponentEventData* evtData, const AliHLTComponentBlockData* blocks, 
163                            AliHLTComponentTriggerData* trigData, AliHLTUInt8_t* outputPtr,
164                            AliHLTUInt32_t* size, AliHLTUInt32_t* outputBlockCnt, 
165                            AliHLTComponentBlockData** outputBlocks,
166                            AliHLTComponentEventDoneData** edd )
167 {
168   // process one event
169   if ( !handle )
170     return ENXIO;
171   AliHLTComponent* comp = reinterpret_cast<AliHLTComponent*>( handle );
172   return comp->ProcessEvent( *evtData, blocks, *trigData, outputPtr, *size, *outputBlockCnt, *outputBlocks, *edd );
173 }
174
175 int AliHLT_C_GetOutputDataType( AliHLTComponentHandle handle, AliHLTComponentDataType* dataType )
176 {
177   // get output data type of a component
178   if ( !handle )
179     return ENXIO;
180   AliHLTComponent* comp = reinterpret_cast<AliHLTComponent*>( handle );
181   *dataType = comp->GetOutputDataType();
182   return 0;
183 }
184
185 int AliHLT_C_GetOutputSize( AliHLTComponentHandle handle, unsigned long* constBase, double* inputMultiplier )
186 {
187   // get output data size of a component
188   if ( !handle )
189     return ENXIO;
190   AliHLTComponent* comp = reinterpret_cast<AliHLTComponent*>( handle );
191   comp->GetOutputDataSize( *constBase, *inputMultiplier );
192   return 0;
193 }