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/osal/osal.h>
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
55 /* ========================================================================== */
56 /* Macros & Typedefs */
57 /* ========================================================================== */
59 /** \brief Log enable for FVID2 Utils. */
60 #define Fvid2Trace (GT_DEFAULT_MASK)
62 /**< \brief Number of timestamp log entries. */
63 #define FVID2UTILS_NUM_TIMESTAMP_LOG (100U)
65 /**
66 * struct Fvid2Utils_LinkListType
67 * \brief Enums for the type of link list to be created.
68 */
69 typedef enum
70 {
71 FVID2UTILS_LLT_DOUBLE,
72 /**< Double link list. */
73 FVID2UTILS_LLT_CIRCULAR
74 /**< Circular link list using double link list. */
75 } Fvid2Utils_LinkListType;
77 /**
78 * struct Fvid2Utils_LinkAddMode
79 * \brief Enums for the different modes of adding a node to a link list.
80 */
81 typedef enum
82 {
83 FVID2UTILS_LAM_TOP,
84 /**< Add nodes to the top of the list. */
85 FVID2UTILS_LAM_BOTTOM,
86 /**< Add nodes to the bottom of the list. */
87 FVID2UTILS_LAM_PRIORITY
88 /**< Add nodes to the list based on ascending order of priority.
89 * Nodes with the same priority are always added to the bottom of the
90 * existing nodes with same priority. */
91 } Fvid2Utils_LinkAddMode;
93 /**
94 * struct Fvid2Utils_LinkAddMode
95 * \brief Enums for the different modes of adding a node to a link list.
96 */
97 typedef enum
98 {
99 FVID2UTILS_NODE_DIR_HEAD,
100 /**< Add nodes to the top of the list. */
101 FVID2UTILS_NODE_DIR_TAIL
102 /**< Add nodes to the bottom of the list. */
103 } Fvid2Utils_NodDir;
105 /* ========================================================================== */
106 /* Structure Declarations */
107 /* ========================================================================== */
109 /**< \brief Typedef for Node structure. */
110 typedef struct Fvid2Utils_Node_t Fvid2Utils_Node;
112 /**
113 * struct Fvid2Utils_Node_t
114 * \brief Self referential structure for double link list.
115 */
116 struct Fvid2Utils_Node_t
117 {
118 Fvid2Utils_Node *next;
119 /**< Pointer to the next node. */
120 Fvid2Utils_Node *prev;
121 /**< Pointer to the previous node. */
122 void *data;
123 /**< Node data pointer. */
124 uint32_t priority;
125 /**< Priority of the node. Used for priority based linked list. */
126 };
128 /**
129 * struct Fvid2Utils_QElem
130 * \brief Typedef for Queue Node element.
131 */
132 typedef Fvid2Utils_Node Fvid2Utils_QElem;
134 /**
135 * struct Fvid2Utils_PoolParams
136 * \brief Create parameters for the fixed size pool.
137 */
138 typedef struct
139 {
140 void *mem;
141 /**< Pointer to the pool memory. */
142 uint32_t numElem;
143 /**< Number of elements in the pool. */
144 uint32_t elemSize;
145 /**< Size of each element in bytes. */
146 uint32_t *flag;
147 /**< Array of flag variable used by pool manager to indicate whether a pool
148 * element is allocated or not. The size of this array should be
149 * equal to the number of elements in this pool. */
150 uint32_t numFreeElem;
151 /**< Count to keep track of the number of free elements in the pool. */
152 } Fvid2Utils_PoolParams;
154 /**
155 * struct Fvid2Utils_TsPrfLog
156 * \brief Timestamp Performance log structure in OSAL timestamp ticks.
157 */
158 typedef struct
159 {
160 uint64_t startTime;
161 /**< Start time - updated in start function. */
162 uint64_t total;
163 /**< Total duration of all log entires.
164 * To get average divide by totalCnt. */
165 uint32_t totalCnt;
166 /**< Total number of log entires. */
167 uint64_t min;
168 /**< Minimum duration. */
169 uint64_t max;
170 /**< Maximum duration. */
171 uint32_t prfLogIndex;
172 /**< Current index. This will wrap around every FVID2UTILS_NUM_TIMESTAMP_LOG
173 * count. */
174 uint64_t prfLog[FVID2UTILS_NUM_TIMESTAMP_LOG];
175 /**< History of the past FVID2UTILS_NUM_TIMESTAMP_LOG entires. */
176 } Fvid2Utils_TsPrfLog;
178 /**
179 * struct Fvid2UtilsLinkListObj
180 * \brief Structure to the link list object information.
181 */
182 typedef struct
183 {
184 Fvid2Utils_LinkListType listType;
185 /**< Type of linked list. */
186 Fvid2Utils_LinkAddMode addMode;
187 /**< Mode of adding a node to a link list. */
188 Fvid2Utils_Node *headNode;
189 /**< Head Node. */
190 Fvid2Utils_Node *tailNode;
191 /**< Tail Node Node. */
192 uint32_t numElements;
193 /**< Number of elements in the linked list. */
194 uint32_t priorityCnt;
195 /**< Priority count of the linked list. */
196 } Fvid2UtilsLinkListObj;
198 /**
199 * struct Fvid2Utils_Handle
200 * \brief Typedef for FVID2 Utils handle.
201 */
202 typedef Fvid2UtilsLinkListObj *Fvid2Utils_Handle;
204 /**
205 * struct Fvid2Utils_QHandle
206 * \brief Typedef for FVID2 Utils Queue Handle.
207 */
208 typedef Fvid2Utils_Handle Fvid2Utils_QHandle;
210 /* ========================================================================== */
211 /* Function Declarations */
212 /* ========================================================================== */
214 /**
215 * Fvid2Utils_memset
216 * \brief Sets the memory with the given value. Access memory as byte.
217 * Returns the memory pointer.
218 *
219 * \param mem Destination memory pointer.
220 * \param ch Byte value to fill with.
221 * \param byteCount Number of bytes to fill.
222 *
223 * \return The destination memory pointer.
224 */
225 void *Fvid2Utils_memset(void *mem, uint8_t ch, uint32_t byteCount);
227 /**
228 * Fvid2Utils_memsetw
229 * \brief Sets the memory with the given value. Access memory as word.
230 * Hence memory pointer should be aligned to 4 byte boundary
231 * Returns the memory pointer.
232 *
233 * \param mem Destination memory pointer.
234 * \param word Word value to fill with.
235 * \param wordCount Number of words to fill.
236 *
237 * \return The destination memory pointer.
238 */
239 void *Fvid2Utils_memsetw(void *mem, uint32_t word, uint32_t wordCount);
241 /**
242 * Fvid2Utils_memcpy
243 * \brief Copies source memory into destination memory. Access memory as byte.
244 * Returns the destination memory pointer.
245 *
246 * \param dest Destination memory pointer.
247 * \param src Source memory pointer.
248 * \param byteCount Number of bytes to copy.
249 *
250 * \return The destination memory pointer.
251 */
252 void *Fvid2Utils_memcpy(void *dest, const void *src, uint32_t byteCount);
254 /**
255 * Fvid2Utils_memcmp
256 * \brief Compare memory block 1 with memory block 2. Access memory as byte.
257 * Returns 0 if two memories are identical.
258 *
259 * \param mem1 memory block 1
260 * \param mem2 memory block 2
261 * \param byteCount Number of bytes to compare
262 *
263 * \return 0 if two memory are identical other return 1
264 */
265 int32_t Fvid2Utils_memcmp(const void *mem1,
266 const void *mem2,
267 uint32_t byteCount);
269 /**
270 * Fvid2Utils_constructLinkList
271 * \brief Constructs a link list object with the provided properties.
272 *
273 * \param llobj pointer to object of type Fvid2UtilsLinkListObj
274 * \param listType List type - circular/double link list.
275 * \param addMode Node addition mode - Top/Bottom/Priority based.
276 *
277 * \return Returns FVID2_SOK on success else returns error
278 * value.
279 */
280 int32_t Fvid2Utils_constructLinkList(Fvid2UtilsLinkListObj *llobj,
281 Fvid2Utils_LinkListType listType,
282 Fvid2Utils_LinkAddMode addMode);
284 /**
285 * Fvid2Utils_destructLinkList
286 * \brief Destructs a link list object.
287 *
288 * \param llobj pointer to object of type Fvid2UtilsLinkListObj
289 *
290 * \return Returns FVID2_SOK on success else returns error value.
291 */
292 int32_t Fvid2Utils_destructLinkList(Fvid2UtilsLinkListObj *llobj);
294 /**
295 * Fvid2Utils_linkNodePri
296 * \brief Links a node to the linked list according to the list type
297 * The memory to the node object should be allocated by the caller. This
298 * is used for link list with priority.
299 *
300 * \param handle Link list handle.
301 * \param node Node object pointer used for linking.
302 * \param priority Priority of the node used for priority based
303 * addition of nodes. Priority is in descending order
304 * the value. So 0 is the highest priority and is
305 * added to the top of the node.
306 * Nodes with the same priority are always added to
307 * the bottom of the existing nodes with same
308 * priority.
309 * For non-priority based modes, this parameter
310 * is ignored and could be set to 0.
311 */
312 void Fvid2Utils_linkNodePri(Fvid2Utils_Handle handle,
313 Fvid2Utils_Node *node,
314 uint32_t priority);
316 /**
317 * Fvid2Utils_linkUniqePriNode
318 * \brief Very similar to Fvid2Utils_linkNode, except that on equal priority
319 * nodes will not be inserted. An error (FVID2_EBADARGS) would be
320 * returned.
321 * Applicable for double linked list only.
322 * ToDo Update to handle circular list also.
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 int32_t Fvid2Utils_linkUniqePriNode(Fvid2Utils_Handle handle,
337 Fvid2Utils_Node *node,
338 uint32_t priority);
340 /**
341 * Fvid2Utils_unLinkAllNodes
342 * \brief Releases all nodes without modifying any of the property.
343 * CAUTION - Memory is NOT de-allocated, its the responsibility of the
344 * caller to ensure de-allocation of memory.
345 *
346 * \param handle Link list handle.
347 *
348 * \return Returns 0 on success else returns error value.
349 */
350 int32_t Fvid2Utils_unLinkAllNodes(Fvid2Utils_Handle handle);
352 /**
353 * Fvid2Utils_unLinkNodePri
354 * \brief Unlinks the node from the list. Used for the Priority linklists.
355 *
356 * \param handle Link list handle.
357 * \param node Node pointer to be unlinked from the list.
358 */
359 void Fvid2Utils_unLinkNodePri(Fvid2Utils_Handle handle, Fvid2Utils_Node *node);
361 /**
362 * Fvid2Utils_unLinkNode
363 * \brief Unlinks the node from the list. Used for Non-priority linked lists
364 *
365 * \param handle Link list handle.
366 * \param node Node pointer to be unlinked from the list.
367 */
368 void Fvid2Utils_unLinkNode(Fvid2Utils_Handle handle,
369 const Fvid2Utils_Node *node);
371 /**
372 * Fvid2Utils_linkNodeToHead
373 * \brief Link the node to the head of the double linked list.
374 * No priority
375 *
376 * \param handle Link list handle.
377 * \param node Node object pointer used for linking.
378 *
379 */
380 void Fvid2Utils_linkNodeToHead(Fvid2Utils_Handle handle,
381 Fvid2Utils_Node *node);
383 /**
384 * Fvid2Utils_linkNodeToTail
385 * \brief Link the node to the tail of the double linked list.
386 * No priority
387 *
388 * \param handle Link list handle.
389 * \param node Node object pointer used for linking.
390 */
391 void Fvid2Utils_linkNodeToTail(Fvid2Utils_Handle handle,
392 Fvid2Utils_Node *node);
394 /**
395 * Fvid2Utils_unLinkNodeFromHead
396 * \brief Returns the node from head. Removes the node from
397 * the list.
398 *
399 * \param handle Link list handle.
400 * \return Pointer to unlinked node.
401 *
402 */
403 Fvid2Utils_Node *Fvid2Utils_unLinkNodeFromHead(Fvid2Utils_Handle handle);
405 /**
406 * Fvid2Utils_unLinkNodeFromTail
407 * \brief Returns the node from tail. Removes the node from
408 * the list.
409 *
410 * \param handle Link list handle.
411 * \return Pointer to unlinked node.
412 *
413 */
414 Fvid2Utils_Node *Fvid2Utils_unLinkNodeFromTail(Fvid2Utils_Handle handle);
416 /**
417 * Fvid2Utils_getHeadNode
418 * \brief Returns the reference to the headNode. Does
419 * not remove the node from the head.
420 *
421 * \param handle Link list handle.
422 */
423 Fvid2Utils_Node *Fvid2Utils_getHeadNode(Fvid2Utils_Handle handle);
425 /**
426 * Fvid2Utils_getTailNode
427 * \brief Returns the reference to the TailNode. Does
428 * not remove the node from the head.
429 *
430 * \param handle Link list handle.
431 * \return Reference to tail node. Acutally node is not
432 * unlinked from list.
433 *
434 */
435 Fvid2Utils_Node *Fvid2Utils_getTailNode(Fvid2Utils_Handle handle);
437 /**
438 * Fvid2Utils_isListEmpty
439 * \brief Checks whether a list is empty or not.
440 *
441 * \param handle List handle.
442 *
443 * \return TRUE if List is empty else returns FALSE.
444 */
445 uint32_t Fvid2Utils_isListEmpty(Fvid2Utils_Handle handle);
447 /**
448 * Fvid2Utils_getNumNodes
449 * \brief Returns the number of nodes present in a list.
450 *
451 * \param handle List handle.
452 *
453 * \return Number of nodes present in a list.
454 */
455 uint32_t Fvid2Utils_getNumNodes(Fvid2Utils_Handle handle);
457 /**
458 * Fvid2Utils_getNodeCnt
459 * \brief Returns the number of nodes in the link list
460 *
461 * \param handle Link list handle.
462 *
463 *
464 * \return Returns the number of nodes in the list.
465 */
466 uint32_t Fvid2Utils_getNodeCnt(Fvid2Utils_Handle handle);
468 /**
469 * Fvid2Utils_constructQ
470 * \brief Constructs a Queue object.
471 *
472 * \param llobj pointer to object of type Fvid2UtilsLinkListObj
473 *
474 * \return Returns FVID2_SOK on success else returns error value.
475 */
476 int32_t Fvid2Utils_constructQ(Fvid2UtilsLinkListObj *llobj);
478 /**
479 * Fvid2Utils_destructQ
480 * \brief Destructs a Queue object.
481 *
482 * \param llobj pointer to object of type Fvid2UtilsLinkListObj
483 *
484 * \return Returns FVID2_SOK on success else returns error value.
485 */
486 int32_t Fvid2Utils_destructQ(Fvid2UtilsLinkListObj *llobj);
488 /**
489 * Fvid2Utils_queue
490 * \brief Adds the data to the queue.
491 * The memory to the node object should be allocated by the caller.
492 *
493 * \param handle Queue handle.
494 * \param qElem Queue Element object pointer used for linking.
495 * \param data Data pointer to be added to the list.
496 * This should be unique.
497 */
498 void Fvid2Utils_queue(Fvid2Utils_QHandle handle,
499 Fvid2Utils_QElem *qElem,
500 void *data);
502 /**
503 * Fvid2Utils_dequeue
504 * \brief Removes a element from the queue.
505 *
506 * \param handle Queue handle.
507 *
508 * \return Returns the removed data pointer.
509 */
510 void *Fvid2Utils_dequeue(Fvid2Utils_QHandle handle);
512 /**
513 * Fvid2Utils_peakHead
514 * \brief Returns the reference of the first queued element.
515 *
516 * This doesn't remove the element from the queue.
517 * If the queue is empty, then this returns NULL.
518 *
519 * \param handle Queue handle.
520 *
521 * \return Returns the reference of the first element.
522 */
523 void *Fvid2Utils_peakHead(Fvid2Utils_QHandle handle);
525 /**
526 * Fvid2Utils_peakTail
527 * \brief Returns the reference of the last queued element.
528 *
529 * This doesn't remove the element from the queue.
530 * If the queue is empty, then this returns NULL.
531 *
532 * \param handle Queue handle.
533 *
534 * \return Returns the reference of the last queued element.
535 */
536 void *Fvid2Utils_peakTail(Fvid2Utils_QHandle handle);
538 /**
539 * Fvid2Utils_queueBack
540 * \brief Adds the data to start of the queue.
541 * The memory to the node object should be allocated by the caller.
542 *
543 * \param handle Queue handle.
544 * \param qElem Queue Element object pointer used for linking.
545 * \param data Data pointer to be added to the list.
546 * This should be unique.
547 */
548 void Fvid2Utils_queueBack(Fvid2Utils_QHandle handle,
549 Fvid2Utils_QElem *qElem,
550 void *data);
552 /**
553 * Fvid2Utils_isQEmpty
554 * \brief Checks whether a queue is empty or not.
555 *
556 * \param handle Queue handle.
557 *
558 * \return TRUE if queue is empty else returns FALSE.
559 */
560 uint32_t Fvid2Utils_isQEmpty(Fvid2Utils_QHandle handle);
562 /**
563 * Fvid2Utils_getNumQElem
564 * \brief Returns the number of Queue Elements present in a Queue.
565 *
566 * \param handle Queue handle.
567 *
568 * \return Number of Queue Elements present in a Queue.
569 */
570 uint32_t Fvid2Utils_getNumQElem(Fvid2Utils_QHandle handle);
572 /**
573 * Fvid2Utils_initPool
574 * \brief Initializes the pool object structure.
575 *
576 * \param params Pool parameters to be initialized.
577 * \param mem Pointer to the pool memory.
578 * \param numElem Number of elements in the pool.
579 * \param elemSize Size of each element in bytes.
580 * \param flag Array of flag variable used by pool manager to
581 * indicate whether a pool element is allocated
582 * or not. The size of this array should be
583 * equal to the number of elements in this pool.
584 * \param traceMask Trace mask used in trace prints.
585 */
586 static inline void Fvid2Utils_initPool(Fvid2Utils_PoolParams *params,
587 void *mem,
588 uint32_t numElem,
589 uint32_t elemSize,
590 uint32_t *flag,
591 uint32_t traceMask);
593 /**
594 * Fvid2Utils_alloc
595 * \brief Allocates one element from the pool.
596 *
597 * \param params Pool parameters.
598 * \param size Size in bytes to allocate.
599 * \param traceMask Trace mask used in trace prints.
600 *
601 * \return Returns memory pointer on success else returns
602 * NULL.
603 */
604 static inline void *Fvid2Utils_alloc(Fvid2Utils_PoolParams *params,
605 uint32_t size,
606 uint32_t traceMask);
608 /**
609 * Fvid2Utils_free
610 * \brief Frees the element and returns to the pool.
611 *
612 * \param params Pool parameters.
613 * \param mem Memory pointer to deallocate.
614 * \param traceMask Trace mask used in trace prints.
615 *
616 * \return Returns 0 on success else returns error value.
617 */
618 static inline int32_t Fvid2Utils_free(Fvid2Utils_PoolParams *params,
619 const void *mem,
620 uint32_t traceMask);
622 /**
623 * Fvid2Utils_initTsPrfLog
624 * \brief Initializes the structure. This should be called once before
625 * calling any other performance functions.
626 *
627 * \param tsPrf Pointer to Fvid2Utils_TsPrfLog structure.
628 *
629 */
630 static inline void Fvid2Utils_initTsPrfLog(Fvid2Utils_TsPrfLog *tsPrf);
632 /**
633 * Fvid2Utils_startTsPrfLog
634 * \brief Log the start entry to the performance structure.
635 *
636 * \param tsPrf Pointer to Fvid2Utils_TsPrfLog structure.
637 */
638 static inline void Fvid2Utils_startTsPrfLog(Fvid2Utils_TsPrfLog *tsPrf);
640 /**
641 * Fvid2Utils_endTsPrfLog
642 * \brief Log the entry to the performance structure. This uses the previous
643 * startTime stored when calling Fvid2Utils_startTsPrfLog() to calculate the
644 * difference.
645 *
646 * \param tsPrf Pointer to Fvid2Utils_TsPrfLog structure.
647 */
648 static inline void Fvid2Utils_endTsPrfLog(Fvid2Utils_TsPrfLog *tsPrf);
650 //TODO
651 static inline uint64_t Osal_getTimestamp64(void);
653 /* ========================================================================== */
654 /* Static Function Definitions */
655 /* ========================================================================== */
657 static inline void Fvid2Utils_initPool(Fvid2Utils_PoolParams *params,
658 void *mem,
659 uint32_t numElem,
660 uint32_t elemSize,
661 uint32_t *flag,
662 uint32_t traceMask)
663 {
664 uint32_t cnt;
666 /* NULL pointer check */
667 GT_assert(traceMask, (NULL != params));
668 GT_assert(traceMask, (NULL != mem));
669 GT_assert(traceMask, (NULL != flag));
671 /* Init pool parameters */
672 params->mem = mem;
673 params->numElem = numElem;
674 params->elemSize = elemSize;
675 params->flag = flag;
676 params->numFreeElem = numElem;
678 /* Set pool flags as free */
679 for (cnt = 0U; cnt < params->numElem; cnt++)
680 {
681 params->flag[cnt] = (uint32_t) FALSE;
682 }
684 return;
685 }
687 static inline void *Fvid2Utils_alloc(Fvid2Utils_PoolParams *params,
688 uint32_t size,
689 uint32_t traceMask)
690 {
691 uint32_t cnt;
692 uintptr_t cookie;
693 uint32_t tempVal;
694 void *allocMem = NULL;
696 /* NULL pointer check */
697 GT_assert(traceMask, (NULL != params));
698 /* Check if the requested size if within each fixed size element */
699 GT_assert(traceMask, (size <= params->elemSize));
700 GT_assert(traceMask, (0U != size));
702 /* Disable global interrupts */
703 cookie = HwiP_disable();
705 for (cnt = 0U; cnt < params->numElem; cnt++)
706 {
707 if ((uint32_t) FALSE == params->flag[cnt])
708 {
709 tempVal = ((uint32_t) params->mem) + (params->elemSize * cnt);
710 allocMem = (void *) (tempVal);
711 params->flag[cnt] = (uint32_t) TRUE;
713 /* Decrement free count.
714 * Assert if it is zero as it can never happen. */
715 GT_assert(traceMask, (0U != params->numFreeElem));
716 params->numFreeElem--;
717 break;
718 }
719 }
721 /* Restore global interrupts */
722 HwiP_restore(cookie);
724 return (allocMem);
725 }
727 static inline int32_t Fvid2Utils_free(Fvid2Utils_PoolParams *params,
728 const void *mem,
729 uint32_t traceMask)
730 {
731 uint32_t cnt;
732 uint32_t tempVal;
733 int32_t retVal = FVID2_EFAIL;
734 uintptr_t cookie;
736 /* NULL pointer check */
737 GT_assert(traceMask, (NULL != params));
738 GT_assert(traceMask, (NULL != mem));
740 /* Disable global interrupts */
741 cookie = HwiP_disable();
743 for (cnt = 0U; cnt < params->numElem; cnt++)
744 {
745 tempVal = ((uint32_t) params->mem) + (params->elemSize * cnt);
746 if ((void *) (tempVal) == mem)
747 {
748 /* Check if the memory is already allocated */
749 GT_assert(traceMask, ((uint32_t) TRUE == params->flag[cnt]));
750 params->flag[cnt] = (uint32_t) FALSE;
752 /* Increment free count.
753 * Assert if it is more than num elements as it can never happen. */
754 params->numFreeElem++;
755 GT_assert(traceMask, (params->numFreeElem <= params->numElem));
757 retVal = FVID2_SOK;
758 break;
759 }
760 }
762 /* Restore global interrupts */
763 HwiP_restore(cookie);
765 return (retVal);
766 }
768 static inline void Fvid2Utils_initTsPrfLog(Fvid2Utils_TsPrfLog *tsPrf)
769 {
770 /* NULL pointer check */
771 GT_assert(GT_DEFAULT_MASK, (NULL != tsPrf));
773 Fvid2Utils_memset(tsPrf, 0, sizeof (Fvid2Utils_TsPrfLog));
774 /* Min can't be init to 0, set a higher value so that actual min gets
775 * registered */
776 tsPrf->min = 0xFFFFFFFFU;
778 return;
779 }
781 static inline void Fvid2Utils_startTsPrfLog(Fvid2Utils_TsPrfLog *tsPrf)
782 {
783 /* NULL pointer check */
784 GT_assert(GT_DEFAULT_MASK, (NULL != tsPrf));
786 tsPrf->startTime = Osal_getTimestamp64();
788 return;
789 }
791 static inline void Fvid2Utils_endTsPrfLog(Fvid2Utils_TsPrfLog *tsPrf)
792 {
793 uint64_t endTime, diff;
795 /* NULL pointer check */
796 GT_assert(GT_DEFAULT_MASK, (NULL != tsPrf));
798 endTime = Osal_getTimestamp64();
799 diff = endTime - tsPrf->startTime;
801 /* Log the difference */
802 tsPrf->total += diff;
803 tsPrf->totalCnt++;
804 if (diff < tsPrf->min)
805 {
806 tsPrf->min = diff;
807 }
808 if (diff > tsPrf->max)
809 {
810 tsPrf->max = diff;
811 }
812 if (tsPrf->prfLogIndex >= FVID2UTILS_NUM_TIMESTAMP_LOG)
813 {
814 tsPrf->prfLogIndex = 0U;
815 }
816 tsPrf->prfLog[tsPrf->prfLogIndex] = diff;
817 tsPrf->prfLogIndex++;
819 return;
820 }
822 static inline uint64_t Osal_getTimestamp64(void)
823 {
824 //TODO: Call OSAL API once implementation is done
825 return (0U);
826 }
828 #ifdef __cplusplus
829 }
830 #endif
832 #endif /* #ifndef FVID2_UTILS_H_ */