wiki:ForeignData

Version 2 (modified by john@…, 8 years ago) (diff)

--

Allocating space in the bss and data segment from haskell

One is forced to use an external C file to allocate data in the bss or data segment even though no code at all will be output and the object file will simply contain a linker directive to allocate some space. This is a deficiency in the current FFI spec.

Proposal (experimental in jhc)

allow declarations of the form

foreign space [const] <n> :: Ptr <type>

where n is the number of elements to allocate (default 1) and type is a basic type or a renaming thereof.

the space allocated will be n*sizeof type for the sizeof as specified by the Storable class. user defined types (other than simple newtype or type renaming of built in types) may not be used.

if the type is 'forall a . Ptr a' then the size will be assumed to be one byte.

if 'const' is specified then that is an assertion the contents of memory there will never change and the haskell compiler may make use of that and the data may be allocated in the shared among processes, read-only data segment.

initialized data

initialized data is trickier, a possible syntax is

foreign space [const] <n> :: Ptr <type> = constant

where constant may be one of 


an initialized list [ 0, 1, 2, ...]

a "string" to be interpreted as utf8 or unicode code points depending on what type of pointer it is assigned to.