Esempio n. 1
0
        private void AddMapping(long offset, long qwOffset, long length)
        {
            var mapping = MemoryMappedFile.CreateFromFile(fileStream, null, length, MemoryMappedFileAccess.ReadWrite,
                                                          null, HandleInheritability.None, true);

            var   lastArea       = areas[areas.Count - 1];
            long *desiredAddress = lastArea.Address + lastArea.Length;
            long *address        = MapViewOfFileEx(mapping.SafeMemoryMappedFileHandle.DangerousGetHandle(),
                                                   FileMapAccess.Read | FileMapAccess.Write, (uint)(offset >> 32), (uint)offset,
                                                   new UIntPtr((ulong)offset), (byte *)desiredAddress);

            if (address == null)
            {
                //if we cannot extend region to map file, let OS choose address to map
                address = MapViewOfFileEx(mapping.SafeMemoryMappedFileHandle.DangerousGetHandle(),
                                          FileMapAccess.Read | FileMapAccess.Write, (uint)(offset >> 32), (uint)offset,
                                          new UIntPtr((ulong)offset), null);
            }
            if (address == null)
            {
                throw new Win32Exception();
            }
            var area = new MemoryMappedArea {
                Address = address,
                File    = mapping,
                Length  = qwOffset
            };

            areas.Add(area);
            if (desiredAddress != address)
            {
                offsets.Add(qwOffset);
                addresses = Add(addresses, address);
            }
        }
Esempio n. 2
0
        internal static MemoryMapping Grow(long bytesToGrow, MemoryMapping mapping)
        {
            if (bytesToGrow <= 0 || bytesToGrow % Constants.AllocationGranularity != 0)
            {
                throw new ArgumentException("The growth must be a multiple of 64Kb and greater than zero");
            }
            long offset = mapping.fileStream.Length;

            mapping.fileStream.SetLength(mapping.fileStream.Length + bytesToGrow);
            var   mmf            = MemoryMappedFile.CreateFromFile(mapping.fileStream, null, mapping.fileStream.Length, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true);
            uint *offsetPointer  = (uint *)&offset;
            var   lastArea       = mapping.areas[mapping.areas.Count - 1];
            byte *desiredAddress = lastArea.Address + lastArea.Size;
            ulong bytesToMap     = (ulong)bytesToGrow;
            var   address        = Win32FileMapping.MapViewOfFileEx(mmf.SafeMemoryMappedFileHandle.DangerousGetHandle(),
                                                                    Win32FileMapping.FileMapAccess.Read | Win32FileMapping.FileMapAccess.Write,
                                                                    offsetPointer[1], offsetPointer[0], new UIntPtr(bytesToMap), desiredAddress);

            if (address == null)
            {
                bytesToMap = (ulong)mapping.fileStream.Length;
                address    = Win32FileMapping.MapViewOfFileEx(mmf.SafeMemoryMappedFileHandle.DangerousGetHandle(),
                                                              Win32FileMapping.FileMapAccess.Read | Win32FileMapping.FileMapAccess.Write,
                                                              0, 0, new UIntPtr(bytesToMap), null);
                if (address == null)
                {
                    throw new Win32Exception();
                }
                mapping = new MemoryMapping()
                {
                    baseAddress = address,
                    fileStream  = mapping.fileStream,
                    refCount    = 1
                };
            }

            var area = new MemoryMappedArea
            {
                Address = address,
                Mmf     = mmf,
                Size    = (long)bytesToMap
            };

            mapping.areas.Add(area);
            return(mapping);
        }
Esempio n. 3
0
        public void Grow(long bytesToGrow)
        {
            CheckDisposed();
            if (bytesToGrow <= 0 || bytesToGrow % AllocationGranularity != 0)
            {
                throw new ArgumentException("The growth must be a multiple of 64Kb and greater than zero");
            }
            long offset = fs.Length;

            fs.SetLength(fs.Length + bytesToGrow);
            var   mmf            = MemoryMappedFile.CreateFromFile(fs, null, fs.Length, MemoryMappedFileAccess.ReadWrite, null, HandleInheritability.None, true);
            uint *offsetPointer  = (uint *)&offset;
            var   lastArea       = areas[areas.Count - 1];
            byte *desiredAddress = lastArea.Address + lastArea.Size;
            var   address        = Win32FileMapping.MapViewOfFileEx(mmf.SafeMemoryMappedFileHandle.DangerousGetHandle(),
                                                                    Win32FileMapping.FileMapAccess.Read | Win32FileMapping.FileMapAccess.Write,
                                                                    offsetPointer[1], offsetPointer[0], new UIntPtr((ulong)bytesToGrow), desiredAddress);

            if (address == null)
            {
                address = Win32FileMapping.MapViewOfFileEx(mmf.SafeMemoryMappedFileHandle.DangerousGetHandle(),
                                                           Win32FileMapping.FileMapAccess.Read | Win32FileMapping.FileMapAccess.Write,
                                                           offsetPointer[1], offsetPointer[0], new UIntPtr((ulong)bytesToGrow), null);
            }
            if (address == null)
            {
                throw new Win32Exception();
            }
            var area = new MemoryMappedArea {
                Address = address,
                Mmf     = mmf,
                Size    = bytesToGrow
            };

            areas.Add(area);
            if (desiredAddress != address)
            {
                offsets   = offsets.Add(offset);
                addresses = addresses.Add(address);
            }
        }
Esempio n. 4
0
        private void CreateFirstArea()
        {
            var mmf     = MemoryMappedFile.CreateFromFile(fs, null, fs.Length, MemoryMappedFileAccess.ReadWrite, null, HandleInheritability.None, true);
            var address = Win32FileMapping.MapViewOfFileEx(mmf.SafeMemoryMappedFileHandle.DangerousGetHandle(),
                                                           Win32FileMapping.FileMapAccess.Read | Win32FileMapping.FileMapAccess.Write,
                                                           0, 0, new UIntPtr((ulong)fs.Length), null);

            if (address == null)
            {
                throw new Win32Exception();
            }

            var area = new MemoryMappedArea
            {
                Address = address,
                Mmf     = mmf,
                Size    = fs.Length
            };

            areas.Add(area);

            addresses = new byte *[] { address };
            offsets   = new long[] { 0 };
        }