Exemple #1
0
        private MemoryAccessor(MemoryMappedFile memoryMappedFile, bool usingDefaultData = false)
        {
            UsingDefaultData = usingDefaultData;
            MemoryMappedFile = memoryMappedFile;
            using (var viewAccessor = MemoryMappedFile?.CreateViewAccessor())
            {
                var    bytesCheck = new byte[] { 0x50, 0x53, 0x2D, 0x58, 0x20, 0x45, 0x58, 0x45 };
                byte[] bytes      = new byte[8];
                if (viewAccessor?.ReadArray(0, bytes, 0, 8) != 8)
                {
                    MemoryMappedFile?.Dispose();
                    throw new Exception("This file has the wrong header size returning early.");
                }

                if (bytesCheck.Where((t, i) => bytes[i] != t).Any())
                {
                    MemoryMappedFile?.Dispose();
                    throw new Exception("This file has the wrong header returning early.");
                }

                MemoryMappedFilesToBeDisposed.Add(MemoryMappedFile);
                IsOpen = true;
                GetData(viewAccessor);
            }
        }
Exemple #2
0
        public override void Close()
        {
            base.Close();

            reader?.Dispose();
            viewStream?.Dispose();
            mappedFile?.Dispose();
            fileStream?.Dispose();
        }
Exemple #3
0
        /// <summary>
        /// Dispose
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                m_MemoryFile?.Dispose();
                m_MemoryFile = null;
            }

            base.Dispose(disposing);
        }
            public void Dispose()
            {
                lock (SyncLock)
                {
                    if (IsDisposed)
                    {
                        return;
                    }
                    IsDisposed = true;

                    BackBufferView?.Dispose();
                    BackBufferFile?.Dispose();
                }
            }
        // Инициализая файла
        void InitFile()
        {
            if (header_mma != null)
            {
                header_mma.SafeMemoryMappedViewHandle.ReleasePointer();
                header_mma.Dispose();
            }

            file?.Dispose();

            file = fs.CreateMMF(Key);

            header_mma = file.CreateViewAccessor(0, fs.Length, MemoryMappedFileAccess.ReadWrite);
            ptr        = header_mma.Pointer(0);
        }
Exemple #6
0
 public void Dispose()
 {
     _reader?.Dispose();
     _writer?.Dispose();
     _memoryMappedFile?.Dispose();
     _keystoreLock?.Close();
 }
Exemple #7
0
            private void DisposeNativeResources()
            {
                if (_CharBufferBase != null)
                {
                    _CharBufferGCHandle.Free();
                    _CharBufferBase = null;
                }

                if (_Accessor != null)
                {
                    _Accessor.SafeMemoryMappedViewHandle.ReleasePointer();
                    _Accessor.Dispose();
                    _Accessor = null;
                }
                if (_MMF != null)
                {
                    _MMF.Dispose();
                    _MMF = null;
                }
                if (_FS != null)
                {
                    _FS.Dispose();
                    _FS = null;
                }
            }
Exemple #8
0
        public void Close()
        {
            for (int k = 0; k < oscTypes.NUM_SCENES; k++)
            {
                clientToOscBuff?[k].Close();
                oscToClientBuff?[k].Close();
            }
            oscToClientBuff = clientToOscBuff = null;

            if (mapAccess != null)
            {
                mapAccess.Write(SERVERUP_POS, 0);
                MemoryMappedViewAccessor handle = mapAccess;
                mapAccess = null;
                handle?.Dispose();
            }

            // Close the file handle
            if (m_hMapFile != null)
            {
                MemoryMappedFile handle = m_hMapFile;
                m_hMapFile = null;
                handle?.Dispose();
            }
        }
