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