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