1491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta/*
2491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * Copyright (C) 2016 The Android Open Source Project
3491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta *
4491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * Licensed under the Apache License, Version 2.0 (the "License");
5491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * you may not use this file except in compliance with the License.
6491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * You may obtain a copy of the License at
7491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta *
8491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta *      http://www.apache.org/licenses/LICENSE-2.0
9491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta *
10491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * Unless required by applicable law or agreed to in writing, software
11491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * distributed under the License is distributed on an "AS IS" BASIS,
12491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * See the License for the specific language governing permissions and
14491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * limitations under the License.
15491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta */
16491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta
17491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Guptapackage libcore.util;
18491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta
19491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Guptaimport com.android.layoutlib.bridge.impl.DelegateManager;
20491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Guptaimport com.android.tools.layoutlib.annotations.LayoutlibDelegate;
21491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta
22491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta/**
23491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * Delegate implementing the native methods of {@link NativeAllocationRegistry}
24491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta *
25491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * Through the layoutlib_create tool, the original native methods of NativeAllocationRegistry have
26491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * been replaced by calls to methods of the same name in this delegate class.
27491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta *
28491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * This class behaves like the original native implementation, but in Java, keeping previously
29491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * native data into its own objects and mapping them to int that are sent back and forth between
30491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * it and the original NativeAllocationRegistry class.
31491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta *
32491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta * @see DelegateManager
33491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta */
34491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Guptapublic class NativeAllocationRegistry_Delegate {
35491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta
36491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta    // ---- delegate manager ----
37491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta    private static final DelegateManager<NativeAllocationRegistry_Delegate> sManager =
38491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta            new DelegateManager<>(NativeAllocationRegistry_Delegate.class);
39491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta
40491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta    private final FreeFunction mFinalizer;
41491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta
42491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta    private NativeAllocationRegistry_Delegate(FreeFunction finalizer) {
43491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta        mFinalizer = finalizer;
44491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta    }
45491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta
46491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta    /**
47491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta     * The result of this method should be cached by the class and reused.
48491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta     */
49491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta    public static long createFinalizer(FreeFunction finalizer) {
50491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta        return sManager.addNewDelegate(new NativeAllocationRegistry_Delegate(finalizer));
51491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta    }
52491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta
53491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta    @LayoutlibDelegate
54491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta    /*package*/ static void applyFreeFunction(long freeFunction, long nativePtr) {
55c5708ef76d6ace5251f2d7f05bc7e8bd97655185Diego Perez        // This method MIGHT run in the context of the finalizer thread. If the delegate method
56c5708ef76d6ace5251f2d7f05bc7e8bd97655185Diego Perez        // crashes, it could bring down the VM. That's why we catch all the exceptions and ignore
57c5708ef76d6ace5251f2d7f05bc7e8bd97655185Diego Perez        // them.
58c5708ef76d6ace5251f2d7f05bc7e8bd97655185Diego Perez        try {
59c5708ef76d6ace5251f2d7f05bc7e8bd97655185Diego Perez            NativeAllocationRegistry_Delegate delegate = sManager.getDelegate(freeFunction);
60c5708ef76d6ace5251f2d7f05bc7e8bd97655185Diego Perez            if (delegate != null) {
61c5708ef76d6ace5251f2d7f05bc7e8bd97655185Diego Perez                delegate.mFinalizer.free(nativePtr);
62c5708ef76d6ace5251f2d7f05bc7e8bd97655185Diego Perez            }
63c5708ef76d6ace5251f2d7f05bc7e8bd97655185Diego Perez        } catch (Throwable ignore) {
64491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta        }
65491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta    }
66491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta
67491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta    public interface FreeFunction {
68491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta        void free(long nativePtr);
69491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta    }
70491523d52cd8368ef9a92e95fb3e9332bf86a996Deepanshu Gupta}
71