Skip to content

Instantly share code, notes, and snippets.

@yuga
Last active January 2, 2016 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuga/8297657 to your computer and use it in GitHub Desktop.
Save yuga/8297657 to your computer and use it in GitHub Desktop.

GHC管理のメモリを表現する型の違い

        | Immutable   | Mutable           
--------|-------------|-------------------
Boxed   | Array#      | MutableArray#
--------|-------------|-------------------
Unboxed | ByteArray#  | MutableByteArray#
------------------------------------------

コンテナとメモリの関係

         |           | Boxed                    | Unboxed
---------|-----------|--------------------------|--------------------------------
Unpinned | Pure      | Array   (Arary#)         | UArray     (ByteArray#)
         |-----------|--------------------------|--------------------------------
         | IO monad  | IOArray (MutableArray#)  | IOUArray   (MutableByteArray#)
         |-----------|--------------------------|--------------------------------
         | ST monad  | STArray (MutableArray#)  | STUArray   (MutableByteArray#)
---------|-----------|--------------------------|--------------------------------
Pinned   | Pure      |             -            | ByteString (MutableByteArray#)
         |           |                          | *1 *2
         |-----------|--------------------------|--------------------------------
         | IO monad  |             -            | Storable   (MutableByteArray#)
         |           |                          | *2
---------------------------------------------------------------------------------

*1 ポインタアクセス時はIO monad
*2 ForeignPtrなどで管理されていればGHC管理外のメモリでも利用可能

Haskellコードからの生メモリ取得関数

  • C言語の標準ライブラリで管理されるメモリ
    • Local allocation (ただしGHC実装はGC時に移動しないランタイム管理メモリ)
      • Foreign.Marshal.alloca :: Storable a => (Ptr a -> IO b) -> IO b
      • Foreign.Marshal.allocaBytes :: Int -> (Ptr a -> IO b) -> IO b
      • Foreign.Marshal.allocaBytesAligned :: Int -> (Ptr a -> IO b) -> IO b
    • Dynamic allocation
      • Foreign.Marshal.malloc :: Storable a => IO (Ptr a)
      • Foreign.Marshal.mallocBytes :: Int -> IO (Ptr a)
      • Foreign.Marshal.realloc :: Storable b => Ptr a -> IO (Ptr b)
      • Foreign.Marshal.reallocBytes :: Ptr a -> Int -> IO (Ptr b)
      • Foreign.Marshal.free :: Ptr a -> IO ()
      • Foreign.Marshal.finalizerFree :: FinalizerPtr a
  • GHCのランタイムで管理されるメモリ (GC時に領域が移動する)
    • 以下のデータ型で配列の格納に使用しているが、Ptr型を公開するインターフェイスはない。
      • Boxed Array
        • Data.Array.Array i e
        • Data.Array.IOArray i e
        • Data.Array.STArray s i e
      • Unboxed Array
        • Data.Array.Unbox.UArray i e
        • Data.Array.IOUArray i e
        • Data.Array.STUArray s i e
  • GHCのランタイムで管理されるメモリ (GC時も領域が移動しない)
    • Foreign.mallocForeignPtr :: Storable a => IO (ForeignPtr a)
    • Foreign.mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
    • GHC.ForeignPtr.mallocForeignPtrAlignedBytes :: Int -> Int -> IO (ForeignPtr a)
    • GHC.ForeignPtr.mallocPlainForeignPtr :: Storable a => IO (ForeignPtr a)
    • GHC.ForeignPtr.mallocPlainForeignPtrBytes :: Int -> IO (ForeignPtr a)
    • GHC.ForeignPtr.mallocPlainForeignPtrAlignedBytes :: Int -> Int -> IO (ForeignPtr a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment