KTransferMemory.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Ryujinx.HLE.HOS.Kernel.Common;
  2. using Ryujinx.HLE.HOS.Kernel.Process;
  3. using Ryujinx.Memory.Range;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace Ryujinx.HLE.HOS.Kernel.Memory
  7. {
  8. class KTransferMemory : KAutoObject
  9. {
  10. private KProcess _creator;
  11. // TODO: Remove when we no longer need to read it from the owner directly.
  12. public KProcess Creator => _creator;
  13. private readonly List<HostMemoryRange> _ranges;
  14. public ulong Address { get; private set; }
  15. public ulong Size { get; private set; }
  16. public KMemoryPermission Permission { get; private set; }
  17. private bool _hasBeenInitialized;
  18. private bool _isMapped;
  19. public KTransferMemory(KernelContext context) : base(context)
  20. {
  21. _ranges = new List<HostMemoryRange>();
  22. }
  23. public KernelResult Initialize(ulong address, ulong size, KMemoryPermission permission)
  24. {
  25. KProcess creator = KernelStatic.GetCurrentProcess();
  26. _creator = creator;
  27. KernelResult result = creator.MemoryManager.BorrowTransferMemory(_ranges, address, size, permission);
  28. if (result != KernelResult.Success)
  29. {
  30. return result;
  31. }
  32. creator.IncrementReferenceCount();
  33. Permission = permission;
  34. Address = address;
  35. Size = size;
  36. _hasBeenInitialized = true;
  37. _isMapped = false;
  38. return result;
  39. }
  40. protected override void Destroy()
  41. {
  42. if (_hasBeenInitialized)
  43. {
  44. if (!_isMapped && _creator.MemoryManager.UnborrowTransferMemory(Address, Size, _ranges) != KernelResult.Success)
  45. {
  46. throw new InvalidOperationException("Unexpected failure restoring transfer memory attributes.");
  47. }
  48. _creator.ResourceLimit?.Release(LimitableResource.TransferMemory, 1);
  49. _creator.DecrementReferenceCount();
  50. }
  51. }
  52. }
  53. }