Movatterモバイル変換
[0]ホーム
{-# LANGUAGE CPP #-}{-# LANGUAGE ExistentialQuantification #-}{-# LANGUAGE MagicHash #-}{-# LANGUAGE UnboxedTuples #-}------------------------------------------------------------------------------- |-- Module : GHC.StaticPtr-- Copyright : (C) 2014 I/O Tweag-- License : see libraries/base/LICENSE---- Maintainer : cvs-ghc@haskell.org-- Stability : internal-- Portability : non-portable (GHC Extensions)---- Symbolic references to values.---- References to values are usually implemented with memory addresses, and this-- is practical when communicating values between the different pieces of a-- single process.---- When values are communicated across different processes running in possibly-- different machines, though, addresses are no longer useful since each-- process may use different addresses to store a given value.---- To solve such concern, the references provided by this module offer a key-- that can be used to locate the values on each process. Each process maintains-- a global table of references which can be looked up with a given key. This-- table is known as the Static Pointer Table. The reference can then be-- dereferenced to obtain the value.---- The various communicating processes need to aggree on the keys used to refer-- to the values in the Static Pointer Table, or lookups will fail. Only-- processes launched from the same program binary are guaranteed to use the-- same set of keys.-------------------------------------------------------------------------------moduleGHC.StaticPtr(StaticPtr,deRefStaticPtr,StaticKey,staticKey,unsafeLookupStaticPtr,StaticPtrInfo(..),staticPtrInfo,staticPtrKeys,IsStatic(..))whereimportForeign.C.Types(CInt(..))importForeign.Marshal(allocaArray,peekArray,withArray)importForeign.Ptr(castPtr)importGHC.Exts(addrToAny#)importGHC.Ptr(Ptr(..),nullPtr)importGHC.Fingerprint(Fingerprint(..))importGHC.PrimimportGHC.Word(Word64(..))#include "MachDeps.h"-- | A reference to a value of type 'a'.#if WORD_SIZE_IN_BITS < 64dataStaticPtra=StaticPtrWord64#Word64#-- The flattened Fingerprint is-- convenient in the compiler.StaticPtrInfoa#elsedataStaticPtra=StaticPtrWord#Word#StaticPtrInfoa#endif-- | Dereferences a static pointer.deRefStaticPtr::StaticPtra->adeRefStaticPtr(StaticPtr___v)=v-- | A key for `StaticPtrs` that can be serialized and used with-- 'unsafeLookupStaticPtr'.typeStaticKey=Fingerprint-- | The 'StaticKey' that can be used to look up the given 'StaticPtr'.staticKey::StaticPtra->StaticKeystaticKey(StaticPtrw0w1__)=Fingerprint(W64#w0)(W64#w1)-- | Looks up a 'StaticPtr' by its 'StaticKey'.---- If the 'StaticPtr' is not found returns @Nothing@.---- This function is unsafe because the program behavior is undefined if the type-- of the returned 'StaticPtr' does not match the expected one.--unsafeLookupStaticPtr::StaticKey->IO(Maybe(StaticPtra))unsafeLookupStaticPtr(Fingerprintw1w2)=doptr@(Ptraddr)<-withArray[w1,w2](hs_spt_lookup.castPtr)if(ptr==nullPtr)thenreturnNothingelsecaseaddrToAny#addrof(#spe#)->return(Justspe)foreignimportccallunsafehs_spt_lookup::Ptr()->IO(Ptra)-- | A class for things buildable from static pointers.classIsStaticpwherefromStaticPtr::StaticPtra->pa-- | @since 4.9.0.0instanceIsStaticStaticPtrwherefromStaticPtr=id-- | Miscelaneous information available for debugging purposes.dataStaticPtrInfo=StaticPtrInfo{-- | Package key of the package where the static pointer is definedspInfoUnitId::String-- | Name of the module where the static pointer is defined,spInfoModuleName::String-- | Source location of the definition of the static pointer as a-- @(Line, Column)@ pair.,spInfoSrcLoc::(Int,Int)}derivingShow-- ^ @since 4.8.0.0-- | 'StaticPtrInfo' of the given 'StaticPtr'.staticPtrInfo::StaticPtra->StaticPtrInfostaticPtrInfo(StaticPtr__n_)=n-- | A list of all known keys.staticPtrKeys::IO[StaticKey]staticPtrKeys=dokeyCount<-hs_spt_key_countallocaArray(fromIntegralkeyCount)$\p->docount<-hs_spt_keyspkeyCountpeekArray(fromIntegralcount)p>>=mapM(\pa->peekArray2pa>>=\[w1,w2]->return$Fingerprintw1w2){-# NOINLINEstaticPtrKeys#-}foreignimportccallunsafehs_spt_key_count::IOCIntforeignimportccallunsafehs_spt_keys::Ptra->CInt->IOCInt
[8]ページ先頭