Skip to content

Commit 7525d74

Browse files
committed
[GR-29191] Update HPy Import.
PullRequest: graalpython/1594
2 parents d6bb23a + 69e2393 commit 7525d74

File tree

30 files changed

+757
-59
lines changed

30 files changed

+757
-59
lines changed

graalpython/com.oracle.graal.python.cext/include/common/hpydef.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ typedef struct {
150150
};
151151

152152

153-
#define HPyDef_METH(SYM, NAME, IMPL, SIG) \
153+
#define HPyDef_METH(SYM, NAME, IMPL, SIG, ...) \
154154
HPyFunc_DECLARE(IMPL, SIG); \
155155
HPyFunc_TRAMPOLINE(SYM##_trampoline, IMPL, SIG); \
156156
HPyDef SYM = { \
@@ -159,7 +159,8 @@ typedef struct {
159159
.name = NAME, \
160160
.impl = IMPL, \
161161
.cpy_trampoline = SYM##_trampoline, \
162-
.signature = SIG \
162+
.signature = SIG, \
163+
__VA_ARGS__ \
163164
} \
164165
};
165166

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* MIT License
2+
*
3+
* Copyright (c) 2021, Oracle and/or its affiliates.
4+
* Copyright (c) 2019 pyhandle
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#ifndef HPY_COMMON_RUNTIME_CTX_TRACKER_H
26+
#define HPY_COMMON_RUNTIME_CTX_TRACKER_H
27+
28+
#include "hpy.h"
29+
30+
_HPy_HIDDEN HPyTracker
31+
ctx_Tracker_New(HPyContext ctx, HPy_ssize_t size);
32+
33+
_HPy_HIDDEN int
34+
ctx_Tracker_Add(HPyContext ctx, HPyTracker ht, HPy h);
35+
36+
_HPy_HIDDEN void
37+
ctx_Tracker_RemoveAll(HPyContext ctx, HPyTracker ht);
38+
39+
_HPy_HIDDEN void
40+
ctx_Tracker_Free(HPyContext ctx, HPyTracker ht);
41+
42+
#endif /* HPY_COMMON_RUNTIME_CTX_TRACKER_H */

graalpython/com.oracle.graal.python.cext/include/cpython/hpy.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
typedef struct { PyObject *_o; } HPy;
5757
typedef struct { Py_ssize_t _lst; } HPyListBuilder;
5858
typedef struct { Py_ssize_t _tup; } HPyTupleBuilder;
59+
typedef struct { void *_o; } HPyTracker;
5960
typedef Py_ssize_t HPy_ssize_t;
6061
typedef Py_hash_t HPy_hash_t;
6162

@@ -165,6 +166,7 @@ HPy_AsPyObject(HPyContext ctx, HPy h)
165166
#include "../common/runtime/ctx_module.h"
166167
#include "../common/runtime/ctx_type.h"
167168
#include "../common/runtime/ctx_listbuilder.h"
169+
#include "../common/runtime/ctx_tracker.h"
168170
#include "../common/runtime/ctx_tuple.h"
169171
#include "../common/runtime/ctx_tuplebuilder.h"
170172

@@ -260,4 +262,28 @@ HPyTuple_FromArray(HPyContext ctx, HPy items[], HPy_ssize_t n)
260262
return ctx_Tuple_FromArray(ctx, items, n);
261263
}
262264

265+
HPyAPI_FUNC(HPyTracker)
266+
HPyTracker_New(HPyContext ctx, HPy_ssize_t size)
267+
{
268+
return ctx_Tracker_New(ctx, size);
269+
}
270+
271+
HPyAPI_FUNC(int)
272+
HPyTracker_Add(HPyContext ctx, HPyTracker ht, HPy h)
273+
{
274+
return ctx_Tracker_Add(ctx, ht, h);
275+
}
276+
277+
HPyAPI_FUNC(void)
278+
HPyTracker_RemoveAll(HPyContext ctx, HPyTracker ht)
279+
{
280+
ctx_Tracker_RemoveAll(ctx, ht);
281+
}
282+
283+
HPyAPI_FUNC(void)
284+
HPyTracker_Free(HPyContext ctx, HPyTracker ht)
285+
{
286+
ctx_Tracker_Free(ctx, ht);
287+
}
288+
263289
#endif /* !HPy_CPYTHON_H */

graalpython/com.oracle.graal.python.cext/include/universal/autogen_ctx.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef HPy _HPyConst;
3838
#define HPy void*
3939
#define HPyListBuilder void*
4040
#define HPyTupleBuilder void*
41+
#define HPyTracker void*
4142

4243