Exemple #9
0
 void ClosePfs()
 {
     accessor?.Dispose();
     pfs?.Dispose();
     pfs      = null;
     accessor = null;
 }
        protected override async Task <UploadResponse> InternalUploadAsync(CancellationToken cancellationToken)
        {
            MemoryMappedFile memoryMappedFile = null;

            try
            {
                if (FileStream.Length > 0)
                {
                    memoryMappedFile = CreateMemoryMappedFile();
                    AdjustNumberOfThreadsByFileSize();
                    var partSizeCalc = new PartSizeCalculator(Config.NumberOfThreads, Config.PartConfig);

                    await UploadChunks(
                        memoryMappedFile,
                        partSizeCalc,
                        Config.NumberOfThreads,
                        cancellationToken).ConfigureAwait(false);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        throw CancellationException();
                    }
                }
                return(await FinishUpload(cancellationToken).ConfigureAwait(false));
            }
            finally
            {
                memoryMappedFile?.Dispose();
            }
        }
        internal static OperationResult <InboundChannel> Open(string fullName, string name)
        {
            Semaphore readerSemaphore = null;

            MemoryMappedFile file = null;

            Semaphore exclusiveAccessSemaphore = null;

            EventWaitHandle hasMessagesEvent = null;

            EventWaitHandle noMessagesEvent = null;

            EventWaitHandle clientConnectedEvent = null;

            try
            {
                var readerSemaphoreOperationResult = LifecycleHelper.OpenAndSetReaderSemaphore(fullName);

                if (readerSemaphoreOperationResult.Status != OperationStatus.Completed)
                {
                    return(new OperationResult <InboundChannel>(readerSemaphoreOperationResult.Status, null));
                }

                readerSemaphore = readerSemaphoreOperationResult.Data;

                var openCommonSharedObjectsStatus = LifecycleHelper.OpenCommonSharedObjects(fullName, out file, out exclusiveAccessSemaphore, out hasMessagesEvent, out noMessagesEvent, out clientConnectedEvent);

                if (openCommonSharedObjectsStatus != OperationStatus.Completed)
                {
                    try { readerSemaphore.Release(); } catch { }

                    LifecycleHelper.PlatformSpecificDispose(readerSemaphore);
                }

                var result = new InboundChannel(name, readerSemaphore, exclusiveAccessSemaphore, file, hasMessagesEvent, noMessagesEvent, clientConnectedEvent, null);

                return(new OperationResult <InboundChannel>(OperationStatus.Completed, result));
            }
            catch
            {
                if (readerSemaphore != null)
                {
                    try { readerSemaphore.Release(); } catch { }

                    LifecycleHelper.PlatformSpecificDispose(readerSemaphore);
                }

                LifecycleHelper.PlatformSpecificDispose(exclusiveAccessSemaphore);

                file?.Dispose();

                LifecycleHelper.PlatformSpecificDispose(hasMessagesEvent);

                LifecycleHelper.PlatformSpecificDispose(noMessagesEvent);

                LifecycleHelper.PlatformSpecificDispose(clientConnectedEvent);

                throw;
            }
        }
Exemple #12
0
        /// <summary>
        /// Sets the <see cref="ShuttingDown"/> flag, and disposes of the MemoryMappedFile and MemoryMappedViewAccessor.<br />
        /// Attempting to read/write to the buffer after closing will result in a <see cref="System.NullReferenceException"/>.
        /// </summary>
        public virtual void Close()
        {
            if (IsOwnerOfSharedMemory && View != null)
            {
                // Indicates to any open instances that the owner is no longer open
#pragma warning disable 0420 // ignore ref to volatile warning - Interlocked API
                Interlocked.Exchange(ref Header->Shutdown, 1);
#pragma warning restore 0420
            }

            // Allow additional close logic
            DoClose();

            // Close the MemoryMappedFile and MemoryMappedViewAccessor
            if (View != null)
            {
                View.SafeMemoryMappedViewHandle.ReleasePointer();
                View.Dispose();
            }
            if (Mmf != null)
            {
                Mmf.Dispose();
            }
            Header         = null;
            ViewPtr        = null;
            BufferStartPtr = null;
            View           = null;
            Mmf            = null;
        }
Exemple #13
0
        private void CloseLogFile()
        {
            MemoryMappedFile mmf = Interlocked.CompareExchange(ref this.memoryMappedFile, null, this.memoryMappedFile);

            if (mmf != null)
            {
                // Each thread has its own MemoryMappedViewStream created from the only one MemoryMappedFile.
                // Once worker thread closes the MemoryMappedFile, all the ViewStream objects should be disposed
                // properly.
                foreach (MemoryMappedViewStream stream in this.viewStream.Values)
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                    }
                }

                mmf.Dispose();
            }

            FileStream fs = Interlocked.CompareExchange(
                ref this.underlyingFileStreamForMemoryMappedFile,
                null,
                this.underlyingFileStreamForMemoryMappedFile);

            fs?.Dispose();
        }
