All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jnr.ffi.provider.jffi.NativeClosureManager Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2011 Wayne Meissner
 *
 * This file is part of the JNR project.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jnr.ffi.provider.jffi;

import jnr.ffi.Pointer;
import jnr.ffi.mapper.*;
import jnr.ffi.provider.ClosureManager;

import java.util.IdentityHashMap;
import java.util.Map;

/**
 *
 */
final class NativeClosureManager implements ClosureManager {
    private volatile Map, NativeClosureFactory> factories = new IdentityHashMap, NativeClosureFactory>();
    private final jnr.ffi.Runtime runtime;
    private final SignatureTypeMapper typeMapper;
    private final AsmClassLoader classLoader;

    NativeClosureManager(jnr.ffi.Runtime runtime, SignatureTypeMapper typeMapper, AsmClassLoader classLoader) {
        this.runtime = runtime;
        this.typeMapper = new CompositeTypeMapper(typeMapper, new CachingTypeMapper(new ClosureTypeMapper()));
        this.classLoader = classLoader;
    }

     NativeClosureFactory getClosureFactory(Class closureClass) {
        NativeClosureFactory factory = factories.get(closureClass);
        if (factory != null) {
            return factory;
        }

        return initClosureFactory(closureClass);
    }

    public  T newClosure(Class closureClass, T instance) {
        NativeClosureFactory factory = factories.get(closureClass);
        if (factory != null) {
            //return factory.newClosure(instance);
        }
        return null;
    }

    public final  jnr.ffi.Pointer getClosurePointer(Class closureClass, T instance) {
        return getClosureFactory(closureClass).getClosureReference(instance).getPointer();
    }

    synchronized  NativeClosureFactory initClosureFactory(Class closureClass) {
        NativeClosureFactory factory = factories.get(closureClass);
        if (factory != null) {
            return factory;
        }


        factory = NativeClosureFactory.newClosureFactory(runtime, closureClass, typeMapper, classLoader);
        Map, NativeClosureFactory> factories = new IdentityHashMap, NativeClosureFactory>();
        factories.putAll(this.factories);
        factories.put(closureClass, factory);
        this.factories = factories;

        return factory;
    }

     ToNativeConverter newClosureSite(Class closureClass) {
        return new ClosureSite(getClosureFactory(closureClass));
    }

    @ToNativeConverter.NoContext
    public static final class ClosureSite implements ToNativeConverter {
        private final NativeClosureFactory factory;
        private NativeClosureFactory.ClosureReference closureReference = null;

        private ClosureSite(NativeClosureFactory factory) {
            this.factory = factory;
        }

        public Pointer toNative(T value, ToNativeContext context) {
            // If passing down a function pointer, don't re-wrap it
            if (value instanceof ClosureFromNativeConverter.AbstractClosurePointer) {
                return (ClosureFromNativeConverter.AbstractClosurePointer) value;
            }

            NativeClosureFactory.ClosureReference ref = closureReference;
            // Fast path - same delegate as last call to this site - just re-use the native closure
            if (ref != null && ref.getCallable() == value) {
                return ref.getPointer();
            }

            ref = factory.getClosureReference(value);
            // Cache the new native closure, if this site has no valid native closure
            if (closureReference == null || closureReference.get() == null) {
                closureReference = ref;
            }

            return ref.getPointer();
        }

        public Class nativeType() {
            return Pointer.class;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy