Skip to content

Commit

Permalink
Merge pull request #378 from fangerer/fa/capsule_dtor
Browse files Browse the repository at this point in the history
Improve API/implementation for HPyCapsule_Destructor
  • Loading branch information
fangerer authored Nov 23, 2022
2 parents 414be8b + a818bb7 commit ff1488d
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 295 deletions.
2 changes: 1 addition & 1 deletion hpy/debug/src/autogen_debug_ctx_init.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion hpy/debug/src/autogen_debug_wrappers.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions hpy/debug/src/debug_ctx_cpython.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ void debug_ctx_CallRealFunctionFromTrampoline(HPyContext *dctx,
a->visit, a->arg);
return;
}
case HPyFunc_CAPSULE_DESTRUCTOR: {
HPyFunc_Capsule_Destructor f = (HPyFunc_Capsule_Destructor)func;
PyObject *capsule = (PyObject *)args;
const char *name = PyCapsule_GetName(capsule);
f(name, PyCapsule_GetPointer(capsule, name),
PyCapsule_GetContext(capsule));
return;
}
#include "autogen_debug_ctx_call.i"
default:
Py_FatalError("Unsupported HPyFunc_Signature in debug_ctx_cpython.c");
Expand Down
2 changes: 0 additions & 2 deletions hpy/devel/include/hpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ typedef struct _HPyContext_s HPyContext;
typedef Py_UCS4 HPy_UCS4;
#endif

typedef void (*HPyCapsule_Destructor)(const char *name, void *pointer, void *context);


/* ~~~~~~~~~~~~~~~~ Additional #includes ~~~~~~~~~~~~~~~~ */

Expand Down
1 change: 1 addition & 0 deletions hpy/devel/include/hpy/cpy_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typedef int (*cpy_visitproc)(cpy_PyObject *, void *);
typedef cpy_PyObject *(*cpy_PyCFunction)(cpy_PyObject *, cpy_PyObject *);
typedef cpy_PyObject *(*cpy_getter)(cpy_PyObject *, void *);
typedef int (*cpy_setter)(cpy_PyObject *, cpy_PyObject *, void *);
typedef void (*cpy_PyCapsule_Destructor)(cpy_PyObject *);


#endif /* HPY_UNIVERSAL_CPY_TYPES_H */
8 changes: 8 additions & 0 deletions hpy/devel/include/hpy/cpython/hpyfunc_trampolines.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,12 @@ typedef int (*_HPyCFunction_RELEASEBUFFERPROC)(HPyContext *, HPy, HPy_buffer *);
visit, arg); \
}

#define HPyCapsule_DESTRUCTOR_TRAMPOLINE(SYM, IMPL) \
static void SYM(PyObject *capsule) \
{ \
const char *name = PyCapsule_GetName(capsule); \
IMPL(name, PyCapsule_GetPointer(capsule, name), \
PyCapsule_GetContext(capsule)); \
}

#endif // HPY_CPYTHON_HPYFUNC_TRAMPOLINES_H
9 changes: 2 additions & 7 deletions hpy/devel/include/hpy/cpython/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ HPyAPI_FUNC int HPyErr_Occurred(HPyContext *ctx) {
return ctx_Err_Occurred(ctx);
}

HPyAPI_FUNC HPy HPyCapsule_New(HPyContext *ctx, void *pointer, const char *name, HPyCapsule_Destructor destructor)
HPyAPI_FUNC HPy HPyCapsule_New(HPyContext *ctx, void *pointer, const char *name, HPyCapsule_Destructor *destructor)
{
return ctx_Capsule_New(ctx, pointer, name, destructor);
}
Expand All @@ -404,11 +404,6 @@ HPyAPI_FUNC void * HPyCapsule_GetContext(HPyContext *ctx, HPy capsule)
return PyCapsule_GetContext(_h2py(capsule));
}

HPyAPI_FUNC HPyCapsule_Destructor HPyCapsule_GetDestructor(HPyContext *ctx, HPy capsule)
{
return ctx_Capsule_GetDestructor(ctx, capsule);
}

