VBcoders Browse New Submit Contact Sign In

No account? Register free

Forgot password?

Array backed by memory mapped file

Karl M. Syring  (1 Submission)   Windows API Call/Explanation   Visual Basic 3.0   Advanced   Wed 3rd February 2021

The memory for an array is allocated from a memory mapped file. This is an big advantage for huge arrays, as they will not fill the pagefile.

Assumes
The size of allocated memory is limited by the free virtual address space (1GB max on 9x, 2GB max on NTx)

Side Effects
Does IO to a file without using VB IO-functions

API Declarations
' Allocate a 2D array from a memory mapped file
' Autor: Karl M. Syring ([email protected])
' inspired by
' http://www.vbaccelerator.com/codelib/gfx/dibsect.htm
' http://www.vb2themax.com/HtmlDoc.asp?Table=Books&ID=1501&Page=3
' http://www.devx.com/premier/mgznarch/vbpj/2000/07jul00/bb0007/bb0007.asp
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (Ptr() As Any) As Long
' Memory mapping API-function
Private Const GENERIC_READ = &H80000000, GENERIC_WRITE = &H40000000
Private Const CREATE_ALWAYS = 2, OPEN_ALWAYS = 4, FILE_ATTRIBUTE_NORMAL = &H80
Private Const PAGE_READWRITE = 4, FILE_MAP_ALL_ACCESS = &HF001F
Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function CreateFileMapping Lib "kernel32" Alias "CreateFileMappingA" (ByVal hFile As Long, ByVal lpFileMappigAttributes As Long, ByVal flProtect As Long, ByVal dwMaximumSizeHigh As Long, ByVal dwMaximumSizeLow As Long, ByVal lpName As String) As Long
Public Declare Function OpenFileMapping Lib "kernel32" Alias "OpenFileMappingA" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As String) As Long
Public Declare Function MapViewOfFile Lib "kernel32" (ByVal hFileMappingObject As Long, ByVal dwDesiredAccess As Long, ByVal dwFileOffsetHigh As Long, ByVal dwFileOffsetLow As Long, ByVal dwNumberOfBytesToMap As Long) As Long
Public Declare Function UnmapViewOfFile Lib "kernel32" (ByVal lpBaseAddress As Long) As Long

Rate Array backed by memory mapped file (3(3 Vote))
Array backed by memory mapped file.bas

Array backed by memory mapped file Comments

No comments yet — be the first to post one!

Post a Comment

0/1000 characters