RAMDisk.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Copyright (C) 2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using Utilities;
  11. namespace DiskAccessLibrary
  12. {
  13. public partial class RAMDisk : Disk
  14. {
  15. public const int BytesPerRAMDiskSector = 512;
  16. private byte[] m_diskBytes;
  17. /// <summary>
  18. /// A single-dimensional byte array cannot contain more than 0X7FFFFFC7 bytes (2047.999 MiB).
  19. /// https://msdn.microsoft.com/en-us/library/System.Array(v=vs.110).aspx
  20. /// </summary>
  21. public RAMDisk(int size)
  22. {
  23. m_diskBytes = new byte[size];
  24. }
  25. public void Free()
  26. {
  27. m_diskBytes = null;
  28. GC.Collect();
  29. GC.WaitForPendingFinalizers();
  30. }
  31. public override byte[] ReadSectors(long sectorIndex, int sectorCount)
  32. {
  33. return ByteReader.ReadBytes(m_diskBytes, (int)sectorIndex * BytesPerRAMDiskSector, sectorCount * BytesPerRAMDiskSector);
  34. }
  35. public override void WriteSectors(long sectorIndex, byte[] data)
  36. {
  37. ByteWriter.WriteBytes(m_diskBytes, (int)sectorIndex * BytesPerRAMDiskSector, data);
  38. }
  39. public override int BytesPerSector
  40. {
  41. get
  42. {
  43. return BytesPerRAMDiskSector;
  44. }
  45. }
  46. public override long Size
  47. {
  48. get
  49. {
  50. return m_diskBytes.Length;
  51. }
  52. }
  53. }
  54. }