Example #1
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);
        }
Example #2
0
        internal static MemoryMapping Create(FileStream fs, long bytesToMap = 0)
        {
            var mmf     = MemoryMappedFile.CreateFromFile(fs, null, fs.Length, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true);
            var address = Win32FileMapping.MapViewOfFileEx(mmf.SafeMemoryMappedFileHandle.DangerousGetHandle(),
                                                           Win32FileMapping.FileMapAccess.Read | Win32FileMapping.FileMapAccess.Write,
                                                           0, 0, new UIntPtr((ulong)bytesToMap), null);

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

            var mapping = new MemoryMapping()
            {
                refCount    = 1,
                fileStream  = fs,
                baseAddress = address
            };

            mapping.areas.Add(new MemoryMappedArea(mmf, address, fs.Length));
            return(mapping);
        }