Skip to content

Commit 5d0380f

Browse files
committed
[GR-26508] Migrate to new handle API.
PullRequest: graalpython/1346
2 parents 5e796bf + c1a5fc6 commit 5d0380f

File tree

6 files changed

+25
-24
lines changed

6 files changed

+25
-24
lines changed

graalpython/com.oracle.graal.python.cext/hpy/hpy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ void get_next_vaarg(va_list *p_va, OutVarPtr *p_outvar) {
348348
}
349349

350350
void* graal_hpy_context_to_native(void* cobj) {
351-
return truffle_deref_handle_for_managed(cobj);
351+
return create_deref_handle(cobj);
352352
}
353353

354354
#define PRIMITIVE_ARRAY_TO_NATIVE(__jtype__, __ctype__, __polyglot_type__, __element_cast__) \

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static void initialize_upcall_functions() {
9696

9797
__attribute__((constructor (__COUNTER__)))
9898
static void initialize_handle_cache() {
99-
cache = (cache_t) polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_HandleCache_Create", truffle_managed_from_handle);
99+
cache = (cache_t) polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_HandleCache_Create", resolve_handle);
100100
ptr_cache = (ptr_cache_t) polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_PtrCache_Create", 0);
101101
ptr_cache_stealing = (ptr_cache_t) polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_PtrCache_Create", 1);
102102
type_ptr_cache = (type_ptr_cache_t) polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_PtrCache_Create", 0);
@@ -267,8 +267,8 @@ void* native_long_to_java(uint64_t val) {
267267
return Py_NoValue;
268268
} else if (obj == Py_None) {
269269
return Py_None;
270-
} else if (!truffle_cannot_be_handle(obj)) {
271-
return resolve_handle(cache, (uint64_t)obj);
270+
} else if (points_to_handle_space(obj)) {
271+
return resolve_handle_cached(cache, (uint64_t)obj);
272272
}
273273
return obj;
274274
}
@@ -451,16 +451,16 @@ uint64_t PyTruffle_Wchar_Size() {
451451

452452
/** free's a native pointer or releases a Sulong handle; DO NOT CALL WITH MANAGED POINTERS ! */
453453
void PyTruffle_Free(void* obj) {
454-
if(!truffle_cannot_be_handle(obj) && truffle_is_handle_to_managed(obj)) {
455-
truffle_release_handle(obj);
454+
if(points_to_handle_space(obj) && is_handle(obj)) {
455+
release_handle(obj);
456456
} else {
457457
free(obj);
458458
}
459459
}
460460

461461
/** to be used from Java code only; creates the deref handle for a sequence wrapper */
462462
void* NativeHandle_ForArray(void* jobj, ssize_t element_size) {
463-
return truffle_deref_handle_for_managed(jobj);
463+
return create_deref_handle(jobj);
464464
}
465465

466466
const char* PyTruffle_StringToCstr(void* o, int32_t strLen) {

graalpython/com.oracle.graal.python.cext/src/capi.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
#include "Python.h"
5050
#include <truffle.h>
51+
#include <graalvm/llvm/handles.h>
5152

5253
#define SRC_CS "utf-8"
5354

@@ -223,7 +224,7 @@ extern free_upcall_fun_t free_upcall;
223224
// TODO we need a reliable solution for that
224225
#define IS_POINTER(__val__) (polyglot_is_value(__val__) && !polyglot_fits_in_i64(__val__))
225226

226-
#define resolve_handle(__cache__, __addr__) (__cache__)(__addr__)
227+
#define resolve_handle_cached(__cache__, __addr__) (__cache__)(__addr__)
227228

228229
void initialize_type_structure(PyTypeObject* structure, PyTypeObject* ptype, polyglot_typeid tid);
229230

@@ -232,33 +233,33 @@ void register_native_slots(PyTypeObject* managed_class, PyGetSetDef* getsets, Py
232233

233234
MUST_INLINE
234235
PyObject* native_to_java(PyObject* obj) {
235-
if (!truffle_cannot_be_handle(obj)) {
236-
return resolve_handle(cache, (uint64_t)obj);
236+
if (points_to_handle_space(obj)) {
237+
return resolve_handle_cached(cache, (uint64_t)obj);
237238
}
238239
return ptr_cache(obj);
239240
}
240241

241242
MUST_INLINE
242243
PyObject* native_to_java_stealing(PyObject* obj) {
243-
if (!truffle_cannot_be_handle(obj)) {
244-
return resolve_handle(cache, (uint64_t)obj);
244+
if (points_to_handle_space(obj)) {
245+
return resolve_handle_cached(cache, (uint64_t)obj);
245246
}
246247
return ptr_cache_stealing(obj);
247248
}
248249

249250

250251
MUST_INLINE
251252
PyTypeObject* native_type_to_java(PyTypeObject* type) {
252-
if (!truffle_cannot_be_handle(type)) {
253-
return (PyTypeObject *)truffle_managed_from_handle(type);
253+
if (points_to_handle_space(type)) {
254+
return (PyTypeObject *)resolve_handle(type);
254255
}
255256
return type_ptr_cache(type, Py_REFCNT(type));
256257
}
257258

258259
MUST_INLINE
259260
void* native_pointer_to_java(void* obj) {
260-
if (!truffle_cannot_be_handle(obj)) {
261-
return resolve_handle(cache, (uint64_t)obj);
261+
if (points_to_handle_space(obj)) {
262+
return resolve_handle_cached(cache, (uint64_t)obj);
262263
}
263264
return obj;
264265
}

graalpython/com.oracle.graal.python.cext/src/obmalloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void _PyObject_Free(void* ptr) {
6767
if (ptr == NULL) {
6868
return;
6969
}
70-
if((!truffle_cannot_be_handle(ptr) && truffle_is_handle_to_managed(ptr)) || polyglot_is_value(ptr)) {
70+
if((points_to_handle_space(ptr) && is_handle(ptr)) || polyglot_is_value(ptr)) {
7171
if(free_upcall(native_pointer_to_java(ptr))) {
7272
/* If 1 is returned, the upcall function already took care of freeing */
7373
return;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CExtNodes.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3013,8 +3013,8 @@ static PythonNativeWrapper resolveObjectCached(@SuppressWarnings("unused") Objec
30133013
static Object resolveGeneric(Object pointerObject,
30143014
@Cached PCallCapiFunction callTruffleCannotBeHandleNode,
30153015
@Cached PCallCapiFunction callTruffleManagedFromHandleNode) {
3016-
if (!((boolean) callTruffleCannotBeHandleNode.call(NativeCAPISymbols.FUN_TRUFFLE_CANNOT_BE_HANDLE, pointerObject))) {
3017-
return callTruffleManagedFromHandleNode.call(NativeCAPISymbols.FUN_TRUFFLE_MANAGED_FROM_HANDLE, pointerObject);
3016+
if (((boolean) callTruffleCannotBeHandleNode.call(NativeCAPISymbols.FUN_POINTS_TO_HANDLE_SPACE, pointerObject))) {
3017+
return callTruffleManagedFromHandleNode.call(NativeCAPISymbols.FUN_RESOLVE_HANDLE, pointerObject);
30183018
}
30193019
// In this case, it cannot be a handle so we can just return the pointer object. It
30203020
// could, of course, still be a native pointer.
@@ -3023,8 +3023,8 @@ static Object resolveGeneric(Object pointerObject,
30233023

30243024
static PythonNativeWrapper resolveHandleUncached(Object pointerObject) {
30253025
CompilerAsserts.neverPartOfCompilation();
3026-
if (!((boolean) PCallCapiFunction.getUncached().call(NativeCAPISymbols.FUN_TRUFFLE_CANNOT_BE_HANDLE, pointerObject))) {
3027-
Object resolved = PCallCapiFunction.getUncached().call(NativeCAPISymbols.FUN_TRUFFLE_MANAGED_FROM_HANDLE, pointerObject);
3026+
if (((boolean) PCallCapiFunction.getUncached().call(NativeCAPISymbols.FUN_POINTS_TO_HANDLE_SPACE, pointerObject))) {
3027+
Object resolved = PCallCapiFunction.getUncached().call(NativeCAPISymbols.FUN_RESOLVE_HANDLE, pointerObject);
30283028
if (resolved instanceof PythonNativeWrapper) {
30293029
return (PythonNativeWrapper) resolved;
30303030
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/NativeCAPISymbols.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public abstract class NativeCAPISymbols {
7272
public static final String FUN_GET_TP_DICTOFFSET = "get_tp_dictoffset";
7373
public static final String FUN_GET_TP_BASICSIZE = "get_tp_basicsize";
7474
public static final String FUN_GET_TP_ITEMSIZE = "get_tp_itemsize";
75-
public static final String FUN_DEREF_HANDLE = "truffle_deref_handle_for_managed";
75+
public static final String FUN_DEREF_HANDLE = "_graalvm_llvm_create_deref_handle";
7676
public static final String FUN_GET_BYTE_ARRAY_TYPE_ID = "get_byte_array_typeid";
7777
public static final String FUN_GET_PTR_ARRAY_TYPE_ID = "get_ptr_array_typeid";
7878
public static final String FUN_PTR_COMPARE = "truffle_ptr_compare";
@@ -100,8 +100,8 @@ public abstract class NativeCAPISymbols {
100100
public static final String FUN_DECREF = "Py_DecRef";
101101
public static final String FUN_ADDREF = "PyTruffle_ADDREF";
102102
public static final String FUN_SUBREF = "PyTruffle_SUBREF";
103-
public static final String FUN_TRUFFLE_MANAGED_FROM_HANDLE = "truffle_managed_from_handle";
104-
public static final String FUN_TRUFFLE_CANNOT_BE_HANDLE = "truffle_cannot_be_handle";
103+
public static final String FUN_RESOLVE_HANDLE = "_graalvm_llvm_resolve_handle";
104+
public static final String FUN_POINTS_TO_HANDLE_SPACE = "_graalvm_llvm_points_to_handle_space";
105105
public static final String FUN_GET_LONG_BITS_PER_DIGIT = "get_long_bits_in_digit";
106106
public static final String FUN_BULK_SUBREF = "PyTruffle_bulk_SUBREF";
107107
private static final String FUN_GET_INT8_T_TYPEID = "get_int8_t_typeid";

0 commit comments

Comments
 (0)