Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  package org.jruby.ext.ffi.jffi;
  
  import java.util.Arrays;
  import java.util.EnumMap;
  import java.util.Map;
  import org.jruby.Ruby;
  import org.jruby.RubyHash;
Some utility functions for FFI <=> jffi conversions
 
 public final class FFIUtil {
     private static final com.kenai.jffi.MemoryIO IO = com.kenai.jffi.MemoryIO.getInstance();
     
     private FFIUtil() {}
     private static final Map<NativeTypecom.kenai.jffi.TypetypeMap = buildTypeMap();
 
     private static final Map<NativeTypecom.kenai.jffi.TypebuildTypeMap() {
         Map<NativeTypecom.kenai.jffi.Typem = new EnumMap<NativeTypecom.kenai.jffi.Type>(NativeType.class);
         m.put(.....);
         m.put(.....);
 
         m.put(.....);
         m.put(.....);
         m.put(.....);
         m.put(.....);
 
         m.put(.....);
         m.put(.....);
         m.put(.....);
         m.put(.....);
 
         m.put(.....);
         m.put(.....);
         m.put(.....);
         m.put(.....);
 
         return m;
     }
 
     static final com.kenai.jffi.Type getFFIType(Type type) {
         Object jffiType;
 
         if ((jffiType = type.getFFIHandle()) instanceof com.kenai.jffi.Type) {
             return (com.kenai.jffi.TypejffiType;
         }
 
         return cacheFFIType(type);
     }
 
     private static com.kenai.jffi.Type cacheFFIType(Type type) {
         Object ffiType;
         synchronized (type) {
 
             if ((ffiType = type.getFFIHandle()) instanceof com.kenai.jffi.Type) {
                 return (com.kenai.jffi.TypeffiType;
             }
 
             if (type instanceof Type.Builtin || type instanceof CallbackInfo) {
 
                 ffiType = FFIUtil.getFFIType(type.getNativeType());
 
             } else if (type instanceof org.jruby.ext.ffi.StructLayout) {
 
                 ffiType = FFIUtil.newStruct((org.jruby.ext.ffi.StructLayouttype);
 
             } else if (type instanceof org.jruby.ext.ffi.StructByValue) {
 
                 ffiType = FFIUtil.newStruct(((org.jruby.ext.ffi.StructByValuetype).getStructLayout());
 
             } else if (type instanceof org.jruby.ext.ffi.Type.Array) {
 
                 ffiType = FFIUtil.newArray((org.jruby.ext.ffi.Type.Arraytype);
 
             } else if (type instanceof org.jruby.ext.ffi.MappedType) {
 
                 ffiType = FFIUtil.getFFIType(((org.jruby.ext.ffi.MappedTypetype).getRealType());
 
             } else {
                 return null;
             }
 
             type.setFFIHandle(ffiType);
        }
        return (com.kenai.jffi.TypeffiType;
    }
    static final com.kenai.jffi.Type getFFIType(NativeType type) {
        return .get(type);
    }
   
    
Creates a new JFFI Struct descriptor for a StructLayout

Parameters:
layout The structure layout
Returns:
A new Struct descriptor.
    static final com.kenai.jffi.Aggregate newStruct(org.jruby.ext.ffi.StructLayout layout) {
        if (layout.isUnion()) {
            //
            // The jffi union type is broken, so emulate a union with a Struct type, containing
            // an array of elements of the correct alignment.
            //
            com.kenai.jffi.Type[] alignmentTypes = {
                    ....,
                    ....,
                    ....,
                    ....,
                    ....,
                    ....,
                    ....,
            };
            com.kenai.jffi.Type alignmentType = null;
            for (com.kenai.jffi.Type t : alignmentTypes) {
                if (t.alignment() == layout.getNativeAlignment()) {
                    alignmentType = t;
                    break;
                }
            }
            if (alignmentType == null) {
                throw layout.getRuntime().newRuntimeError("cannot discern base alignment type for union of alignment "
                        + layout.getNativeAlignment());
            }
            com.kenai.jffi.Type[] fields = new com.kenai.jffi.Type[layout.getNativeSize() / alignmentType.size()];
            Arrays.fill(fieldsalignmentType);
            return com.kenai.jffi.Struct.newStruct(fields);
        } else {
            Collection<StructLayout.MemberstructMembers = layout.getMembers();
            java.util.List<com.kenai.jffi.Typefields = new java.util.ArrayList<com.kenai.jffi.Type>();
            for (StructLayout.Member m : structMembers) {
                com.kenai.jffi.Type fieldType;
                fieldType = FFIUtil.getFFIType(m.type());
                if (fieldType == null) {
                    throw layout.getRuntime().newTypeError("unsupported Struct field type " + m);
                }
                if (fieldType.size() > 0) fields.add(fieldType);
            }
            return com.kenai.jffi.Struct.newStruct(fields.toArray(new com.kenai.jffi.Type[fields.size()]));
        }
    }

    
Creates a new JFFI type descriptor for an array

Parameters:
arrayType The structure layout
Returns:
A new Struct descriptor.
    static com.kenai.jffi.Array newArray(org.jruby.ext.ffi.Type.Array arrayType) {
        com.kenai.jffi.Type componentType = FFIUtil.getFFIType(arrayType.getComponentType());
        if (componentType == null) {
            throw arrayType.getRuntime().newTypeError("unsupported array element type " + arrayType.getComponentType());
        }
        return com.kenai.jffi.Array.newArray(componentTypearrayType.length());
    }

    
Reads a nul-terminated string from native memory and boxes it up in a ruby string.

Parameters:
runtime The ruby runtime for the resulting string.
address The memory address to read the string from.
Returns:
A ruby string.
    static final IRubyObject getString(Ruby runtimelong address) {
        if (address == 0) {
            return runtime.getNil();
        }
        byte[] bytes = .getZeroTerminatedByteArray(address);
        if (bytes.length == 0) {
            return RubyString.newEmptyString(runtime);
        }
        RubyString s = RubyString.newStringNoCopy(runtimebytes);
        s.setTaint(true);
        return s;
    }
New to GrepCode? Check out our FAQ X