private void CreateMemoryMap(ulong sharedMemorySize)
        {
            if (Native.FileTruncate(sharedMemoryHandle, (long)sharedMemorySize) == -1)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new FileNotFoundException(
                          $"Failed to ftruncate {sharedMemoryMapName} {sharedMemoryHandle}",
                          innerException: new Win32Exception(errno));
            }

            Buffer = Native.MapMemory(
                address: IntPtr.Zero,
                length: sharedMemorySize,
                protFlags: Native.ProtFlags.PROT_READ | Native.ProtFlags.PROT_WRITE,
                mapFlags: Native.MapFlags.MAP_SHARED,
                handle: sharedMemoryHandle,
                offset: 0);
            if (Buffer == Native.InvalidPointer)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new InvalidOperationException(
                          $"Failed to mmap {sharedMemoryMapName} {sharedMemoryHandle}",
                          innerException: new Win32Exception(errno));
            }

            MemSize = sharedMemorySize;
        }
Beispiel #2
0
        private SharedMemoryMapView(string sharedMemoryMapName, ulong sharedMemorySize, Native.OpenFlags openFlags)
        {
            this.sharedMemoryMapName = sharedMemoryMapName;

            // Create shared memory view.
            //
            sharedMemoryHandle = Native.SharedMemoryOpen(
                sharedMemoryMapName,
                openFlags,
                Native.ModeFlags.S_IRUSR | Native.ModeFlags.S_IWUSR);

            if (sharedMemoryHandle.IsInvalid)
            {
                int errno = Marshal.GetLastWin32Error();

                throw new FileNotFoundException(
                          $"Failed to shm_open {sharedMemoryMapName}",
                          innerException: new Win32Exception(errno));
            }

            if (Native.FileTruncate(sharedMemoryHandle, (long)sharedMemorySize) == -1)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new FileNotFoundException(
                          $"Failed to ftruncate {sharedMemoryMapName} {sharedMemoryHandle}",
                          innerException: new Win32Exception(errno));
            }

            Buffer = Native.MapMemory(
                address: IntPtr.Zero,
                length: sharedMemorySize,
                protFlags: Native.ProtFlags.PROT_READ | Native.ProtFlags.PROT_WRITE,
                mapFlags: Native.MapFlags.MAP_SHARED,
                handle: sharedMemoryHandle,
                offset: 0);
            if (Buffer == Native.InvalidPointer)
            {
                int errno = Marshal.GetLastWin32Error();
                throw new InvalidOperationException(
                          $"Failed to mmap {sharedMemoryMapName} {sharedMemoryHandle}",
                          innerException: new Win32Exception(errno));
            }

            MemSize = sharedMemorySize;
        }