Skip to content

Commit

Permalink
Rebase PointerParameterStrategy hierarchy on jnr.ffi.ObjectParameterS…
Browse files Browse the repository at this point in the history
…trategy
  • Loading branch information
vp-of-awesome committed Apr 1, 2012
1 parent 6cc1072 commit f29b9f6
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 134 deletions.
17 changes: 7 additions & 10 deletions src/org/jruby/ext/ffi/jffi/AbstractNumericMethodGenerator.java
@@ -1,20 +1,17 @@
package org.jruby.ext.ffi.jffi;

import com.kenai.jffi.ObjectParameterInfo;
import com.kenai.jffi.ObjectParameterStrategy;
import com.kenai.jffi.Platform;
import org.jruby.ext.ffi.Buffer;
import org.jruby.RubyString;
import org.jruby.ext.ffi.AbstractMemory;
import org.jruby.ext.ffi.Pointer;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.compiler.impl.SkinnyMethodAdapter;
import org.jruby.ext.ffi.NativeType;
import org.jruby.ext.ffi.Struct;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.objectweb.asm.Label;

import static org.jruby.util.CodegenUtils.*;
import static org.objectweb.asm.Opcodes.*;
import static org.objectweb.asm.Opcodes.ACC_FINAL;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;

/**
*
Expand Down Expand Up @@ -139,15 +136,15 @@ public void generate(AsmClassBuilder builder, SkinnyMethodAdapter mv, JITSignatu
sig(PointerParameterStrategy.class, IRubyObject.class));
mv.astore(nextStrategyVar);
mv.aload(nextStrategyVar);
mv.invokevirtual(p(PointerParameterStrategy.class), "isDirect", sig(boolean.class));
mv.invokevirtual(p(ObjectParameterStrategy.class), "isDirect", sig(boolean.class));
mv.iftrue(address);
mv.iinc(heapPointerCountVar, 1);

mv.label(address);
// It is now direct, get the address, and convert to the native int type
mv.aload(nextStrategyVar);
mv.aload(paramVar);
mv.invokevirtual(p(PointerParameterStrategy.class), "getAddress", sig(long.class, IRubyObject.class));
mv.invokevirtual(p(ObjectParameterStrategy.class), "address", sig(long.class, Object.class));
narrow(mv, long.class, nativeIntType);
nextStrategyVar++;
mv.label(next);
Expand Down
Expand Up @@ -15,7 +15,7 @@ public ConstStringPointerParameterStrategy() {
}

@Override
public long getAddress(IRubyObject parameter) {
public long address(Object parameter) {
RubyString s = (RubyString) parameter;
Object existingHandle = s.getFFIHandle();
if (existingHandle instanceof NativeStringHandle) {
Expand All @@ -26,8 +26,8 @@ public long getAddress(IRubyObject parameter) {
}

ByteList bl = s.getByteList();
StringSupport.checkStringSafety(parameter.getRuntime(), parameter);
DirectMemoryIO memory = TransientNativeMemoryIO.allocateAligned(parameter.getRuntime(), bl.length() + 1, 1, false);
StringSupport.checkStringSafety(s.getRuntime(), s);
DirectMemoryIO memory = TransientNativeMemoryIO.allocateAligned(s.getRuntime(), bl.length() + 1, 1, false);
memory.putZeroTerminatedByteArray(0, bl.getUnsafeBytes(), bl.begin(), bl.length());
s.setByteListShared();
s.setFFIHandle(new NativeStringHandle(memory, s.getByteList()));
Expand All @@ -36,18 +36,18 @@ public long getAddress(IRubyObject parameter) {
}

@Override
public Object array(IRubyObject parameter) {
StringSupport.checkStringSafety(parameter.getRuntime(), parameter);
public Object object(Object parameter) {
StringSupport.checkStringSafety(((IRubyObject) parameter).getRuntime(), (IRubyObject) parameter);
return ((RubyString) parameter).getByteList().unsafeBytes();
}

@Override
public int arrayOffset(IRubyObject parameter) {
public int offset(Object parameter) {
return ((RubyString) parameter).getByteList().begin();
}

@Override
public int arrayLength(IRubyObject parameter) {
public int length(Object parameter) {
return ((RubyString) parameter).getByteList().length();
}

Expand Down
9 changes: 2 additions & 7 deletions src/org/jruby/ext/ffi/jffi/DefaultMethodFactory.java
Expand Up @@ -7,7 +7,6 @@
import com.kenai.jffi.InvocationBuffer;
import com.kenai.jffi.Invoker;
import com.kenai.jffi.ArrayFlags;
import org.jruby.RubyBoolean;
import org.jruby.RubyHash;
import org.jruby.RubyModule;
import org.jruby.RubyNumeric;
Expand All @@ -22,18 +21,14 @@
import org.jruby.ext.ffi.MemoryPointer;
import org.jruby.ext.ffi.NativeType;
import org.jruby.ext.ffi.Platform;
import org.jruby.ext.ffi.Pointer;
import org.jruby.ext.ffi.Struct;
import org.jruby.ext.ffi.StructByValue;
import org.jruby.ext.ffi.StructLayout;
import org.jruby.ext.ffi.Type;
import org.jruby.ext.ffi.Util;
import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
import org.jruby.util.StringSupport;


public final class DefaultMethodFactory extends MethodFactory {
Expand Down Expand Up @@ -620,10 +615,10 @@ public PointerParameterMarshaller(int flags) {
public final void marshal(ThreadContext context, InvocationBuffer buffer, IRubyObject parameter,
PointerParameterStrategy strategy) {
if (strategy.isDirect()) {
buffer.putAddress(strategy.getAddress(parameter));
buffer.putAddress(strategy.address(parameter));

} else {
buffer.putArray(byte[].class.cast(strategy.array(parameter)), strategy.arrayOffset(parameter), strategy.arrayLength(parameter),
buffer.putArray(byte[].class.cast(strategy.object(parameter)), strategy.offset(parameter), strategy.length(parameter),
flags);
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/org/jruby/ext/ffi/jffi/DelegatingPointerParameterStrategy.java
@@ -1,39 +1,39 @@
package org.jruby.ext.ffi.jffi;

import org.jruby.ext.ffi.AbstractMemory;
import org.jruby.ext.ffi.ArrayMemoryIO;
import com.kenai.jffi.ObjectParameterType;
import org.jruby.runtime.builtin.IRubyObject;

/**
*
*/
public final class DelegatingPointerParameterStrategy extends PointerParameterStrategy {
private static final ObjectParameterType OBJECT_TYPE = ObjectParameterType.create(ObjectParameterType.ARRAY, ObjectParameterType.ComponentType.BYTE);
private final IRubyObject value;
private final PointerParameterStrategy strategy;

public DelegatingPointerParameterStrategy(IRubyObject value, PointerParameterStrategy strategy) {
super(strategy.isDirect());
super(strategy.isDirect(), OBJECT_TYPE);
this.value = value;
this.strategy = strategy;
}

@Override
public long getAddress(IRubyObject parameter) {
return strategy.getAddress(value);
public long address(Object parameter) {
return strategy.address(value);
}

@Override
public Object array(IRubyObject parameter) {
return strategy.array(value);
public Object object(Object parameter) {
return strategy.object(value);
}

@Override
public int arrayOffset(IRubyObject parameter) {
return strategy.arrayOffset(value);
public int offset(Object parameter) {
return strategy.offset(value);
}

@Override
public int arrayLength(IRubyObject parameter) {
return strategy.arrayLength(value);
public int length(Object parameter) {
return strategy.length(value);
}
}
10 changes: 4 additions & 6 deletions src/org/jruby/ext/ffi/jffi/DirectPointerParameterStrategy.java
Expand Up @@ -2,8 +2,6 @@

import org.jruby.ext.ffi.AbstractMemory;
import org.jruby.ext.ffi.DirectMemoryIO;
import org.jruby.ext.ffi.Pointer;
import org.jruby.runtime.builtin.IRubyObject;

/**
*
Expand All @@ -14,23 +12,23 @@ public final class DirectPointerParameterStrategy extends PointerParameterStrate
}

@Override
public final long getAddress(IRubyObject parameter) {
public final long address(Object parameter) {
// System.out.printf("direct pointer strategy returning address=%x\n", ((DirectMemoryIO) ((AbstractMemory) parameter).getMemoryIO()).getAddress());
return ((DirectMemoryIO) ((AbstractMemory) parameter).getMemoryIO()).getAddress();
}

@Override
public Object array(IRubyObject parameter) {
public Object object(Object parameter) {
throw new RuntimeException("no array");
}

@Override
public int arrayOffset(IRubyObject parameter) {
public int offset(Object parameter) {
throw new RuntimeException("no array");
}

@Override
public int arrayLength(IRubyObject parameter) {
public int length(Object parameter) {
throw new RuntimeException("no array");
}
}
11 changes: 4 additions & 7 deletions src/org/jruby/ext/ffi/jffi/DirectStructParameterStrategy.java
@@ -1,10 +1,7 @@
package org.jruby.ext.ffi.jffi;

import org.jruby.ext.ffi.AbstractMemory;
import org.jruby.ext.ffi.DirectMemoryIO;
import org.jruby.ext.ffi.Pointer;
import org.jruby.ext.ffi.Struct;
import org.jruby.runtime.builtin.IRubyObject;

/**
*
Expand All @@ -15,22 +12,22 @@ public DirectStructParameterStrategy() {
}

@Override
public long getAddress(IRubyObject parameter) {
public long address(Object parameter) {
return ((DirectMemoryIO) ((Struct) parameter).getMemory().getMemoryIO()).getAddress();
}

@Override
public Object array(IRubyObject parameter) {
public Object object(Object parameter) {
throw new RuntimeException("no array");
}

@Override
public int arrayOffset(IRubyObject parameter) {
public int offset(Object parameter) {
throw new RuntimeException("no array");
}

@Override
public int arrayLength(IRubyObject parameter) {
public int length(Object parameter) {
throw new RuntimeException("no array");
}
}
13 changes: 6 additions & 7 deletions src/org/jruby/ext/ffi/jffi/HeapPointerParameterStrategy.java
@@ -1,35 +1,34 @@
package org.jruby.ext.ffi.jffi;

import com.kenai.jffi.ObjectParameterType;
import org.jruby.ext.ffi.AbstractMemory;
import org.jruby.ext.ffi.ArrayMemoryIO;
import org.jruby.ext.ffi.Buffer;
import org.jruby.runtime.builtin.IRubyObject;

/**
*
*/
public final class HeapPointerParameterStrategy extends PointerParameterStrategy {
public HeapPointerParameterStrategy() {
super(false);
super(HEAP, ObjectParameterType.create(ObjectParameterType.ARRAY, ObjectParameterType.ComponentType.BYTE));
}

@Override
public long getAddress(IRubyObject parameter) {
public long address(Object parameter) {
return 0;
}

@Override
public Object array(IRubyObject parameter) {
public Object object(Object parameter) {
return ((ArrayMemoryIO) ((AbstractMemory) parameter).getMemoryIO()).array();
}

@Override
public int arrayOffset(IRubyObject parameter) {
public int offset(Object parameter) {
return ((ArrayMemoryIO) ((AbstractMemory) parameter).getMemoryIO()).arrayOffset();
}

@Override
public int arrayLength(IRubyObject parameter) {
public int length(Object parameter) {
return ((ArrayMemoryIO) ((AbstractMemory) parameter).getMemoryIO()).arrayLength();
}
}
13 changes: 6 additions & 7 deletions src/org/jruby/ext/ffi/jffi/HeapStructParameterStrategy.java
@@ -1,35 +1,34 @@
package org.jruby.ext.ffi.jffi;

import org.jruby.ext.ffi.AbstractMemory;
import com.kenai.jffi.ObjectParameterType;
import org.jruby.ext.ffi.ArrayMemoryIO;
import org.jruby.ext.ffi.Struct;
import org.jruby.runtime.builtin.IRubyObject;

/**
*
*/
public final class HeapStructParameterStrategy extends PointerParameterStrategy {
public HeapStructParameterStrategy() {
super(false);
super(HEAP, ObjectParameterType.create(ObjectParameterType.ARRAY, ObjectParameterType.ComponentType.BYTE));
}

@Override
public long getAddress(IRubyObject parameter) {
public long address(Object parameter) {
return 0;
}

@Override
public Object array(IRubyObject parameter) {
public Object object(Object parameter) {
return ((ArrayMemoryIO) ((Struct) parameter).getMemory().getMemoryIO()).array();
}

@Override
public int arrayOffset(IRubyObject parameter) {
public int offset(Object parameter) {
return ((ArrayMemoryIO) ((Struct) parameter).getMemory().getMemoryIO()).arrayOffset();
}

@Override
public int arrayLength(IRubyObject parameter) {
public int length(Object parameter) {
return ((ArrayMemoryIO) ((Struct) parameter).getMemory().getMemoryIO()).arrayLength();
}
}

0 comments on commit f29b9f6

Please sign in to comment.