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 struct NativeReferenceHandler { 19 NativeReference* reference = nullptr; 20 NativeReferenceHandler* next = nullptr; 21 }; 22 NativeReferenceManager()23NativeReferenceManager::NativeReferenceManager() : referenceHandlers_(nullptr) {} 24 ~NativeReferenceManager()25NativeReferenceManager::~NativeReferenceManager() 26 { 27 for (auto handler = referenceHandlers_; handler != nullptr; handler = referenceHandlers_) { 28 referenceHandlers_ = handler->next; 29 delete handler->reference; 30 delete handler; 31 } 32 } 33 CreateHandler(NativeReference * reference)34void NativeReferenceManager::CreateHandler(NativeReference* reference) 35 { 36 NativeReferenceHandler* temp = new NativeReferenceHandler(); 37 temp->reference = reference; 38 temp->next = referenceHandlers_; 39 referenceHandlers_ = temp; 40 } 41 ReleaseHandler(NativeReference * reference)42void NativeReferenceManager::ReleaseHandler(NativeReference* reference) 43 { 44 NativeReferenceHandler* tmp = nullptr; 45 for (auto handler = referenceHandlers_; handler != nullptr; handler = handler->next) { 46 if (handler->reference == reference) { 47 if (tmp) { 48 tmp->next = handler->next; 49 } else { 50 referenceHandlers_ = handler->next; 51 } 52 delete handler; 53 break; 54 } 55 tmp = handler; 56 } 57 } 58