Skip to content

Commit

Permalink
Fix style
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Marek committed Sep 2, 2022
1 parent a8df096 commit 034fd27
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2764,7 +2764,7 @@ public Object FASTR_DATAPTR(Object x) {

@Override
public Object FASTR_GlobalVarAlloc(RContext context) {
return context.stateglobalNativeVar.allocGlobalVarDescr();
return GlobalNativeVarContext.allocGlobalVarDescr();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
import com.oracle.truffle.r.runtime.context.RContext;

/**
* Up-calls specific to FastR used in FastR native code. Most of the functions are not exported in any API,
* except for the global var API ({@code FASTR_GlobalVar*}, which is exported in {@code Rinternals.h}.
* Up-calls specific to FastR used in FastR native code. Most of the functions are not exported in
* any API, except for the global var API ({@code FASTR_GlobalVar*}, which is exported in
* {@code Rinternals.h}.
*/
public interface FastRUpCalls {
// Checkstyle: stop method name check
Expand Down Expand Up @@ -71,9 +72,9 @@ public interface FastRUpCalls {
Object FASTR_GlobalVarAlloc(@RFFIInject RContext context);

/**
* Initializes the {@code globVarDescr} with the information about the current context.
* i.e. assign an index into the per-context array of global native variables.
* Must be called at most once for every context.
* Initializes the {@code globVarDescr} with the information about the current context. i.e.
* assign an index into the per-context array of global native variables. Must be called at most
* once for every context.
*/
void FASTR_GlobalVarInit(Object globVarDescr, @RFFIInject RContext context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright (c) 1998--2014, The R Core Team
* Copyright (c) 2002--2010, The R Foundation
* Copyright (C) 2005--2006, Morten Welinder
* Copyright (c) 2014, 2019, Oracle and/or its affiliates
* Copyright (c) 2014, 2022, Oracle and/or its affiliates
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
11 changes: 1 addition & 10 deletions com.oracle.truffle.r.native/fficall/src/truffle_nfi/rffiutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,7 @@ static __thread int callErrorJmpBufStackIndex = 0;
static __thread int exceptionFlag = 0;

void exitCall() {
if (callErrorJmpBufStackIndex > 0 && peekJmpBuf() != NULL) {
jmp_buf *error_jmpbuf = peekJmpBuf();
if (error_jmpbuf == NULL) {
fatalError("exitCall: peekJmpBuf() should return non-null");
}
longjmp(*error_jmpbuf, 1);
}
// callErrorJmpBufStackIndex == 0 can happen when there is bad pairing of push/pop to errorjmpBufStack.
// We do not consider it a fatal error here, since there should already be a Java exception
// raised in the JVM, therefore, should be handled after this downcall returns.
longjmp(*callErrorJmpBufStack[callErrorJmpBufStackIndex - 1], 1);
}

void checkExitCall() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 3 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 3 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.r.runtime.context;

import com.oracle.truffle.api.CompilerDirectives;
Expand All @@ -17,12 +39,13 @@
import java.util.HashMap;
import java.util.Map;

public class GlobalNativeVarContext implements RContext.ContextState {
public final class GlobalNativeVarContext implements RContext.ContextState {
/**
* We have to wrap {@link GlobalVarDescriptor descriptors} in {@link RForeignObjectWrapper} and
* {@link com.oracle.truffle.r.runtime.data.NativeDataAccess.NativeMirror} in this class, because
* we expect all the descriptors to have static life cycle, whereas, usually, foreign object
* wrappers and native mirrors have a life cycle tied to the life cycle of a specific context.
* {@link com.oracle.truffle.r.runtime.data.NativeDataAccess.NativeMirror} in this class,
* because we expect all the descriptors to have static life cycle, whereas, usually, foreign
* object wrappers and native mirrors have a life cycle tied to the life cycle of a specific
* context.
*
* Note that the static life cycle of descriptors is expected, because the descriptors are
* allocated once per every DLL load.
Expand All @@ -49,7 +72,7 @@ public static GlobalNativeVarContext newContextState(RContext context) {
* wrapper, and assigns it a native mirror.
*/
@CompilerDirectives.TruffleBoundary
public RForeignObjectWrapper allocGlobalVarDescr() {
public static RForeignObjectWrapper allocGlobalVarDescr() {
var foreignWrapper = new RForeignObjectWrapper(new GlobalVarDescriptor());
NativeDataAccess.createNativeMirror(foreignWrapper);
globalNativeVarDescriptors.add(foreignWrapper);
Expand Down Expand Up @@ -85,41 +108,41 @@ public Object getGlobalVar(Object descr, InteropLibrary interop) {
}

@Override
public void beforeFinalize(RContext context) {
callAllDestructors(context, InteropLibrary.getUncached());
public void beforeFinalize(RContext ctx) {
callAllDestructors(ctx, InteropLibrary.getUncached());
}

/**
* Removes {@code context} from all teh global native variable descriptors.
*
* @param context Context used as the key for global native variable descriptors.
* @param ctx Context used as the key for global native variable descriptors.
*/
@Override
public void beforeDispose(RContext context) {
public void beforeDispose(RContext ctx) {
for (int i = 0; i < globalNativeVarDescriptors.size(); i++) {
GlobalVarDescriptor descriptor = getDesriptorAt(i);
// Some descriptors were created only for a particular context, so it is possible
// that some descriptors do not have any information about some contexts.
// Therefore, we have to check for `containsKey`.
if (descriptor.containsKey(context.getId())) {
if (descriptor.containsKey(ctx.getId())) {
try {
descriptor.removeHashEntry(context.getId());
descriptor.removeHashEntry(ctx.getId());
} catch (UnknownKeyException e) {
throw RInternalError.shouldNotReachHere(e);
}
}
}
}

public void callAllDestructors(RContext context, InteropLibrary interop) {
public void callAllDestructors(RContext ctx, InteropLibrary interop) {
// Do not wrap the argument to the native function.
boolean[] whichArgToWrap = {false};
for (int i = 0; i < destructors.size(); i++) {
var destructor = destructors.get(i);
Object ptrForDestructor = getGlobalVar(destructor.globalVarDescr, interop);
interop.toNative(ptrForDestructor);
assert interop.isPointer(ptrForDestructor);
Object ret = context.getRFFI().callNativeFunction(destructor.nativeFunc, destructor.nativeFuncType, Destructor.SIGNATURE,
Object ret = ctx.getRFFI().callNativeFunction(destructor.nativeFunc, destructor.nativeFuncType, Destructor.SIGNATURE,
new Object[]{ptrForDestructor}, whichArgToWrap);
assert interop.isNull(ret);
}
Expand All @@ -134,8 +157,8 @@ public static void printAllDescriptors(RContext context) {
for (int i = 0; i < globalNativeVarDescriptors.size(); i++) {
var descrWrapper = globalNativeVarDescriptors.get(i);
context.getConsole().println(
String.format(" RForeignObjectWrapper{nativeMirror = %s, delegate = %s}, ",
descrWrapper.getNativeMirror(), descrWrapper.getDelegate()));
String.format(" RForeignObjectWrapper{nativeMirror = %s, delegate = %s}, ",
descrWrapper.getNativeMirror(), descrWrapper.getDelegate()));
}
context.getConsole().println("]");
}
Expand Down Expand Up @@ -178,6 +201,7 @@ static final class GlobalVarDescriptor implements TruffleObject {
private final Map<Integer, Integer> contextIndexes = new HashMap<>();

@ExportMessage
@SuppressWarnings("static-method")
boolean hasHashEntries() {
return true;
}
Expand All @@ -196,7 +220,7 @@ boolean isHashEntryReadable(Object key) {
@ExportMessage
@CompilerDirectives.TruffleBoundary
Object readHashValue(Object key) {
return contextIndexes.get((Integer) key);
return contextIndexes.get(key);
}

@ExportMessage
Expand All @@ -206,14 +230,15 @@ boolean isHashEntryModifiable(Object key) {
}

@ExportMessage
boolean isHashEntryInsertable(Object key) {
@SuppressWarnings("static-method")
boolean isHashEntryInsertable(@SuppressWarnings("unused") Object key) {
return true;
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
void writeHashEntry(Object key, Object value) {
contextIndexes.put(((Integer) key), (Integer) value);
contextIndexes.put((Integer) key, (Integer) value);
}

@ExportMessage
Expand All @@ -225,20 +250,21 @@ boolean isHashEntryRemovable(Object key) {
@ExportMessage
@CompilerDirectives.TruffleBoundary
void removeHashEntry(Object key) throws UnknownKeyException {
Integer previousValue = contextIndexes.remove((Integer) key);
Integer previousValue = contextIndexes.remove(key);
if (previousValue == null) {
throw UnknownKeyException.create(key);
}
}

@ExportMessage
@SuppressWarnings("static-method")
Object getHashEntriesIterator() {
return null;
}

private boolean containsKey(Object key) {
assert key instanceof Integer;
return contextIndexes.containsKey((Integer) key);
return contextIndexes.containsKey(key);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ default void beforeDispose(RContext context) {
* context finalization happens before context disposal, and generally, it should be safe to
* call native functions in the context finalization.
*/
@SuppressWarnings("unused")
default void beforeFinalize(RContext context) {
// default empty implementation
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -24,7 +24,6 @@

import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.r.runtime.RRuntime;
import com.oracle.truffle.r.runtime.RType;
import com.oracle.truffle.r.runtime.data.NativeDataAccess.NativeMirror;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.oracle.truffle.r.runtime.RInternalError;
import com.oracle.truffle.r.runtime.RRuntime;
import com.oracle.truffle.r.runtime.context.RContext;
import com.oracle.truffle.r.runtime.data.NativeDataAccess;
import com.oracle.truffle.r.runtime.data.RBaseObject;
import com.oracle.truffle.r.runtime.data.RComplex;
import com.oracle.truffle.r.runtime.data.RDataFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,15 @@ public final void registerReferenceUsedInNative(Object obj, ConditionProfile nop
* Calls a native function in slow-path. This method is a simpler alternative to
* {@link com.oracle.truffle.r.runtime.ffi.DownCallNodeFactory.DownCallNode} machinery. Use it
* once the native function is not known during FastR build, i.e., whenever you cannot use
* {@link NativeFunction}, e.g., when calling a native function that wass passed to the
* Java side as a pointer.
* {@link NativeFunction}, e.g., when calling a native function that wass passed to the Java
* side as a pointer.
* <p>
* Note that unlike {@code DownCallNode} machinery, <b>it is not safe to use the native R API
* from the native function</b>, i.e., it is not safe to do any upcalls. This is because
* {@code DownCallNode} machinery uses trampolines that sets the longjump in case of errors from
* upcalls, whereas the native functions called by this method cannot use this trampoline
* mechanism, because these native functions are expected to be provided by the user, and
* therefore, we cannot modify their sources.
*
* @param nativeFunc (Truffle) object representing the native function to be called.
* @param nativeFuncType Type of the native function - can be either {@code LLVM} or
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down
2 changes: 1 addition & 1 deletion mx.fastr/mx_fastr_edinclude.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
Expand Down

0 comments on commit 034fd27

Please sign in to comment.