Exemple #14
0
        private bool disposedValue = false; // To detect redundant calls
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                // release / clear up streams
                if (disposing)
                {
                    if (mappedAccess != null)
                    {
                        mappedAccess.Dispose();
                    }

                    if (mappedFile != null)
                    {
                        mappedFile.Dispose();
                    }

                    if (mapStream != null)
                    {
                        mapStream.Dispose();
                    }

                    mappedAccess = null;
                    mappedFile   = null;
                    mapStream    = null;
                }
                // no unmanaged resources e.g. AllocHGlobal etc...
                disposedValue = true;
            }
        }
Exemple #15
0
 public void Dispose()
 {
     _allocated.Do(s => s.Dispose());
     _mmap.Dispose();
     _fileStream.Dispose();
     _file.Dispose();
 }
Exemple #16
0
 public void Dispose()
 {
     requestReadyEvent?.Dispose();
     replyReadyEvent?.Dispose();
     stream.Dispose();
     file?.Dispose();
 }
        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            // Always set the dispose wait handle even when dispised  by the finalizer
            // otherwize the file watcher task will needleessly have to wait for its timeout.
            disposeWaitHandle?.Set();
            fileWatcherTask?.Wait(TinyReadWriteLock.DefaultWaitTimeout);

            if (disposing)
            {
                memoryMappedFile.Dispose();

                if (disposeLock && readWriteLock is IDisposable disposableLock)
                {
                    disposableLock.Dispose();
                }

                fileWaitHandle.Dispose();
                disposeWaitHandle?.Dispose();
            }

            disposed = true;
        }
Exemple #18
0
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            ReleaseUnmanagedResources();
            if (disposing)
            {
                // We own the _mmva, so always dispose it
                _mmva?.Dispose();

                var what = this;
                while (what._parent != null)
                {
                    what = what._parent;
                }

                if (0 == Interlocked.Decrement(ref what._references))
                {
                    // Only dispose the _mmf if we are the last reference to it
                    _mmf?.Dispose();
                }
            }

            _disposed = true;
        }
Exemple #19
0
        public void CustomOnDestroy()
        {
            if (fileKeyFloat != null)
            {
                fileKeyFloat.Dispose();
                fileKeyFloat.Close();
            }

            if (streamFloatValue != null)
            {
                streamFloatValue.Dispose();
                streamFloatValue.Close();
            }

            if (fileHeader != null)
            {
                fileHeader.Dispose();
                fileHeader.Close();
            }

            if (fileValueFloat != null)
            {
                fileValueFloat.Dispose();
                fileValueFloat.Close();
            }
        }
Exemple #20
0
 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
     if (m_memMap != null)
     {
         m_memMap.Dispose();
     }
 }
Exemple #21
0
        private string ReadIPCClassNameMMF()
        {
            MemoryMappedFile         mmf = null;
            MemoryMappedViewAccessor mma = null;

            try
            {
                byte[] buffer = new byte[128];
                mmf = MemoryMappedFile.OpenExisting("DS4Windows_IPCClassName.dat");
                mma = mmf.CreateViewAccessor(0, 128);
                mma.ReadArray(0, buffer, 0, buffer.Length);
                return(Encoding.ASCII.GetString(buffer));
            }
            catch (Exception)
            {
                // Eat all exceptions
            }
            finally
            {
                mma?.Dispose();
                mmf?.Dispose();
            }

            return(null);
        }
