private static final int getNativeAlignment(NativeType type) { return isPrimitive(type) ? Factory.getInstance().alignmentOf(type) : 1; } private static final int getNativeSize(NativeType type) {
static MemoryPointer allocate(Ruby runtime, int typeSize, int count, boolean clear) { final int total = typeSize * count; MemoryIO io = Factory.getInstance().allocateDirectMemory(runtime, total > 0 ? total : 1, clear); if (io == null) { throw RaiseException.from(runtime, runtime.getNoMemoryError(), String.format("Failed to allocate %d objects of %d bytes", count, typeSize)); } return new MemoryPointer(runtime, runtime.getFFI().memptrClass, io, total, typeSize); }
private static MemoryIO allocateMemoryIO(Ruby runtime, int size) { return Factory.getInstance().allocateTransientDirectMemory(runtime, size, 8, true); } }
private static final int getNativeSize(NativeType type) { return isPrimitive(type) ? Factory.getInstance().sizeOf(type) : 0; }
public final MemoryIO getMemoryIO(long offset) { checkBounds(offset, ADDRESS_SIZE >> 3); return Factory.getInstance().wrapDirectMemory(runtime, getAddress(offset)); }
@JRubyMethod(name = "put_callback", required = 3) public IRubyObject put_callback(ThreadContext context, IRubyObject offset, IRubyObject proc, IRubyObject cbInfo) { if (!(cbInfo instanceof CallbackInfo)) { throw context.runtime.newArgumentError("invalid CallbackInfo"); } Pointer ptr = Factory.getInstance().getCallbackManager().getCallback(context.runtime, (CallbackInfo) cbInfo, proc); getMemoryIO().putMemoryIO(getOffset(offset), ((AbstractMemory) ptr).getMemoryIO()); return this; }
public IRubyObject get(ThreadContext context, StructLayout.Storage cache, Member m, AbstractMemory ptr) { final long address = ((Pointer) ptr).getMemoryIO().getAddress(m.getOffset(ptr)); AbstractInvoker fptr = (AbstractInvoker) cache.getCachedValue(m); if (fptr != null && fptr.getAddress() == address) { return fptr; } fptr = Factory.getInstance().newFunction(context.runtime, ((Pointer) ptr).getPointer(context.runtime, m.getOffset(ptr)), (CallbackInfo) m.type); cache.putCachedValue(m, fptr); return fptr; }
public void load(final Ruby runtime, boolean wrap) throws IOException { if (!runtime.getInstanceConfig().isNativeEnabled()) { throw runtime.newLoadError("Native API access is disabled"); } if (!Platform.getPlatform().isSupported()) { throw runtime.newLoadError("Unsupported platform: " + Platform.getPlatform().getName()); } RubyModule ffi = runtime.defineModule("FFI"); try { Factory.getInstance().init(runtime, ffi); } catch (Exception e) { throw runtime.newLoadError("Could not load FFI Provider: " + e.getLocalizedMessage() + " See http://jira.codehaus.org/browse/JRUBY-4583"); } } }
@Override public void init(Ruby runtime, RubyModule ffi) { super.init(runtime, ffi); synchronized (ffi) { if (ffi.getClass("DynamicLibrary") == null) { DynamicLibrary.createDynamicLibraryClass(runtime, ffi); } if (ffi.getClass("Invoker") == null) { JFFIInvoker.createInvokerClass(runtime, ffi); } if (ffi.getClass("VariadicInvoker") == null) { VariadicInvoker.createVariadicInvokerClass(runtime, ffi); } if (ffi.getClass("Callback") == null) { CallbackManager.createCallbackClass(runtime, ffi); } if (ffi.getClass("Function") == null) { Function.createFunctionClass(runtime, ffi); } if (ffi.getClass("LastError") == null) { ffi.defineModuleUnder("LastError").defineAnnotatedMethods(LastError.class); } } runtime.setFFI(new FFI(ffi)); }
Pointer cb = Factory.getInstance().getCallbackManager().getCallback(runtime, (CallbackInfo) type, value); buffer.setAddressReturn(addressValue(cb)); } else {
public final MemoryIO getMemoryIO(long offset) { checkBounds(offset, ADDRESS_SIZE >> 3); return Factory.getInstance().wrapDirectMemory(runtime, getAddress(offset)); }
@JRubyMethod(name = "put_callback", required = 3) public IRubyObject put_callback(ThreadContext context, IRubyObject offset, IRubyObject proc, IRubyObject cbInfo) { if (!(cbInfo instanceof CallbackInfo)) { throw context.runtime.newArgumentError("invalid CallbackInfo"); } Pointer ptr = Factory.getInstance().getCallbackManager().getCallback(context.runtime, (CallbackInfo) cbInfo, proc); getMemoryIO().putMemoryIO(getOffset(offset), ((AbstractMemory) ptr).getMemoryIO()); return this; }
private static final int getNativeSize(NativeType type) { return isPrimitive(type) ? Factory.getInstance().sizeOf(type) : 0; }
public IRubyObject get(ThreadContext context, StructLayout.Storage cache, Member m, AbstractMemory ptr) { final long address = ((Pointer) ptr).getMemoryIO().getAddress(m.getOffset(ptr)); AbstractInvoker fptr = (AbstractInvoker) cache.getCachedValue(m); if (fptr != null && fptr.getAddress() == address) { return fptr; } fptr = Factory.getInstance().newFunction(context.runtime, ((Pointer) ptr).getPointer(context.runtime, m.getOffset(ptr)), (CallbackInfo) m.type); cache.putCachedValue(m, fptr); return fptr; }
public void load(final Ruby runtime, boolean wrap) throws IOException { if (!runtime.getInstanceConfig().isNativeEnabled()) { throw runtime.newLoadError("Native API access is disabled"); } if (!Platform.getPlatform().isSupported()) { throw runtime.newLoadError("Unsupported platform: " + Platform.getPlatform().getName()); } RubyModule ffi = runtime.defineModule("FFI"); try { Factory.getInstance().init(runtime, ffi); } catch (Exception e) { throw runtime.newLoadError("Could not load FFI Provider: " + e.getLocalizedMessage() + " See http://jira.codehaus.org/browse/JRUBY-4583"); } } }
@Override public void init(Ruby runtime, RubyModule ffi) { super.init(runtime, ffi); synchronized (ffi) { if (ffi.getClass("DynamicLibrary") == null) { DynamicLibrary.createDynamicLibraryClass(runtime, ffi); } if (ffi.getClass("Invoker") == null) { JFFIInvoker.createInvokerClass(runtime, ffi); } if (ffi.getClass("VariadicInvoker") == null) { VariadicInvoker.createVariadicInvokerClass(runtime, ffi); } if (ffi.getClass("Callback") == null) { CallbackManager.createCallbackClass(runtime, ffi); } if (ffi.getClass("Function") == null) { Function.createFunctionClass(runtime, ffi); } if (ffi.getClass("LastError") == null) { ffi.defineModuleUnder("LastError").defineAnnotatedMethods(LastError.class); } } runtime.setFFI(new FFI(ffi)); }
Pointer cb = Factory.getInstance().getCallbackManager().getCallback(runtime, (CallbackInfo) type, value); buffer.setAddressReturn(addressValue(cb)); } else {
public final MemoryIO getMemoryIO(long offset) { checkBounds(offset, ADDRESS_SIZE >> 3); return Factory.getInstance().wrapDirectMemory(runtime, getAddress(offset)); }
static MemoryPointer allocate(Ruby runtime, int typeSize, int count, boolean clear) { final int total = typeSize * count; MemoryIO io = Factory.getInstance().allocateDirectMemory(runtime, total > 0 ? total : 1, clear); if (io == null) { throw new RaiseException(runtime, runtime.getNoMemoryError(), String.format("Failed to allocate %d objects of %d bytes", count, typeSize), true); } return new MemoryPointer(runtime, runtime.getFFI().memptrClass, io, total, typeSize); }
@JRubyMethod(name = "put_callback", required = 3) public IRubyObject put_callback(ThreadContext context, IRubyObject offset, IRubyObject proc, IRubyObject cbInfo) { if (!(cbInfo instanceof CallbackInfo)) { throw context.runtime.newArgumentError("invalid CallbackInfo"); } Pointer ptr = Factory.getInstance().getCallbackManager().getCallback(context.runtime, (CallbackInfo) cbInfo, proc); getMemoryIO().putMemoryIO(getOffset(offset), ((AbstractMemory) ptr).getMemoryIO()); return this; }