private IpcCircularBuffer(Section section, string sectionName, Semaphore readSemaphore, Semaphore writeSemaphore)
        {
            BufferHeader header;

            _section = section;

            _sectionView = section.MapView(Marshal.SizeOf(typeof(BufferHeader)));
            header       = _sectionView.ReadStruct <BufferHeader>();
            _sectionView.Dispose();

            if (readSemaphore == null || writeSemaphore == null)
            {
                _readSemaphore  = new Semaphore(sectionName + "_" + header.ReadSemaphoreId.ToString("x"));
                _writeSemaphore = new Semaphore(sectionName + "_" + header.WriteSemaphoreId.ToString("x"));
            }
            else
            {
                _readSemaphore  = readSemaphore;
                _writeSemaphore = writeSemaphore;
            }

            _sectionView = _section.MapView(header.BlockSize * header.NumberOfBlocks);
            _header      = (BufferHeader *)_sectionView.Memory;
            _data        = &_header->Data;
        }
        public static IpcCircularBuffer Create(string name, int blockSize, int numberOfBlocks)
        {
            Random  r = new Random();
            long    readSemaphoreId  = ((long)r.Next() << 32) + r.Next();
            long    writeSemaphoreId = ((long)r.Next() << 32) + r.Next();
            Section section;

            section = new Section(name, blockSize * numberOfBlocks, MemoryProtection.ReadWrite);

            using (var view = section.MapView(Marshal.SizeOf(typeof(BufferHeader))))
            {
                BufferHeader header = new BufferHeader();

                header.BlockSize        = blockSize;
                header.NumberOfBlocks   = numberOfBlocks;
                header.ReadSemaphoreId  = readSemaphoreId;
                header.WriteSemaphoreId = writeSemaphoreId;
                header.ReadPosition     = 0;
                header.WritePosition    = 0;

                view.WriteStruct <BufferHeader>(header);
            }

            return(new IpcCircularBuffer(
                       section,
                       name,
                       new Semaphore(name + "_" + readSemaphoreId.ToString("x"), 0, numberOfBlocks),
                       new Semaphore(name + "_" + writeSemaphoreId.ToString("x"), numberOfBlocks, numberOfBlocks)
                       ));
        }
Example #3
0
        public static IpcCircularBuffer Create(string name, int blockSize, int numberOfBlocks)
        {
            Random r = new Random();
            long   readSemaphoreId  = ((long)r.Next() << 32) + r.Next();
            long   writeSemaphoreId = ((long)r.Next() << 32) + r.Next();

            Section section = new Section(name, blockSize * numberOfBlocks, MemoryProtection.ReadWrite);

            using (SectionView view = section.MapView(BufferHeader.SizeOf))
            {
                BufferHeader header = new BufferHeader
                {
                    BlockSize        = blockSize,
                    NumberOfBlocks   = numberOfBlocks,
                    ReadSemaphoreId  = readSemaphoreId,
                    WriteSemaphoreId = writeSemaphoreId,
                    ReadPosition     = 0, WritePosition = 0
                };

                view.WriteStruct(header);
            }

            return(new IpcCircularBuffer(
                       section,
                       name,
                       new Semaphore(name + "_" + readSemaphoreId.ToString("x"), 0, numberOfBlocks),
                       new Semaphore(name + "_" + writeSemaphoreId.ToString("x"), numberOfBlocks, numberOfBlocks)
                       ));
        }
Example #4
0
        private void MapAndLoad(FileHandle fileHandle, bool readOnly)
        {
            using (Section section = new Section(
                       fileHandle,
                       false,
                       readOnly ? MemoryProtection.ExecuteRead : MemoryProtection.ExecuteReadWrite
                       ))
            {
                _size   = (int)fileHandle.GetSize();
                _view   = section.MapView(_size);
                _memory = _view;

                byte *start = (byte *)_memory;

                if (start[0] != 'M' || start[1] != 'Z')
                {
                    throw new Exception("The file is not a valid executable image.");
                }

                _ntHeaders = this.GetNtHeaders();
                _sections  = (ImageSectionHeader *)((byte *)&_ntHeaders->OptionalHeader + _ntHeaders->FileHeader.SizeOfOptionalHeader);
                _magic     = _ntHeaders->OptionalHeader.Magic;

                if (_magic != Win32.Pe32Magic && _magic != Win32.Pe32PlusMagic)
                {
                    throw new Exception("The file is not a PE32 or PE32+ image.");
                }
            }
        }
        private IntPtr AllocateBlock(out ushort blockId)
        {
            blockId = _header->NextFreeBlock++;
            _section.Extend(_header->NextFreeBlock * _blockSize);

            SectionView     view   = _section.MapView(blockId * _blockSize, _blockSize, _protection);
            MfsBlockHeader *header = (MfsBlockHeader *)view.Memory;

            header->Hash         = 0;
            header->NextFreeCell = 1;

            ViewDescriptor vd = _vdFreeList.Allocate();

            vd.RefCount = 1;
            vd.BlockId  = blockId;
            vd.View     = view;

            _views.Add(blockId, vd);
            _views2.Add(view.Memory, vd);

            return(view);
        }
