Skip to content

Commit

Permalink
Showing 5 changed files with 114 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -585,9 +585,9 @@ public Integer block() throws InterruptedException {
return nativeSockets().select(
max(readableFds) + 1,
readableSet.getPointer(),
PointerPrimitiveNodes.NULL_POINTER,
PointerPrimitiveNodes.NULL_POINTER,
PointerPrimitiveNodes.NULL_POINTER);
PointerNodes.NULL_POINTER,
PointerNodes.NULL_POINTER,
PointerNodes.NULL_POINTER);
}
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.nodes.rubinius;

import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.object.*;
import jnr.ffi.Pointer;
import org.jruby.truffle.nodes.objects.Allocator;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyBasicObject;
import org.jruby.truffle.runtime.core.RubyClass;

import java.util.EnumSet;

public abstract class PointerNodes {

public static final Pointer NULL_POINTER = jnr.ffi.Runtime.getSystemRuntime().getMemoryManager().newOpaquePointer(0);

private static final HiddenKey POINTER_IDENTIFIER = new HiddenKey("pointer");
private static final Property POINTER_PROPERTY;
private static final DynamicObjectFactory POINTER_FACTORY;

static {
final Shape.Allocator allocator = RubyBasicObject.LAYOUT.createAllocator();
POINTER_PROPERTY = Property.create(POINTER_IDENTIFIER, allocator.locationForType(Pointer.class, EnumSet.of(LocationModifier.NonNull)), 0);
POINTER_FACTORY = RubyBasicObject.EMPTY_SHAPE.addProperty(POINTER_PROPERTY).createFactory();
}

public static class PointerAllocator implements Allocator {
@Override
public RubyBasicObject allocate(RubyContext context, RubyClass rubyClass, Node currentNode) {
return createPointer(rubyClass, NULL_POINTER);
}
}

public static RubyBasicObject createPointer(RubyClass rubyClass, Pointer pointer) {
if (pointer == null) {
pointer = NULL_POINTER;
}

return new RubyBasicObject(rubyClass, POINTER_FACTORY.newInstance(pointer));
}

public static void setPointer(RubyBasicObject pointer, Pointer newPointer) {
assert newPointer != null;
assert pointer.getDynamicObject().getShape().hasProperty(POINTER_IDENTIFIER);

try {
POINTER_PROPERTY.set(pointer.getDynamicObject(), newPointer, pointer.getDynamicObject().getShape());
} catch (IncompatibleLocationException | FinalLocationException e) {
throw new UnsupportedOperationException(e);
}
}

public static Pointer getPointer(RubyBasicObject pointer) {
assert pointer.getDynamicObject().getShape().hasProperty(POINTER_IDENTIFIER);
return (Pointer) POINTER_PROPERTY.get(pointer.getDynamicObject(), true);
}

}
Original file line number Diff line number Diff line change
@@ -30,49 +30,6 @@