Exemple #22
0
        public void LeaveOpenRespected_OutstandingViews(bool leaveOpen)
        {
            const int Capacity = 4096;

            using (TempFile file = new TempFile(GetTestFilePath()))
                using (FileStream fs = File.Open(file.Path, FileMode.Open))
                {
                    // Handle should still be open
                    SafeFileHandle handle = fs.SafeFileHandle;
                    Assert.False(handle.IsClosed);

                    // Create the map, create each of the views, then close the map
                    using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fs, null, Capacity, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, leaveOpen))
                        using (MemoryMappedViewAccessor acc = mmf.CreateViewAccessor(0, Capacity))
                            using (MemoryMappedViewStream s = mmf.CreateViewStream(0, Capacity))
                            {
                                // Explicitly close the map. The handle should now be open iff leaveOpen.
                                mmf.Dispose();
                                Assert.NotEqual(leaveOpen, handle.IsClosed);

                                // But the views should still be usable.
                                ValidateMemoryMappedViewAccessor(acc, Capacity, MemoryMappedFileAccess.ReadWrite);
                                ValidateMemoryMappedViewStream(s, Capacity, MemoryMappedFileAccess.ReadWrite);
                            }
                }
        }
Exemple #23
0
        public void Grow(long minCapacity)
        {
            lock (SyncRoot) {
                _fileStream?.Dispose();
                _fileStream = new FileStream(_filename, FileMode.OpenOrCreate,
                                             FileAccess.ReadWrite, FileShare.ReadWrite, 4096,
                                             FileOptions.RandomAccess);

                // NB another thread could have increase the map size and _capacity could be stale
                var bytesCapacity = Math.Max(_fileStream.Length, minCapacity);

                var sec = new MemoryMappedFileSecurity();
                _mmf?.Dispose();
                var unique = ((long)Process.GetCurrentProcess().Id << 32) | _counter++;
                // NB: map name must be unique
                var mmf = MemoryMappedFile.CreateFromFile(_fileStream,
                                                          $@"{Path.GetFileName(_filename)}.{unique}", bytesCapacity,
                                                          MemoryMappedFileAccess.ReadWrite, sec, HandleInheritability.Inheritable,
                                                          false);
                _mmf = mmf;

                unsafe
                {
                    byte *ptr = (byte *)0;
                    _va = _mmf.CreateViewAccessor(0, bytesCapacity, MemoryMappedFileAccess.ReadWrite);
                    var sh = _va.SafeMemoryMappedViewHandle;
                    sh.AcquirePointer(ref ptr);
                    var ptrV = new IntPtr(ptr);
                    _buffer = new DirectBuffer(bytesCapacity, ptrV);
                }
                _capacity = bytesCapacity;
            }
        }
Exemple #24
0
        public static MemoryMapping Create(string path)
        {
            MemoryMappedFile           file     = null;
            MemoryMappedViewAccessor   accessor = null;
            SafeMemoryMappedViewHandle handle   = null;
            MemoryMapping mapping = null;
            FileStream    stream  = null;
            byte *        ptr     = null;

            try {
                stream   = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite);
                file     = MemoryMappedFile.CreateFromFile(stream, null, 0, MemoryMappedFileAccess.Read, null, HandleInheritability.None, true);
                accessor = file.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read);
                mapping  = new MemoryMapping();

                // we need to make sure that the handle and the acquired pointer get stored to MemoryMapping:
                RuntimeHelpers.PrepareConstrainedRegions();
                try { } finally {
                    handle = accessor.SafeMemoryMappedViewHandle;
                    handle.AcquirePointer(ref ptr);
                    if (ptr == null)
                    {
                        throw new IOException("Cannot create a file mapping");
                    }
                    mapping._handle   = handle;
                    mapping._pointer  = ptr;
                    mapping._capacity = accessor.Capacity;
                }
            } finally {
                stream?.Dispose();
                accessor?.Dispose();
                file?.Dispose();
            }
            return(mapping);
        }
Exemple #25
0
        private bool disposedValue = false; // To detect redundant calls
        protected virtual void Dispose(bool disposing)
        {
            // Mem is a very shared construct looking to find where a consumer is causing dispose before were ready
            if (!disposedValue)
            {
                // release / clear up streams
                if (disposing)
                {
                    if (mappedAccess != null)
                    {
                        mappedAccess.Dispose();
                    }

                    if (mappedFile != null)
                    {
                        mappedFile.Dispose();
                    }

                    if (mapStream != null)
                    {
                        mapStream.Dispose();
                    }

                    mappedAccess = null;
                    mappedFile   = null;
                    mapStream    = null;
                }
                // no unmanaged resources e.g. AllocHGlobal etc...
                disposedValue = true;
            }
        }
