use of org.graalvm.nativeimage.PinnedObject in project graal by oracle.
the class Target_com_oracle_truffle_nfi_impl_NFIContext method executeNative.
@Substitute
void executeNative(long cif, long functionPointer, byte[] primArgs, int patchCount, int[] patchOffsets, Object[] objArgs, byte[] ret) {
try (LocalNativeScope scope = TruffleNFISupport.createLocalScope(patchCount);
PinnedObject retBuffer = PinnedObject.create(ret)) {
NativeTruffleContext ctx = WordFactory.pointer(nativeContext);
ffi_cif ffiCif = WordFactory.pointer(cif);
ExecuteHelper.execute(ctx, ffiCif, retBuffer.addressOfArrayElement(0), functionPointer, primArgs, patchCount, patchOffsets, objArgs, scope);
}
}
use of org.graalvm.nativeimage.PinnedObject in project graal by oracle.
the class LocalNativeScope method pinArray.
@TruffleBoundary
PointerBase pinArray(Object arr) {
PinnedObject ret = PinnedObject.create(arr);
pinned[pinCount++] = ret;
return ret.addressOfArrayElement(0);
}
use of org.graalvm.nativeimage.PinnedObject in project graal by oracle.
the class PosixUtils method readBytes.
static int readBytes(byte[] b, int off, int len, FileDescriptor fd) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (PosixUtils.outOfBounds(off, len, b)) {
throw new IndexOutOfBoundsException();
}
if (len == 0) {
return 0;
}
SignedWord nread;
CCharPointer buf = LibC.malloc(WordFactory.unsigned(len));
try {
if (buf.equal(WordFactory.zero())) {
throw new OutOfMemoryError();
}
int handle = getFDHandle(fd);
nread = read(handle, buf, WordFactory.unsigned(len));
if (nread.greaterThan(0)) {
/*
* We do not read directly into the (pinned) result array because read can block,
* and that could lead to object pinned for an unexpectedly long time.
*/
try (PinnedObject pin = PinnedObject.create(b)) {
LibC.memcpy(pin.addressOfArrayElement(off), buf, (UnsignedWord) nread);
}
} else if (nread.equal(-1)) {
throw new IOException(lastErrorString("Read error"));
} else {
// EOF
nread = WordFactory.signed(-1);
}
} finally {
LibC.free(buf);
}
return (int) nread.rawValue();
}
use of org.graalvm.nativeimage.PinnedObject in project graal by oracle.
the class DarwinExecutableName method apply.
/**
* This implementation of {@link CompilerCommandPlugin#apply(Object[])} does not use the
* arguments, and returns a String, possibly null.
*/
@Override
public Object apply(Object[] args) {
/* Find out how long the executable path is. */
final CIntPointer sizePointer = StackValue.get(SizeOf.get(CIntPointer.class));
sizePointer.write(0);
if (DarwinDyld._NSGetExecutablePath(WordFactory.nullPointer(), sizePointer) != -1) {
VMError.shouldNotReachHere("DarwinExecutableName.getExecutableName: Executable path length is 0?");
}
/* Allocate a correctly-sized buffer and ask again. */
final byte[] byteBuffer = new byte[sizePointer.read()];
try (PinnedObject pinnedBuffer = PinnedObject.create(byteBuffer)) {
final CCharPointer bufferPointer = pinnedBuffer.addressOfArrayElement(0);
if (DarwinDyld._NSGetExecutablePath(bufferPointer, sizePointer) == -1) {
/* Failure to find executable path. */
return null;
}
final String executableString = CTypeConversion.toJavaString(bufferPointer);
final String result = realpath(executableString);
return result;
}
}
use of org.graalvm.nativeimage.PinnedObject in project graal by oracle.
the class Util_jni method getHostByAddr.
@Substitute
@SuppressWarnings({ "static-method" })
public String getHostByAddr(byte[] addrArray) throws UnknownHostException {
String ret = null;
CCharPointer host = StackValue.get(Netdb.NI_MAXHOST() + 1);
int error = 0;
int len = 0;
CCharPointer caddr = StackValue.get(16);
NetinetIn.sockaddr_in him4 = StackValue.get(SizeOf.get(NetinetIn.sockaddr_in.class));
NetinetIn.sockaddr_in6 him6 = StackValue.get(SizeOf.get(NetinetIn.sockaddr_in6.class));
Socket.sockaddr sa;
if (addrArray.length == 4) {
/*
* For IPv4 addresses construct a sockaddr_in structure.
*/
int addr = 0;
addr |= ((addrArray[0] << 24) & 0xff000000);
addr |= ((addrArray[1] << 16) & 0xff0000);
addr |= ((addrArray[2] << 8) & 0xff00);
addr |= ((addrArray[3] << 0) & 0xff);
LibC.memset(him4, WordFactory.signed(0), SizeOf.unsigned(NetinetIn.sockaddr_in.class));
him4.sin_addr().set_s_addr(NetinetIn.htonl(addr));
him4.set_sin_family(Socket.AF_INET());
sa = (Socket.sockaddr) him4;
len = SizeOf.get(NetinetIn.sockaddr_in.class);
} else {
/*
* For IPv6 address construct a sockaddr_in6 structure.
*/
try (PinnedObject pinnedAddrArray = PinnedObject.create(addrArray)) {
CCharPointer addrArray0 = pinnedAddrArray.addressOfArrayElement(0);
LibC.memcpy(caddr, addrArray0, WordFactory.unsigned(16));
}
LibC.memset(him6, WordFactory.signed(0), SizeOf.unsigned(NetinetIn.sockaddr_in6.class));
LibC.memcpy(him6.sin6_addr(), caddr, SizeOf.unsigned(NetinetIn.in6_addr.class));
him6.set_sin6_family(Socket.AF_INET6());
sa = (Socket.sockaddr) him6;
len = SizeOf.get(NetinetIn.sockaddr_in6.class);
}
error = Netdb.getnameinfo(sa, len, host, Netdb.NI_MAXHOST(), WordFactory.nullPointer(), 0, Netdb.NI_NAMEREQD());
if (error == 0) {
ret = CTypeConversion.toJavaString(host);
}
if (ret == null) {
throw new UnknownHostException();
}
return ret;
}
Aggregations