예제 #1
0
 /// <summary>
 ///	Removes all the memory mapped files from the store.
 /// </summary>
 public void Flush()
 {
     if (_mvPtr != IntPtr.Zero)
     {
         Win32Mmf.FlushViewOfFile(_mvPtr, viewLength);
     }
 }
예제 #2
0
파일: MmfFile.cs 프로젝트: yongwuhou/NCache
        public static MmfFile Create(string name, ulong maxLength, bool resetContents)
        {
            if (name == null)
            {
                return(Create(INVALID_HANDLE, maxLength));
            }

            IntPtr hFile = Win32Mmf.CreateFile(name,
                                               Win32FileAccess.GENERIC_READ | Win32FileAccess.GENERIC_WRITE,
                                               Win32FileShare.FILE_SHARE_READ,
                                               IntPtr.Zero,
                                               resetContents ? Win32FileMode.CREATE_ALWAYS : Win32FileMode.OPEN_ALWAYS,
                                               Win32FileAttributes.FILE_ATTRIBUTE_NORMAL,
                                               IntPtr.Zero);

            if (hFile == IntPtr.Zero)
            {
                throw new IOException(Win32Mmf.GetWin32ErrorMessage(Win32.GetLastError()));
            }

            if (maxLength <= 0)
            {
                if (Win32Mmf.GetFileSizeEx(hFile, out maxLength) == false)
                {
                    throw new IOException(Win32Mmf.GetWin32ErrorMessage(Win32.GetLastError()));
                }
            }

            return(Create(hFile, maxLength));
        }
예제 #3
0
 /// <summary>
 ///	Close the memory mapped file.
 /// </summary>
 /// <remarks>
 ///	Close();
 /// </remarks>
 public void Close()
 {
     if (_mvPtr != IntPtr.Zero)
     {
         Win32Mmf.UnmapViewOfFile(_mvPtr);
     }
 }
예제 #4
0
파일: MmfFile.cs 프로젝트: yongwuhou/NCache
        /// <summary>
        ///	Set the maximum length of the file.
        /// </summary>
        /// <remarks>
        ///	SetMaxLength( maxLength );
        /// </remarks>
        /// <param name="maxLength">
        ///	Specifies the length to be set
        /// </param>
        public void SetMaxLength(ulong maxLength)
        {
            CloseMapHandle();
            _mmLength = maxLength;
            _mmHandle = Win32Mmf.CreateFileMapping(
                _hFile,
                IntPtr.Zero,
                _protection,
                (uint)(_mmLength >> 32),
                (uint)_mmLength & 0xffffffff,
                objectName);

            if (_mmHandle == IntPtr.Zero)
            {
                uint error = Win32.GetLastError();
                if (error == Constants.ERROR_NOT_ENOUGH_MEMORY)
                {
                    throw new OutOfMemoryException();
                }
                if (error == Constants.ERROR_DISK_FULL)
                {
                    throw new OutOfMemoryException();
                }
                if (error == Constants.ERROR_COMMITMENT_LIMIT)
                {
                    throw new OutOfMemoryException("Limited Virtual Memory. Your system has no paging file, or the paging file is too small.");
                }
                Trace.error("MmfFile.SetMaxLength() Error Number is ::" + error, Win32Mmf.GetWin32ErrorMessage(error));
                throw new IOException(Win32Mmf.GetWin32ErrorMessage(error));
            }
        }
예제 #5
0
파일: MmfFile.cs 프로젝트: yongwuhou/NCache
        /// <summary>
        ///	View the length and the offset of the memory
        ///	mapped file.
        /// </summary>
        /// <remarks>
        ///	MapView( offSet, count );
        /// </remarks>
        /// <param name="offSet">
        ///	The start position
        /// </param>
        /// <param name="count">
        ///	The length of the binary stream
        /// </param>
        public MmfFileView MapView(ulong offSet, uint count)
        {
            if (offSet < 0)
            {
                throw new ArgumentOutOfRangeException("offSet");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            IntPtr mapViewPointer = Win32Mmf.MapViewOfFile(
                _mmHandle,
                Win32Mmf.GetWin32FileMapAccess(_protection),
                (uint)(offSet >> 32),
                (uint)offSet & 0xffffffff,
                (uint)count);

            if (mapViewPointer == IntPtr.Zero)
            {
                uint error = Win32.GetLastError();
                if (error == Constants.ERROR_NOT_ENOUGH_MEMORY)
                {
                    throw new OutOfMemoryException();
                }
                throw new IOException(Win32Mmf.GetWin32ErrorMessage(error));
            }
            return(new MmfFileView(mapViewPointer, count));
        }
예제 #6
0
파일: MmfFile.cs 프로젝트: yongwuhou/NCache
 /// <summary>
 ///	Close the map handle of the memory map.
 /// </summary>
 /// <remarks>
 ///	CloseMapHandle();
 /// </remarks>
 public void CloseMapHandle()
 {
     if (_mmHandle != IntPtr.Zero)
     {
         Win32Mmf.CloseHandle(_mmHandle);
     }
 }
예제 #7
0
파일: MmfFile.cs 프로젝트: yongwuhou/NCache
 /// <summary>
 ///	Close the memory mapped file.
 /// </summary>
 /// <remarks>
 ///	Close();
 /// </remarks>
 public void Close()
 {
     CloseMapHandle();
     if (_hFile != IntPtr.Zero)
     {
         Win32Mmf.CloseHandle(_hFile);
     }
     System.GC.SuppressFinalize(this);
 }
예제 #8
0
파일: MmfFile.cs 프로젝트: yongwuhou/NCache
 public void UnMapView(MmfFileView view)
 {
     try
     {
         Win32Mmf.UnmapViewOfFile(view.ViewPtr);
     }
     catch (Exception e)
     {
         throw e;
     }
 }