Skip to content

Commit d3954ee

Browse files
committed
Merge branch 'hpy-import'
2 parents 173cc45 + 05bc6c1 commit d3954ee

File tree

15 files changed

+414
-30
lines changed

15 files changed

+414
-30
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef HPY_COMMON_RUNTIME_CTX_TRACKER_H
2+
#define HPY_COMMON_RUNTIME_CTX_TRACKER_H
3+
4+
#include "hpy.h"
5+
6+
_HPy_HIDDEN HPyTracker
7+
ctx_Tracker_New(HPyContext ctx, HPy_ssize_t size);
8+
9+
_HPy_HIDDEN int
10+
ctx_Tracker_Add(HPyContext ctx, HPyTracker ht, HPy h);
11+
12+
_HPy_HIDDEN void
13+
ctx_Tracker_RemoveAll(HPyContext ctx, HPyTracker ht);
14+
15+
_HPy_HIDDEN void
16+
ctx_Tracker_Free(HPyContext ctx, HPyTracker ht);
17+
18+
#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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ struct _HPyContext_s {
158158
void (*ctx_TupleBuilder_Set)(HPyContext ctx, HPyTupleBuilder builder, HPy_ssize_t index, HPy h_item);
159159
HPy (*ctx_TupleBuilder_Build)(HPyContext ctx, HPyTupleBuilder builder);
160160
void (*ctx_TupleBuilder_Cancel)(HPyContext ctx, HPyTupleBuilder builder);
161+
HPyTracker (*ctx_Tracker_New)(HPyContext ctx, HPy_ssize_t size);
162+
int (*ctx_Tracker_Add)(HPyContext ctx, HPyTracker ht, HPy h);
163+
void (*ctx_Tracker_RemoveAll)(HPyContext ctx, HPyTracker ht);
164+
void (*ctx_Tracker_Free)(HPyContext ctx, HPyTracker ht);
161165
};
162166

163167
#undef HPy

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,19 @@ static inline void HPyTupleBuilder_Cancel(HPyContext ctx, HPyTupleBuilder builde
451451
ctx->ctx_TupleBuilder_Cancel ( ctx, UNWRAP_TUPLE_BUILDER(builder));
452452
}
453453

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

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/lib-graalpython/modules/hpy/devel/src/runtime/ctx_listbuilder.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ _HPy_HIDDEN void
7171
ctx_ListBuilder_Cancel(HPyContext ctx, HPyListBuilder builder)
7272
{
7373
PyObject *lst = (PyObject *)builder._lst;
74+
if (lst == NULL) {
75+
// we don't report the memory error here: the builder
76+
// is being cancelled (so the result of the builder is not being used)
77+
// and likely it's being cancelled during the handling of another error
78+
return;
79+
}
7480
builder._lst = 0;
7581
Py_XDECREF(lst);
7682
}

