aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Memory/WritableRegion.cs
blob: 54facb5085bf07f93d2bdd29a08219c664f7fddf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using Ryujinx.Common.Memory;
using System;

namespace Ryujinx.Memory
{
    public sealed class WritableRegion : IDisposable
    {
        private readonly IWritableBlock _block;
        private readonly ulong _va;
        private readonly MemoryOwner<byte> _memoryOwner;
        private readonly bool _tracked;

        private bool NeedsWriteback => _block != null;

        public Memory<byte> Memory { get; }

        public WritableRegion(IWritableBlock block, ulong va, Memory<byte> memory, bool tracked = false)
        {
            _block = block;
            _va = va;
            _tracked = tracked;
            Memory = memory;
        }

        public WritableRegion(IWritableBlock block, ulong va, MemoryOwner<byte> memoryOwner, bool tracked = false)
            : this(block, va, memoryOwner.Memory, tracked)
        {
            _memoryOwner = memoryOwner;
        }

        public void Dispose()
        {
            if (NeedsWriteback)
            {
                if (_tracked)
                {
                    _block.Write(_va, Memory.Span);
                }
                else
                {
                    _block.WriteUntracked(_va, Memory.Span);
                }
            }

            _memoryOwner?.Dispose();
        }
    }
}