using System;
using System.Buffers;
using System.Runtime.InteropServices;
namespace Ryujinx.Memory
{
///
/// A concrete implementation of ,
/// with methods to help build a full sequence.
///
public sealed class BytesReadOnlySequenceSegment : ReadOnlySequenceSegment
{
public BytesReadOnlySequenceSegment(Memory memory) => Memory = memory;
public BytesReadOnlySequenceSegment Append(Memory memory)
{
var nextSegment = new BytesReadOnlySequenceSegment(memory)
{
RunningIndex = RunningIndex + Memory.Length
};
Next = nextSegment;
return nextSegment;
}
///
/// Attempts to determine if the current and are contiguous.
/// Only works if both were created by a .
///
/// The segment to check if continuous with the current one
/// The starting address of the contiguous segment
/// The size of the contiguous segment
/// True if the segments are contiguous, otherwise false
public unsafe bool IsContiguousWith(Memory other, out nuint contiguousStart, out int contiguousSize)
{
if (MemoryMarshal.TryGetMemoryManager>(Memory, out var thisMemoryManager) &&
MemoryMarshal.TryGetMemoryManager>(other, out var otherMemoryManager) &&
thisMemoryManager.Pointer + thisMemoryManager.Length == otherMemoryManager.Pointer)
{
contiguousStart = (nuint)thisMemoryManager.Pointer;
contiguousSize = thisMemoryManager.Length + otherMemoryManager.Length;
return true;
}
else
{
contiguousStart = 0;
contiguousSize = 0;
return false;
}
}
///
/// Replaces the current value with the one provided.
///
/// The new segment to hold in this
public void Replace(Memory memory)
=> Memory = memory;
}
}