HPyAPI_FUNC int HPyCapsule_SetPointer(HPyContext *ctx, HPy capsule, void *pointer)
{
return PyCapsule_SetPointer(_h2py(capsule), pointer);
Expand All @@ -424,7 +419,7 @@ HPyAPI_FUNC int HPyCapsule_SetContext(HPyContext *ctx, HPy capsule, void *contex
return PyCapsule_SetContext(_h2py(capsule), context);
}

HPyAPI_FUNC int HPyCapsule_SetDestructor(HPyContext *ctx, HPy capsule, HPyCapsule_Destructor destructor)
HPyAPI_FUNC int HPyCapsule_SetDestructor(HPyContext *ctx, HPy capsule, HPyCapsule_Destructor *destructor)
{
return ctx_Capsule_SetDestructor(ctx, capsule, destructor);
}
Expand Down
49 changes: 32 additions & 17 deletions hpy/devel/include/hpy/hpydef.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern "C" {
#include "hpy/cpy_types.h"

typedef void* (*HPyCFunction)();
typedef void (*HPyFunc_Capsule_Destructor)(const char *name, void *pointer, void *context);

typedef struct {
HPySlot_Slot slot; // The slot to fill
Expand Down Expand Up @@ -93,6 +94,12 @@ typedef struct {
};
} HPyDef;

typedef struct {
cpy_PyCapsule_Destructor cpy_trampoline;
HPyFunc_Capsule_Destructor impl;
} HPyCapsule_Destructor;


// macros to automatically define HPyDefs of various kinds

/* ~~~ HPySlot_SIG ~~~
Expand All @@ -114,18 +121,18 @@ typedef struct {
more detailed explanation in the comments around HPyFunc_DECLARE in
hpyfunc.h
*/
#define HPyDef_SLOT_IMPL(SYM, IMPL, SLOT) \
enum { SYM##_slot = SLOT }; \
#define HPyDef_SLOT_IMPL(SYM, IMPL, SLOT) \
enum { SYM##_slot = SLOT }; \
_HPyDef_SLOT(SYM, IMPL, SLOT, HPySlot_SIG(SLOT))

#define HPyDef_SLOT(SYM, SLOT) \
#define HPyDef_SLOT(SYM, SLOT) \
HPyDef_SLOT_IMPL(SYM, SYM##_impl, SLOT)


// this is the actual implementation, after we determined the SIG
#define _HPyDef_SLOT(SYM, IMPL, SLOT, SIG) \
HPyFunc_DECLARE(IMPL, SIG); \
HPyFunc_TRAMPOLINE(SYM##_trampoline, IMPL, SIG); \
HPyFunc_TRAMPOLINE(SYM##_trampoline, IMPL, SIG); \
HPyDef SYM = { \
.kind = HPyDef_Kind_Slot, \
.slot = { \
Expand All @@ -136,9 +143,9 @@ typedef struct {
};


#define HPyDef_METH_IMPL(SYM, NAME, IMPL, SIG, ...) \
#define HPyDef_METH_IMPL(SYM, NAME, IMPL, SIG, ...) \
HPyFunc_DECLARE(IMPL, SIG); \
HPyFunc_TRAMPOLINE(SYM##_trampoline, IMPL, SIG); \
HPyFunc_TRAMPOLINE(SYM##_trampoline, IMPL, SIG); \
HPyDef SYM = { \
.kind = HPyDef_Kind_Meth, \
.meth = { \
Expand All @@ -150,7 +157,7 @@ typedef struct {
} \
};

#define HPyDef_METH(SYM, NAME, SIG, ...) \
#define HPyDef_METH(SYM, NAME, SIG, ...) \
HPyDef_METH_IMPL(SYM, NAME, SYM##_impl, SIG, __VA_ARGS__)

#define HPyDef_MEMBER(SYM, NAME, TYPE, OFFSET, ...) \
Expand All @@ -164,9 +171,9 @@ typedef struct {
} \
};

#define HPyDef_GET_IMPL(SYM, NAME, GETIMPL, ...) \
#define HPyDef_GET_IMPL(SYM, NAME, GETIMPL, ...) \
HPyFunc_DECLARE(GETIMPL, HPyFunc_GETTER); \
HPyFunc_TRAMPOLINE(SYM##_get_trampoline, GETIMPL, HPyFunc_GETTER); \
HPyFunc_TRAMPOLINE(SYM##_get_trampoline, GETIMPL, HPyFunc_GETTER); \
HPyDef SYM = { \
.kind = HPyDef_Kind_GetSet, \
.getset = { \
Expand All @@ -177,12 +184,12 @@ typedef struct {
} \
};

#define HPyDef_GET(SYM, NAME, ...) \
#define HPyDef_GET(SYM, NAME, ...) \
HPyDef_GET_IMPL(SYM, NAME, SYM##_get, __VA_ARGS__)

#define HPyDef_SET_IMPL(SYM, NAME, SETIMPL, ...) \
#define HPyDef_SET_IMPL(SYM, NAME, SETIMPL, ...) \
HPyFunc_DECLARE(SETIMPL, HPyFunc_SETTER); \
HPyFunc_TRAMPOLINE(SYM##_set_trampoline, SETIMPL, HPyFunc_SETTER); \
HPyFunc_TRAMPOLINE(SYM##_set_trampoline, SETIMPL, HPyFunc_SETTER); \
HPyDef SYM = { \
.kind = HPyDef_Kind_GetSet, \
.getset = { \
Expand All @@ -193,14 +200,14 @@ typedef struct {
} \
};

#define HPyDef_SET(SYM, NAME, ...) \
#define HPyDef_SET(SYM, NAME, ...) \
HPyDef_SET_IMPL(SYM, NAME, SYM##_set, __VA_ARGS__)

#define HPyDef_GETSET_IMPL(SYM, NAME, GETIMPL, SETIMPL, ...) \
#define HPyDef_GETSET_IMPL(SYM, NAME, GETIMPL, SETIMPL, ...) \
HPyFunc_DECLARE(GETIMPL, HPyFunc_GETTER); \
HPyFunc_TRAMPOLINE(SYM##_get_trampoline, GETIMPL, HPyFunc_GETTER); \
HPyFunc_TRAMPOLINE(SYM##_get_trampoline, GETIMPL, HPyFunc_GETTER); \
HPyFunc_DECLARE(SETIMPL, HPyFunc_SETTER); \
HPyFunc_TRAMPOLINE(SYM##_set_trampoline, SETIMPL, HPyFunc_SETTER); \
HPyFunc_TRAMPOLINE(SYM##_set_trampoline, SETIMPL, HPyFunc_SETTER); \
HPyDef SYM = { \
.kind = HPyDef_Kind_GetSet, \
.getset = { \
Expand All @@ -213,9 +220,17 @@ typedef struct {
} \
};

#define HPyDef_GETSET(SYM, NAME, ...) \
#define HPyDef_GETSET(SYM, NAME, ...) \
HPyDef_GETSET_IMPL(SYM, NAME, SYM##_get, SYM##_set, __VA_ARGS__)

#define HPyCapsule_DESTRUCTOR(SYM) \
static void SYM##_impl(const char *name, void *pointer, void *context); \
HPyCapsule_DESTRUCTOR_TRAMPOLINE(SYM##_trampoline, SYM##_impl); \
static HPyCapsule_Destructor SYM = { \
.cpy_trampoline = SYM##_trampoline, \
.impl = SYM##_impl \
};

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions hpy/devel/include/hpy/hpyfunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ typedef enum {
HPyFunc_OBJOBJPROC,
HPyFunc_TRAVERSEPROC,
HPyFunc_DESTRUCTOR,
HPyFunc_CAPSULE_DESTRUCTOR,

} HPyFunc_Signature;

Expand Down
6 changes: 2 additions & 4 deletions hpy/devel/include/hpy/runtime/ctx_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ _HPy_HIDDEN HPy ctx_Tuple_FromArray(HPyContext *ctx, HPy items[], HPy_ssize_t n)
_HPy_HIDDEN HPy ctx_Capsule_New(HPyContext *ctx,
void *pointer,
const char *name,
HPyCapsule_Destructor destructor);
_HPy_HIDDEN HPyCapsule_Destructor ctx_Capsule_GetDestructor(HPyContext *ctx,
HPy h_capsule);
HPyCapsule_Destructor *destructor);
_HPy_HIDDEN int ctx_Capsule_SetDestructor(HPyContext *ctx,
HPy h_capsule,
HPyCapsule_Destructor destructor);
HPyCapsule_Destructor *destructor);
#ifdef HPY_UNIVERSAL_ABI
_HPy_HIDDEN void* ctx_Capsule_Get(HPyContext *ctx,
HPy capsule,
Expand Down
2 changes: 1 addition & 1 deletion hpy/devel/include/hpy/universal/autogen_ctx.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion hpy/devel/include/hpy/universal/autogen_trampolines.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions hpy/devel/include/hpy/universal/hpyfunc_trampolines.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ typedef struct {
return a.result; \
}

#define HPyCapsule_DESTRUCTOR_TRAMPOLINE(SYM, IMPL) \
static void SYM(cpy_PyObject *capsule) \
{ \
_HPy_CallRealFunctionFromTrampoline(_ctx_for_trampolines, \
HPyFunc_CAPSULE_DESTRUCTOR, (HPyCFunction)IMPL, capsule); \
}


#endif // HPY_UNIVERSAL_HPYFUNC_TRAMPOLINES_H
9 changes: 1 addition & 8 deletions hpy/devel/include/hpy/universal/misc_trampolines.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ HPyCapsule_GetContext(HPyContext *ctx, HPy capsule)
ctx, capsule, HPyCapsule_key_Context, NULL);
}

static inline HPyCapsule_Destructor
HPyCapsule_GetDestructor(HPyContext *ctx, HPy capsule)
{
return (HPyCapsule_Destructor) ctx->ctx_Capsule_Get(
ctx, capsule, HPyCapsule_key_Destructor, NULL);
}

static inline int
HPyCapsule_SetPointer(HPyContext *ctx, HPy capsule, void *pointer)
{
Expand All @@ -84,7 +77,7 @@ HPyCapsule_SetContext(HPyContext *ctx, HPy capsule, void *context)

static inline int
HPyCapsule_SetDestructor(HPyContext *ctx, HPy capsule,
HPyCapsule_Destructor destructor)
HPyCapsule_Destructor *destructor)
{
return ctx->ctx_Capsule_Set(
ctx, capsule, HPyCapsule_key_Destructor, (void *) destructor);
Expand Down
Loading

0 comments on commit ff1488d

Please sign in to comment.