1 /*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "jsi_list.h"
17 #include "memory.h"
18
19 static LOS_DL_LIST g_mdObjListHeader = { 0 };
20 static LOS_DL_LIST g_randObjListHeader = { 0 };
21
22 namespace OHOS {
23 namespace ACELite {
24
25 ListInfo g_listMap[] = {
26 { JSI_ALG_MD, &g_mdObjListHeader },
27 { JSI_ALG_RAND, &g_randObjListHeader }
28 };
29
GetListHeader(LiteAlgType type)30 LOS_DL_LIST *GetListHeader(LiteAlgType type)
31 {
32 for (uint32_t index = 0; index < sizeof(g_listMap) / sizeof(g_listMap[0]); index++) {
33 if (type == g_listMap[index].type) {
34 return g_listMap[index].objListHeader;
35 }
36 }
37
38 return nullptr;
39 }
40
ListObjInit(LiteAlgType type)41 void ListObjInit(LiteAlgType type)
42 {
43 LOS_ListInit(GetListHeader(type));
44 }
45
ListAddObjNode(LiteAlgType type,uint32_t addAddr)46 HcfResult ListAddObjNode(LiteAlgType type, uint32_t addAddr)
47 {
48 ObjList *obj = static_cast<ObjList *>(HcfMalloc(sizeof(ObjList), 0));
49 if (obj == nullptr) {
50 return HCF_ERR_MALLOC;
51 }
52 obj->objAddr = addAddr;
53
54 if (GetListHeader(type)->pstNext == nullptr) {
55 LOS_ListInit(GetListHeader(type));
56 }
57 LOS_ListAdd(GetListHeader(type), &(obj->listNode));
58
59 return HCF_SUCCESS;
60 }
61
ListDeleteObjNode(LiteAlgType type,uint32_t deleteAddr)62 void ListDeleteObjNode(LiteAlgType type, uint32_t deleteAddr)
63 {
64 ObjList *obj = nullptr;
65 ObjList *objNext = nullptr;
66 LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(obj, objNext, GetListHeader(type), ObjList, listNode) {
67 if (obj == nullptr) {
68 return;
69 }
70 if ((obj->objAddr != 0) && (obj->objAddr == deleteAddr)) {
71 LOS_ListDelete(&(obj->listNode));
72 HcfObjDestroy(reinterpret_cast<void *>(deleteAddr));
73 obj->objAddr = 0;
74 HcfFree(obj);
75 obj = nullptr;
76 }
77 }
78 }
79
ListDestroy(LiteAlgType type)80 void ListDestroy(LiteAlgType type)
81 {
82 ObjList *obj = nullptr;
83 ObjList *objNext = nullptr;
84 uint32_t i = 0;
85 LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(obj, objNext, GetListHeader(type), ObjList, listNode) {
86 if (obj == nullptr) {
87 return;
88 }
89 LOS_ListDelete(&(obj->listNode));
90 HcfObjDestroy(reinterpret_cast<void *>(obj->objAddr));
91 HcfFree(obj);
92 obj = nullptr;
93 }
94 }
95
96 } // ACELite
97 } // OHOS
98