hv_comm_SafeQueue.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @file hv_comm_SafeQueue.h
  3. * @brief Header file of SafeQueue.
  4. *
  5. * @verbatim
  6. * ==============================================================================
  7. * ##### How to use #####
  8. * ==============================================================================
  9. *
  10. *
  11. * @endverbatim
  12. *
  13. * @author HiView SoC Software Team
  14. * @version 1.0.0
  15. * @date 2023-05-18
  16. */
  17. #ifndef __SDK_COMMON_UTIL_HVQUEUE_INC_HV_SAFE_QUEUE_H__
  18. #define __SDK_COMMON_UTIL_HVQUEUE_INC_HV_SAFE_QUEUE_H__
  19. #include "hv_comm_DataType.h"
  20. #include "hv_comm_Assert.h"
  21. #define DECLARE_HS_SAFE_QUEUE(NAME, QUEUE_LENGTH, ELEM_SIZE) \
  22. typedef struct _##NAME##Elem\
  23. {\
  24. UCHAR8 ucBuffer[ELEM_SIZE];\
  25. USHORT16 bufferSize;\
  26. }__attribute__((aligned(4)))NAME##Elem;\
  27. typedef struct _##NAME\
  28. {\
  29. NAME##Elem stElem[QUEUE_LENGTH];\
  30. USHORT16 m_usCount;\
  31. }__attribute__((aligned(4)))NAME;\
  32. VOID NAME##SafeQueueInit(NAME* pstHsQueue);\
  33. NAME##Elem* NAME##SafeQueueEnqueue(NAME* pstHsQueue, VOID* pvdata, USHORT16 usSize);\
  34. #define DEFINE_HS_SAFE_QUEUE(NAME, QUEUE_LENGTH, ELEM_SIZE)\
  35. VOID NAME##SafeQueueInit(NAME* pstHsQueue)\
  36. {\
  37. memset(pstHsQueue, 0, sizeof(NAME));\
  38. return;\
  39. }\
  40. NAME##Elem* NAME##SafeQueueEnqueue(NAME* pstHsQueue, VOID* pvdata, USHORT16 usSize)\
  41. {\
  42. NAME##Elem* pElem = NULL;\
  43. HV_ASSERT_VALID_PTR_RET_NO_LOG(pstHsQueue, NULL);\
  44. HV_ASSERT_VALID_PTR_RET_NO_LOG(pvdata, NULL);\
  45. HV_ASSERT_TRUE_RET_NO_LOG(usSize <= ELEM_SIZE, NULL);\
  46. HV_ASSERT_TRUE_RET_NO_LOG(pstHsQueue->m_usCount < QUEUE_LENGTH, NULL);\
  47. memcpy(pstHsQueue->stElem[pstHsQueue->m_usCount].ucBuffer, pvdata, usSize);\
  48. pstHsQueue->stElem[pstHsQueue->m_usCount].bufferSize = usSize;\
  49. pElem = &pstHsQueue->stElem[pstHsQueue->m_usCount];\
  50. pstHsQueue->m_usCount++;\
  51. pstHsQueue->m_usCount = pstHsQueue->m_usCount % QUEUE_LENGTH;\
  52. return pElem;\
  53. }
  54. /*For Test Eg*/
  55. /*DECLARE_HS_SAFE_QUEUE(SystemQueue, 50, sizeof(Event)); .h file*/
  56. /* DEFINE_HS_SAFE_QUEUE(SystemQueue, 50, sizeof(Event)); .c file*/
  57. /* SystemQueue g_SystemQueue; .c file*/
  58. #endif/*endif ---- __SDK_COMMON_UTIL_HSQUEUE_INC_HS_SAFE_QUEUE_H__ */