Esempio n. 1
0
        public Win32MemoryMapPager(string file,
            NativeFileAttributes options = NativeFileAttributes.Normal,
            NativeFileAccess access = NativeFileAccess.GenericRead | NativeFileAccess.GenericWrite)
        {
            _access = access;
            _fileInfo = new FileInfo(file);
            bool noData = _fileInfo.Exists == false || _fileInfo.Length == 0;
            _handle = NativeFileMethods.CreateFile(file, access,
                NativeFileShare.Read | NativeFileShare.Write | NativeFileShare.Delete, IntPtr.Zero,
                NativeFileCreationDisposition.OpenAlways, options, IntPtr.Zero);
            if (_handle.IsInvalid)
            {
                int lastWin32ErrorCode = Marshal.GetLastWin32Error();
                throw new IOException("Failed to open file storage of Win32MemoryMapPager",
                    new Win32Exception(lastWin32ErrorCode));
            }

            _fileStream = new FileStream(_handle, FileAccess.ReadWrite);

            if (noData)
            {
                NumberOfAllocatedPages = 0;
            }
            else
            {
                NumberOfAllocatedPages = _fileInfo.Length/PageSize;
                PagerState.Release();
                PagerState = CreateNewPagerState();
            }
        }
Esempio n. 2
0
        public Win32MemoryMapPager(string file,
                                   NativeFileAttributes options = NativeFileAttributes.Normal,
                                   NativeFileAccess access      = NativeFileAccess.GenericRead | NativeFileAccess.GenericWrite)
        {
            _access   = access;
            _fileInfo = new FileInfo(file);
            bool noData = _fileInfo.Exists == false || _fileInfo.Length == 0;

            _handle = NativeFileMethods.CreateFile(file, access,
                                                   NativeFileShare.Read | NativeFileShare.Write | NativeFileShare.Delete, IntPtr.Zero,
                                                   NativeFileCreationDisposition.OpenAlways, options, IntPtr.Zero);
            if (_handle.IsInvalid)
            {
                int lastWin32ErrorCode = Marshal.GetLastWin32Error();
                throw new IOException("Failed to open file storage of Win32MemoryMapPager",
                                      new Win32Exception(lastWin32ErrorCode));
            }

            _fileStream = new FileStream(_handle, FileAccess.ReadWrite);

            if (noData)
            {
                NumberOfAllocatedPages = 0;
            }
            else
            {
                NumberOfAllocatedPages = _fileInfo.Length / PageSize;
                PagerState.Release();
                PagerState = CreateNewPagerState();
            }
        }