4344
struct _HPyContext_s {
@@ -158,8 +159,13 @@ struct _HPyContext_s {
158159
void (*ctx_TupleBuilder_Set)(HPyContext ctx, HPyTupleBuilder builder, HPy_ssize_t index, HPy h_item);
159160
HPy (*ctx_TupleBuilder_Build)(HPyContext ctx, HPyTupleBuilder builder);
160161
void (*ctx_TupleBuilder_Cancel)(HPyContext ctx, HPyTupleBuilder builder);
162+
HPyTracker (*ctx_Tracker_New)(HPyContext ctx, HPy_ssize_t size);
163+
int (*ctx_Tracker_Add)(HPyContext ctx, HPyTracker ht, HPy h);
164+
void (*ctx_Tracker_RemoveAll)(HPyContext ctx, HPyTracker ht);
165+
void (*ctx_Tracker_Free)(HPyContext ctx, HPyTracker ht);
161166
};
162167

163168
#undef HPy
164169
#undef HPyListBuilder
165170
#undef HPyTupleBuilder
171+
#undef HPyTracker

graalpython/com.oracle.graal.python.cext/include/universal/autogen_trampolines.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#define WRAP_TUPLE_BUILDER(_ptr) ((HPyTupleBuilder){(_ptr)})
3939
#define UNWRAP_LIST_BUILDER(_h) ((_h)._lst)
4040
#define WRAP_LIST_BUILDER(_ptr) ((HPyListBuilder){(_ptr)})
41+
#define UNWRAP_TRACKER(_h) ((_h)._i)
42+
#define WRAP_TRACKER(_ptr) ((HPyTracker){(_ptr)})
4143

4244
static inline HPy HPyModule_Create(HPyContext ctx, HPyModuleDef *def) {
4345
return WRAP(ctx->ctx_Module_Create ( ctx, def ));
@@ -451,3 +453,19 @@ static inline void HPyTupleBuilder_Cancel(HPyContext ctx, HPyTupleBuilder builde
451453
ctx->ctx_TupleBuilder_Cancel ( ctx, UNWRAP_TUPLE_BUILDER(builder));
452454
}
453455

456+
static inline HPyTracker HPyTracker_New(HPyContext ctx, HPy_ssize_t size) {
457+
return WRAP_TRACKER(ctx->ctx_Tracker_New ( ctx, size ));
458+
}
459+
460+
static inline int HPyTracker_Add(HPyContext ctx, HPyTracker ht, HPy h) {
461+
return ctx->ctx_Tracker_Add ( ctx, UNWRAP_TRACKER(ht), UNWRAP(h) );
462+
}
463+
464+
static inline void HPyTracker_RemoveAll(HPyContext ctx, HPyTracker ht) {
465+
ctx->ctx_Tracker_RemoveAll ( ctx, UNWRAP_TRACKER(ht) );
466+
}
467+
468+
static inline void HPyTracker_Free(HPyContext ctx, HPyTracker ht) {
469+
ctx->ctx_Tracker_Free ( ctx, UNWRAP_TRACKER(ht) );
470+
}
471+

graalpython/com.oracle.graal.python.cext/include/universal/hpy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ typedef intptr_t HPy_hash_t;
5959
typedef struct _HPy_s { void* _i; } HPy;
6060
typedef struct { void* _lst; } HPyListBuilder;
6161
typedef struct { void* _tup; } HPyTupleBuilder;
62+
typedef struct { void* _i; } HPyTracker;
6263

6364
typedef struct _HPyContext_s *HPyContext;
6465

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyContext.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@
108108
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPySetAttr;
109109
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPySetItem;
110110
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPyTernaryArithmetic;
111+
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPyTrackerAdd;
112+
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPyTrackerCleanup;
113+
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPyTrackerNew;
111114
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPyTupleFromArray;
112115
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPyTypeFromSpec;
113116
import com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyContextFunctions.GraalHPyTypeGenericNew;
@@ -289,19 +292,19 @@ enum HPyContextMembers {
289292
CTX_LIST_BUILDER_NEW("ctx_ListBuilder_New"),
290293
CTX_LIST_BUILDER_SET("ctx_ListBuilder_Set"),
291294
CTX_LIST_BUILDER_BUILD("ctx_ListBuilder_Build"),
292-
CTX_LIST_BUILDER_CANCEL("ctx_ListBuilder_Cancel");
295+
CTX_LIST_BUILDER_CANCEL("ctx_ListBuilder_Cancel"),
296+
CTX_TRACKER_NEW("ctx_Tracker_New"),
297+
CTX_TRACKER_ADD("ctx_Tracker_Add"),
298+
CTX_TRACKER_REMOVE_ALL("ctx_Tracker_RemoveAll"),
299+
CTX_TRACKER_FREE("ctx_Tracker_Free");
293300

294301
private final String name;
295302

296303
HPyContextMembers(String name) {
297304
this.name = name;
298305
}
299306

300-
@CompilationFinal(dimensions = 1) private static final HPyContextMembers[] VALUES;
301-
static {
302-
HPyContextMembers[] members = values();
303-
VALUES = Arrays.copyOf(members, members.length);
304-
}
307+
@CompilationFinal(dimensions = 1) private static final HPyContextMembers[] VALUES = values();
305308

306309
@ExplodeLoop(kind = LoopExplosionKind.FULL_UNROLL_UNTIL_RETURN)
307310
public static HPyContextMembers getByName(String name) {
@@ -751,6 +754,11 @@ private static Object[] createMembers(PythonContext context) {
751754
members[HPyContextMembers.CTX_LIST_BUILDER_SET.ordinal()] = graalHPyBuilderSet;
752755
members[HPyContextMembers.CTX_LIST_BUILDER_BUILD.ordinal()] = new GraalHPyBuilderBuild(PList);
753756
members[HPyContextMembers.CTX_LIST_BUILDER_CANCEL.ordinal()] = graalHPyBuilderCancel;
757+
758+
members[HPyContextMembers.CTX_TRACKER_NEW.ordinal()] = new GraalHPyTrackerNew();
759+
members[HPyContextMembers.CTX_TRACKER_ADD.ordinal()] = new GraalHPyTrackerAdd();
760+
members[HPyContextMembers.CTX_TRACKER_REMOVE_ALL.ordinal()] = new GraalHPyTrackerCleanup(true);
761+
members[HPyContextMembers.CTX_TRACKER_FREE.ordinal()] = new GraalHPyTrackerCleanup(false);
754762
return members;
755763
}
756764

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyContextFunctions.java

Lines changed: 116 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
135135
import com.oracle.graal.python.runtime.sequence.PSequence;
136136
import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage;
137+
import com.oracle.graal.python.util.OverflowException;
137138
import com.oracle.graal.python.util.PythonUtils;
138139
import com.oracle.truffle.api.CompilerAsserts;
139140
import com.oracle.truffle.api.CompilerDirectives;
@@ -1598,11 +1599,11 @@ Object execute(Object[] arguments,
15981599
}
15991600
}
16001601

1601-
@ExportLibrary(InteropLibrary.class)
1602-
public static final class GraalHPyBuilderNew extends GraalHPyContextFunction {
1602+
abstract static class GraalHPyBuilderNewBase extends GraalHPyContextFunction {
16031603

1604-
@ExportMessage
1605-
Object execute(Object[] arguments,
1604+
protected abstract Object createObject(int capacity);
1605+
1606+
protected final Object doExecute(Object[] arguments,
16061607
@Cached HPyAsContextNode asContextNode,
16071608
@Cached CastToJavaIntExactNode castToJavaIntExactNode,
16081609
@Cached HPyAsHandleNode asHandleNode) throws ArityException {
@@ -1614,11 +1615,7 @@ Object execute(Object[] arguments,
16141615
try {
16151616
int capacity = castToJavaIntExactNode.execute(arguments[1]);
16161617
if (capacity >= 0) {
1617-
Object[] data = new Object[capacity];
1618-
for (int i = 0; i < data.length; i++) {
1619-
data[i] = PNone.NONE;
1620-
}
1621-
return asHandleNode.execute(nativeContext, new ObjectSequenceStorage(data));
1618+
return asHandleNode.execute(nativeContext, createObject(capacity));
16221619
}
16231620
} catch (CannotCastException e) {
16241621
// fall through
@@ -1627,6 +1624,26 @@ Object execute(Object[] arguments,
16271624
}
16281625
}
16291626

1627+
@ExportLibrary(InteropLibrary.class)
1628+
public static final class GraalHPyBuilderNew extends GraalHPyBuilderNewBase {
1629+
@ExportMessage
1630+
Object execute(Object[] arguments,
1631+
@Cached HPyAsContextNode asContextNode,
1632+
@Cached CastToJavaIntExactNode castToJavaIntExactNode,
1633+
@Cached HPyAsHandleNode asHandleNode) throws ArityException {
1634+
return doExecute(arguments, asContextNode, castToJavaIntExactNode, asHandleNode);
1635+
}
1636+
1637+
@Override
1638+
protected Object createObject(int capacity) {
1639+
Object[] data = new Object[capacity];
1640+
for (int i = 0; i < data.length; i++) {
1641+
data[i] = PNone.NONE;
1642+
}
1643+
return new ObjectSequenceStorage(data);
1644+
}
1645+
}
1646+
16301647
@ExportLibrary(InteropLibrary.class)
16311648
public static final class GraalHPyBuilderSet extends GraalHPyContextFunction {
16321649

@@ -1759,4 +1776,94 @@ private static ObjectSequenceStorage cast(Object object) {
17591776

17601777
}
17611778

1779+
@ExportLibrary(InteropLibrary.class)
1780+
public static final class GraalHPyTrackerNew extends GraalHPyBuilderNewBase {
1781+
@ExportMessage
1782+
Object execute(Object[] arguments,
1783+
@Cached HPyAsContextNode asContextNode,
1784+
@Cached CastToJavaIntExactNode castToJavaIntExactNode,
1785+
@Cached HPyAsHandleNode asHandleNode) throws ArityException {
1786+
return doExecute(arguments, asContextNode, castToJavaIntExactNode, asHandleNode);
1787+
}
1788+
1789+
@Override
1790+
protected Object createObject(int capacity) {
1791+
return new GraalHPyTracker(capacity);
1792+
}
1793+
}
1794+
1795+
@ExportLibrary(InteropLibrary.class)
1796+
public static final class GraalHPyTrackerAdd extends GraalHPyContextFunction {
1797+
@ExportMessage
1798+
Object execute(Object[] arguments,
1799+
@Cached HPyAsContextNode asContextNode,
1800+
@Cached HPyAsPythonObjectNode asPythonObjectNode,
1801+
@Cached HPyEnsureHandleNode ensureHandleNode) throws ArityException, UnsupportedTypeException {
1802+
if (arguments.length != 3) {
1803+
CompilerDirectives.transferToInterpreterAndInvalidate();
1804+
throw ArityException.create(2, arguments.length);
1805+
}
1806+
GraalHPyContext nativeContext = asContextNode.execute(arguments[0]);
1807+
GraalHPyTracker builder = cast(asPythonObjectNode.execute(nativeContext, arguments[1]));
1808+
if (builder == null) {
1809+
// that's really unexpected since the C signature should enforce a valid builder but
1810+
// someone could have messed it up
1811+
CompilerDirectives.transferToInterpreterAndInvalidate();
1812+
throw UnsupportedTypeException.create(arguments, "invalid builder object");
1813+
}
1814+
try {
1815+
builder.add(ensureHandleNode.execute(nativeContext, arguments[2]));
1816+
} catch (OverflowException | OutOfMemoryError e) {
1817+
return -1;
1818+
}
1819+
return 0;
1820+
}
1821+
1822+
private static GraalHPyTracker cast(Object object) {
1823+
if (object instanceof GraalHPyTracker) {
1824+
return (GraalHPyTracker) object;
1825+
}
1826+
return null;
1827+
}
1828+
}
1829+
1830+
@ExportLibrary(InteropLibrary.class)
1831+
public static final class GraalHPyTrackerCleanup extends GraalHPyContextFunction {
1832+
private final boolean removeAll;
1833+
1834+
public GraalHPyTrackerCleanup(boolean removeAll) {
1835+
this.removeAll = removeAll;
1836+
}
1837+
1838+
@ExportMessage
1839+
Object execute(Object[] arguments,
1840+
@Cached HPyAsContextNode asContextNode,
1841+
@Cached HPyAsPythonObjectNode asPythonObjectNode) throws ArityException, UnsupportedTypeException {
1842+
if (arguments.length != 2) {
1843+
CompilerDirectives.transferToInterpreterAndInvalidate();
1844+
throw ArityException.create(2, arguments.length);
1845+
}
1846+
GraalHPyContext nativeContext = asContextNode.execute(arguments[0]);
1847+
GraalHPyTracker builder = cast(asPythonObjectNode.execute(nativeContext, arguments[1]));
1848+
if (builder == null) {
1849+
// that's really unexpected since the C signature should enforce a valid builder but
1850+
// someone could have messed it up
1851+
CompilerDirectives.transferToInterpreterAndInvalidate();
1852+
throw UnsupportedTypeException.create(arguments, "invalid builder object");
1853+
}
1854+
if (removeAll) {
1855+
builder.removeAll();
1856+
} else {
1857+
builder.free(nativeContext, ConditionProfile.getUncached());
1858+
}
1859+
return 0;
1860+
}
1861+
1862+
private static GraalHPyTracker cast(Object object) {
1863+
if (object instanceof GraalHPyTracker) {
1864+
return (GraalHPyTracker) object;
1865+
}
1866+
return null;
1867+
}
1868+
}
17621869
}

0 commit comments

Comments
 (0)