public abstract class PointerPrimitiveNodes {

public static final Pointer NULL_POINTER = jnr.ffi.Runtime.getSystemRuntime().getMemoryManager().newOpaquePointer(0);

private static final HiddenKey POINTER_IDENTIFIER = new HiddenKey("pointer");
private static final Property POINTER_PROPERTY;
private static final DynamicObjectFactory POINTER_FACTORY;

static {
final Shape.Allocator allocator = RubyBasicObject.LAYOUT.createAllocator();
POINTER_PROPERTY = Property.create(POINTER_IDENTIFIER, allocator.locationForType(Pointer.class, EnumSet.of(LocationModifier.NonNull)), 0);
POINTER_FACTORY = RubyBasicObject.EMPTY_SHAPE.addProperty(POINTER_PROPERTY).createFactory();
}

public static class PointerAllocator implements Allocator {
@Override
public RubyBasicObject allocate(RubyContext context, RubyClass rubyClass, Node currentNode) {
return PointerPrimitiveNodes.createPointer(rubyClass, NULL_POINTER);
}
}

public static RubyBasicObject createPointer(RubyClass rubyClass, Pointer pointer) {
if (pointer == null) {
pointer = NULL_POINTER;
}

return new RubyBasicObject(rubyClass, POINTER_FACTORY.newInstance(pointer));
}

public static void setPointer(RubyBasicObject pointer, Pointer newPointer) {
assert newPointer != null;
assert pointer.getDynamicObject().getShape().hasProperty(POINTER_IDENTIFIER);

try {
POINTER_PROPERTY.set(pointer.getDynamicObject(), newPointer, pointer.getDynamicObject().getShape());
} catch (IncompatibleLocationException | FinalLocationException e) {
throw new UnsupportedOperationException(e);
}
}

public static Pointer getPointer(RubyBasicObject pointer) {
assert pointer.getDynamicObject().getShape().hasProperty(POINTER_IDENTIFIER);
return (Pointer) POINTER_PROPERTY.get(pointer.getDynamicObject(), true);
}

@RubiniusPrimitive(name = "pointer_malloc")
public static abstract class PointerMallocPrimitiveNode extends RubiniusPrimitiveNode {

@@ -87,7 +44,7 @@ public RubyBasicObject malloc(RubyClass pointerClass, int size) {

@Specialization
public RubyBasicObject malloc(RubyClass pointerClass, long size) {
return createPointer(pointerClass, getMemoryManager().newPointer(UnsafeHolder.U.allocateMemory(size)));
return PointerNodes.createPointer(pointerClass, getMemoryManager().newPointer(UnsafeHolder.U.allocateMemory(size)));
}

}
@@ -101,7 +58,7 @@ public PointerFreePrimitiveNode(RubyContext context, SourceSection sourceSection

@Specialization
public RubyBasicObject free(RubyBasicObject pointer) {
UnsafeHolder.U.freeMemory(getPointer(pointer).address());
UnsafeHolder.U.freeMemory(PointerNodes.getPointer(pointer).address());
return pointer;
}

@@ -121,7 +78,7 @@ public long setAddress(RubyBasicObject pointer, int address) {

@Specialization
public long setAddress(RubyBasicObject pointer, long address) {
setPointer(pointer, getMemoryManager().newPointer(address));
PointerNodes.setPointer(pointer, getMemoryManager().newPointer(address));
return address;
}

@@ -141,7 +98,7 @@ public RubyBasicObject add(RubyBasicObject a, int b) {

@Specialization
public RubyBasicObject add(RubyBasicObject a, long b) {
return createPointer(a.getLogicalClass(), getMemoryManager().newPointer(getPointer(a).address() + b));
return PointerNodes.createPointer(a.getLogicalClass(), getMemoryManager().newPointer(PointerNodes.getPointer(a).address() + b));
}

}
@@ -155,7 +112,7 @@ public PointerReadIntPrimitiveNode(RubyContext context, SourceSection sourceSect

@Specialization(guards = "isSigned(signed)")
public int readInt(RubyBasicObject pointer, boolean signed) {
return getPointer(pointer).getInt(0);
return PointerNodes.getPointer(pointer).getInt(0);
}

protected boolean isSigned(boolean signed) {
@@ -174,7 +131,7 @@ public PointerReadStringPrimitiveNode(RubyContext context, SourceSection sourceS
@Specialization
public RubyBasicObject readString(RubyBasicObject pointer, int length) {
final byte[] bytes = new byte[length];
getPointer(pointer).get(0, bytes, 0, length);
PointerNodes.getPointer(pointer).get(0, bytes, 0, length);
return createString(bytes);
}

@@ -205,25 +162,25 @@ public PointerSetAtOffsetPrimitiveNode(RubyContext context, SourceSection source

@Specialization(guards = "type == TYPE_INT")
public int setAtOffsetInt(RubyBasicObject pointer, int offset, int type, int value) {
getPointer(pointer).putInt(offset, value);
PointerNodes.getPointer(pointer).putInt(offset, value);
return value;
}

@Specialization(guards = "type == TYPE_LONG")
public long setAtOffsetLong(RubyBasicObject pointer, int offset, int type, long value) {
getPointer(pointer).putLong(offset, value);
PointerNodes.getPointer(pointer).putLong(offset, value);
return value;
}

@Specialization(guards = "type == TYPE_ULONG")
public long setAtOffsetULong(RubyBasicObject pointer, int offset, int type, long value) {
getPointer(pointer).putLong(offset, value);
PointerNodes.getPointer(pointer).putLong(offset, value);
return value;
}

@Specialization(guards = "type == TYPE_ULL")
public long setAtOffsetULL(RubyBasicObject pointer, int offset, int type, long value) {
getPointer(pointer).putLongLong(offset, value);
PointerNodes.getPointer(pointer).putLongLong(offset, value);
return value;
}

@@ -238,7 +195,7 @@ public PointerReadPointerPrimitiveNode(RubyContext context, SourceSection source

@Specialization
public RubyBasicObject readPointer(RubyBasicObject pointer) {
return createPointer(pointer.getLogicalClass(), getPointer(pointer).getPointer(0));
return PointerNodes.createPointer(pointer.getLogicalClass(), PointerNodes.getPointer(pointer).getPointer(0));
}

}
@@ -252,7 +209,7 @@ public PointerAddressPrimitiveNode(RubyContext context, SourceSection sourceSect

@Specialization
public long address(RubyBasicObject pointer) {
return getPointer(pointer).address();
return PointerNodes.getPointer(pointer).address();
}

}
@@ -267,57 +224,57 @@ public PointerGetAtOffsetPrimitiveNode(RubyContext context, SourceSection source

@Specialization(guards = "type == TYPE_CHAR")
public int getAtOffsetChar(RubyBasicObject pointer, int offset, int type) {
return getPointer(pointer).getByte(offset);
return PointerNodes.getPointer(pointer).getByte(offset);
}

@Specialization(guards = "type == TYPE_UCHAR")
public int getAtOffsetUChar(RubyBasicObject pointer, int offset, int type) {
return getPointer(pointer).getByte(offset);
return PointerNodes.getPointer(pointer).getByte(offset);
}

@Specialization(guards = "type == TYPE_INT")
public int getAtOffsetInt(RubyBasicObject pointer, int offset, int type) {
return getPointer(pointer).getInt(offset);
return PointerNodes.getPointer(pointer).getInt(offset);
}

@Specialization(guards = "type == TYPE_SHORT")
public int getAtOffsetShort(RubyBasicObject pointer, int offset, int type) {
return getPointer(pointer).getShort(offset);
return PointerNodes.getPointer(pointer).getShort(offset);
}

@Specialization(guards = "type == TYPE_USHORT")
public int getAtOffsetUShort(RubyBasicObject pointer, int offset, int type) {
return getPointer(pointer).getShort(offset);
return PointerNodes.getPointer(pointer).getShort(offset);
}

@Specialization(guards = "type == TYPE_LONG")
public long getAtOffsetLong(RubyBasicObject pointer, int offset, int type) {
return getPointer(pointer).getLong(offset);
return PointerNodes.getPointer(pointer).getLong(offset);
}

@Specialization(guards = "type == TYPE_ULONG")
public long getAtOffsetULong(RubyBasicObject pointer, int offset, int type) {
return getPointer(pointer).getLong(offset);
return PointerNodes.getPointer(pointer).getLong(offset);
}

@Specialization(guards = "type == TYPE_ULL")
public long getAtOffsetULL(RubyBasicObject pointer, int offset, int type) {
return getPointer(pointer).getLongLong(offset);
return PointerNodes.getPointer(pointer).getLongLong(offset);
}

@Specialization(guards = "type == TYPE_STRING")
public RubyBasicObject getAtOffsetString(RubyBasicObject pointer, int offset, int type) {
return createString(getPointer(pointer).getString(offset));
return createString(PointerNodes.getPointer(pointer).getString(offset));
}

@Specialization(guards = "type == TYPE_PTR")
public RubyBasicObject getAtOffsetPointer(RubyBasicObject pointer, int offset, int type) {
final Pointer readPointer = getPointer(pointer).getPointer(offset);
final Pointer readPointer = PointerNodes.getPointer(pointer).getPointer(offset);

if (readPointer == null) {
return nil();
} else {
return createPointer(pointer.getLogicalClass(), readPointer);
return PointerNodes.createPointer(pointer.getLogicalClass(), readPointer);
}
}

@@ -334,7 +291,7 @@ public PointerWriteStringPrimitiveNode(RubyContext context, SourceSection source
public RubyBasicObject address(RubyBasicObject pointer, RubyString string, int maxLength) {
final ByteList bytes = StringNodes.getByteList(string);
final int length = Math.min(bytes.length(), maxLength);
getPointer(pointer).put(0, bytes.unsafeBytes(), bytes.begin(), length);
PointerNodes.getPointer(pointer).put(0, bytes.unsafeBytes(), bytes.begin(), length);
return pointer;
}

@@ -349,7 +306,7 @@ public PointerReadStringToNullPrimitiveNode(RubyContext context, SourceSection s

@Specialization
public RubyBasicObject readStringToNull(RubyBasicObject pointer) {
return createString(MemoryIO.getInstance().getZeroTerminatedByteArray(getPointer(pointer).address()));
return createString(MemoryIO.getInstance().getZeroTerminatedByteArray(PointerNodes.getPointer(pointer).address()));
}

}
@@ -363,7 +320,7 @@ public PointerWriteIntPrimitiveNode(RubyContext context, SourceSection sourceSec

@Specialization
public RubyBasicObject address(RubyBasicObject pointer, int value) {
getPointer(pointer).putInt(0, value);
PointerNodes.getPointer(pointer).putInt(0, value);
return pointer;
}

Original file line number Diff line number Diff line change
@@ -184,7 +184,7 @@ public GetGroupsNode(RubyContext context, SourceSection sourceSection) {
public int getGroups(int max, RubyBasicObject pointer) {
final long[] groups = Platform.getPlatform().getGroups(null);

final Pointer pointerValue = PointerPrimitiveNodes.getPointer(pointer);
final Pointer pointerValue = PointerNodes.getPointer(pointer);

for (int n = 0; n < groups.length && n < max; n++) {
// TODO CS 16-May-15 this is platform dependent
@@ -206,7 +206,7 @@ public GetRLimitNode(RubyContext context, SourceSection sourceSection) {

@Specialization
public int getrlimit(int resource, RubyBasicObject pointer) {
final int result = posix().getrlimit(resource, PointerPrimitiveNodes.getPointer(pointer));
final int result = posix().getrlimit(resource, PointerNodes.getPointer(pointer));

if (result == -1) {
CompilerDirectives.transferToInterpreter();
@@ -246,7 +246,7 @@ public RubyBasicObject memset(RubyBasicObject pointer, int c, int length) {

@Specialization
public RubyBasicObject memset(RubyBasicObject pointer, int c, long length) {
PointerPrimitiveNodes.getPointer(pointer).setMemory(0, length, (byte) c);
PointerNodes.getPointer(pointer).setMemory(0, length, (byte) c);
return pointer;
}

@@ -263,7 +263,7 @@ public ReadlinkNode(RubyContext context, SourceSection sourceSection) {
public int readlink(RubyString path, RubyBasicObject pointer, int bufsize) {
final String pathString = RubyEncoding.decodeUTF8(StringNodes.getByteList(path).getUnsafeBytes(), StringNodes.getByteList(path).getBegin(), StringNodes.getByteList(path).getRealSize());

final int result = posix().readlink(pathString, PointerPrimitiveNodes.getPointer(pointer), bufsize);
final int result = posix().readlink(pathString, PointerNodes.getPointer(pointer), bufsize);
if (result == -1) {
CompilerDirectives.transferToInterpreter();
throw new RaiseException(getContext().getCoreLibrary().errnoError(posix().errno(), this));
@@ -329,7 +329,7 @@ public UtimesNode(RubyContext context, SourceSection sourceSection) {
public int utimes(RubyString path, RubyBasicObject pointer) {
final String pathString = RubyEncoding.decodeUTF8(StringNodes.getByteList(path).getUnsafeBytes(), StringNodes.getByteList(path).getBegin(), StringNodes.getByteList(path).getRealSize());

final int result = posix().utimes(pathString, PointerPrimitiveNodes.getPointer(pointer));
final int result = posix().utimes(pathString, PointerNodes.getPointer(pointer));
if (result == -1) {
CompilerDirectives.transferToInterpreter();
throw new RaiseException(getContext().getCoreLibrary().errnoError(posix().errno(), this));
@@ -469,7 +469,7 @@ public SetRLimitNode(RubyContext context, SourceSection sourceSection) {

@Specialization
public int setrlimit(int resource, RubyBasicObject pointer) {
final int result = posix().setrlimit(resource, PointerPrimitiveNodes.getPointer(pointer));
final int result = posix().setrlimit(resource, PointerNodes.getPointer(pointer));

if (result == -1) {
CompilerDirectives.transferToInterpreter();
@@ -750,8 +750,8 @@ public int getaddrinfo(RubyString hostName, RubyString serviceName, RubyBasicObj
return nativeSockets().getaddrinfo(
StringNodes.getByteList(hostName),
StringNodes.getByteList(serviceName),
PointerPrimitiveNodes.getPointer(hintsPointer),
PointerPrimitiveNodes.getPointer(resultsPointer));
PointerNodes.getPointer(hintsPointer),
PointerNodes.getPointer(resultsPointer));
}

}
@@ -765,7 +765,7 @@ public FreeAddrInfoNode(RubyContext context, SourceSection sourceSection) {

@Specialization
public RubyBasicObject freeaddrinfo(RubyBasicObject addrInfo) {
nativeSockets().freeaddrinfo(PointerPrimitiveNodes.getPointer(addrInfo));
nativeSockets().freeaddrinfo(PointerNodes.getPointer(addrInfo));
return nil();
}

@@ -781,11 +781,11 @@ public GetNameInfoNode(RubyContext context, SourceSection sourceSection) {
@Specialization
public int getnameinfo(RubyBasicObject sa, int salen, RubyBasicObject host, int hostlen, RubyBasicObject serv, int servlen, int flags) {
return nativeSockets().getnameinfo(
PointerPrimitiveNodes.getPointer(sa),
PointerNodes.getPointer(sa),
salen,
PointerPrimitiveNodes.getPointer(host),
PointerNodes.getPointer(host),
hostlen,
PointerPrimitiveNodes.getPointer(serv),
PointerNodes.getPointer(serv),
servlen,
flags);
}
@@ -815,7 +815,7 @@ public SetSockOptNode(RubyContext context, SourceSection sourceSection) {

@Specialization
public int setsockopt(int socket, int level, int optionName, RubyBasicObject optionValue, int optionLength) {
return nativeSockets().setsockopt(socket, level, optionName, PointerPrimitiveNodes.getPointer(optionValue), optionLength);
return nativeSockets().setsockopt(socket, level, optionName, PointerNodes.getPointer(optionValue), optionLength);
}

}
@@ -829,7 +829,7 @@ public BindNode(RubyContext context, SourceSection sourceSection) {

@Specialization
public int bind(int socket, RubyBasicObject address, int addressLength) {
return nativeSockets().bind(socket, PointerPrimitiveNodes.getPointer(address), addressLength);
return nativeSockets().bind(socket, PointerNodes.getPointer(address), addressLength);
}

}
@@ -857,7 +857,7 @@ public GetHostNameNode(RubyContext context, SourceSection sourceSection) {

@Specialization
public int getHostName(RubyBasicObject name, int nameLength) {
return nativeSockets().gethostname(PointerPrimitiveNodes.getPointer(name), nameLength);
return nativeSockets().gethostname(PointerNodes.getPointer(name), nameLength);
}

}
@@ -871,7 +871,7 @@ public GetPeerNameNode(RubyContext context, SourceSection sourceSection) {

@Specialization
public int getPeerName(int socket, RubyBasicObject address, RubyBasicObject addressLength) {
return nativeSockets().getpeername(socket, PointerPrimitiveNodes.getPointer(address), PointerPrimitiveNodes.getPointer(addressLength));
return nativeSockets().getpeername(socket, PointerNodes.getPointer(address), PointerNodes.getPointer(addressLength));
}

}
@@ -885,7 +885,7 @@ public GetSockNameNode(RubyContext context, SourceSection sourceSection) {

@Specialization
public int getSockName(int socket, RubyBasicObject address, RubyBasicObject addressLength) {
return nativeSockets().getsockname(socket, PointerPrimitiveNodes.getPointer(address), PointerPrimitiveNodes.getPointer(addressLength));
return nativeSockets().getsockname(socket, PointerNodes.getPointer(address), PointerNodes.getPointer(addressLength));
}

}
Original file line number Diff line number Diff line change
@@ -357,7 +357,7 @@ public CoreLibrary(RubyContext context) {

rubiniusFFIModule = defineModule(rubiniusModule, "FFI");
defineModule(defineModule(rubiniusFFIModule, "Platform"), "POSIX");
defineClass(rubiniusFFIModule, objectClass, "Pointer", new PointerPrimitiveNodes.PointerAllocator());
defineClass(rubiniusFFIModule, objectClass, "Pointer", new PointerNodes.PointerAllocator());
defineModule(rubiniusModule, "Type");

byteArrayClass = defineClass(rubiniusModule, objectClass, "ByteArray");

0 comments on commit 3912fb7

Please sign in to comment.