Search in sources :

Example 1 with WordFactory.nullPointer

use of org.graalvm.word.WordFactory.nullPointer in project graal by oracle.

the class PosixVirtualMemoryProvider method reserve.

@Override
@Uninterruptible(reason = "May be called from uninterruptible code.", mayBeInlined = true)
public Pointer reserve(UnsignedWord nbytes, UnsignedWord alignment, boolean executable) {
    if (nbytes.equal(0)) {
        return WordFactory.nullPointer();
    }
    UnsignedWord granularity = getGranularity();
    boolean customAlignment = !UnsignedUtils.isAMultiple(granularity, alignment);
    UnsignedWord mappingSize = nbytes;
    if (customAlignment) {
        mappingSize = mappingSize.add(alignment);
    }
    mappingSize = UnsignedUtils.roundUp(mappingSize, granularity);
    int flags = MAP_ANON() | MAP_PRIVATE() | MAP_NORESERVE();
    if (Platform.includedIn(Platform.MACOS_AARCH64.class) && executable) {
        flags |= MAP_JIT();
    }
    Pointer mappingBegin = mmap(nullPointer(), mappingSize, PROT_NONE(), flags, NO_FD, NO_FD_OFFSET);
    if (mappingBegin.equal(MAP_FAILED())) {
        return nullPointer();
    }
    if (!customAlignment) {
        return mappingBegin;
    }
    Pointer begin = PointerUtils.roundUp(mappingBegin, alignment);
    UnsignedWord clippedBegin = begin.subtract(mappingBegin);
    if (clippedBegin.aboveOrEqual(granularity)) {
        munmap(mappingBegin, UnsignedUtils.roundDown(clippedBegin, granularity));
    }
    Pointer mappingEnd = mappingBegin.add(mappingSize);
    UnsignedWord clippedEnd = mappingEnd.subtract(begin.add(nbytes));
    if (clippedEnd.aboveOrEqual(granularity)) {
        UnsignedWord rounded = UnsignedUtils.roundDown(clippedEnd, granularity);
        munmap(mappingEnd.subtract(rounded), rounded);
    }
    return begin;
}
Also used : UnsignedWord(org.graalvm.word.UnsignedWord) WordFactory.nullPointer(org.graalvm.word.WordFactory.nullPointer) WordPointer(org.graalvm.nativeimage.c.type.WordPointer) Pointer(org.graalvm.word.Pointer) Uninterruptible(com.oracle.svm.core.annotate.Uninterruptible)

Example 2 with WordFactory.nullPointer

use of org.graalvm.word.WordFactory.nullPointer in project graal by oracle.

the class AbstractCommittedMemoryProvider method allocate.

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
private Pointer allocate(UnsignedWord size, UnsignedWord alignment, boolean executable) {
    int access = VirtualMemoryProvider.Access.READ | VirtualMemoryProvider.Access.WRITE;
    if (executable) {
        access |= VirtualMemoryProvider.Access.EXECUTE;
    }
    Pointer reserved = WordFactory.nullPointer();
    if (!UnsignedUtils.isAMultiple(getGranularity(), alignment)) {
        reserved = VirtualMemoryProvider.get().reserve(size, alignment, executable);
        if (reserved.isNull()) {
            return nullPointer();
        }
    }
    Pointer committed = VirtualMemoryProvider.get().commit(reserved, size, access);
    if (committed.isNull()) {
        if (reserved.isNonNull()) {
            VirtualMemoryProvider.get().free(reserved, size);
        }
        return nullPointer();
    }
    assert reserved.isNull() || reserved.equal(committed);
    tracker.track(size);
    return committed;
}
Also used : WordFactory.nullPointer(org.graalvm.word.WordFactory.nullPointer) Pointer(org.graalvm.word.Pointer) Uninterruptible(com.oracle.svm.core.annotate.Uninterruptible)

Example 3 with WordFactory.nullPointer

use of org.graalvm.word.WordFactory.nullPointer in project graal by oracle.

the class PosixVirtualMemoryProvider method commit.

@Override
@Uninterruptible(reason = "May be called from uninterruptible code.", mayBeInlined = true)
public Pointer commit(PointerBase start, UnsignedWord nbytes, int access) {
    if ((start.isNonNull() && !isAligned(start)) || nbytes.equal(0)) {
        return WordFactory.nullPointer();
    }
    int flags = MAP_ANON() | MAP_PRIVATE();
    if (start.isNonNull()) {
        flags |= MAP_FIXED();
    }
    boolean isWX = (access & Access.WRITE) != 0 && (access & Access.EXECUTE) != 0;
    if (Platform.includedIn(Platform.MACOS_AARCH64.class) && isWX) {
        flags |= MAP_JIT();
    }
    /* The memory returned by mmap is guaranteed to be zeroed. */
    final Pointer result = mmap(start, nbytes, accessAsProt(access), flags, NO_FD, NO_FD_OFFSET);
    return result.notEqual(MAP_FAILED()) ? result : nullPointer();
}
Also used : WordFactory.nullPointer(org.graalvm.word.WordFactory.nullPointer) WordPointer(org.graalvm.nativeimage.c.type.WordPointer) Pointer(org.graalvm.word.Pointer) Uninterruptible(com.oracle.svm.core.annotate.Uninterruptible)

Example 4 with WordFactory.nullPointer

use of org.graalvm.word.WordFactory.nullPointer in project graal by oracle.

the class PosixVirtualMemoryProvider method mapFile.

@Override
@Uninterruptible(reason = "May be called from uninterruptible code.", mayBeInlined = true)
public Pointer mapFile(PointerBase start, UnsignedWord nbytes, WordBase fileHandle, UnsignedWord offset, int access) {
    if ((start.isNonNull() && !isAligned(start)) || nbytes.equal(0)) {
        return WordFactory.nullPointer();
    }
    int flags = MAP_PRIVATE();
    if (start.isNonNull()) {
        flags |= MAP_FIXED();
    }
    int fd = (int) fileHandle.rawValue();
    Pointer result = mmap(start, nbytes, accessAsProt(access), flags, fd, offset.rawValue());
    return result.notEqual(MAP_FAILED()) ? result : WordFactory.nullPointer();
}
Also used : WordFactory.nullPointer(org.graalvm.word.WordFactory.nullPointer) WordPointer(org.graalvm.nativeimage.c.type.WordPointer) Pointer(org.graalvm.word.Pointer) Uninterruptible(com.oracle.svm.core.annotate.Uninterruptible)

Aggregations

Uninterruptible (com.oracle.svm.core.annotate.Uninterruptible)4 Pointer (org.graalvm.word.Pointer)4 WordFactory.nullPointer (org.graalvm.word.WordFactory.nullPointer)4 WordPointer (org.graalvm.nativeimage.c.type.WordPointer)3 UnsignedWord (org.graalvm.word.UnsignedWord)1