X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fdocs%2Fusers_guide%2Fffi-chap.sgml;h=99d21a3cc19b23274074faa323bc9920a83d19f7;hb=eda14cd313f3fe5f9f1a08d801c0d60d17093b98;hp=a92689f2dcc5b90dfea6be2d736849e8e1a8d74d;hpb=94fb7e7288a47f7b411ffd4bfa432f94f96f197e;p=ghc-hetmet.git diff --git a/ghc/docs/users_guide/ffi-chap.sgml b/ghc/docs/users_guide/ffi-chap.sgml index a92689f..99d21a3 100644 --- a/ghc/docs/users_guide/ffi-chap.sgml +++ b/ghc/docs/users_guide/ffi-chap.sgml @@ -11,12 +11,6 @@ - The routines hs_init(), - hs_exit(), and hs_set_argv() from - Chapter 6.1 of the Addendum are not supported yet. - - - Syntactic forms and library functions proposed in earlier versions of the FFI are still supported for backwards compatibility. @@ -42,15 +36,6 @@ resulting code. - Arrays - - The types ByteArray and - MutableByteArray may be used as basic foreign types - (see FFI Addendum, Section 3.2). In C land, they map to - (char *). - - - Unboxed types The following unboxed types may be used as basic foreign types @@ -116,7 +101,7 @@ extern HsInt foo(HsInt a0); invoke foo() from C, just #include "Foo_stub.h" and call foo(). - + Using your own <literal>main()</literal> Normally, GHC's runtime system provides a @@ -306,6 +291,85 @@ to be inlined across modules, use the command-line and package-configuration + + + Memory Allocation + + The FFI libraries provide several ways to allocate memory + for use with the FFI, and it isn't always clear which way is the + best. This decision may be affected by how efficient a + particular kind of allocation is on a given compiler/platform, + so this section aims to shed some light on how the different + kinds of allocation perform with GHC. + + + + alloca and friends + + Useful for short-term allocation when the allocation + is intended to scope over a given IO + compuatation. This kind of allocation is commonly used + when marshalling data to and from FFI functions. + + In GHC, alloca is implemented + using MutableByteArray#, so allocation + and deallocation are fast: much faster than C's + malloc/free, but not quite as fast as + stack allocation in C. Use alloca + whenever you can. + + + + + mallocForeignPtr + + Useful for longer-term allocation which requires + garbage collection. If you intend to store the pointer to + the memory in a foreign data structure, then + mallocForeignPtr is + not a good choice, however. + + In GHC, mallocForeignPtr is also + implemented using MutableByteArray#. + Although the memory is pointed to by a + ForeignPtr, there are no actual + finalizers involved (unless you add one with + addForeignPtrFinalizer), and the + deallocation is done using GC, so + mallocForeignPtr is normally very + cheap. + + + + + malloc/free + + If all else fails, then you need to resort to + Foreign.malloc and + Foreign.free. These are just wrappers + around the C funcitons of the same name, and their + efficiency will depend ultimately on the implementations + of these functions in your platform's C library. We + usually find malloc and + free to be significantly slower than + the other forms of allocation above. + + + + + Foreign.Marhsal.Pool + + Pools are currently implemented using + malloc/free, so while they might be a + more convenient way to structure your memory allocation + than using one of the other forms of allocation, they + won't be any more efficient. We do plan to provide an + improved-performance implementaiton of Pools in the + future, however. + + + +