LinuxPageAllocator.cs 969 B

1234567891011121314151617181920212223242526272829
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using MonoMod.Utils;
  6. namespace BepInEx.IL2CPP.Allocator
  7. {
  8. internal class LinuxPageAllocator : UnixPageAllocator
  9. {
  10. protected override IEnumerable<(IntPtr, IntPtr)> MapMemoryAreas()
  11. {
  12. // Each row of /proc/self/maps is as follows:
  13. // <start_address>-<end_address> <perms> <offset> <dev> <inode> <owner_name>
  14. // More info: https://stackoverflow.com/a/1401595
  15. using var procMap = new StreamReader(File.OpenRead("/proc/self/maps"));
  16. string line;
  17. while ((line = procMap.ReadLine()) != null)
  18. {
  19. int startIndex = line.IndexOf('-');
  20. int endIndex = line.IndexOf(' ');
  21. long startAddr = long.Parse(line.Substring(0, startIndex), NumberStyles.HexNumber);
  22. long endAddr = long.Parse(line.Substring(startIndex + 1, endIndex - startIndex - 1), NumberStyles.HexNumber);
  23. yield return (new IntPtr(startAddr), new IntPtr(endAddr));
  24. }
  25. }
  26. }
  27. }