Allocation & Marshalling Question (again)

Simon Marlow simonmar at microsoft.com
Wed May 28 05:32:27 EDT 2003


 
> Judging by the silence that greeted my last posts re this a 
> couple of weeks
> ago I suspect this is of no interest to anyone but me :-(
> 
> An extra couple of weeks cogitation on this hasn't really 
> changed MHO much
> (other than I'm no longer so pessimistic about the safety of 
> my <+ operator). 
> 
> Seeing as the FFI seems to be close to finalization I thought I'd have
> another go. So excuse me if this seems half baked, but am I 
> the only one who
> thinks the type signatures of utilities like this..
>  allocaBytes :: Int -> (Ptr a -> IO b) -> IO b
> ..are rather inconvenient?
> 
> Wouldn't..
>  allocaBytes :: Int -> (Ptr a -> b) -> b
> ..be more useful?

I haven't thought about this too hard, but it seems "technically
difficult" if not "technically impossible".

In an implementation which uses a foreign call to malloc to implement
allocaBytes, then there has to be a reliable way to ensure that the
memory is deallocated later.  If we're not in the IO monad, then I can't
see how to do this.

Even in GHC where we use garbage collected memory instead of malloc, we
still make use of the IO monad to control the lifetime of the allocated
memory.

Cheers,
	Simon



More information about the FFI mailing list