{-# LINE 1 "System\\Win32\\FileMapping.hsc" #-}{-# LINE 2 "System\\Win32\\FileMapping.hsc" #-}{-# LANGUAGE Safe #-}{-# LINE 6 "System\\Win32\\FileMapping.hsc" #-}----------------------------------------------------------------------------- -- | -- Module : System.Win32.FileMapping -- Copyright : (c) Esa Ilari Vuokko, 2006 -- License : BSD-style (see the file LICENSE) -- -- Maintainer : Esa Ilari Vuokko <ei@vuokko.info> -- Stability : provisional -- Portability : portable -- -- A collection of FFI declarations for interfacing with Win32 mapped files. -- ----------------------------------------------------------------------------- moduleSystem.Win32.FileMapping(mapFile,MappedObject(..),withMappedFile,withMappedArea-- * Enums -- ** Section protection flags ,ProtectSectionFlags,sEC_COMMIT,sEC_IMAGE,sEC_NOCACHE,sEC_RESERVE-- ** Access falgs ,FileMapAccess,fILE_MAP_ALL_ACCESS,fILE_MAP_COPY,fILE_MAP_READ,fILE_MAP_WRITE,fILE_SHARE_WRITE-- * Mapping files ,createFileMapping,openFileMapping,mapViewOfFileEx,mapViewOfFile,unmapViewOfFile)whereimportSystem.Win32.FileMapping.InternalimportSystem.Win32.Types(HANDLE,BOOL,SIZE_T,withTString,failIf,failIfNull,DDWORD,ddwordToDwords,iNVALID_HANDLE_VALUE)importSystem.Win32.MemimportSystem.Win32.FileimportSystem.Win32.InfoimportControl.Exception(mask_,bracket)importForeign(Ptr,nullPtr,plusPtr,maybeWith,ForeignPtr,newForeignPtr) #include "windows_cconv.h" --------------------------------------------------------------------------- -- Derived functions --------------------------------------------------------------------------- -- | Maps file fully and returns ForeignPtr and length of the mapped area. -- The mapped file is opened read-only and shared reading. mapFile::FilePath->IO(ForeignPtra,Int)mapFilepath=dobracket(createFilepathgENERIC_READfILE_SHARE_READNothingoPEN_EXISTINGfILE_ATTRIBUTE_NORMALNothing)(closeHandle)$\fh->bracket(createFileMapping(Justfh)pAGE_READONLY0Nothing)(closeHandle)$\fm->dofi<-getFileInformationByHandlefhfp<-mask_$doptr<-mapViewOfFilefmfILE_MAP_READ00newForeignPtrc_UnmapViewOfFileFinaliserptrreturn(fp,fromIntegral$bhfiSizefi)-- | Opens an existing file and creates mapping object to it. withMappedFile::FilePath-- ^ Path ->Bool-- ^ Write? (False = read-only) ->MaybeBool-- ^ Sharing mode, no sharing, share read, share read+write ->(Integer->MappedObject->IOa)-- ^ Action ->IOawithMappedFilepathwriteshareact=bracket(createFilepathaccessshare'NothingoPEN_EXISTINGfILE_ATTRIBUTE_NORMALNothing)(closeHandle)$\fh->bracket(createFileMapping(Justfh)page0Nothing)(closeHandle)$\fm->dobhfi<-getFileInformationByHandlefhact(fromIntegral$bhfiSizebhfi)(MappedObjectfhfmmapaccess)whereaccess=ifwritethengENERIC_READ+gENERIC_WRITEelsegENERIC_READpage=ifwritethenpAGE_READWRITEelsepAGE_READONLYmapaccess=ifwritethenfILE_MAP_ALL_ACCESSelsefILE_MAP_READshare'=caseshareofNothing->fILE_SHARE_NONEJustFalse->fILE_SHARE_READJustTrue->fILE_SHARE_READ+fILE_SHARE_WRITE-- | Maps area into memory. withMappedArea::MappedObject-- ^ Mapped object, from withMappedFile ->Integer-- ^ Position in file ->Int-- ^ Size of mapped area ->(Ptra->IOb)-- ^ Action ->IObwithMappedArea(MappedObject_mhaccess)possizeact=dosi<-getSystemInfoletgran=fromIntegral$siAllocationGranularitysi(blocks,offset)=divModposgranstart=blocks*gransize'=fromIntegral$size+fromIntegral(pos-start)bracket(mapViewOfFileExmhaccess(fromIntegralstart)size'nullPtr)(unmapViewOfFile)(act.flipplusPtr(fromIntegraloffset))--------------------------------------------------------------------------- -- API in Haskell --------------------------------------------------------------------------- createFileMapping::MaybeHANDLE->ProtectFlags->DDWORD->MaybeString->IOHANDLEcreateFileMappingmhflagsmosizename=maybeWithwithTStringname$\c_name->failIf(==nullPtr)"createFileMapping: CreateFileMapping"$c_CreateFileMappinghandlenullPtrflagsmoshimoslowc_namewhere(moshi,moslow)=ddwordToDwordsmosizehandle=maybeiNVALID_HANDLE_VALUEidmhopenFileMapping::FileMapAccess->BOOL->MaybeString->IOHANDLEopenFileMappingaccessinheritname=maybeWithwithTStringname$\c_name->failIf(==nullPtr)"openFileMapping: OpenFileMapping"$c_OpenFileMappingaccessinheritc_namemapViewOfFileEx::HANDLE->FileMapAccess->DDWORD->SIZE_T->Ptra->IO(Ptrb)mapViewOfFileExhaccessoffsetsizebase=failIfNull"mapViewOfFile(Ex): c_MapViewOfFileEx"$c_MapViewOfFileExhaccessohiolowsizebasewhere(ohi,olow)=ddwordToDwordsoffsetmapViewOfFile::HANDLE->FileMapAccess->DDWORD->SIZE_T->IO(Ptra)mapViewOfFilehaos=mapViewOfFileExhaosnullPtrunmapViewOfFile::Ptra->IO()unmapViewOfFilev=c_UnmapViewOfFilev>>return()