Deepanshu Gupta | 491523d | 2015-10-06 17:56:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package libcore.util; |
| 18 | |
| 19 | import com.android.layoutlib.bridge.impl.DelegateManager; |
| 20 | import com.android.tools.layoutlib.annotations.LayoutlibDelegate; |
| 21 | |
| 22 | /** |
| 23 | * Delegate implementing the native methods of {@link NativeAllocationRegistry} |
| 24 | * |
| 25 | * Through the layoutlib_create tool, the original native methods of NativeAllocationRegistry have |
| 26 | * been replaced by calls to methods of the same name in this delegate class. |
| 27 | * |
| 28 | * This class behaves like the original native implementation, but in Java, keeping previously |
| 29 | * native data into its own objects and mapping them to int that are sent back and forth between |
| 30 | * it and the original NativeAllocationRegistry class. |
| 31 | * |
| 32 | * @see DelegateManager |
| 33 | */ |
| 34 | public class NativeAllocationRegistry_Delegate { |
| 35 | |
| 36 | // ---- delegate manager ---- |
| 37 | private static final DelegateManager<NativeAllocationRegistry_Delegate> sManager = |
| 38 | new DelegateManager<>(NativeAllocationRegistry_Delegate.class); |
| 39 | |
| 40 | private final FreeFunction mFinalizer; |
| 41 | |
| 42 | private NativeAllocationRegistry_Delegate(FreeFunction finalizer) { |
| 43 | mFinalizer = finalizer; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * The result of this method should be cached by the class and reused. |
| 48 | */ |
| 49 | public static long createFinalizer(FreeFunction finalizer) { |
| 50 | return sManager.addNewDelegate(new NativeAllocationRegistry_Delegate(finalizer)); |
| 51 | } |
| 52 | |
| 53 | @LayoutlibDelegate |
| 54 | /*package*/ static void applyFreeFunction(long freeFunction, long nativePtr) { |
Diego Perez | c5708ef | 2016-07-21 17:54:42 +0100 | [diff] [blame] | 55 | // This method MIGHT run in the context of the finalizer thread. If the delegate method |
| 56 | // crashes, it could bring down the VM. That's why we catch all the exceptions and ignore |
| 57 | // them. |
| 58 | try { |
| 59 | NativeAllocationRegistry_Delegate delegate = sManager.getDelegate(freeFunction); |
| 60 | if (delegate != null) { |
| 61 | delegate.mFinalizer.free(nativePtr); |
| 62 | } |
| 63 | } catch (Throwable ignore) { |
Deepanshu Gupta | 491523d | 2015-10-06 17:56:37 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | public interface FreeFunction { |
| 68 | void free(long nativePtr); |
| 69 | } |
| 70 | } |