1 /*
2 * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #ifndef __OAL_DATA_STRU_H__
20 #define __OAL_DATA_STRU_H__
21
22 /* ****************************************************************************
23 1 其他头文件包含
24 **************************************************************************** */
25 #include "hi_types.h"
26 #include "oal_err_wifi.h"
27 #include "oal_mem.h"
28
29 #ifdef __cplusplus
30 #if __cplusplus
31 extern "C" {
32 #endif
33 #endif
34
35 /* ****************************************************************************
36 2 宏定义
37 **************************************************************************** */
38 #define OAL_QUEUE_DESTROY
39
40 /* 判断x是否是2的整数幂 */
41 #define oal_is_not_pow_of_2(_x) ((_x) & ((_x)-1))
42
43 /* ****************************************************************************
44 3 枚举定义
45 **************************************************************************** */
46 /* ****************************************************************************
47 4 全局变量声明
48 **************************************************************************** */
49 /* ****************************************************************************
50 5 消息头定义
51 **************************************************************************** */
52 /* ****************************************************************************
53 6 消息定义
54 **************************************************************************** */
55 /* ****************************************************************************
56 7 STRUCT定义
57 **************************************************************************** */
58 typedef struct {
59 hi_u8 element_cnt; /* 本队列中已经存放的元素个数 */
60 hi_u8 max_elements; /* 本队列中所能存放的最大元素个数 */
61 hi_u8 tail_index; /* 指向下一个元素入队位置的索引 */
62 hi_u8 head_index; /* 指向当前元素出队位置的索引 */
63 uintptr_t *pul_buf; /* 队列缓存 */
64 } oal_queue_stru;
65
66 /* ****************************************************************************
67 8 UNION定义
68 **************************************************************************** */
69 /* ****************************************************************************
70 9 OTHERS定义
71 **************************************************************************** */
72 /* ****************************************************************************
73 10 函数声明
74 **************************************************************************** */
75 /* ****************************************************************************
76 功能描述 : 设置队列参数
77 输入参数 : pst_queue : 队列指针
78 pul_buf : 指向队列缓冲区的指针
79 us_max_elements: 最大元素个数
80 输出参数 : 无
81 返 回 值 : 无
82 **************************************************************************** */
oal_queue_set(oal_queue_stru * queue,hi_u32 * pul_buf,hi_u8 max_elements)83 static inline hi_void oal_queue_set(oal_queue_stru *queue, hi_u32 *pul_buf, hi_u8 max_elements)
84 {
85 queue->pul_buf = (uintptr_t *)pul_buf;
86
87 queue->tail_index = 0;
88 queue->head_index = 0;
89 queue->element_cnt = 0;
90 queue->max_elements = max_elements;
91 }
92
93 /* ****************************************************************************
94 功能描述 : 销毁队列
95 输入参数 : pst_queue: 队列指针
96 输出参数 : 无
97 返 回 值 : 无
98 **************************************************************************** */
oal_queue_destroy(oal_queue_stru * queue)99 static inline hi_void oal_queue_destroy(oal_queue_stru *queue)
100 {
101 if (queue == HI_NULL) {
102 return;
103 }
104
105 if (queue->pul_buf == HI_NULL) {
106 return;
107 }
108
109 oal_mem_free(queue->pul_buf);
110
111 oal_queue_set(queue, HI_NULL, 0);
112 }
113
114 /* ****************************************************************************
115 功能描述 : 元素入队
116 输入参数 : pst_queue: 队列指针
117 p_element: 元素指针
118 输出参数 : 无
119 返 回 值 : HI_SUCCESS 或其它错误码
120 **************************************************************************** */
oal_queue_enqueue(oal_queue_stru * queue,hi_void * element)121 static inline hi_u32 oal_queue_enqueue(oal_queue_stru *queue, hi_void *element)
122 {
123 /* 异常: 队列已满 */
124 if (queue->element_cnt == queue->max_elements) {
125 return HI_FAIL;
126 }
127 hi_u8 tail_index = queue->tail_index;
128 /* 将元素的地址保存在队列中 */
129 queue->pul_buf[tail_index++] = (hi_u32)(uintptr_t)element;
130 queue->tail_index = ((tail_index >= queue->max_elements) ? 0 : tail_index);
131 queue->element_cnt++;
132
133 return HI_SUCCESS;
134 }
135
136 /* ****************************************************************************
137 功能描述 : 元素出队
138 输入参数 : pst_queue: 队列指针
139 输出参数 : 无
140 返 回 值 : 成功: 事件指针
141 失败: HI_NULL
142 **************************************************************************** */
oal_queue_dequeue(oal_queue_stru * queue)143 static inline hi_void *oal_queue_dequeue(oal_queue_stru *queue)
144 {
145 /* 异常: 队列为空 */
146 if (queue->element_cnt == 0) {
147 return HI_NULL;
148 }
149 hi_u8 head_index = queue->head_index;
150 hi_void *element = (hi_void *)queue->pul_buf[head_index++];
151 queue->head_index = ((head_index >= queue->max_elements) ? 0 : head_index);
152 queue->element_cnt--;
153 return element;
154 }
155
156 /* ****************************************************************************
157 功能描述 : 获取队列中已经存放的元素个数
158 输入参数 : pst_queue: 队列指针
159 输出参数 : 无
160 返 回 值 : 队列中存放的事件个数
161 **************************************************************************** */
oal_queue_get_length(const oal_queue_stru * queue)162 static inline hi_u8 oal_queue_get_length(const oal_queue_stru *queue)
163 {
164 if (queue == HI_NULL) {
165 return 0;
166 }
167
168 return queue->element_cnt;
169 }
170
171 #ifdef __cplusplus
172 #if __cplusplus
173 }
174 #endif
175 #endif
176
177 #endif /* end of oal_data_stru.h */
178