Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *
   */
  package org.jruby.ext.ffi.jffi;
  
  import java.util.HashMap;
 import java.util.Map;
 
 
 class JITCompiler {
     
     private final Map<JITSignatureHandleRef
             handles = new HashMap<JITSignatureHandleRef>();
 
     private final Map<Class<? extends NativeInvoker>, JITHandle>
             classes = new WeakHashMap();
 
     private final ReferenceQueue referenceQueue = new ReferenceQueue();
     
     private final JITHandle failedHandle = new JITHandle(this,
             new JITSignature(.new NativeType[0], falsenew boolean[0], .false),
             true);
 
     private static class SingletonHolder {
         private static final JITCompiler INSTANCE = new JITCompiler();
     }
     
     public static JITCompiler getInstance() {
         return .;
     }
     
     private static final class HandleRef extends WeakReference<JITHandle> {
         JITSignature signature;
 
         public HandleRef(JITHandle handleJITSignature signatureReferenceQueue refqueue) {
             super(handlerefqueue);
             this. = signature;
         }
     }
 
     private void cleanup() {
         HandleRef ref;
         while ((ref = (HandleRef.poll()) != null) {
             .remove(ref.signature);
         }
     }
     
     
     JITHandle getHandle(Signature signatureboolean unique) {
         
         boolean hasResultConverter = !(signature.getResultType() instanceof Type.Builtin);
         NativeType nativeResultType;
         Type resultType = signature.getResultType();
         
         if (resultType instanceof Type.Builtin || resultType instanceof CallbackInfo) {
             nativeResultType = resultType.getNativeType();
         
         } else if (resultType instanceof MappedType) {
             nativeResultType = ((MappedTyperesultType).getRealType().getNativeType();
         
         } else {
             return ;
         }
 
         NativeType[] nativeParameterTypes = new NativeType[signature.getParameterCount()];
         boolean[] hasParameterConverter = new boolean[signature.getParameterCount()];
         
         for (int i = 0; i < hasParameterConverter.lengthi++) {
             Type parameterType = signature.getParameterType(i);
             if (parameterType instanceof Type.Builtin || parameterType instanceof CallbackInfo) {
                 nativeParameterTypes[i] = parameterType.getNativeType();
         
             } else if (parameterType instanceof MappedType) {
                 nativeParameterTypes[i] = ((MappedTypeparameterType).getRealType().getNativeType();
         
             } else {
                 return ;
             }
 
             hasParameterConverter[i] = !(parameterType instanceof Type.Builtin)
                     || DataConverters.isEnumConversionRequired(parameterTypesignature.getEnums());
         }
         
         JITSignature jitSignature = new JITSignature(nativeResultTypenativeParameterTypes
                 hasResultConverterhasParameterConvertersignature.getCallingConvention(), signature.isIgnoreError());
         
        if (unique) {
            return new JITHandle(thisjitSignature"OFF".equalsIgnoreCase(..load()));
        }
        synchronized (this) {
            cleanup();
            HandleRef ref = .get(jitSignature);
            JITHandle handle = ref != null ? ref.get() : null;
            if (handle == null) {
                handle = new JITHandle(thisjitSignature"OFF".equalsIgnoreCase(..load()));
                .put(jitSignaturenew HandleRef(handlejitSignature));
            }
            
            return handle;
        }
    }
    void registerClass(JITHandle handleClass<? extends NativeInvokerklass) {
        .put(klasshandle);
    }
New to GrepCode? Check out our FAQ X