1 /*
2  * Copyright (c) 2021 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 "native_reference_manager.h"
17 
18 #include "native_engine/impl/ark/ark_native_reference.h"
19 
~NativeReferenceManager()20 NativeReferenceManager::~NativeReferenceManager()
21 {
22     for (auto handler = references_; handler != nullptr; handler = references_) {
23         references_ = reinterpret_cast<ArkNativeReference*>(handler)->next_;
24         delete handler;
25     }
26 }
27 
CreateHandler(NativeReference * reference)28 void NativeReferenceManager::CreateHandler(NativeReference* reference)
29 {
30     if (references_) {
31         reinterpret_cast<ArkNativeReference*>(references_)->prev_ = reference;
32         reinterpret_cast<ArkNativeReference*>(reference)->next_ = references_;
33     }
34     references_ = reference;
35 }
36 
ReleaseHandler(NativeReference * reference)37 void NativeReferenceManager::ReleaseHandler(NativeReference* reference)
38 {
39     NativeReference* prev = reinterpret_cast<ArkNativeReference*>(reference)->prev_;
40     NativeReference* next = reinterpret_cast<ArkNativeReference*>(reference)->next_;
41 
42     if (prev) {
43         reinterpret_cast<ArkNativeReference*>(prev)->next_ = next;
44     }
45     if (reference == references_) {
46         // reference is the head node.
47         references_ = next;
48     }
49     if (next) {
50         reinterpret_cast<ArkNativeReference*>(next)->prev_ = prev;
51     }
52 }
53