Esempio n. 1
0
        protected unsafe byte *GetSharedMemory(int iSize)
        {
            // Build our name
            String strName = GetMemorySectionName();

            IntPtr mappedFileHandle;

            // This gets shared memory for our map.  If its can't, it gives us clean memory.
            Byte *pMemorySection = EncodingTable.nativeCreateOpenFileMapping(strName, iSize, out mappedFileHandle);

            Contract.Assert(pMemorySection != null,
                            "[BaseCodePageEncoding.GetSharedMemory] Expected non-null memory section to be opened");

            // If that failed, we have to die.
            if (pMemorySection == null)
            {
                throw new OutOfMemoryException(
                          Environment.GetResourceString("Arg_OutOfMemoryException"));
            }

            // if we have null file handle. this means memory was allocated after
            // failing to open the mapped file.

            if (mappedFileHandle != IntPtr.Zero)
            {
                safeMemorySectionHandle = new SafeViewOfFileHandle((IntPtr)pMemorySection, true);
                safeFileMappingHandle   = new SafeFileMappingHandle(mappedFileHandle, true);
            }

            return(pMemorySection);
        }
Esempio n. 2
0
 public MapFileStream(SafeViewOfFileHandle viewHandle, int length, bool writable)
 {
     this.viewHandle = viewHandle;
     this.length     = (long)length;
     this.writable   = writable;
     this.position   = 0L;
 }
        internal unsafe MemoryMapFile(String fileName, String fileMappingName)
        {
            //
            // Use native API to create the file directly.
            //
            SafeFileHandle fileHandle = Win32Native.UnsafeCreateFile(fileName, FileStream.GENERIC_READ, FileShare.Read, null, FileMode.Open, 0, IntPtr.Zero);
            int            lastError  = Marshal.GetLastWin32Error();

            if (fileHandle.IsInvalid)
            {
                BCLDebug.Assert(false, "Failed to create file " + fileName + ", GetLastError = " + lastError);
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidOperation_UnexpectedWin32Error"), lastError));
            }

            int highSize;
            int lowSize = Win32Native.GetFileSize(fileHandle, out highSize);

            if (lowSize == Win32Native.INVALID_FILE_SIZE)
            {
                BCLDebug.Assert(false, "Failed to get the file size of " + fileName + ", GetLastError = " + lastError);
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidOperation_UnexpectedWin32Error"), lastError));
            }

            fileSize = (((long)highSize) << 32) | ((uint)lowSize);

            if (fileSize == 0)
            {
                // we cannot map zero size file. the caller should check for the file size.
                fileHandle.Close();
                return;
            }

            SafeFileMappingHandle fileMapHandle = Win32Native.CreateFileMapping(fileHandle, IntPtr.Zero, PAGE_READONLY, 0, 0, fileMappingName);

            fileHandle.Close();
            lastError = Marshal.GetLastWin32Error();
            if (fileMapHandle.IsInvalid)
            {
                BCLDebug.Assert(false, "Failed to create file mapping for file " + fileName + ", GetLastError = " + lastError);
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidOperation_UnexpectedWin32Error"), lastError));
            }

            viewOfFileHandle = Win32Native.MapViewOfFile(fileMapHandle, SECTION_MAP_READ, 0, 0, UIntPtr.Zero);
            lastError        = Marshal.GetLastWin32Error();
            if (viewOfFileHandle.IsInvalid)
            {
                BCLDebug.Assert(false, "Failed to map a view of file " + fileName + ", GetLastError = " + lastError);
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidOperation_UnexpectedWin32Error"), lastError));
            }

            bytes = (byte *)viewOfFileHandle.DangerousGetHandle();

            fileMapHandle.Close();
        }
        protected unsafe byte *GetSharedMemory(int iSize)
        {
            IntPtr ptr;
            byte * numPtr = EncodingTable.nativeCreateOpenFileMapping(this.GetMemorySectionName(), iSize, out ptr);

            if (numPtr == null)
            {
                throw new OutOfMemoryException(Environment.GetResourceString("Arg_OutOfMemoryException"));
            }
            if (ptr != IntPtr.Zero)
            {
                this.safeMemorySectionHandle = new SafeViewOfFileHandle((IntPtr)numPtr, true);
                this.safeFileMappingHandle   = new SafeFileMappingHandle(ptr, true);
            }
            return(numPtr);
        }
        protected unsafe byte *GetSharedMemory(int iSize)
        {
            IntPtr mappedFileHandle;
            byte * openFileMapping = EncodingTable.nativeCreateOpenFileMapping(this.GetMemorySectionName(), iSize, out mappedFileHandle);

            if ((IntPtr)openFileMapping == IntPtr.Zero)
            {
                throw new OutOfMemoryException(Environment.GetResourceString("Arg_OutOfMemoryException"));
            }
            if (mappedFileHandle != IntPtr.Zero)
            {
                this.safeMemorySectionHandle = new SafeViewOfFileHandle((IntPtr)((void *)openFileMapping), true);
                this.safeFileMappingHandle   = new SafeFileMappingHandle(mappedFileHandle, true);
            }
            return(openFileMapping);
        }
Esempio n. 6
0
        static bool GetView(SafeFileMappingHandle fileMapping, bool writable, out SafeViewOfFileHandle handle)
        {
            handle = UnsafeNativeMethods.MapViewOfFile(fileMapping, writable ? UnsafeNativeMethods.FILE_MAP_WRITE : UnsafeNativeMethods.FILE_MAP_READ, 0, 0, (IntPtr)sizeof(SharedMemoryContents));
            int errorCode = Marshal.GetLastWin32Error();

            if (!handle.IsInvalid)
            {
                return(true);
            }
            else
            {
                handle.SetHandleAsInvalid();
                fileMapping.Close();

                // Only return false when it's reading time.
                if (!writable && errorCode == UnsafeNativeMethods.ERROR_FILE_NOT_FOUND)
                {
                    return(false);
                }

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(errorCode));
            }
        }
Esempio n. 7
0
        public MapFileStream CreateView(int offset, int size)
        {
            if (this.fileHandle == null)
            {
                throw new ObjectDisposedException("MemoryMappedFile");
            }
            if (this.fileHandle.IsInvalid)
            {
                throw new InvalidOperationException("MemoryMappedFile");
            }
            if ((long)(offset + size) > (long)((ulong)this.size))
            {
                throw new ArgumentException("size");
            }
            SafeViewOfFileHandle safeViewOfFileHandle = NativeMethods.MapViewOfFile(this.fileHandle, this.mapMode, 0U, (uint)offset, new UIntPtr((uint)size));
            int lastWin32Error = Marshal.GetLastWin32Error();

            if (safeViewOfFileHandle.IsInvalid)
            {
                throw new IOException("MapViewOfFile(" + this.mapMode + ") failed", (lastWin32Error == 0) ? null : new Win32Exception(lastWin32Error));
            }
            return(new MapFileStream(safeViewOfFileHandle, size, this.mapMode == NativeMethods.FileMapAccessControl.Write));
        }
        private static bool GetView(SafeFileMappingHandle fileMapping, bool writable, out SafeViewOfFileHandle handle)
        {
            handle = UnsafeNativeMethods.MapViewOfFile(fileMapping, writable ? 2 : 4, 0, 0, (IntPtr)sizeof(SharedMemoryContents));
            int error = Marshal.GetLastWin32Error();

            if (!handle.IsInvalid)
            {
                return(true);
            }
            handle.SetHandleAsInvalid();
            fileMapping.Close();
            if (writable || (error != 2))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(error));
            }
            return(false);
        }