更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
vid.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "vid_type.h"
33#include "vid_api.h"
34#include "los_memory.h"
35//虚拟ID映射链表初始化
37{
38 (void)memset_s(&processCB->timerIdMap, sizeof(TimerIdMap), 0, sizeof(TimerIdMap));
39 LOS_ListInit(&processCB->timerIdMap.head);
40 processCB->timerIdMap.bitMap = (UINT32*)LOS_MemAlloc(m_aucSysMem0, sizeof(UINT32));
41 if (processCB->timerIdMap.bitMap == NULL) {
42 PRINT_ERR("%s %d, alloc memory failed\n", __FUNCTION__, __LINE__);
43 return LOS_NOK;
44 }
45
46 processCB->timerIdMap.mapCount = 1;
47 (void)memset_s(processCB->timerIdMap.bitMap, sizeof(UINT32), 0, sizeof(UINT32));
48 if (LOS_MuxInit(&processCB->timerIdMap.vidMapLock, NULL) != LOS_OK) {
49 PRINT_ERR("%s %d, Create mutex for vmm failed\n", __FUNCTION__, __LINE__);
51 processCB->timerIdMap.bitMap = NULL;
52 return LOS_NOK;
53 }
54 return LOS_OK;
55}
56///销毁虚拟ID映射
58{
59 TimerIdMapNode *idNode = NULL;
60 TimerIdMapNode *idNodeNext = NULL;
61
62 LOS_MuxLock(&processCB->timerIdMap.vidMapLock, LOS_WAIT_FOREVER);
63 LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(idNode, idNodeNext, &processCB->timerIdMap.head, TimerIdMapNode, node) {
64 LOS_ListDelete(&idNode->node);
66 }
67
71}
72
74{
75 TimerIdMapNode *idNode = NULL;
76 LosProcessCB *processCB = OsCurrProcessGet();
77
78 LOS_MuxLock(&processCB->timerIdMap.vidMapLock, LOS_WAIT_FOREVER);
79 LOS_DL_LIST_FOR_EACH_ENTRY(idNode, &processCB->timerIdMap.head, TimerIdMapNode, node) {
80 if (vid == idNode->vid) {
82 return idNode;
83 }
84 }
86
87 return NULL;
88}
89
91{
92 TimerIdMapNode *idNode = NULL;
93 LosProcessCB *processCB = OsCurrProcessGet();
94
95 LOS_MuxLock(&processCB->timerIdMap.vidMapLock, LOS_WAIT_FOREVER);
96 LOS_DL_LIST_FOR_EACH_ENTRY(idNode, &processCB->timerIdMap.head, TimerIdMapNode, node) {
97 if (rid == idNode->rid) {
99 return idNode;
100 }
101 }
103
104 return NULL;
105}
106
107static UINT16 GetFreeVid(VOID)
108{
109 UINT16 i, j;
110 UINT32 num;
111 UINT32 *tmp = NULL;
112 LosProcessCB *processCB = OsCurrProcessGet();
113 TimerIdMap *idMap = &processCB->timerIdMap;
114 UINT16 mapMaxNum = idMap->mapCount;
115
116 for (i = 0; i < mapMaxNum; i++) {
117 num = idMap->bitMap[i];
118 for (j = 0; j < INT_BIT_COUNT; j++) {
119 if ((num & (1 << j)) == 0) {
120 num |= 1 << j;
121 idMap->bitMap[i] = num;
122 return (INT_BIT_COUNT * i + j);
123 }
124 }
125 }
126
127 /* expand bit map */
128 mapMaxNum++;
129 if (mapMaxNum > VID_MAP_MAX_NUM) {
130 PRINT_ERR("%s %d, timer vid run out\n", __FUNCTION__, __LINE__);
131 return MAX_INVALID_TIMER_VID;
132 }
133
134 tmp = (UINT32*)LOS_MemAlloc(m_aucSysMem0, mapMaxNum * sizeof(UINT32));
135 if (tmp == NULL) {
136 PRINT_ERR("%s %d, alloc memory failed\n", __FUNCTION__, __LINE__);
137 return MAX_INVALID_TIMER_VID;
138 }
139
140 (void)memcpy_s(tmp, mapMaxNum * sizeof(UINT32), idMap->bitMap, (mapMaxNum - 1) * sizeof(UINT32));
142 idMap->bitMap = tmp;
143 idMap->mapCount = mapMaxNum;
144 idMap->bitMap[i] = 1;
145 return (INT_BIT_COUNT * i);
146}
147
148static VOID ReleaseVid(UINT16 vid)
149{
150 UINT16 a, b;
151 UINT32 *tmpMap = NULL;
152 LosProcessCB *processCB = OsCurrProcessGet();
153 TimerIdMap *idMap = &processCB->timerIdMap;
154 UINT16 mapMaxNum = idMap->mapCount;
155
156 if (vid >= (VID_MAP_MAX_NUM * INT_BIT_COUNT)) {
157 return;
158 }
159
160 a = vid >> INT_BIT_SHIFT;
161 b = vid & (INT_BIT_COUNT - 1);
162
163 idMap->bitMap[a] &= ~(1 << b);
164
165 /* shrink bit map */
166 if (mapMaxNum > 1) {
167 if (idMap->bitMap[mapMaxNum - 1] == 0) {
168 mapMaxNum--;
169 tmpMap = LOS_MemRealloc(m_aucSysMem0, idMap->bitMap, mapMaxNum * sizeof(UINT32));
170 if (tmpMap) {
171 idMap->bitMap = tmpMap;
172 idMap->mapCount = mapMaxNum;
173 }
174 }
175 }
176}
177
179{
180 TimerIdMapNode *tmp = NULL;
181 LosProcessCB *processCB = OsCurrProcessGet();
182 UINT16 vid;
183
184 tmp = FindListNodeByRid(rid);
185 if (tmp) {
186 return tmp->vid;
187 }
188
189 LOS_MuxLock(&processCB->timerIdMap.vidMapLock, LOS_WAIT_FOREVER);
190 vid = GetFreeVid();
191 if (vid == MAX_INVALID_TIMER_VID) {
193 errno = ENOMEM;
194 return MAX_INVALID_TIMER_VID;
195 }
197 if (tmp == NULL) {
198 PRINT_ERR("%s %d, alloc memory failed\n", __FUNCTION__, __LINE__);
200 errno = ENOMEM;
201 return MAX_INVALID_TIMER_VID;
202 }
203 tmp->rid = rid;
204 tmp->vid = vid;
205
206 LOS_ListTailInsert(&processCB->timerIdMap.head, &tmp->node);
208
209 return vid;
210}
211
213{
215 if (tmp) {
216 return tmp->rid;
217 }
218 return 0xffff;
219}
220
222{
223 TimerIdMapNode *tmp = NULL;
224 LosProcessCB *processCB = OsCurrProcessGet();
225
226 tmp = FindListNodeByVid(vid);
227 if (tmp == NULL) {
228 return;
229 }
230
231 LOS_MuxLock(&processCB->timerIdMap.vidMapLock, LOS_WAIT_FOREVER);
232 LOS_ListDelete(&tmp->node);
233 ReleaseVid(tmp->vid);
236
237 return;
238}
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListInit(LOS_DL_LIST *list)
Definition: los_list.h:104
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListTailInsert(LOS_DL_LIST *list, LOS_DL_LIST *node)
Insert a node to the tail of a doubly linked list.
Definition: los_list.h:244
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListDelete(LOS_DL_LIST *node)
Definition: los_list.h:292
VOID * LOS_MemAlloc(VOID *pool, UINT32 size)
从指定内存池中申请size长度的内存,注意这可不是从内核堆空间中申请内存
Definition: los_memory.c:1123
VOID * LOS_MemRealloc(VOID *pool, VOID *ptr, UINT32 size)
按size大小重新分配内存块,并将原内存块内容拷贝到新内存块。如果新内存块申请成功,则释放原内存块
Definition: los_memory.c:1510
UINT32 LOS_MemFree(VOID *pool, VOID *ptr)
释放从指定动态内存中申请的内存
Definition: los_memory.c:1369
UINT8 * m_aucSysMem0
异常交互动态内存池地址的起始地址,当不支持异常交互特性时,m_aucSysMem0等于m_aucSysMem1。
Definition: los_memory.c:107
LITE_OS_SEC_TEXT UINT32 LOS_MuxInit(LosMux *mutex, const LosMuxAttr *attr)
初始化互斥锁
Definition: los_mux.c:262
LITE_OS_SEC_TEXT UINT32 LOS_MuxUnlock(LosMux *mutex)
释放锁
Definition: los_mux.c:559
LITE_OS_SEC_TEXT UINT32 LOS_MuxDestroy(LosMux *mutex)
销毁互斥锁
Definition: los_mux.c:289
LITE_OS_SEC_TEXT UINT32 LOS_MuxLock(LosMux *mutex, UINT32 timeout)
拿互斥锁,
Definition: los_mux.c:437
STATIC INLINE LosProcessCB * OsCurrProcessGet(VOID)
unsigned short UINT16
Definition: los_typedef.h:56
unsigned int UINT32
Definition: los_typedef.h:57
TimerIdMap timerIdMap
LOS_DL_LIST head
Definition: vid_type.h:38
LosMux vidMapLock
Definition: vid_type.h:37
UINT16 mapCount
Definition: vid_type.h:39
UINT32 * bitMap
Definition: vid_type.h:40
UINT16 rid
Definition: vid_type.h:45
LOS_DL_LIST node
Definition: vid_type.h:44
UINT16 vid
Definition: vid_type.h:46
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void
static VOID ReleaseVid(UINT16 vid)
Definition: vid.c:148
static TimerIdMapNode * FindListNodeByRid(UINT32 rid)
Definition: vid.c:90
UINT16 GetRidByVid(UINT16 vid)
Definition: vid.c:212
void VidMapDestroy(LosProcessCB *processCB)
销毁虚拟ID映射
Definition: vid.c:57
void RemoveNodeByVid(UINT16 vid)
Definition: vid.c:221
UINT32 VidMapListInit(LosProcessCB *processCB)
Definition: vid.c:36
static TimerIdMapNode * FindListNodeByVid(UINT16 vid)
Definition: vid.c:73
static UINT16 GetFreeVid(VOID)
Definition: vid.c:107
UINT16 AddNodeByRid(UINT16 rid)
Definition: vid.c:178