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 = { .pstPrev = nullptr, .pstNext = nullptr };
20 static LOS_DL_LIST g_randObjListHeader = { .pstPrev = nullptr, .pstNext = nullptr };
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 if (GetListHeader(type) == nullptr) {
49 return HCF_INVALID_PARAMS;
50 }
51 ObjList *obj = static_cast<ObjList *>(HcfMalloc(sizeof(ObjList), 0));
52 if (obj == nullptr) {
53 return HCF_ERR_MALLOC;
54 }
55 obj->objAddr = addAddr;
56
57 if (GetListHeader(type)->pstNext == nullptr) {
58 LOS_ListInit(GetListHeader(type));
59 }
60 LOS_ListAdd(GetListHeader(type), &(obj->listNode));
61
62 return HCF_SUCCESS;
63 }
64
ListDeleteObjNode(LiteAlgType type,uint32_t deleteAddr)65 void ListDeleteObjNode(LiteAlgType type, uint32_t deleteAddr)
66 {
67 ObjList *obj = nullptr;
68 ObjList *objNext = nullptr;
69 if (GetListHeader(type) == nullptr) {
70 return;
71 }
72 LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(obj, objNext, GetListHeader(type), ObjList, listNode) {
73 if (obj == nullptr) {
74 return;
75 }
76 if ((obj->objAddr != 0) && (obj->objAddr == deleteAddr)) {
77 LOS_ListDelete(&(obj->listNode));
78 HcfObjDestroy(reinterpret_cast<void *>(deleteAddr));
79 obj->objAddr = 0;
80 HcfFree(obj);
81 obj = nullptr;
82 }
83 }
84 }
85
ListDestroy(LiteAlgType type)86 void ListDestroy(LiteAlgType type)
87 {
88 ObjList *obj = nullptr;
89 ObjList *objNext = nullptr;
90 if (GetListHeader(type) == nullptr) {
91 return;
92 }
93 LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(obj, objNext, GetListHeader(type), ObjList, listNode) {
94 if (obj == nullptr) {
95 return;
96 }
97 LOS_ListDelete(&(obj->listNode));
98 HcfObjDestroy(reinterpret_cast<void *>(obj->objAddr));
99 HcfFree(obj);
100 obj = nullptr;
101 }
102 }
103
104 } // ACELite
105 } // OHOS
106