Exemple #26
0
        public static unsafe PEReader OpenPEFile(string filePath, out MemoryMappedViewAccessor mappedViewAccessor)
        {
            // System.Reflection.Metadata has heuristic that tries to save virtual address space. This heuristic does not work
            // well for us since it can make IL access very slow (call to OS for each method IL query). We will map the file
            // ourselves to get the desired performance characteristics reliably.

            FileStream               fileStream = null;
            MemoryMappedFile         mappedFile = null;
            MemoryMappedViewAccessor accessor   = null;

            try
            {
                // Create stream because CreateFromFile(string, ...) uses FileShare.None which is too strict
                fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1);
                mappedFile = MemoryMappedFile.CreateFromFile(
                    fileStream, null, fileStream.Length, MemoryMappedFileAccess.Read, HandleInheritability.None, true);
                accessor = mappedFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read);

                var safeBuffer = accessor.SafeMemoryMappedViewHandle;
                var peReader   = new PEReader((byte *)safeBuffer.DangerousGetHandle(), (int)safeBuffer.ByteLength);

                // MemoryMappedFile does not need to be kept around. MemoryMappedViewAccessor is enough.

                mappedViewAccessor = accessor;
                accessor           = null;

                return(peReader);
            }
            finally
            {
                accessor?.Dispose();
                mappedFile?.Dispose();
                fileStream?.Dispose();
            }
        }
Exemple #27
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         mmva.Dispose();
         mmf.Dispose();
     }
 }
 /// <summary>
 /// Close method implementation
 /// </summary>
 public void Close()
 {
     if (isopen)
     {
         memoryMappedFile.Dispose();
         isopen = false;
     }
 }
 public void Dispose()
 {
     writer.Close();
     writer.Dispose();
     stream.Close();
     stream.Dispose();
     memoryMappedFile.Dispose();
 }
Exemple #30
0
 private void Dispose(bool disposing)
 {
     if (disposing)
     {
         _stream?.Dispose();
         _memoryMappedFile?.Dispose();
     }
 }
    public virtual DocumentText CreateDocumentText(string fileName, CompilerResults results, CompilerParameters options, ErrorNodeList errorNodes, bool canUseMemoryMap){
#if !ROTOR
      if (canUseMemoryMap){
        int applicableCodePage = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ANSICodePage;
        CompilerOptions coptions = options as CompilerOptions;
        if (coptions != null && coptions.CodePage != null) applicableCodePage = (int)coptions.CodePage;
        int asciiCodePage = System.Globalization.CultureInfo.InvariantCulture.TextInfo.ANSICodePage;
        if (applicableCodePage == asciiCodePage){
          //If there is no unicode signature at the start of the file, it seems reasonably safe to assume that 1 byte == 1 char
          //In that case we can bypass the overhead of BCL file classes and use a memory mapped file instead.
          unsafe{
            try{
              MemoryMappedFile mmFile = new MemoryMappedFile(fileName);
              try{
                byte b0 = *mmFile.Buffer;
                if (b0 == 'M' && *(mmFile.Buffer+1) == 'Z'){
                  //This is a binary file. Give an appropriate error.
                  errorNodes.Add(this.CreateErrorNode(Error.IsBinaryFile, System.IO.Path.GetFullPath(fileName)));
                  this.ProcessErrors(options, results, errorNodes);
                  mmFile.Dispose();
                  return null;
                }else if (b0 != 0xff && b0 != 0xfe && b0 != 0xef){
                  // No unicode signature, it seems. Go ahead and compile using the memory mapped file.
                  return new DocumentText(mmFile);
                } 
              }catch(Exception e){
                errorNodes.Add(this.CreateErrorNode(Error.InternalCompilerError, e.Message));
                this.ProcessErrors(options, results, errorNodes);
                return new DocumentText("");
              }
            }catch{}
          }
        }
      }
#endif
      return new DocumentText(this.ReadSourceText(fileName, options, errorNodes));
    }