]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/fvid2.git/blob - include/fvid2_utils.h
NOTICE OF RELOCATION
[keystone-rtos/fvid2.git] / include / fvid2_utils.h
1 /*
2  *  Copyright (c) Texas Instruments Incorporated 2018
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *
8  *    Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  *    Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the
14  *    distribution.
15  *
16  *    Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
33 /**
34  *  \file fvid2_utils.h
35  *
36  *  \brief FVID2 Utility functions header file
37  *  This file declares the functions required to create, add and remove nodes.
38  *  Also provides various memset functions.
39  *
40  */
42 #ifndef FVID2_UTILS_H_
43 #define FVID2_UTILS_H_
45 /* ========================================================================== */
46 /*                             Include Files                                  */
47 /* ========================================================================== */
49 #include <ti/csl/csl_types.h>
50 #include <ti/osal/osal.h>
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
56 /* ========================================================================== */
57 /*                           Macros & Typedefs                                */
58 /* ========================================================================== */
60 /** \brief Number of timestamps. */
61 #define FVID2UTILS_NUM_TIMESTAMP_LOG    (100U)
63 /**
64  *  \anchor Fvid2Utils_LinkListType
65  *  \name Link List Type
66  *  @{
67  */
68 /**
69  *  \brief Enums for the type of link list to be created.
70  */
71 typedef uint32_t Fvid2Utils_LinkListType;
72 #define FVID2UTILS_LLT_DOUBLE           (0U)
73 /**< Double link list. */
74 #define FVID2UTILS_LLT_CIRCULAR         (1U)
75 /**< Circular link list using double link list. */
76 /* @} */
78 /**
79  *  \anchor Fvid2Utils_LinkAddMode
80  *  \name Link Add Mode
81  *  @{
82  */
83 /**
84  *  \brief Enums for the different modes of adding a node to a link list.
85  */
86 typedef uint32_t Fvid2Utils_LinkAddMode;
87 #define FVID2UTILS_LAM_TOP              (0U)
88 /**< Add nodes to the top of the list. */
89 #define FVID2UTILS_LAM_BOTTOM           (1U)
90 /**< Add nodes to the bottom of the list. */
91 #define FVID2UTILS_LAM_PRIORITY         (2U)
92 /**< Add nodes to the list based on ascending order of priority.
93  *   Nodes with the same priority are always added to the bottom of the
94  *   existing nodes with same priority. */
95 /* @} */
97 /**
98  *  \anchor Fvid2Utils_NodDir
99  *  \name Node Direction
100  *  @{
101  */
102 /**
103  *  \brief Enums for the different modes of adding a node to a link list.
104  */
105 typedef uint32_t Fvid2Utils_NodDir;
106 #define FVID2UTILS_NODE_DIR_HEAD        (0U)
107 /**< Add nodes to the top of the list. */
108 #define FVID2UTILS_NODE_DIR_TAIL        (1U)
109 /**< Add nodes to the bottom of the list. */
110 /* @} */
112 /* ========================================================================== */
113 /*                         Structure Declarations                             */
114 /* ========================================================================== */
116 /**< \brief Typedef for Node structure. */
117 typedef struct Fvid2Utils_Node_t Fvid2Utils_Node;
119 /**
120  *  struct Fvid2Utils_Node_t
121  *  \brief Self referential structure for double link list.
122  */
123 struct Fvid2Utils_Node_t
125     Fvid2Utils_Node *next;
126     /**< Pointer to the next node. */
127     Fvid2Utils_Node *prev;
128     /**< Pointer to the previous node. */
129     void            *data;
130     /**< Node data pointer. */
131     uint32_t         priority;
132     /**< Priority of the node. Used for priority based linked list. */
133 };
135 /**
136  *  struct Fvid2Utils_QElem
137  *  \brief Typedef for Queue Node element.
138  */
139 typedef Fvid2Utils_Node Fvid2Utils_QElem;
141 /**
142  *  struct Fvid2Utils_PoolParams
143  *  \brief Create parameters for the fixed size pool.
144  */
145 typedef struct
147     void       *mem;
148     /**< Pointer to the pool memory. */
149     uint32_t    numElem;
150     /**< Number of elements in the pool. */
151     uint32_t    elemSize;
152     /**< Size of each element in bytes. */
153     uint32_t   *flag;
154     /**< Array of flag variable used by pool manager to indicate whether a pool
155      *   element is allocated or not. The size of this array should be
156      *   equal to the number of elements in this pool. */
157     uint32_t    numFreeElem;
158     /**< Count to keep track of the number of free elements in the pool. */
159 } Fvid2Utils_PoolParams;
161 /**
162  *  struct Fvid2Utils_TsPrfLog
163  *  \brief Timestamp Performance log structure in OSAL timestamp ticks.
164  */
165 typedef struct
167     uint64_t startTime;
168     /**< Start time - updated in start function. */
169     uint64_t total;
170     /**< Total duration of all log entires.
171      *   To get average divide by totalCnt. */
172     uint32_t totalCnt;
173     /**< Total number of log entires. */
174     uint64_t min;
175     /**< Minimum duration. */
176     uint64_t max;
177     /**< Maximum duration. */
178     uint32_t prfLogIndex;
179     /**< Current index. This will wrap around every FVID2UTILS_NUM_TIMESTAMP_LOG
180      *   count. */
181     uint64_t prfLog[FVID2UTILS_NUM_TIMESTAMP_LOG];
182     /**< History of the past FVID2UTILS_NUM_TIMESTAMP_LOG entires. */
183 } Fvid2Utils_TsPrfLog;
185 /**
186  *  struct Fvid2UtilsLinkListObj
187  *  \brief Structure to the link list object information.
188  */
189 typedef struct
191     Fvid2Utils_LinkListType listType;
192     /**< Type of linked list. */
193     Fvid2Utils_LinkAddMode  addMode;
194     /**< Mode of adding a node to a link list. */
195     Fvid2Utils_Node        *headNode;
196     /**< Head Node. */
197     Fvid2Utils_Node        *tailNode;
198     /**< Tail Node Node. */
199     uint32_t                numElements;
200     /**< Number of elements in the linked list. */
201     uint32_t                priorityCnt;
202     /**< Priority count of the linked list. */
203 } Fvid2UtilsLinkListObj;
205 /**
206  *  struct Fvid2Utils_Handle
207  *  \brief Typedef for FVID2 Utils handle.
208  */
209 typedef Fvid2UtilsLinkListObj *Fvid2Utils_Handle;
211 /**
212  *  struct Fvid2Utils_QHandle
213  *  \brief Typedef for FVID2 Utils Queue Handle.
214  */
215 typedef Fvid2Utils_Handle Fvid2Utils_QHandle;
217 /* ========================================================================== */
218 /*                          Function Declarations                             */
219 /* ========================================================================== */
220 /**
221  *  Fvid2Utils_init
222  *  \brief Initializes the Util objects and create pool semaphore
223  *
224  *  \returns                Returns 0 on success else returns error value
225  */
226 int32_t Fvid2Utils_init(void);
228 /**
229  *  Fvid2Utils_deInit
230  *  \brief De-Initializes the Util objects by removing pool semaphore
231  *
232  *  \param arg              Not used currently. Meant for future purpose
233  *
234  *  \return                 Returns 0 on success else returns error value
235  */
236 int32_t Fvid2Utils_deInit(void *arg);
238 /**
239  *  Fvid2Utils_memset
240  *  \brief Sets the memory with the given value. Access memory as byte.
241  *  Returns the memory pointer.
242  *
243  *  \param mem              Destination memory pointer.
244  *  \param ch               Byte value to fill with.
245  *  \param byteCount        Number of bytes to fill.
246  *
247  *  \return                 None.
248  */
249 void Fvid2Utils_memset(void *mem, uint8_t ch, size_t byteCount);
251 /**
252  *  Fvid2Utils_memsetw
253  *  \brief Sets the memory with the given value. Access memory as word.
254  *  Hence memory pointer should be aligned to 4 byte boundary
255  *  Returns the memory pointer.
256  *
257  *  \param mem              Destination memory pointer.
258  *  \param word             Word value to fill with.
259  *  \param wordCount        Number of words to fill.
260  *
261  *  \return                 The destination memory pointer.
262  */
263 void *Fvid2Utils_memsetw(void *mem, uint32_t word, uint32_t wordCount);
265 /**
266  *  Fvid2Utils_memcpy
267  *  \brief Copies source memory into destination memory. Access memory as byte.
268  *  Returns the destination memory pointer.
269  *
270  *  \param dest             Destination memory pointer.
271  *  \param src              Source memory pointer.
272  *  \param byteCount        Number of bytes to copy.
273  *
274  *  \return                 None
275  */
276 void Fvid2Utils_memcpy(void *dest, const void *src, size_t byteCount);
278 /**
279  *  Fvid2Utils_memcmp
280  *  \brief Compare memory block 1 with memory block 2. Access memory as byte.
281  *  Returns 0 if two memories are  identical.
282  *
283  *  \param mem1             memory block 1
284  *  \param mem2              memory block 2
285  *  \param byteCount        Number of bytes to compare
286  *
287  *  \return                 0 if two memory are identical other return 1
288  */
289 int32_t Fvid2Utils_memcmp(const void *mem1,
290                           const void *mem2,
291                           uint32_t byteCount);
293 /**
294  *  Fvid2Utils_constructLinkList
295  *  \brief Constructs a link list object with the provided properties.
296  *
297  *  \param llobj pointer to object of type Fvid2UtilsLinkListObj
298  *  \param listType         List type - circular/double link list.
299  *  \param addMode          Node addition mode - Top/Bottom/Priority based.
300  *
301  *  \return                 Returns FVID2_SOK on success else returns error
302  *                          value.
303  */
304 int32_t Fvid2Utils_constructLinkList(Fvid2UtilsLinkListObj  *llobj,
305                                      Fvid2Utils_LinkListType listType,
306                                      Fvid2Utils_LinkAddMode  addMode);
308 /**
309  *  Fvid2Utils_destructLinkList
310  *  \brief Destructs a link list object.
311  *
312  *  \param llobj pointer to object of type Fvid2UtilsLinkListObj
313  *
314  *  \return                 None
315  */
316 void Fvid2Utils_destructLinkList(Fvid2UtilsLinkListObj *llobj);
318 /**
319  *  Fvid2Utils_linkNodePri
320  *  \brief Links a node to the linked list according to the list type
321  *  The memory to the node object should be allocated by the caller. This
322  *  is used for link list with priority.
323  *
324  *  \param handle           Link list handle.
325  *  \param node             Node object pointer used for linking.
326  *  \param priority         Priority of the node used for priority based
327  *                          addition of nodes. Priority is in descending order
328  *                          the value. So 0 is the highest priority and is
329  *                          added to the top of the node.
330  *                          Nodes with the same priority are always added to
331  *                          the bottom of the existing nodes with same
332  *                          priority.
333  *                          For non-priority based modes, this parameter
334  *                          is ignored and could be set to 0.
335  */
336 void Fvid2Utils_linkNodePri(Fvid2Utils_Handle handle,
337                             Fvid2Utils_Node  *node,
338                             uint32_t          priority);
340 /**
341  *  Fvid2Utils_linkUniqePriNode
342  *  \brief Very similar to Fvid2Utils_linkNode, except that on equal priority
343  *          nodes will not be inserted. An error (FVID2_EBADARGS) would be
344  *          returned.
345  *          Applicable for double linked list only.
346  *  ToDo Update to handle circular list also.
347  *
348  *  \param handle           Link list handle.
349  *  \param node             Node object pointer used for linking.
350  *  \param priority         Priority of the node used for priority based
351  *                          addition of nodes. Priority is in descending order
352  *                          the value. So 0 is the highest priority and is
353  *                          added to the top of the node.
354  *                          Nodes with the same priority are always added to
355  *                          the bottom of the existing nodes with same
356  *                          priority.
357  *                          For non-priority based modes, this parameter
358  *                          is ignored and could be set to 0.
359  */
360 int32_t Fvid2Utils_linkUniqePriNode(Fvid2Utils_Handle handle,
361                                     Fvid2Utils_Node  *node,
362                                     uint32_t          priority);
364 /**
365  *  Fvid2Utils_unLinkAllNodes
366  *  \brief Releases all nodes without modifying any of the property.
367  *         CAUTION - Memory is NOT de-allocated, its the responsibility of the
368  *                   caller to ensure de-allocation of memory.
369  *
370  *  \param handle           Link list handle.
371  *
372  *  \return                 Returns 0 on success else returns error value.
373  */
374 int32_t Fvid2Utils_unLinkAllNodes(Fvid2Utils_Handle handle);
376 /**
377  *  Fvid2Utils_unLinkNodePri
378  *  \brief Unlinks the node from the list. Used for the Priority linklists.
379  *
380  *  \param handle           Link list handle.
381  *  \param node             Node pointer to be unlinked from the list.
382  */
383 void Fvid2Utils_unLinkNodePri(Fvid2Utils_Handle handle, Fvid2Utils_Node *node);
385 /**
386  *  Fvid2Utils_unLinkNode
387  *  \brief Unlinks the node from the list. Used for Non-priority linked lists
388  *
389  *  \param handle           Link list handle.
390  *  \param node             Node pointer to be unlinked from the list.
391  */
392 void Fvid2Utils_unLinkNode(Fvid2Utils_Handle handle,
393                            const Fvid2Utils_Node *node);
395 /**
396  *  Fvid2Utils_linkNodeToHead
397  *  \brief                  Link the node to the head of the double linked list.
398  *                          No priority
399  *
400  *  \param handle           Link list handle.
401  *  \param node             Node object pointer used for linking.
402  *
403  */
404 void Fvid2Utils_linkNodeToHead(Fvid2Utils_Handle handle,
405                                Fvid2Utils_Node  *node);
407 /**
408  *  Fvid2Utils_linkNodeToTail
409  *  \brief                  Link the node to the tail of the double linked list.
410  *                          No priority
411  *
412  *  \param handle           Link list handle.
413  *  \param node             Node object pointer used for linking.
414  */
415 void Fvid2Utils_linkNodeToTail(Fvid2Utils_Handle handle,
416                              Fvid2Utils_Node  *node);
418 /**
419  *  Fvid2Utils_unLinkNodeFromHead
420  *  \brief                  Returns the node from head. Removes the  node from
421  *                          the list.
422  *
423  *  \param handle           Link list handle.
424  *  \return                 Pointer to unlinked node.
425  *
426  */
427 Fvid2Utils_Node *Fvid2Utils_unLinkNodeFromHead(Fvid2Utils_Handle handle);
429 /**
430  *  Fvid2Utils_unLinkNodeFromTail
431  *  \brief                  Returns the node from tail. Removes the  node from
432  *                          the list.
433  *
434  *  \param handle           Link list handle.
435  *  \return                 Pointer to unlinked node.
436  *
437  */
438 Fvid2Utils_Node *Fvid2Utils_unLinkNodeFromTail(Fvid2Utils_Handle handle);
440 /**
441  *  Fvid2Utils_getHeadNode
442  *  \brief                  Returns the reference to the headNode. Does
443  *                          not remove the node from the head.
444  *
445  *  \param handle           Link list handle.
446  */
447 Fvid2Utils_Node *Fvid2Utils_getHeadNode(Fvid2Utils_Handle handle);
449 /**
450  *  Fvid2Utils_getTailNode
451  *  \brief                  Returns the reference to the TailNode. Does
452  *                          not remove the node from the head.
453  *
454  *  \param handle           Link list handle.
455  *  \return                 Reference  to tail node.  Acutally  node is not
456  *                          unlinked from list.
457  *
458  */
459 Fvid2Utils_Node *Fvid2Utils_getTailNode(Fvid2Utils_Handle handle);
461 /**
462  *  Fvid2Utils_isListEmpty
463  *  \brief Checks whether a list is empty or not.
464  *
465  *  \param handle           List handle.
466  *
467  *  \return                 TRUE if List is empty else returns FALSE.
468  */
469 uint32_t Fvid2Utils_isListEmpty(Fvid2Utils_Handle handle);
471 /**
472  *  Fvid2Utils_getNumNodes
473  *  \brief Returns the number of nodes present in a list.
474  *
475  *  \param handle           List handle.
476  *
477  *  \return                 Number of nodes present in a list.
478  */
479 uint32_t Fvid2Utils_getNumNodes(Fvid2Utils_Handle handle);
481 /**
482  *  Fvid2Utils_getNodeCnt
483  *  \brief Returns the number of nodes in the link list
484  *
485  *  \param handle           Link list handle.
486  *
487  *
488  *  \return                 Returns the number of nodes in the list.
489  */
490 uint32_t Fvid2Utils_getNodeCnt(Fvid2Utils_Handle handle);
492 /**
493  *  Fvid2Utils_constructQ
494  *  \brief Constructs a Queue object.
495  *
496  * \param llobj pointer to object of type Fvid2UtilsLinkListObj
497  *
498  *  \return                 Returns FVID2_SOK on success else returns error value.
499  */
500 int32_t Fvid2Utils_constructQ(Fvid2UtilsLinkListObj *llobj);
502 /**
503  *  Fvid2Utils_destructQ
504  *  \brief Destructs a Queue object.
505  *
506  * \param llobj pointer to object of type Fvid2UtilsLinkListObj
507  *
508  *  \return                 None
509  */
510 void Fvid2Utils_destructQ(Fvid2UtilsLinkListObj *llobj);
512 /**
513  *  Fvid2Utils_queue
514  *  \brief Adds the data to the queue.
515  *  The memory to the node object should be allocated by the caller.
516  *
517  *  \param handle           Queue handle.
518  *  \param qElem            Queue Element object pointer used for linking.
519  *  \param data             Data pointer to be added to the list.
520  *                          This should be unique.
521  */
522 void Fvid2Utils_queue(Fvid2Utils_QHandle handle,
523                       Fvid2Utils_QElem  *qElem,
524                       void              *data);
526 /**
527  *  Fvid2Utils_dequeue
528  *  \brief Removes a element from the queue.
529  *
530  *  \param handle           Queue handle.
531  *
532  *  \return                 Returns the removed data pointer.
533  */
534 void *Fvid2Utils_dequeue(Fvid2Utils_QHandle handle);
536 /**
537  *  Fvid2Utils_peakHead
538  *  \brief Returns the reference of the first queued element.
539  *
540  *  This doesn't remove the element from the queue.
541  *  If the queue is empty, then this returns NULL.
542  *
543  *  \param handle           Queue handle.
544  *
545  *  \return                 Returns the reference of the first element.
546  */
547 void *Fvid2Utils_peakHead(Fvid2Utils_QHandle handle);
549 /**
550  *  Fvid2Utils_peakTail
551  *  \brief Returns the reference of the last queued element.
552  *
553  *  This doesn't remove the element from the queue.
554  *  If the queue is empty, then this returns NULL.
555  *
556  *  \param handle           Queue handle.
557  *
558  *  \return                 Returns the reference of the last queued element.
559  */
560 void *Fvid2Utils_peakTail(Fvid2Utils_QHandle handle);
562 /**
563  *  Fvid2Utils_queueBack
564  *  \brief Adds the data to start of the queue.
565  *  The memory to the node object should be allocated by the caller.
566  *
567  *  \param handle           Queue handle.
568  *  \param qElem            Queue Element object pointer used for linking.
569  *  \param data             Data pointer to be added to the list.
570  *                          This should be unique.
571  */
572 void Fvid2Utils_queueBack(Fvid2Utils_QHandle handle,
573                           Fvid2Utils_QElem  *qElem,
574                           void              *data);
576 /**
577  *  Fvid2Utils_isQEmpty
578  *  \brief Checks whether a queue is empty or not.
579  *
580  *  \param handle           Queue handle.
581  *
582  *  \return                 TRUE if queue is empty else returns FALSE.
583  */
584 uint32_t Fvid2Utils_isQEmpty(Fvid2Utils_QHandle handle);
586 /**
587  *  Fvid2Utils_getNumQElem
588  *  \brief Returns the number of Queue Elements present in a Queue.
589  *
590  *  \param handle           Queue handle.
591  *
592  *  \return                 Number of Queue Elements present in a Queue.
593  */
594 uint32_t Fvid2Utils_getNumQElem(Fvid2Utils_QHandle handle);
596 /**
597  *  Fvid2Utils_floor
598  *  \brief Floor a integer value.
599  *
600  *  \param val          [IN] Integer to floor.
601  *  \param align        [IN] Alignment.
602  *
603  *  \return Returns the floored integer.
604  */
605 static inline uint32_t Fvid2Utils_floor(uint32_t val, uint32_t align);
607 /**
608  *  Fvid2Utils_align
609  *  \brief Align a integer value.
610  *
611  *  \param val          [IN] Integer to align.
612  *  \param align        [IN] Alignment.
613  *
614  *  \return Returns the aligned integer.
615  */
616 static inline uint32_t Fvid2Utils_align(uint32_t val, uint32_t align);
618 /**
619  *  Fvid2Utils_initPool
620  *  \brief Initializes the pool object structure.
621  *
622  *  \param params           Pool parameters to be initialized.
623  *  \param mem              Pointer to the pool memory.
624  *  \param numElem          Number of elements in the pool.
625  *  \param elemSize         Size of each element in bytes.
626  *  \param flag             Array of flag variable used by pool manager to
627  *                          indicate whether a pool element is allocated
628  *                          or not. The size of this array should be
629  *                          equal to the number of elements in this pool.
630  *  \param traceMask        Trace mask used in trace prints.
631  */
632 static inline void Fvid2Utils_initPool(Fvid2Utils_PoolParams *params,
633                                        void                  *mem,
634                                        uint32_t               numElem,
635                                        uint32_t               elemSize,
636                                        uint32_t              *flag,
637                                        uint32_t               traceMask);
639 /**
640  *  Fvid2Utils_alloc
641  *  \brief Allocates one element from the pool.
642  *
643  *  \param params           Pool parameters.
644  *  \param size             Size in bytes to allocate.
645  *  \param traceMask        Trace mask used in trace prints.
646  *
647  *  \return                 Returns memory pointer on success else returns
648  *                          NULL.
649  */
650 static inline void *Fvid2Utils_alloc(Fvid2Utils_PoolParams *params,
651                                    uint32_t               size,
652                                    uint32_t               traceMask);
654 /**
655  *  Fvid2Utils_free
656  *  \brief Frees the element and returns to the pool.
657  *
658  *  \param params           Pool parameters.
659  *  \param mem              Memory pointer to deallocate.
660  *  \param traceMask        Trace mask used in trace prints.
661  *
662  *  \return                 Returns 0 on success else returns error value.
663  */
664 static inline int32_t Fvid2Utils_free(Fvid2Utils_PoolParams *params,
665                                       const void            *mem,
666                                       uint32_t               traceMask);
668 /**
669  *  Fvid2Utils_initTsPrfLog
670  *  \brief Initializes the structure. This should be called once before
671  *  calling any other performance functions.
672  *
673  *  \param tsPrf            Pointer to Fvid2Utils_TsPrfLog structure.
674  *
675  */
676 static inline void Fvid2Utils_initTsPrfLog(Fvid2Utils_TsPrfLog *tsPrf);
678 /**
679  *  Fvid2Utils_startTsPrfLog
680  *  \brief Log the start entry to the performance structure.
681  *
682  *  \param tsPrf            Pointer to Fvid2Utils_TsPrfLog structure.
683  */
684 static inline void Fvid2Utils_startTsPrfLog(Fvid2Utils_TsPrfLog *tsPrf);
686 /**
687  *  Fvid2Utils_endTsPrfLog
688  *  \brief Log the entry to the performance structure. This uses the previous
689  *  startTime stored when calling Fvid2Utils_startTsPrfLog() to calculate the
690  *  difference.
691  *
692  *  \param tsPrf            Pointer to Fvid2Utils_TsPrfLog structure.
693  */
694 static inline void Fvid2Utils_endTsPrfLog(Fvid2Utils_TsPrfLog *tsPrf);
696 /**
697  *  Osal_getTimestamp64
698  *  \brief This API gets the 64 bit time stamp.
699  */
700 static inline uint64_t Osal_getTimestamp64(void);
702 /* ========================================================================== */
703 /*                       Static Function Definitions                          */
704 /* ========================================================================== */
706 static inline uint32_t Fvid2Utils_floor(uint32_t val, uint32_t align)
708     return ((uint32_t) ((val / align) * align));
711 static inline uint32_t Fvid2Utils_align(uint32_t val, uint32_t align)
713     return (Fvid2Utils_floor((uint32_t) (val + (align - 1U)), align));
716 static inline void Fvid2Utils_initPool(Fvid2Utils_PoolParams *params,
717                                        void                  *mem,
718                                        uint32_t               numElem,
719                                        uint32_t               elemSize,
720                                        uint32_t              *flag,
721                                        uint32_t               traceMask)
723     uint32_t cnt;
725     /* NULL pointer check */
726     GT_assert(traceMask, (NULL_PTR != params));
727     GT_assert(traceMask, (NULL_PTR != mem));
728     GT_assert(traceMask, (NULL_PTR != flag));
730     /* Init pool parameters */
731     params->mem         = mem;
732     params->numElem     = numElem;
733     params->elemSize    = elemSize;
734     params->flag        = flag;
735     params->numFreeElem = numElem;
737     /* Set pool flags as free */
738     for (cnt = 0U; cnt < params->numElem; cnt++)
739     {
740         params->flag[cnt] = (uint32_t) FALSE;
741     }
743     return;
746 static inline void *Fvid2Utils_alloc(Fvid2Utils_PoolParams *params,
747                                      uint32_t               size,
748                                      uint32_t               traceMask)
750     uint32_t cnt;
751     uintptr_t cookie;
752     uintptr_t tempVal;
753     void  *allocMem = NULL;
755     /* NULL pointer check */
756     GT_assert(traceMask, (NULL_PTR != params));
757     /* Check if the requested size if within each fixed size element */
758     GT_assert(traceMask, (size <= params->elemSize));
759     GT_assert(traceMask, (0U != size));
761     /* Disable global interrupts */
762     cookie = HwiP_disable();
764     for (cnt = 0U; cnt < params->numElem; cnt++)
765     {
766         if ((uint32_t) FALSE == params->flag[cnt])
767         {
768             tempVal = ((uintptr_t) params->mem) + (((uintptr_t) params->elemSize) * ((uintptr_t) cnt));
769             allocMem          = (void *) (tempVal);
770             params->flag[cnt] = (uint32_t) TRUE;
772             /* Decrement free count.
773              * Assert if it is zero as it can never happen. */
774             GT_assert(traceMask, (0U != params->numFreeElem));
775             params->numFreeElem--;
776             break;
777         }
778     }
780     /* Restore global interrupts */
781     HwiP_restore(cookie);
783     return (allocMem);
786 static inline int32_t Fvid2Utils_free(Fvid2Utils_PoolParams *params,
787                                       const void            *mem,
788                                       uint32_t               traceMask)
790     uint32_t cnt;
791     uintptr_t tempVal;
792     int32_t  retVal = FVID2_EFAIL;
793     uintptr_t cookie;
795     /* NULL pointer check */
796     GT_assert(traceMask, (NULL_PTR != params));
797     GT_assert(traceMask, (NULL_PTR != mem));
799     /* Disable global interrupts */
800     cookie = HwiP_disable();
802     for (cnt = 0U; cnt < params->numElem; cnt++)
803     {
804         tempVal = ((uintptr_t) params->mem) + (((uintptr_t)params->elemSize) * ((uintptr_t)cnt));
805         if ((void *) (tempVal) == mem)
806         {
807             /* Check if the memory is already allocated */
808             GT_assert(traceMask, ((uint32_t) TRUE == params->flag[cnt]));
809             params->flag[cnt] = (uint32_t) FALSE;
811             /* Increment free count.
812              * Assert if it is more than num elements as it can never happen. */
813             params->numFreeElem++;
814             GT_assert(traceMask, (params->numFreeElem <= params->numElem));
816             retVal = FVID2_SOK;
817             break;
818         }
819     }
821     /* Restore global interrupts */
822     HwiP_restore(cookie);
824     return (retVal);
827 static inline void Fvid2Utils_initTsPrfLog(Fvid2Utils_TsPrfLog *tsPrf)
829     /* NULL pointer check */
830     GT_assert(GT_DEFAULT_MASK, (NULL_PTR != tsPrf));
832     Fvid2Utils_memset(tsPrf, 0, sizeof (Fvid2Utils_TsPrfLog));
833     /* Min can't be init to 0, set a higher value so that actual min gets
834      * registered */
835     tsPrf->min = 0xFFFFFFFFU;
837     return;
840 static inline void Fvid2Utils_startTsPrfLog(Fvid2Utils_TsPrfLog *tsPrf)
842     /* NULL pointer check */
843     GT_assert(GT_DEFAULT_MASK, (NULL_PTR != tsPrf));
845     tsPrf->startTime = Osal_getTimestamp64();
847     return;
850 static inline void Fvid2Utils_endTsPrfLog(Fvid2Utils_TsPrfLog *tsPrf)
852     uint64_t endTime, diff;
854     /* NULL pointer check */
855     GT_assert(GT_DEFAULT_MASK, (NULL_PTR != tsPrf));
857     endTime = Osal_getTimestamp64();
858     diff    = endTime - tsPrf->startTime;
860     /* Log the difference */
861     tsPrf->total += diff;
862     tsPrf->totalCnt++;
863     if (diff < tsPrf->min)
864     {
865         tsPrf->min = diff;
866     }
867     if (diff > tsPrf->max)
868     {
869         tsPrf->max = diff;
870     }
871     if (tsPrf->prfLogIndex >= FVID2UTILS_NUM_TIMESTAMP_LOG)
872     {
873         tsPrf->prfLogIndex = 0U;
874     }
875     tsPrf->prfLog[tsPrf->prfLogIndex] = diff;
876     tsPrf->prfLogIndex++;
878     return;
881 static inline uint64_t Osal_getTimestamp64(void)
883     //TODO: Call OSAL API once implementation is done
884     return (0U);
887 #ifdef __cplusplus
889 #endif
891 #endif /* #ifndef FVID2_UTILS_H_ */