Example #6
0
        private void MapAndLoad(FileHandle fileHandle, bool readOnly)
        {
            using (Section section = new Section(
                       fileHandle,
                       false,
                       readOnly ? MemoryProtection.ExecuteRead : MemoryProtection.ExecuteReadWrite
                       ))
            {
                _size = (int)fileHandle.GetSize();
                _view = section.MapView(_size);

                this.Load(_view);
            }
        }
        public MemoryFileSystem(string fileName, MfsOpenMode mode, bool readOnly, MfsParameters createParams)
        {
            FileCreationDispositionWin32 cdWin32;

            if (readOnly && mode != MfsOpenMode.Open)
            {
                throw new ArgumentException("Invalid mode for read only access.");
            }

            switch (mode)
            {
            case MfsOpenMode.Open:
                cdWin32 = FileCreationDispositionWin32.OpenExisting;
                break;

            default:
            case MfsOpenMode.OpenIf:
                cdWin32 = FileCreationDispositionWin32.OpenAlways;
                break;

            case MfsOpenMode.OverwriteIf:
                cdWin32 = FileCreationDispositionWin32.CreateAlways;
                break;
            }

            using (var fhandle = FileHandle.CreateWin32(
                       fileName,
                       FileAccess.GenericRead | (!readOnly ? FileAccess.GenericWrite : 0),
                       FileShareMode.Read,
                       cdWin32
                       ))
            {
                bool justCreated = false;

                _readOnly   = readOnly;
                _protection = !readOnly ? MemoryProtection.ReadWrite : MemoryProtection.ReadOnly;

                if (fhandle.GetSize() == 0)
                {
                    if (readOnly)
                    {
                        throw new MfsInvalidFileSystemException();
                    }
                    else
                    {
                        // File is too small. Make it 1 byte large and we'll deal with it
                        // soon.
                        fhandle.SetEnd(1);
                    }
                }

                _section = new Section(fhandle, _protection);

                _blockSize = MfsBlockSizeBase; // fake block size to begin with; we'll fix it up later.

                if (fhandle.GetSize() < _blockSize)
                {
                    if (readOnly)
                    {
                        throw new MfsInvalidFileSystemException();
                    }
                    else
                    {
                        // We're creating a new file system. We need the correct block size
                        // now.
                        if (createParams != null)
                        {
                            _blockSize = createParams.BlockSize;
                        }
                        else
                        {
                            _blockSize = MfsDefaultBlockSize;
                        }

                        _section.Extend(_blockSize);

                        using (var view = _section.MapView(0, _blockSize, _protection))
                            this.InitializeFs((MfsFsHeader *)view.Memory, createParams);

                        justCreated = true;
                    }
                }

                _header = (MfsFsHeader *)this.ReferenceBlock(0);

                // Check the magic.
                if (_header->Magic != MfsMagic)
                {
                    throw new MfsInvalidFileSystemException();
                }

                // Set up the local constants.
                _blockSize = _header->BlockSize;
                _cellSize  = _header->CellSize;

                // Backwards compatibility.
                if (_blockSize == 0)
                {
                    _blockSize = MfsDefaultBlockSize;
                }
                if (_cellSize == 0)
                {
                    _cellSize = MfsDefaultCellSize;
                }

                // Validate the parameters.
                this.ValidateFsParameters(_blockSize, _cellSize);

                _blockMask             = _blockSize - 1;
                _cellCount             = _blockSize / _cellSize;
                _dataCellDataMaxLength = _cellSize - MfsDataCell.DataOffset;

                // Remap block 0 with the correct block size.
                this.DereferenceBlock(0);

                // If we just created a new file system, fix the section size.
                if (justCreated)
                {
                    _section.Extend(_blockSize);
                }

                _header = (MfsFsHeader *)this.ReferenceBlock(0);

                // Set up the root object.
                _rootObject   = (MfsObjectHeader *)((byte *)_header + _cellSize);
                _rootObjectMo = new MemoryObject(this, _rootObjectCellId, true);

                if (_header->NextFreeBlock != 1 && !readOnly)
                {
                    ushort lastBlockId = (ushort)(_header->NextFreeBlock - 1);

                    this.ReferenceBlock(lastBlockId);
                    _cachedLastBlockView = _views[lastBlockId];
                }
            }
        }
Example #8
0
        private void MapAndLoad(FileHandle fileHandle, bool readOnly)
        {
            using (Section section = new Section(
                fileHandle,
                false,
                readOnly ? MemoryProtection.ExecuteRead : MemoryProtection.ExecuteReadWrite
                ))
            {
                _size = (int)fileHandle.GetSize();
                _view = section.MapView(_size);
                _memory = _view;

                byte* start = (byte*)_memory;

                if (start[0] != 'M' || start[1] != 'Z')
                    throw new Exception("The file is not a valid executable image.");

                _ntHeaders = this.GetNtHeaders();
                _sections = (ImageSectionHeader*)((byte*)&_ntHeaders->OptionalHeader + _ntHeaders->FileHeader.SizeOfOptionalHeader);
                _magic = _ntHeaders->OptionalHeader.Magic;

                if (_magic != Win32.Pe32Magic && _magic != Win32.Pe32PlusMagic)
                    throw new Exception("The file is not a PE32 or PE32+ image.");
            }
        }
        private void MapAndLoad(FileHandle fileHandle, bool readOnly)
        {
            using (Section section = new Section(
                fileHandle,
                false,
                readOnly ? MemoryProtection.ExecuteRead : MemoryProtection.ExecuteReadWrite
                ))
            {
                _size = (int)fileHandle.FileSize;
                _view = section.MapView(_size);

                this.Load(_view);
            }
        }