use of org.graalvm.nativeimage.c.type.CCharPointer 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.c.type.CCharPointer in project graal by oracle.
the class DarwinSystemPropertiesFeature method tmpdirValue.
@Override
protected String tmpdirValue() {
/* Darwin has a per-user temp dir */
int buflen = Limits.PATH_MAX();
CCharPointer tmpPath = StackValue.get(buflen);
UnsignedWord pathSize = Unistd.confstr(Unistd._CS_DARWIN_USER_TEMP_DIR(), tmpPath, WordFactory.unsigned(buflen));
if (pathSize.aboveThan(0) && pathSize.belowOrEqual(buflen)) {
return CTypeConversion.toJavaString(tmpPath);
} else {
/*
* Default as defined in JDK source/jdk/src/solaris/native/java/lang/java_props_md.c
* line 135.
*/
return "/var/tmp";
}
}
use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class JavaMainWrapper method setCRuntimeArgument0.
public static boolean setCRuntimeArgument0(String arg0) {
boolean arg0truncation = false;
try (CCharPointerHolder arg0Pin = CTypeConversion.toCString(arg0)) {
CCharPointer arg0Pointer = arg0Pin.get();
UnsignedWord arg0Length = SubstrateUtil.strlen(arg0Pointer);
UnsignedWord origLength = WordFactory.unsigned(getCRuntimeArgumentBlockLength());
UnsignedWord newArgLength = origLength;
if (arg0Length.add(1).belowThan(origLength)) {
newArgLength = arg0Length.add(1);
}
arg0truncation = arg0Length.aboveThan(origLength);
CCharPointer firstArgPos = argv.read(0);
// Copy the new arg0 to the original argv[0] position
MemoryUtil.copyConjointMemoryAtomic(WordFactory.pointer(arg0Pointer.rawValue()), WordFactory.pointer(firstArgPos.rawValue()), newArgLength);
// Zero-out the remaining space
MemoryUtil.fillToMemoryAtomic((Pointer) WordFactory.unsigned(firstArgPos.rawValue()).add(newArgLength), origLength.subtract(newArgLength), (byte) 0);
}
// Let caller know if truncation happened
return arg0truncation;
}
use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class RealLog method rawBytes.
/**
* Write a raw java array by copying it first to a stack allocated temporary buffer. Caller must
* ensure that the offset and length are within bounds.
*/
private void rawBytes(byte[] value, int offset, int length) {
/*
* Stack allocation needs an allocation size that is a compile time constant, so we split
* the byte array up in multiple chunks and write them separately.
*/
final int chunkSize = 256;
final CCharPointer bytes = StackValue.get(chunkSize);
int chunkOffset = offset;
int inputLength = length;
while (inputLength > 0) {
int chunkLength = Math.min(inputLength, chunkSize);
for (int i = 0; i < chunkLength; i++) {
byte b = value[chunkOffset + i];
bytes.write(i, b);
}
rawBytes(bytes, WordFactory.unsigned(chunkLength));
chunkOffset += chunkLength;
inputLength -= chunkLength;
}
}
use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class RealLog method number.
private Log number(long value, int radix, boolean signed, int fill, int align) {
if (radix < 2 || radix > 36) {
/* Ignore bogus parameter value. */
return this;
}
/* Enough space for 64 digits in binary format, and the '-' for a negative value. */
final int chunkSize = Long.SIZE + 1;
CCharPointer bytes = StackValue.get(chunkSize, SizeOf.get(CCharPointer.class));
int charPos = chunkSize;
boolean negative = signed && value < 0;
long curValue;
if (negative) {
/*
* We do not have to worry about the overflow of Long.MIN_VALUE here, since we treat
* curValue as an unsigned value.
*/
curValue = -value;
} else {
curValue = value;
}
while (UnsignedMath.aboveOrEqual(curValue, radix)) {
charPos--;
bytes.write(charPos, digit(Long.remainderUnsigned(curValue, radix)));
curValue = Long.divideUnsigned(curValue, radix);
}
charPos--;
bytes.write(charPos, digit(curValue));
if (negative) {
charPos--;
bytes.write(charPos, (byte) '-');
}
int length = chunkSize - charPos;
if (align == RIGHT_ALIGN) {
int spaces = fill - length;
spaces(spaces);
}
rawBytes(bytes.addressOf(charPos), WordFactory.unsigned(length));
if (align == LEFT_ALIGN) {
int spaces = fill - length;
spaces(spaces);
}
return this;
}
Aggregations