Skip to content

Commit

Permalink
Fix lambda
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffroy Jamgotchian <[email protected]>
  • Loading branch information
geofjamg committed Nov 7, 2023
1 parent 1dd696d commit 40d02c5
Show file tree
Hide file tree
Showing 16 changed files with 1,494 additions and 863 deletions.
2 changes: 1 addition & 1 deletion cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ ExternalProject_Add(native-image
DEPENDS mvn
SOURCE_DIR ${PYPOWSYBL_JAVA_BIN_DIR}
DOWNLOAD_COMMAND ""
PATCH_COMMAND $ENV{JAVA_HOME}/bin/native-image ${NATIVE_IMAGE_BUILD_OPTIONS} --class-path ${PYPOWSYBL_JAVA_SRC_DIR}/target/pypowsybl-java.jar${EXTRA_JARS} -march=compatibility --no-fallback --shared --gc=${NATIVE_IMAGE_GC} -o pypowsybl-java -H:CLibraryPath=${CMAKE_CURRENT_SOURCE_DIR}/src --add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.c=ALL-UNNAMED
PATCH_COMMAND $ENV{JAVA_HOME}/bin/native-image ${NATIVE_IMAGE_BUILD_OPTIONS} --class-path ${PYPOWSYBL_JAVA_SRC_DIR}/target/pypowsybl-java.jar${EXTRA_JARS} -march=compatibility --no-fallback --shared --gc=${NATIVE_IMAGE_GC} -o pypowsybl-java -H:CLibraryPath=${CMAKE_CURRENT_SOURCE_DIR}/src
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ${PYPOWSYBL_JAVA_BIN_DIR}/${PYPOWSYBL_JAVA_OLD_LIB} ${PYPOWSYBL_JAVA_BIN_DIR}/${PYPOWSYBL_JAVA_LIB} ${NATIVE_IMAGE_INSTALL_EXTRA_COMMAND}
Expand Down
75 changes: 55 additions & 20 deletions java/src/main/java/com/powsybl/python/commons/CommonCFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,21 @@ private CommonCFunctions() {

@CEntryPoint(name = "setJavaLibraryPath")
public static void setJavaLibraryPath(IsolateThread thread, CCharPointer javaLibraryPath, ExceptionHandlerPointer exceptionHandlerPtr) {
doCatch(exceptionHandlerPtr, () -> {
System.setProperty("java.library.path", CTypeUtil.toString(javaLibraryPath));
doCatch(exceptionHandlerPtr, new Runnable() {
@Override
public void run() {
System.setProperty("java.library.path", CTypeUtil.toString(javaLibraryPath));
}
});
}

@CEntryPoint(name = "setConfigRead")
public static void setConfigRead(IsolateThread thread, boolean read, ExceptionHandlerPointer exceptionHandlerPtr) {
doCatch(exceptionHandlerPtr, () -> {
PyPowsyblConfiguration.setReadConfig(read);
doCatch(exceptionHandlerPtr, new Runnable() {
@Override
public void run() {
PyPowsyblConfiguration.setReadConfig(read);
}
});
}

Expand All @@ -52,13 +58,23 @@ public static boolean isConfigRead(IsolateThread thread, ExceptionHandlerPointer

@CEntryPoint(name = "getVersionTable")
public static CCharPointer getVersionTable(IsolateThread thread, ExceptionHandlerPointer exceptionHandlerPtr) {
return doCatch(exceptionHandlerPtr, () -> CTypeUtil.toCharPtr(Version.getTableString()));
return doCatch(exceptionHandlerPtr, new Util.PointerProvider<CCharPointer>() {
@Override
public CCharPointer get() {
return CTypeUtil.toCharPtr(Version.getTableString());
}
});
}

@CEntryPoint(name = "freeStringArray")
public static void freeStringArray(IsolateThread thread, ArrayPointer<CCharPointerPointer> arrayPtr,
ExceptionHandlerPointer exceptionHandlerPtr) {
doCatch(exceptionHandlerPtr, () -> freeArrayContent(arrayPtr));
doCatch(exceptionHandlerPtr, new Runnable() {
@Override
public void run() {
freeArrayContent(arrayPtr);
}
});
}

@CEntryPoint(name = "freeArray")
Expand All @@ -71,11 +87,14 @@ public static <T extends PointerBase> void freeArray(IsolateThread thread, Array
@CEntryPoint(name = "freeSeriesArray")
public static void freeSeriesArray(IsolateThread thread, ArrayPointer<SeriesPointer> seriesPtrArrayPtr,
ExceptionHandlerPointer exceptionHandlerPtr) {
doCatch(exceptionHandlerPtr, () -> {
for (int i = 0; i < seriesPtrArrayPtr.getLength(); i++) {
freeSeries(seriesPtrArrayPtr.getPtr().addressOf(i));
doCatch(exceptionHandlerPtr, new Runnable() {
@Override
public void run() {
for (int i = 0; i < seriesPtrArrayPtr.getLength(); i++) {
freeSeries(seriesPtrArrayPtr.getPtr().addressOf(i));
}
freeArrayPointer(seriesPtrArrayPtr);
}
freeArrayPointer(seriesPtrArrayPtr);
});
}

Expand All @@ -100,20 +119,33 @@ private static void freeArrayContent(ArrayPointer<CCharPointerPointer> array) {

@CEntryPoint(name = "destroyObjectHandle")
public static void destroyObjectHandle(IsolateThread thread, ObjectHandle objectHandle, ExceptionHandlerPointer exceptionHandlerPtr) {
doCatch(exceptionHandlerPtr, () -> ObjectHandles.getGlobal().destroy(objectHandle));
doCatch(exceptionHandlerPtr, new Runnable() {
@Override
public void run() {
ObjectHandles.getGlobal().destroy(objectHandle);
}
});
}

@CEntryPoint(name = "getWorkingVariantId")
public static CCharPointer getWorkingVariantId(IsolateThread thread, ObjectHandle networkHandle, ExceptionHandlerPointer exceptionHandlerPtr) {
return doCatch(exceptionHandlerPtr, () -> {
Network network = ObjectHandles.getGlobal().get(networkHandle);
return CTypeUtil.toCharPtr(network.getVariantManager().getWorkingVariantId());
return doCatch(exceptionHandlerPtr, new Util.PointerProvider<CCharPointer>() {
@Override
public CCharPointer get() {
Network network = ObjectHandles.getGlobal().get(networkHandle);
return CTypeUtil.toCharPtr(network.getVariantManager().getWorkingVariantId());
}
});
}

@CEntryPoint(name = "freeString")
public static void freeString(IsolateThread thread, CCharPointer string, ExceptionHandlerPointer exceptionHandlerPtr) {
doCatch(exceptionHandlerPtr, () -> UnmanagedMemory.free(string));
doCatch(exceptionHandlerPtr, new Runnable() {
@Override
public void run() {
UnmanagedMemory.free(string);
}
});
}

@CEntryPoint(name = "closePypowsybl")
Expand All @@ -123,12 +155,15 @@ public static void closePypowsybl(IsolateThread thread, ExceptionHandlerPointer

@CEntryPoint(name = "freeStringMap")
public static void freeStringMap(IsolateThread thread, StringMap map, ExceptionHandlerPointer exceptionHandlerPtr) {
doCatch(exceptionHandlerPtr, () -> {
for (int i = 0; i < map.getLength(); i++) {
UnmanagedMemory.free(map.getKeys().read(i));
UnmanagedMemory.free(map.getValues().read(i));
doCatch(exceptionHandlerPtr, new Runnable() {
@Override
public void run() {
for (int i = 0; i < map.getLength(); i++) {
UnmanagedMemory.free(map.getKeys().read(i));
UnmanagedMemory.free(map.getValues().read(i));
}
UnmanagedMemory.free(map);
}
UnmanagedMemory.free(map);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,42 +54,72 @@ public void allocate(int seriesCount) {
public StringSeriesWriter newStringIndex(String name, int size) {
CCharPointerPointer dataPtr = UnmanagedMemory.calloc(size * SizeOf.get(CCharPointerPointer.class));
addIndex(name, size, dataPtr, STRING_SERIES_TYPE);
return (i, v) -> dataPtr.addressOf(i).write(CTypeUtil.toCharPtr(v));
return new StringSeriesWriter() {
@Override
public void set(int i, String v) {
dataPtr.addressOf(i).write(CTypeUtil.toCharPtr(v));
}
};
}

@Override
public IntSeriesWriter newIntIndex(String name, int size) {
CIntPointer dataPtr = UnmanagedMemory.calloc(size * SizeOf.get(CIntPointer.class));
addIndex(name, size, dataPtr, INT_SERIES_TYPE);
return (i, v) -> dataPtr.addressOf(i).write(v);
return new IntSeriesWriter() {
@Override
public void set(int i, int v) {
dataPtr.addressOf(i).write(v);
}
};
}

@Override
public StringSeriesWriter newStringSeries(String name, int size) {
CCharPointerPointer dataPtr = UnmanagedMemory.calloc(size * SizeOf.get(CCharPointerPointer.class));
addSeries(name, size, dataPtr, STRING_SERIES_TYPE);
return (i, v) -> dataPtr.addressOf(i).write(CTypeUtil.toCharPtr(v));
return new StringSeriesWriter() {
@Override
public void set(int i, String v) {
dataPtr.addressOf(i).write(CTypeUtil.toCharPtr(v));
}
};
}

@Override
public IntSeriesWriter newIntSeries(String name, int size) {
CIntPointer dataPtr = UnmanagedMemory.calloc(size * SizeOf.get(CIntPointer.class));
addSeries(name, size, dataPtr, INT_SERIES_TYPE);
return (i, v) -> dataPtr.addressOf(i).write(v);
return new IntSeriesWriter() {
@Override
public void set(int i, int v) {
dataPtr.addressOf(i).write(v);
}
};
}

@Override
public BooleanSeriesWriter newBooleanSeries(String name, int size) {
CCharPointer dataPtr = UnmanagedMemory.calloc(size * SizeOf.get(CCharPointer.class));
addSeries(name, size, dataPtr, BOOLEAN_SERIES_TYPE);
return (i, v) -> dataPtr.addressOf(i).write(v ? (byte) 1 : 0);
return new BooleanSeriesWriter() {
@Override
public void set(int i, boolean v) {
dataPtr.addressOf(i).write(v ? (byte) 1 : 0);
}
};
}

@Override
public DoubleSeriesWriter newDoubleSeries(String name, int size) {
CDoublePointer dataPtr = UnmanagedMemory.calloc(size * SizeOf.get(CDoublePointer.class));
addSeries(name, size, dataPtr, DOUBLE_SERIES_TYPE);
return (i, v) -> dataPtr.addressOf(i).write(v);
return new DoubleSeriesWriter() {
@Override
public void set(int i, double v) {
dataPtr.addressOf(i).write(v);
}
};
}

private void addSeries(String name, int count, PointerBase dataPtr, int type) {
Expand Down
Loading

0 comments on commit 40d02c5

Please sign in to comment.