Esempio n. 3
0
 private static extern SafeFileHandle CreateFile(
     [In] string fileName,
     [MarshalAs(UnmanagedType.U4)] NativeFileAccess desiredAccess,
     FileShare shareMode,
     [In] IntPtr securityAttributes,
     [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
     [MarshalAs(UnmanagedType.U4)] NativeFileAttributes flagsAndAttributes,
     [In] IntPtr templateFile);
Esempio n. 4
0
        public Win32MemoryMapPager(string file,
                                   long?initialFileSize         = null,
                                   NativeFileAttributes options = NativeFileAttributes.Normal,
                                   NativeFileAccess access      = NativeFileAccess.GenericRead | NativeFileAccess.GenericWrite)
        {
            NativeMethods.SYSTEM_INFO systemInfo;
            NativeMethods.GetSystemInfo(out systemInfo);

            AllocationGranularity = systemInfo.allocationGranularity;

            _access = access;
            _memoryMappedFileAccess = _access == NativeFileAccess.GenericRead
                                ? MemoryMappedFileAccess.Read
                                : MemoryMappedFileAccess.ReadWrite;

            _handle = NativeFileMethods.CreateFile(file, access,
                                                   NativeFileShare.Read | NativeFileShare.Write | NativeFileShare.Delete, IntPtr.Zero,
                                                   NativeFileCreationDisposition.OpenAlways, options, IntPtr.Zero);
            if (_handle.IsInvalid)
            {
                int lastWin32ErrorCode = Marshal.GetLastWin32Error();
                throw new IOException("Failed to open file storage of Win32MemoryMapPager",
                                      new Win32Exception(lastWin32ErrorCode));
            }

            _fileInfo = new FileInfo(file);

            var streamAccessType = _access == NativeFileAccess.GenericRead
                                ? FileAccess.Read
                                : FileAccess.ReadWrite;

            _fileStream = new FileStream(_handle, streamAccessType);

            _totalAllocationSize = _fileInfo.Length;

            if (_access.HasFlag(NativeFileAccess.GenericWrite) ||
                _access.HasFlag(NativeFileAccess.GenericAll) ||
                _access.HasFlag(NativeFileAccess.FILE_GENERIC_WRITE))
            {
                var fileLength = _fileStream.Length;
                if (fileLength == 0 && initialFileSize.HasValue)
                {
                    fileLength = initialFileSize.Value;
                }

                if (_fileStream.Length == 0 || (fileLength % AllocationGranularity != 0))
                {
                    fileLength = NearestSizeToAllocationGranularity(fileLength);
                    _fileStream.SetLength(fileLength);
                }

                _totalAllocationSize = fileLength;
            }

            NumberOfAllocatedPages = _totalAllocationSize / PageSize;
            PagerState.Release();
            PagerState = CreatePagerState();
        }
Esempio n. 5
0
        public Win32MemoryMapPager(string file,
            long? initialFileSize = null,
            NativeFileAttributes options = NativeFileAttributes.Normal,
            NativeFileAccess access = NativeFileAccess.GenericRead | NativeFileAccess.GenericWrite)
        {
            NativeMethods.SYSTEM_INFO systemInfo;
            NativeMethods.GetSystemInfo(out systemInfo);

            AllocationGranularity = systemInfo.allocationGranularity;

            _access = access;
            _memoryMappedFileAccess = _access == NativeFileAccess.GenericRead
                ? MemoryMappedFileAccess.Read
                : MemoryMappedFileAccess.ReadWrite;

            _handle = NativeFileMethods.CreateFile(file, access,
               NativeFileShare.Read | NativeFileShare.Write | NativeFileShare.Delete, IntPtr.Zero,
               NativeFileCreationDisposition.OpenAlways, options, IntPtr.Zero);
            if (_handle.IsInvalid)
            {
                int lastWin32ErrorCode = Marshal.GetLastWin32Error();
                throw new IOException("Failed to open file storage of Win32MemoryMapPager",
                    new Win32Exception(lastWin32ErrorCode));
            }

            _fileInfo = new FileInfo(file);

            var streamAccessType = _access == NativeFileAccess.GenericRead
                ? FileAccess.Read
                : FileAccess.ReadWrite;
            _fileStream = new FileStream(_handle, streamAccessType);

            _totalAllocationSize = _fileInfo.Length;

            if (_access.HasFlag(NativeFileAccess.GenericWrite) ||
                _access.HasFlag(NativeFileAccess.GenericAll) ||
                _access.HasFlag(NativeFileAccess.FILE_GENERIC_WRITE))
            {
                var fileLength = _fileStream.Length;
                if (fileLength == 0 && initialFileSize.HasValue)
                    fileLength = initialFileSize.Value;

                if (_fileStream.Length == 0 || (fileLength % AllocationGranularity != 0))
                {
                    fileLength = NearestSizeToAllocationGranularity(fileLength);
                    _fileStream.SetLength(fileLength);
                }

                _totalAllocationSize = fileLength;
            }

            NumberOfAllocatedPages = _totalAllocationSize / PageSize;
            PagerState.Release();
            PagerState = CreatePagerState();
        }
Esempio n. 6
0
 public static extern SafeFileHandle CreateFile(string lpFileName,
     NativeFileAccess dwDesiredAccess, NativeFileShare dwShareMode,
     IntPtr lpSecurityAttributes,
     NativeFileCreationDisposition dwCreationDisposition,
     NativeFileAttributes dwFlagsAndAttributes, IntPtr hTemplateFile);
 public static extern SafeFileHandle CreateFile(string lpFileName,
                                                NativeFileAccess dwDesiredAccess, NativeFileShare dwShareMode,
                                                IntPtr lpSecurityAttributes,
                                                NativeFileCreationDisposition dwCreationDisposition,
                                                NativeFileAttributes dwFlagsAndAttributes, IntPtr hTemplateFile);