use of org.graalvm.word.UnsignedWord in project graal by oracle.
the class PosixOSInterface method freeVirtualMemoryAligned.
@Override
public boolean freeVirtualMemoryAligned(PointerBase start, UnsignedWord size, UnsignedWord alignment) {
final UnsignedWord pageSize = getPageSize();
// Re-discover the paged-aligned ends of the memory region.
final Pointer end = ((Pointer) start).add(size);
final Pointer pagedStart = PointerUtils.roundDown(start, pageSize);
final Pointer pagedEnd = PointerUtils.roundUp(end, pageSize);
final UnsignedWord pagedSize = pagedEnd.subtract(pagedStart);
// Return that virtual address space to the operating system.
return freeVirtualMemory(pagedStart, pagedSize);
}
use of org.graalvm.word.UnsignedWord in project graal by oracle.
the class PosixOSInterface method allocateVirtualMemoryAligned.
/**
* Allocate the requested amount of virtual memory at the requested alignment.
*
* @return A Pointer to the aligned memory, or a null Pointer.
*/
@Override
public Pointer allocateVirtualMemoryAligned(UnsignedWord size, UnsignedWord alignment) {
// This happens in stages:
// (1) Reserve a container that is large enough for the requested size *and* the alignment.
// (2) Locate the result at the requested alignment within the container.
// (3) Clean up any over-allocated prefix and suffix pages.
// All communication with mmap and munmap happen in terms of page_sized objects.
final UnsignedWord pageSize = getPageSize();
// (1) Reserve a container that is large enough for the requested size *and* the alignment.
// - The container occupies the open-right interval [containerStart .. containerEnd).
// - This will be too big, but I'll give back the extra later.
final UnsignedWord containerSize = alignment.add(size);
final UnsignedWord pagedContainerSize = UnsignedUtils.roundUp(containerSize, pageSize);
final Pointer containerStart = allocateVirtualMemory(pagedContainerSize, false);
if (containerStart.isNull()) {
// No exception is needed: this is just a failure to reserve the virtual address space.
return WordFactory.nullPointer();
}
final Pointer containerEnd = containerStart.add(pagedContainerSize);
// (2) Locate the result at the requested alignment within the container.
// - The result occupies [start .. end).
final Pointer start = PointerUtils.roundUp(containerStart, alignment);
final Pointer end = start.add(size);
if (virtualMemoryVerboseDebugging) {
Log.log().string("allocateVirtualMemoryAligned(size: ").unsigned(size).string(" ").hex(size).string(", alignment: ").unsigned(alignment).string(" ").hex(alignment).string(")").newline();
Log.log().string(" container: [").hex(containerStart).string(" .. ").hex(containerEnd).string(")").newline();
Log.log().string(" result: [").hex(start).string(" .. ").hex(end).string(")").newline();
}
// (3) Clean up any over-allocated prefix and suffix pages.
// - The prefix occupies [containerStart .. pagedStart).
final Pointer pagedStart = PointerUtils.roundDown(start, pageSize);
final Pointer prefixStart = containerStart;
final Pointer prefixEnd = pagedStart;
final UnsignedWord prefixSize = prefixEnd.subtract(prefixStart);
if (prefixSize.aboveOrEqual(pageSize)) {
if (virtualMemoryVerboseDebugging) {
Log.log().string(" prefix: [").hex(prefixStart).string(" .. ").hex(prefixEnd).string(")").newline();
}
final boolean prefixUnmap = freeVirtualMemory(prefixStart, prefixSize);
if (!prefixUnmap) {
// that I won't be able to give back.
return WordFactory.nullPointer();
}
}
// - The suffix occupies [pagedEnd .. containerEnd).
final Pointer pagedEnd = PointerUtils.roundUp(end, pageSize);
final Pointer suffixStart = pagedEnd;
final Pointer suffixEnd = containerEnd;
final UnsignedWord suffixSize = suffixEnd.subtract(suffixStart);
if (suffixSize.aboveOrEqual(pageSize)) {
if (virtualMemoryVerboseDebugging) {
Log.log().string(" suffix: [").hex(suffixStart).string(" .. ").hex(suffixEnd).string(")").newline();
}
final boolean suffixUnmap = freeVirtualMemory(suffixStart, suffixSize);
if (!suffixUnmap) {
// that I won't be able to give back.
return WordFactory.nullPointer();
}
}
return start;
}
use of org.graalvm.word.UnsignedWord 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.word.UnsignedWord 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.word.UnsignedWord in project graal by oracle.
the class MemoryUtil method fillToMemoryAtomic.
@Uninterruptible(reason = "Arguments may be managed objects")
public static void fillToMemoryAtomic(Pointer to, UnsignedWord size, byte value) {
UnsignedWord bits = to.or(size);
if (bits.unsignedRemainder(8).equal(0)) {
// zero-extend
long fill = value & 0xffL;
if (fill != 0) {
fill += fill << 8;
fill += fill << 16;
fill += fill << 32;
}
for (UnsignedWord offset = WordFactory.zero(); offset.belowThan(size); offset = offset.add(8)) {
to.writeLong(offset, fill);
}
} else if (bits.unsignedRemainder(4).equal(0)) {
// zero-extend
int fill = value & 0xff;
if (fill != 0) {
fill += fill << 8;
fill += fill << 16;
}
for (UnsignedWord offset = WordFactory.zero(); offset.belowThan(size); offset = offset.add(4)) {
to.writeInt(offset, fill);
}
} else if (bits.unsignedRemainder(2).equal(0)) {
// zero-extend
short fill = (short) (value & 0xff);
if (fill != 0) {
fill += fill << 8;
}
for (UnsignedWord offset = WordFactory.zero(); offset.belowThan(size); offset = offset.add(2)) {
to.writeShort(offset, fill);
}
} else {
byte fill = value;
for (UnsignedWord offset = WordFactory.zero(); offset.belowThan(size); offset = offset.add(1)) {
to.writeByte(offset, fill);
}
}
}
Aggregations