graalpython/lib-graalpython/modules/hpy/devel/src/runtime/ctx_module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* MIT License
22
*
3-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates.
3+
* Copyright (c) 2021, Oracle and/or its affiliates.
44
* Copyright (c) 2019 pyhandle
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
/**
26+
* A manager for HPy handles, allowing handles to be tracked
27+
* and closed as a group.
28+
*
29+
* Note::
30+
* Calling HPyTracker_New(ctx, n) will ensure that at least n handles
31+
* can be tracked without a call to HPyTracker_Add failing.
32+
*
33+
* If a call to HPyTracker_Add fails, the tracker still guarantees that
34+
* the handle passed to it has been tracked (internally it does this by
35+
* maintaining space for one more handle).
36+
*
37+
* After HPyTracker_Add fails, HPyTracker_Free should be called without
38+
* any further calls to HPyTracker_Add. Calling HPyTracker_Free will close
39+
* all the tracked handles, including the handled passed to the failed call
40+
* to HPyTracker_Add.
41+
*
42+
* Example usage (inside an HPyDef_METH function)::
43+
*
44+
* long i;
45+
* HPy key, value;
46+
* HPyTracker ht;
47+
*
48+
* ht = HPyTracker_New(ctx, 0); // track the key-value pairs
49+
* if (HPy_IsNull(ht))
50+
* return HPy_NULL;
51+
*
52+
* HPy dict = HPyDict_New(ctx);
53+
* if (HPy_IsNull(dict))
54+
* goto error;
55+
*
56+
* for (i=0; i<5; i++) {
57+
* key = HPyLong_FromLong(ctx, i);
58+
* if (HPy_IsNull(key))
59+
* goto error;
60+
* if (HPyTracker_Add(ctx, ht, key) < 0)
61+
* goto error;
62+
* value = HPyLong_FromLong(ctx, i * i);
63+
* if (HPy_IsNull(value)) {
64+
* goto error;
65+
* }
66+
* if (HPyTracker_Add(ctx, ht, value) < 0)
67+
* goto error;
68+
* result = HPy_SetItem(ctx, dict, key, value);
69+
* if (result < 0)
70+
* goto error;
71+
* }
72+
*
73+
* success:
74+
* HPyTracker_Free(ctx, ht);
75+
* return dict;
76+
*
77+
* error:
78+
* HPyTracker_Free(ctx, ht);
79+
* HPy_Close(ctx, dict);
80+
* // HPyErr will already have been set by the error that occurred.
81+
* return HPy_NULL;
82+
*/
83+
84+
#include <Python.h>
85+
#include "hpy.h"
86+
#include "common/runtime/ctx_type.h"
87+
88+
#ifdef HPY_UNIVERSAL_ABI
89+
#define _ht2hp(x) ((_HPyTracker_s *) (x)._i)
90+
#define _hp2ht(x) ((HPyTracker) {(HPy_ssize_t) (hp)})
91+
#else
92+
#define _ht2hp(x) ((_HPyTracker_s *) (x)._o)
93+
#define _hp2ht(x) ((HPyTracker) {(void *) (hp)})
94+
#endif
95+
96+
static const HPy_ssize_t HPYTRACKER_INITIAL_SIZE = 5;
97+
98+
typedef struct {
99+
HPy_ssize_t size;
100+
HPy_ssize_t next;
101+
HPy *handles;
102+
} _HPyTracker_s;
103+
104+
105+
_HPy_HIDDEN HPyTracker
106+
ctx_Tracker_New(HPyContext ctx, HPy_ssize_t size)
107+
{
108+
_HPyTracker_s *hp;
109+
if (size == 0) {
110+
size = HPYTRACKER_INITIAL_SIZE;
111+
}
112+
size++;
113+
114+
hp = PyMem_Malloc(sizeof(_HPyTracker_s));
115+
if (hp == NULL) {
116+
PyErr_NoMemory();
117+
return _hp2ht(0);
118+
}
119+
hp->handles = PyMem_Calloc(size, sizeof(HPy));
120+
if (hp->handles == NULL) {
121+
PyMem_Free(hp);
122+
PyErr_NoMemory();
123+
return _hp2ht(0);
124+
}
125+
hp->size = size;
126+
hp->next = 0;
127+
return _hp2ht(hp);
128+
}
129+
130+
static int
131+
tracker_resize(HPyContext ctx, _HPyTracker_s *hp, HPy_ssize_t size)
132+
{
133+
HPy *new_handles;
134+
size++;
135+
136+
if (size <= hp->next) {
137+
// refuse a resize that would either 1) lose handles or 2) not leave
138+
// space for one new handle
139+
PyErr_SetString(PyExc_ValueError, "HPyTracker resize would lose handles");
140+
return -1;
141+
}
142+
new_handles = PyMem_Realloc(hp->handles, size * sizeof(HPy));
143+
if (new_handles == NULL) {
144+
PyErr_NoMemory();
145+
return -1;
146+
}
147+
hp->size = size;
148+
hp->handles = new_handles;
149+
return 0;
150+
}
151+
152+
_HPy_HIDDEN int
153+
ctx_Tracker_Add(HPyContext ctx, HPyTracker ht, HPy h)
154+
{
155+
_HPyTracker_s *hp = _ht2hp(ht);
156+
hp->handles[hp->next++] = h;
157+
if (hp->size <= hp->next) {
158+
if (tracker_resize(ctx, hp, hp->size * 2 - 1) < 0)
159+
return -1;
160+
}
161+
return 0;
162+
}
163+
164+
_HPy_HIDDEN void
165+
ctx_Tracker_RemoveAll(HPyContext ctx, HPyTracker ht)
166+
{
167+
_HPyTracker_s *hp = _ht2hp(ht);
168+
hp->next = 0;
169+
}
170+
171+
_HPy_HIDDEN void
172+
ctx_Tracker_Free(HPyContext ctx, HPyTracker ht)
173+
{
174+
_HPyTracker_s *hp = _ht2hp(ht);
175+
HPy_ssize_t i;
176+
for (i=0; i<hp->next; i++) {
177+
HPy_Close(ctx, hp->handles[i]);
178+
}
179+
PyMem_Free(hp->handles);
180+
PyMem_Free(hp);
181+
}

graalpython/lib-graalpython/modules/hpy/devel/src/runtime/ctx_tuple.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* MIT License
22
*
3-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates.
3+
* Copyright (c) 2021, Oracle and/or its affiliates.
44
* Copyright (c) 2019 pyhandle
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy

0 commit comments

Comments
 (0)