0

The problem is FFI wrapping a C-Type that requires two environment variables for initialization and de-allocation with a ForeignPtr

More concretely I want to wrap

void gr_mpoly_init(gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx)

and

void gr_mpoly_clear(gr_mpoly_t A, const mpoly_ctx_t mctx, gr_ctx_t cctx)

for a Haskell type GRMPoly ... .

So the question is how to define something like

addForeignPtrFinalizerEnv2 myfinalizer mypointer env1 env2

I looked at the source of of addForeignPtrFinalizerEnv in ForeignPtr.ForeignPtr but that seems to require an understanding of the underlying "magic" (include #).

So what are "elegant" alternatives?

The simplest solution I could come up with was defining a container for the two pointers including the extra infrastructure for allocating and deallocation and wrapping in Haskell.

I would expect a simpler, more elegant solution ...

1 Answer 1

2

Perhaps you could write a small C wrapper function which combines the two environments.

typedef struct {
    mpoly_ctx_t mpoly_ctx;
    gr_ctx_t gr_ctx;
} *mpoly_gr_ctx_t;

void combined_gr_mpoly_clear(mpoly_gr_ctx_t ctx, gr_mpoly_t A) {
    gr_mpoly_clear(A, ctx->mpoly_ctx, ctx->gr_ctx);
}

I've also swapped the argument order to better match the order that FinalizerEnvPtrs expect their arguments.

It's been a while since I've done C; please forgive me if I've misremembered some of the syntax. If I've misremembered so badly that the idea is unclear, leave me a comment and I'll try to expand a bit more in English.

1
  • Actually, on second read of the question, it seems you may have come up with this idea yourself, too, but didn't like it. Rest assured that I think it is the simplest and most elegant solution available to you, so well done for coming up with it! Commented Aug 9 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.