3 #ifndef ALIHLTDATABUFFER_H
4 #define ALIHLTDATABUFFER_H
5 /* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
6 * See cxx source for full Copyright notice */
10 #include "AliHLTLogging.h"
11 #include "AliHLTDataTypes.h"
12 #include "AliHLTDefinitions.h"
13 #include "AliHLTComponent.h"
17 /* @name internal data structures
20 /* @struct AliHLTDataSegment
21 * @brief descriptor of a data segment within the buffer
24 struct AliHLTDataSegment {
32 AliHLTDataSegment(AliHLTUInt32_t offset, AliHLTUInt32_t size)
35 fSegmentOffset=offset;
39 AliHLTComponent_DataType fDataType; // the data type of this buffer
40 AliHLTUInt32_t fSegmentOffset; // offset in byte within the data buffer
41 AliHLTUInt32_t fSegmentSize; // size of the actual content
42 AliHLTUInt32_t fSpecification; // data specification
45 /* @struct AliHLTRawBuffer
46 * @brief descriptor of the raw data buffer which can host several segments
49 struct AliHLTRawBuffer {
50 AliHLTUInt32_t fSize; // size of the buffer
51 AliHLTUInt32_t fTotalSize; // total size of the buffer, including safety margin
52 void* fPtr; // the buffer
55 /* @struct AliHLTConsumerDescriptor
56 * @brief there is unfortunately no unique determination of the data type from the component
57 * itself possible, thats why both component and data type have to be initialized
58 * and are stored in a compound. The class is intended to make bookkeeping easier
61 class AliHLTConsumerDescriptor : public AliHLTLogging, public TObject {
63 AliHLTComponent* fpConsumer;
64 vector<AliHLTDataSegment> fSegments;
67 /** standard constructur */
68 AliHLTConsumerDescriptor();
70 * @param pConsumer pointer to the consumer component
72 AliHLTConsumerDescriptor(AliHLTComponent* pConsumer);
73 ~AliHLTConsumerDescriptor();
76 * Get the component of this descriptor
77 * @return pointer to the component
79 AliHLTComponent* GetComponent() {return fpConsumer;}
82 * Set an active data segment
83 * the pointer will be handled in a container, not allocation, copy or cleanup
84 * @param offset offset of the segment in the buffer
85 * @param size size of the segment in the buffer
86 * @return >=0 if succeeded
88 int SetActiveDataSegment(AliHLTUInt32_t offset, AliHLTUInt32_t size);
91 * check whether there is an active data segment of certain size with certain offset
92 * @param offset offset of the data segment in the data buffer
93 * @param size size of the data segment in the data buffer
94 * @return > if existend, 0 if not
96 int CheckActiveDataSegment(AliHLTUInt32_t offset, AliHLTUInt32_t size);
98 /** find an active data segment of certain size with certain offset
99 * will see if this is necessary
100 * @param offset offset of the data segment in the data buffer
101 * @param size size of the data segment in the data buffer
102 * @return offset of the data segment
104 //AliHLTUInt32_t FindActiveDataSegment(AliHLTUInt32_t offset, AliHLTUInt32_t size);
106 /** get the number of active segments for this consumer
107 * @return number of active segments
109 int GetNofActiveSegments() {return fSegments.size();};
113 int ReleaseActiveDataSegment(AliHLTUInt32_t offset, AliHLTUInt32_t size);
115 //ClassDef(AliHLTConsumerDescriptor, 0)
119 * @class AliHLTDataBuffer
120 * @brief Handling of data buffers for the HLT.
122 * The class provides handling of data buffers for HLT components. Each component gets its
123 * own Data Buffer instance. The buffer is grouped into different data segments according
124 * to the output of the component.<br>
125 * The Data Buffer keeps control over the data requests of the 'child' componets. Each
126 * component can subscribe to a certain segment of the data buffer. It's state is that
127 * changed from 'reserved' to 'active'. After the data processing the component has to
128 * release the segment and it's state is set to 'processed'.
129 * If all components have requested and released their data, the Raw Buffer is released
130 * and pushed back in the list of available buffers.
131 * @ingroup AliHLTbase
133 class AliHLTDataBuffer : public AliHLTLogging, public TObject {
135 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
136 // condtructors and destructors
138 /* standard constructor
142 virtual ~AliHLTDataBuffer();
144 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
148 * Add component to the list of consumers
149 * @param pConsumer - a consumer of type AliHLTComponent
151 int SetConsumer(AliHLTComponent* pConsumer);
153 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
154 // component to component communication
157 * Determine the number of matching data blocks for the component and a consumer
159 * The first approach will support only one output data type for processing components.
160 * @param pConsumer the component which subscribes to the buffer
161 * @param tgtList (optional) the list to receive the data types
162 * @return: number of data blocks which match the input data types
163 * of the consumer, neg. error code if failed <br>
164 * -EINVAL invalid parameter <br>
166 int FindMatchingDataBlocks(const AliHLTComponent* pConsumer, vector<AliHLTComponent_DataType>* tgtList=NULL);
169 * Subscribe to a segment of the data buffer.
170 * The function prepares the block descriptor for subsequent use with the AliHLTComponent::ProcessEvent
171 * method, the method can prepare several block descriptors up to the array size specified by
172 * iArraySize. The return value is independent from the array size the number of block descriptors
173 * which would have been prepared if there was enough space in the array<br>
174 * The method is used by the consumer component.
175 * @param pConsumer the component which subscribes to the buffer
176 * @param arrayBlockDesc pointer to block descriptor to be filled
177 * @param iArraySize size of the block descriptor array
178 * @return: number of matching data blocks if success, negative error code if failed<br>
179 * -EACCESS the state of the consumer can not be changed (activated)
180 * -EBADF unresolved data segments <br>
181 * -ENOENT consumer component not found <br>
182 * -ENODATA data buffer does not have raw data <br>
183 * -EINVAL invalid parameter <br>
185 int Subscribe(const AliHLTComponent* pConsumer, AliHLTComponent_BlockData* arrayBlockDesc, int iArraySize);
188 * Release an instance of the data buffer.
189 * Resets the variables of the block descriptor.
190 * If all buffer segments are released, the Data Buffer is reseted
191 * and the Raw Buffer released.<br>
192 * The method is used by the consumer component.
193 * @param pBlockDesc descriptor of the data segment
194 * @param pConsumer the component which subscribes to the buffer
195 * @return: >0 if success, negative error code if failed <br>
196 * -EACCESS the state of the consumer can not be changed (de-activated)
197 * -ENOENT consumer component has not subscribed to the buffer <br>
198 * -EINVAL invalid parameter <br>
200 int Release(AliHLTComponent_BlockData* pBlockDesc, const AliHLTComponent* pConsumer);
203 * Get a target buffer of minimum size iMinSize.
204 * The method is used by the component which owns the Data Buffer to
205 * allocate a buffer for the data it is going to produce.
206 * @param iMinSize minumum size of the requested buffer
207 * @return: pointer to target buffer if
209 AliHLTUInt8_t* GetTargetBuffer(int iMinSize);
212 * Set the segments for the data buffer.
213 * This is usually done after the component has written the data to the buffer,
214 * which was requested by the @ref GetTargetBuffer method. The component might
215 * produce different types of data, for each type a segment has to be defined
216 * which describes the data inside the bauffer.<br>
217 * The @ref AliHLTComponent_BlockData segment descriptor comes directly from the
218 * @ref AliHLTComponent::ProcessEvent method.
219 * @param pTgt the target buffer which the segments refer to
220 * @param arraySegments the output block descriptors of the component
221 * @param iSize size of the array
223 int SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponent_BlockData* arraySegments, int iSize);
226 * Check if the data buffer is empty.
227 * @return 1 if empty, 0 if not
232 * Get the total and maximum size of the buffer.
233 * Lets see if this is needed later
235 //int GetTotalSize();
238 * Get the number of segments
239 * @return number of segments
241 int GetNofSegments();
244 * Get the number of consumers
245 * @return number of consumers
247 int GetNofConsumers();
250 * Get the number of active consumers
251 * @return number of active consumers
253 int GetNofActiveConsumers();
256 /* lets see if this is needed
257 AliHLTDataSegment* FindDataSegment(AliHLTComponent_DataType datatype);
261 * Find those data segments which match the input types of a component.
262 * @param pConsumer the component which subscribes to the buffer
263 * @param tgtList the list to receive the data segment descriptors
264 * @return: number of data blocks which match the input data types
265 * of the consumer, neg. error code if failed <br>
266 * -EINVAL invalid parameter <br>
268 int FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataSegment>& tgtList);
271 * Reset the data buffer.
272 * Removes all consumers back to the @ref fConsumers list
273 * and releases the Raw Buffer.
275 int ResetDataBuffer();
278 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
279 // the data description
281 // the data segments within this buffer
282 vector<AliHLTDataSegment> fSegments;
284 // the list of all consumers which are going to subscribe to the buffer
285 vector<AliHLTConsumerDescriptor*> fConsumers;
286 // the list of all consumers which are currently subscribed to the buffer
287 vector<AliHLTConsumerDescriptor*> fActiveConsumers;
288 // the list of all consumers which are already released for the current event
289 vector<AliHLTConsumerDescriptor*> fReleasedConsumers;
291 // the buffer instance
292 AliHLTRawBuffer* fpBuffer;
294 // flags indicating the state of the buffer
295 AliHLTUInt32_t fFlags;
297 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
298 // global buffer handling, internal use only
301 * Create a raw buffer of a certain size.
302 * The function tries to find a buffer of the given size (or a bit bigger by a
303 * certain margin @ref fMargin) from the list of free buffers.
304 * If no buffer is available, a new one is created and added to the buffer handling.
305 * @param size min. size of the requested buffer
306 * @return pointer to raw buffer
308 static AliHLTRawBuffer* CreateRawBuffer(AliHLTUInt32_t size);
311 * Mark a buffer as free.
312 * After the Data Buffer has finnished using the raw buffer, it is released and
313 * added to the list of available buffers.
314 * @param pBuffer the raw buffer to release
315 * @return >=0 if succeeded, neg. error code if failed
317 static int ReleaseRawBuffer(AliHLTRawBuffer* pBuffer);
320 * Deletes all the raw buffers.
321 * When the last Data Buffer object is destructed, all raw data buffers are relesed.
323 static int DeleteRawBuffers();
326 * Number of instances of AliHLTDataBuffer.
327 * The statice variable is incremented and decremented in the constructor/destructor.
328 * All internal data structures are cleaned up when the last instance is exiting.
330 static int fNofInstances;
331 /** global list of free raw buffers */
332 static vector<AliHLTRawBuffer*> fFreeBuffers;
333 /** global list of currently active raw buffers */
334 static vector<AliHLTRawBuffer*> fActiveBuffers;
335 /** determines the raw buffer size margin at buffer requests */
336 static AliHLTUInt32_t fMargin;
338 /** global instance to HLT logging class for static methods */
339 static AliHLTLogging fgLogging;
341 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
342 // internal helper functions
345 * Find the consumer descriptor for a certain component and data type in
346 * a list of consumers.<br>
347 * <b>Note:</b> There are three lists which contain the consumers in the different states.
348 * @param pConsumer pointer to consumer component
349 * @param list list where to search for the consumer
351 AliHLTConsumerDescriptor* FindConsumer(const AliHLTComponent* pConsumer, vector<AliHLTConsumerDescriptor*> &list);
354 * Change the state of a consumer.
355 * The state of a consumer is determined by the list it is strored in, the method moves a consumer from
356 * the source to the target list.
357 * @param pDesc pointer to consumer descriptor
358 * @param srcList list where the consumer is currently to be found
359 * @param tgtList list where to move the consumer
361 int ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, vector<AliHLTConsumerDescriptor*> &srcList, vector<AliHLTConsumerDescriptor*> &tgtList);
364 * Cleanup a consumer list.
365 * Release all allocated data structures. <b>Note:</b> Not the component itself!
367 int CleanupConsumerList();
369 ClassDef(AliHLTDataBuffer, 0)
371 #endif // ALIHLTDATABUFFER_H