comments
This replaces an earlier version I posted as a Class.
File Mapping allows you to access a file as a block of memory.
Instead of read/write to a file; you read/write to a block of memory.
This gives you file access at memory access speed.
Any changes made to the memory, reflect through to the file.
Windows takes care of the file.
This process bypasses a lot of overhead incurred using standard file operations.
Windows handles everything on a very low level.
You can use Dim At to write to an array in the file.
This replaces an earlier version I posted as a Class.
File Mapping allows you to access a file as a block of memory.
Instead of read/write to a file; you read/write to a block of memory.
This gives you file access at memory access speed.
Any changes made to the memory, reflect through to the file.
Windows takes care of the file.
This process bypasses a lot of overhead incurred using standard file operations.
Windows handles everything on a very low level.
You can use Dim At to write to an array in the file.
Code:
' PB 5/9 'FileMap2.inc ' ' WIN32API File Mapping Module ' !!! fastest file access !!! ' ' whole file mapped into memory using API File Mapping/Fiew functions ' file = read/write locked ' ' - file access = memory access speed ' - read/write to system memory, instead of the file ' - modifications reflect through to file ' ' Win32 Programmer's Reference: ' ' "File mapping is the association of a file's contents with a portion of the virtual address space of ' a process." ' "Processes read from and write to the file view using pointers, just as they would with dynamically ' allocated memory." ' ' mapped memory start = FMap_MemStart() ' mapped memory size = FMap_MemSize() - same size as file ' ' use FMap_Resize() to change file size while open ' you need to reestablish any pointer addresses after FMap_Resize() ' ' ReDim a() AT FMap_MemStart(): work directly with file-based array - see sample ' 'note: replaces previous version I did as a class ' 'had problem couldn't find ' 'implemented with PB file functions so that everything is ONE based ' ' instead of a mix of one/zero based file, arrays, ...etc ' 'public domain, use at your own risk ' ' #If Not %Def(%WINAPI) #Include Once "WIN32API.INC" #EndIf ' $FMap_Err_BadFileName = "File Mapping: bad file name" $FMap_Err_FileExist = "File Mapping: file exist" $FMap_Err_FileNotFound = "File Mapping: file not found" $FMap_Err_ZeroFileSize = "File Mapping: zero file size" $FMap_Err_FileCreationFailed = "File Mapping: file creation failed" $FMap_Err_FileOpenFailed = "File Mapping: file open failed" $FMap_Err_FileNotOpen = "File Mapping: file not open" $FMap_Err_FileMappingFailed = "File Mapping: file mapping failed" $FMap_Err_FileMappingNotOpen = "File Mapping: file mapping not open" $FMap_Err_FileViewFailed = "File Mapping: file view failed" $FMap_Err_FileViewNotOpen = "File Mapping: file view not open" ' %FMap_FileAttr_OpenStatus = 0 %FMap_FileAttr_WinHandle = 2 ' Macro FMap_ExitFalse(test, procedure, msg) If test Then Else #Debug Print FuncName$ +": "+ msg t.isErr = %TRUE t.errMsg = msg Exit procedure End If End Macro Macro FMapExitTrue(test, procedure, msg) If test Then #Debug Print FuncName$ +": "+ msg t.isErr = %TRUE t.errMsg = msg Exit procedure End If End Macro Macro FMap_GoFalse(test, MARKER, msg) If test Then Else #Debug Print FuncName$ +": "+ msg t.isErr = %TRUE t.errMsg = msg GoTo MARKER End If End Macro Macro FMap_GoTrue(test, MARKER, msg) If test Then #Debug Print FuncName$ +": "+ msg t.isErr = %TRUE t.errMsg = msg GoTo MARKER End If End Macro ' Macro FMap_hWinFile = FileAttr(t.hFile, %FMap_FileAttr_WinHandle) 'window's file handle ' ' ' Type FMapT isErr As Long errMsg As Asciiz * 256 hFile As Long hWinFile As Long hMap As Long hView As Long End Type ' ' ' Function FMap_Create(t As FMapT, ByVal file As String, ByVal fileSize As Long) As Long 'create new file - fileSize length 'open file for use ' Note: can't create empty file 'True/False success t.isErr = 0 t.hFile = 0 t.hWinFile = 0 t.hMap = 0 t.hView = 0 FMapExitTrue(IsFile(file), Function, $FMap_Err_FileExist) FMapExitTrue(file = "", Function, $FMap_Err_BadFileName) FMapExitTrue(fileSize < 1, Function, $FMap_Err_ZeroFileSize) Try t.hFile = FreeFile Open file For Binary As t.hFile Seek t.hFile, fileSize + 1 SetEof t.hFile t.hWinFile = FMap_hWinFile FMap_GoFalse(FMap_OpenMap(t), CLOSE_STUFF, t.errMsg) FMap_GoFalse(FMap_OpenView(t), CLOSE_STUFF, t.errMsg) ' Function = %TRUE Exit Function ' CLOSE_STUFF: If t.hView Then FMap_CloseView t If t.hMap Then FMap_CloseView t If FileAttr(t.hFile, %FMap_FileAttr_OpenStatus) Then Close t.hFile t.hFile = 0 t.hWinFile = 0 t.hMap = 0 t.hView = 0 Catch If FileAttr(t.hFile, %FMap_FileAttr_OpenStatus) Then Close t.hFile t.isErr = %TRUE t.errMsg = $FMap_Err_FileCreationFailed t.hFile = 0 t.hWinFile = 0 End Try End Function ' Function FMap_Open(t As FMapT, ByVal file As String) As Long 'open existing file - True/False success - can't be empty file Local pz As Asciiz Ptr ' t.isErr = 0 t.hFile = 0 t.hMap = 0 t.hView = 0 FMap_ExitFalse(IsFile(file), Function, $FMap_Err_FileNotFound) Try t.hFile = FreeFile Open file For Binary As t.hFile If Lof(t.hFile) < 1 Then Close t.hFile t.hFile = 0 t.isErr = %TRUE t.errMsg = $FMap_Err_ZeroFileSize Else t.hWinFile = FMap_hWinFile FMap_GoFalse(FMap_OpenMap(t), CLOSE_STUFF, t.errMsg) FMap_GoFalse(FMap_OpenView(t), CLOSE_STUFF, t.errMsg) ' Function = %TRUE Exit Function ' CLOSE_STUFF: If t.hView Then FMap_CloseView t If t.hMap Then FMap_CloseView t If FileAttr(t.hFile, %FMap_FileAttr_OpenStatus) Then Close t.hFile t.hFile = 0 t.hWinFile = 0 t.hMap = 0 t.hView = 0 End If Catch If FileAttr(t.hFile, %FMap_FileAttr_OpenStatus) Then Close t.hFile t.isErr = %TRUE t.errMsg = $FMap_Err_FileOpenFailed t.hFile = 0 t.hWinFile = 0 End Try End Function ' Sub FMap_Close(t As FMapT) 'close file t.isErr = 0 FMap_ExitFalse(t.hFile, Sub, $FMap_Err_FileNotOpen) FMap_CloseView t FMap_CloseMap t Close t.hFile t.hFile = 0 t.hMap = 0 t.hView = 0 End Sub ' Function FMap_MemStart(t As FMapT) As Long 'return memory address to beginning of File View ' you can read/alter memory from MemStart() to MemEnd() ' whatever changes are made to memory will reflect through to file FMap_ExitFalse(t.hFile, Function, $FMap_Err_FileNotOpen) FMap_ExitFalse(t.hMap, Function, $FMap_Err_FileMappingNotOpen) FMap_ExitFalse(t.hView, Function, $FMap_Err_FileViewNotOpen) Function = t.hView End Function ' Function FMap_MemSize(t As FMapT) As Long 'get size of File View ' you can read/alter memory at MemStart() for MemCount() -1 bytes FMap_ExitFalse(t.hFile, Function, $FMap_Err_FileNotOpen) FMap_ExitFalse(t.hMap, Function, $FMap_Err_FileMappingNotOpen) FMap_ExitFalse(t.hView, Function, $FMap_Err_FileViewNotOpen) Function = Lof(t.hFile) End Function ' Sub FMap_Resize(t As FMapT, ByVal newFileSize As Long) 'change size of file ' newFileSize can't be zero t.isErr = 0 FMap_ExitFalse(t.hFile, Sub, $FMap_Err_FileNotOpen) FMapExitTrue(newFileSize < 1, Sub, $FMap_Err_ZeroFileSize) FMap_CloseView t : If t.isErr Then Exit Sub FMap_CloseMap t : If t.isErr Then Exit Sub Seek t.hFile, newFileSize + 1 SetEof t.hFile FMap_ExitFalse(FMap_OpenMap(t), Sub, t.errMsg) FMap_ExitFalse(FMap_OpenView(t), Sub, t.errMsg) End Sub ' Function FMap_LOF(t As FMapT) As Long 'get length of file t.isErr = 0 FMap_ExitFalse(t.hFile, Function, $FMap_Err_FileNotOpen) Function = Lof(t.hFile) End Function ' Function FMap_IsErr(t As FMapT) As Long 'True/False if last operation caused an error Function = t.isErr End Function ' Function FMap_ErrMsg(t As FMapT) As String 'get error message If t.isErr Then Function = t.errMsg End Function ' ' -------------------------------------- ' internal use ' -------------------------------------- ' Function FMap_OpenMap(t As FMapT) As Long t.isErr = 0 t.hView = 0 FMap_ExitFalse(t.hWinFile, Function, $FMap_Err_FileNotOpen) t.hMap = CreateFileMapping(t.hWinFile, ByVal 0, %PAGE_READWRITE, 0, 0, ByVal 0) FMap_ExitFalse(t.hMap, Function, $FMap_Err_FileMappingFailed) Function = %TRUE End Function ' Sub FMap_CloseMap(t As FMapT) t.isErr = 0 t.hView = 0 FMap_ExitFalse(t.hWinFile, Sub, $FMap_Err_FileNotOpen) FMap_ExitFalse(t.hMap, Sub, $FMap_Err_FileMappingNotOpen) CloseHandle(t.hMap) t.hMap = 0 End Sub ' Function FMap_OpenView(t As FMapT) As Long t.isErr = 0 t.hView = 0 FMap_ExitFalse(t.hWinFile, Function, $FMap_Err_FileNotOpen) FMap_ExitFalse(t.hMap, Function, $FMap_Err_FileMappingNotOpen) t.hView = MapViewOfFile(t.hMap, %FILE_MAP_ALL_ACCESS, 0, 0, 0) FMap_ExitFalse(t.hView, Function, $FMap_Err_FileViewFailed) Function = %TRUE End Function ' Sub FMap_CloseView(t As FMapT) t.isErr = 0 FMap_ExitFalse(t.hWinFile, Sub, $FMap_Err_FileNotOpen) FMap_ExitFalse(t.hView, Sub, $FMap_Err_FileViewNotOpen) UnmapViewOfFile(t.hView) t.hView = 0 End Sub '
Comment