• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
VidMapListInit(LosProcessCB * processCB)36 UINT32 VidMapListInit(LosProcessCB *processCB)
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__);
50         LOS_MemFree(m_aucSysMem0, processCB->timerIdMap.bitMap);
51         processCB->timerIdMap.bitMap = NULL;
52         return LOS_NOK;
53     }
54     return LOS_OK;
55 }
56 
VidMapDestroy(LosProcessCB * processCB)57 void VidMapDestroy(LosProcessCB *processCB)
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);
65         LOS_MemFree(m_aucSysMem0, idNode);
66     }
67 
68     LOS_MuxUnlock(&processCB->timerIdMap.vidMapLock);
69     LOS_MemFree(m_aucSysMem0, processCB->timerIdMap.bitMap);
70     LOS_MuxDestroy(&processCB->timerIdMap.vidMapLock);
71 }
72 
FindListNodeByVid(UINT16 vid)73 static TimerIdMapNode *FindListNodeByVid(UINT16 vid)
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) {
81             LOS_MuxUnlock(&processCB->timerIdMap.vidMapLock);
82             return idNode;
83         }
84     }
85     LOS_MuxUnlock(&processCB->timerIdMap.vidMapLock);
86 
87     return NULL;
88 }
89 
FindListNodeByRid(UINT32 rid)90 static TimerIdMapNode *FindListNodeByRid(UINT32 rid)
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) {
98             LOS_MuxUnlock(&processCB->timerIdMap.vidMapLock);
99             return idNode;
100         }
101     }
102     LOS_MuxUnlock(&processCB->timerIdMap.vidMapLock);
103 
104     return NULL;
105 }
106 
GetFreeVid(VOID)107 static 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 & (1U << j)) == 0) {
120                 num |= 1U << 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));
141     LOS_MemFree(m_aucSysMem0, idMap->bitMap);
142     idMap->bitMap = tmp;
143     idMap->mapCount = mapMaxNum;
144     idMap->bitMap[i] = 1;
145     return (INT_BIT_COUNT * i);
146 }
147 
ReleaseVid(UINT16 vid)148 static 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] &= ~(1U << 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 
AddNodeByRid(UINT16 rid)178 UINT16 AddNodeByRid(UINT16 rid)
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) {
192         LOS_MuxUnlock(&processCB->timerIdMap.vidMapLock);
193         errno = ENOMEM;
194         return MAX_INVALID_TIMER_VID;
195     }
196     tmp = (TimerIdMapNode *)LOS_MemAlloc(m_aucSysMem0, sizeof(TimerIdMapNode));
197     if (tmp == NULL) {
198         PRINT_ERR("%s %d, alloc memory failed\n", __FUNCTION__, __LINE__);
199         LOS_MuxUnlock(&processCB->timerIdMap.vidMapLock);
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);
207     LOS_MuxUnlock(&processCB->timerIdMap.vidMapLock);
208 
209     return vid;
210 }
211 
GetRidByVid(UINT16 vid)212 UINT16 GetRidByVid(UINT16 vid)
213 {
214     TimerIdMapNode *tmp = FindListNodeByVid(vid);
215     if (tmp) {
216         return tmp->rid;
217     }
218     return 0xffff;
219 }
220 
RemoveNodeByVid(UINT16 vid)221 void RemoveNodeByVid(UINT16 vid)
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);
234     LOS_MuxUnlock(&processCB->timerIdMap.vidMapLock);
235     LOS_MemFree(m_aucSysMem0, tmp);
236 
237     return;
238 }
239