using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cosmos.Kernel { /// /// Provides Managed Memory allocation for drivers that need memory address access and memory address alignment /// public unsafe class ManagedMemorySpace : MemoryAddressSpace { private byte[] memory; /// /// Create a new buffer with the given size, not aligned /// /// Size of buffer public ManagedMemorySpace(UInt32 size) : this(size, 1, false) { } /// /// Create a Managed buffer reference to an existing buffer /// This can only be used for unaligned buffers. /// /// Existing byte buffer public ManagedMemorySpace(byte[] buffer) : base(0, (uint)buffer.Length) { fixed (byte* bodystart = &buffer[0]) { this.Offset = (UInt32)bodystart; } } /// /// Create a new buffer with the given size, aligned on the byte boundary specified /// /// Size of buffer /// Byte Boundary alignment public ManagedMemorySpace(UInt32 size, byte alignment) : this(size, alignment, true) { } /// /// Create a new buffer with the given size, and aligned on the byte boundary if align is true /// /// Size of buffer /// Byte Boundary alignment /// true if buffer should be aligned, false otherwise public ManagedMemorySpace(UInt32 size, byte alignment, bool align) : base(0, size) { memory = new byte[size + alignment - 1]; fixed (byte* bodystart = memory) { this.Offset = (UInt32)bodystart; } if (align == true) { while (this.Offset % alignment != 0) { this.Offset++; } } } /// /// Get or set the byte at the given offset /// /// Address Offset /// Byte value at given offset public byte this[uint offset] { get { return this.Read8(offset); } set { this.Write8(offset, value); } } } }