From lemming at henning-thielemann.de Thu Aug 18 15:17:08 2016 From: lemming at henning-thielemann.de (Henning Thielemann) Date: Thu, 18 Aug 2016 17:17:08 +0200 (CEST) Subject: type synonyms for dynamic imports and wrappers Message-ID: The Haskell FFI report says that imports and exports of dynamic functions always have those types: foreign import ccall "dynamic" mkFun :: FunPtr fun -> fun foreign import ccall "wrapper" mkCallback :: fun -> IO (FunPtr fun) where 'fun' is an IO function. Since 'fn' can be a considerably large type expression I usually define type Importer fun = FunPtr fun -> fun type Exporter fun = fun -> IO (FunPtr fun) How about adding these type synonyms to Foreign? From lemming at henning-thielemann.de Sat Aug 27 10:23:13 2016 From: lemming at henning-thielemann.de (Henning Thielemann) Date: Sat, 27 Aug 2016 12:23:13 +0200 (CEST) Subject: managing embedded structs Message-ID: I want to write a Haskell interface to a C library that provides two data structures A and B, where B is contained in A. That is, if A is freed, then B is automatically freed, too. There are two ways to obtain a Ptr B. Either construct it separately or as part of A, i.e. b0 :: Ptr B b0 <- createB b1 :: Ptr B b1 <- getBofA =<< createA Now I want to let the garbage collector manage the resources for B in a ForeignPtr. The first case is easy: fb0 :: ForeignPtr B fb0 <- newForeignPtr deleteB =<< createB But how to create an fb1? fa1 :: ForeignPtr A fa1 <- newForeignPtr deleteA =<< createA fb1 :: ForeignPtr B fb1 <- newForeignPtr ??? =<< withForeignPtr fa1 getBofA Obviously, there is nothing I can insert for ???. fb1 must not have its own finalizer, because fa1 already has one. But I have to make sure that fa1 lives at least as long as fb1. How to do that? From lemming at henning-thielemann.de Sun Aug 28 18:17:27 2016 From: lemming at henning-thielemann.de (Henning Thielemann) Date: Sun, 28 Aug 2016 20:17:27 +0200 (CEST) Subject: [Haskell-cafe] managing embedded structs In-Reply-To: References: Message-ID: On Sun, 28 Aug 2016, Alexey Khudyakov wrote: > On 27 August 2016 at 13:23, Henning Thielemann > wrote: >> >> I want to write a Haskell interface to a C library that provides two data >> structures A and B, where B is contained in A. That is, if A is freed, then >> B is automatically freed, too. >> > I don't quite understand. Do A and B live in the same buffer and problem in > keeping buffer alive as long as there're pointers to A or B. If so > it's easy problem > and vector solves exactly this problem for storable vectors. Check > implementation > of basicUnsafeSlice. Idea is to share finalizer between ForeignPtr Sharing finalizer between ForeignPtr A and ForeignPtr B might indeed be a solution. It would require dependence on the ghc package (and thus GHC), though. > If they live in different buffers and B should not be collected as > long as A lives... This would be true in another situation, where I create a finalizer by the LLVM-JIT and need to finalize the finalizer's code if it is no longer needed. https://ghc.haskell.org/trac/ghc/ticket/12547