Movatterモバイル変換


[0]ホーム

URL:


{-# LANGUAGE Trustworthy #-}{-# LANGUAGE CPP, NoImplicitPrelude, ScopedTypeVariables, BangPatterns #-}------------------------------------------------------------------------------- |-- Module      :  Foreign.Storable-- Copyright   :  (c) The FFI task force 2001-- License     :  see libraries/base/LICENSE---- Maintainer  :  ffi@haskell.org-- Stability   :  provisional-- Portability :  portable---- The module "Foreign.Storable" provides most elementary support for-- marshalling and is part of the language-independent portion of the-- Foreign Function Interface (FFI), and will normally be imported via-- the "Foreign" module.-------------------------------------------------------------------------------moduleForeign.Storable(Storable(sizeOf,alignment,peekElemOff,pokeElemOff,peekByteOff,pokeByteOff,peek,poke))where#include "MachDeps.h"#include "HsBaseConfig.h"importGHC.StorableimportGHC.Stable(StablePtr)importGHC.NumimportGHC.IntimportGHC.WordimportGHC.PtrimportGHC.BaseimportGHC.Fingerprint.TypeimportData.BitsimportGHC.Real{- |The member functions of this class facilitate writing values ofprimitive types to raw memory (which may have been allocated with theabove mentioned routines) and reading values from blocks of rawmemory.  The class, furthermore, includes support for computing thestorage requirements and alignment restrictions of storable types.Memory addresses are represented as values of type @'Ptr' a@, for some@a@ which is an instance of class 'Storable'.  The type argument to'Ptr' helps provide some valuable type safety in FFI code (you can\'tmix pointers of different types without an explicit cast), whilehelping the Haskell type system figure out which marshalling method isneeded for a given pointer.All marshalling between Haskell and a foreign language ultimatelyboils down to translating Haskell data structures into the binaryrepresentation of a corresponding data structure of the foreignlanguage and vice versa.  To code this marshalling in Haskell, it isnecessary to manipulate primitive data types stored in unstructuredmemory blocks.  The class 'Storable' facilitates this manipulation onall types for which it is instantiated, which are the standard basictypes of Haskell, the fixed size @Int@ types ('Int8', 'Int16','Int32', 'Int64'), the fixed size @Word@ types ('Word8', 'Word16','Word32', 'Word64'), 'StablePtr', all types from "Foreign.C.Types",as well as 'Ptr'.-}classStorableawhere{-# MINIMALsizeOf,alignment,(peek|peekElemOff|peekByteOff),(poke|pokeElemOff|pokeByteOff)#-}sizeOf::a->Int-- ^ Computes the storage requirements (in bytes) of the argument.-- The value of the argument is not used.alignment::a->Int-- ^ Computes the alignment constraint of the argument.  An-- alignment constraint @x@ is fulfilled by any address divisible-- by @x@.  The value of the argument is not used.peekElemOff::Ptra->Int->IOa-- ^       Read a value from a memory area regarded as an array--         of values of the same kind.  The first argument specifies--         the start address of the array and the second the index into--         the array (the first element of the array has index--         @0@).  The following equality holds,---- > peekElemOff addr idx = IOExts.fixIO $ \result ->-- >   peek (addr `plusPtr` (idx * sizeOf result))----         Note that this is only a specification, not--         necessarily the concrete implementation of the--         function.pokeElemOff::Ptra->Int->a->IO()-- ^       Write a value to a memory area regarded as an array of--         values of the same kind.  The following equality holds:---- > pokeElemOff addr idx x =-- >   poke (addr `plusPtr` (idx * sizeOf x)) xpeekByteOff::Ptrb->Int->IOa-- ^       Read a value from a memory location given by a base--         address and offset.  The following equality holds:---- > peekByteOff addr off = peek (addr `plusPtr` off)pokeByteOff::Ptrb->Int->a->IO()-- ^       Write a value to a memory location given by a base--         address and offset.  The following equality holds:---- > pokeByteOff addr off x = poke (addr `plusPtr` off) xpeek::Ptra->IOa-- ^ Read a value from the given memory location.----  Note that the peek and poke functions might require properly--  aligned addresses to function correctly.  This is architecture--  dependent; thus, portable code should ensure that when peeking or--  poking values of some type @a@, the alignment--  constraint for @a@, as given by the function--  'alignment' is fulfilled.poke::Ptra->a->IO()-- ^ Write the given value to the given memory location.  Alignment-- restrictions might apply; see 'peek'.-- circular default instancespeekElemOff=peekElemOff_undefinedwherepeekElemOff_::a->Ptra->Int->IOapeekElemOff_undefptroff=peekByteOffptr(off*sizeOfundef)pokeElemOffptroffval=pokeByteOffptr(off*sizeOfval)valpeekByteOffptroff=peek(ptr`plusPtr`off)pokeByteOffptroff=poke(ptr`plusPtr`off)peekptr=peekElemOffptr0pokeptr=pokeElemOffptr0-- | @since 4.9.0.0instanceStorable()wheresizeOf_=0alignment_=1peek_=return()poke__=return()-- System-dependent, but rather obvious instances-- | @since 2.01instanceStorableBoolwheresizeOf_=sizeOf(undefined::HTYPE_INT)alignment_=alignment(undefined::HTYPE_INT)peekElemOffpi=liftM(/=(0::HTYPE_INT))$peekElemOff(castPtrp)ipokeElemOffpix=pokeElemOff(castPtrp)i(ifxthen1else0::HTYPE_INT)#define STORABLE(T,size,align,read,write)       \instance Storable (T) where {                   \    sizeOf    _ = size;                         \    alignment _ = align;                        \    peekElemOff = read;                         \    pokeElemOff = write }-- | @since 2.01STORABLE(Char,SIZEOF_INT32,ALIGNMENT_INT32,readWideCharOffPtr,writeWideCharOffPtr)-- | @since 2.01STORABLE(Int,SIZEOF_HSINT,ALIGNMENT_HSINT,readIntOffPtr,writeIntOffPtr)-- | @since 2.01STORABLE(Word,SIZEOF_HSWORD,ALIGNMENT_HSWORD,readWordOffPtr,writeWordOffPtr)-- | @since 2.01STORABLE((Ptra),SIZEOF_HSPTR,ALIGNMENT_HSPTR,readPtrOffPtr,writePtrOffPtr)-- | @since 2.01STORABLE((FunPtra),SIZEOF_HSFUNPTR,ALIGNMENT_HSFUNPTR,readFunPtrOffPtr,writeFunPtrOffPtr)-- | @since 2.01STORABLE((StablePtra),SIZEOF_HSSTABLEPTR,ALIGNMENT_HSSTABLEPTR,readStablePtrOffPtr,writeStablePtrOffPtr)-- | @since 2.01STORABLE(Float,SIZEOF_HSFLOAT,ALIGNMENT_HSFLOAT,readFloatOffPtr,writeFloatOffPtr)-- | @since 2.01STORABLE(Double,SIZEOF_HSDOUBLE,ALIGNMENT_HSDOUBLE,readDoubleOffPtr,writeDoubleOffPtr)-- | @since 2.01STORABLE(Word8,SIZEOF_WORD8,ALIGNMENT_WORD8,readWord8OffPtr,writeWord8OffPtr)-- | @since 2.01STORABLE(Word16,SIZEOF_WORD16,ALIGNMENT_WORD16,readWord16OffPtr,writeWord16OffPtr)-- | @since 2.01STORABLE(Word32,SIZEOF_WORD32,ALIGNMENT_WORD32,readWord32OffPtr,writeWord32OffPtr)-- | @since 2.01STORABLE(Word64,SIZEOF_WORD64,ALIGNMENT_WORD64,readWord64OffPtr,writeWord64OffPtr)-- | @since 2.01STORABLE(Int8,SIZEOF_INT8,ALIGNMENT_INT8,readInt8OffPtr,writeInt8OffPtr)-- | @since 2.01STORABLE(Int16,SIZEOF_INT16,ALIGNMENT_INT16,readInt16OffPtr,writeInt16OffPtr)-- | @since 2.01STORABLE(Int32,SIZEOF_INT32,ALIGNMENT_INT32,readInt32OffPtr,writeInt32OffPtr)-- | @since 2.01STORABLE(Int64,SIZEOF_INT64,ALIGNMENT_INT64,readInt64OffPtr,writeInt64OffPtr)-- | @since 4.8.0.0instance(Storablea,Integrala)=>Storable(Ratioa)wheresizeOf_=2*sizeOf(undefined::a)alignment_=alignment(undefined::a)peekp=doq<-return$castPtrpr<-peekqi<-peekElemOffq1return(r%i)pokep(r:%i)=doq<-return$(castPtrp)pokeqrpokeElemOffq1i-- XXX: here to avoid orphan instance in GHC.Fingerprint-- | @since 4.4.0.0instanceStorableFingerprintwheresizeOf_=16alignment_=8peek=peekFingerprintpoke=pokeFingerprint-- peek/poke in fixed BIG-endian 128-bit formatpeekFingerprint::PtrFingerprint->IOFingerprintpeekFingerprintp0=doletpeekW64::PtrWord8->Int->Word64->IOWord64peekW64_0!i=returnipeekW64!p!n!i=dow8<-peekppeekW64(p`plusPtr`1)(n-1)((i`shiftL`8).|.fromIntegralw8)high<-peekW64(castPtrp0)80low<-peekW64(castPtrp0`plusPtr`8)80return(Fingerprinthighlow)pokeFingerprint::PtrFingerprint->Fingerprint->IO()pokeFingerprintp0(Fingerprinthighlow)=doletpokeW64::PtrWord8->Int->Word64->IO()pokeW64_0_=return()pokeW64p!n!i=dopokeElemOffp(n-1)(fromIntegrali)pokeW64p(n-1)(i`shiftR`8)pokeW64(castPtrp0)8highpokeW64(castPtrp0`plusPtr`8)8low

[8]ページ先頭

©2009-2025 Movatter.jp