summaryrefslogtreecommitdiff
path: root/libraries/ghci/GHCi/StaticPtrTable.hs
blob: 623e8ef30756e98de21b041f0ba4e824f3fa0982 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ForeignFunctionInterface #-}

module GHCi.StaticPtrTable ( sptAddEntry ) where

import Prelude -- See note [Why do we import Prelude here?]
import Data.Word
import Foreign
import GHC.Fingerprint
import GHCi.RemoteTypes

-- | Used by GHCi to add an SPT entry for a set of interactive bindings.
sptAddEntry :: Fingerprint -> HValue -> IO ()
sptAddEntry (Fingerprint a b) (HValue x) = do
    -- We own the memory holding the key (fingerprint) which gets inserted into
    -- the static pointer table and can't free it until the SPT entry is removed
    -- (which is currently never).
    fpr_ptr <- newArray [a,b]
    sptr <- newStablePtr x
    ent_ptr <- malloc
    poke ent_ptr (castStablePtrToPtr sptr)
    spt_insert_stableptr fpr_ptr ent_ptr

foreign import ccall "hs_spt_insert_stableptr"
    spt_insert_stableptr :: Ptr Word64 -> Ptr (Ptr ()) -> IO ()