diff --git a/src/LibHac/Fs/Buffers/Buffer.cs b/src/LibHac/Fs/Buffers/Buffer.cs deleted file mode 100644 index de6aab11..00000000 --- a/src/LibHac/Fs/Buffers/Buffer.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.ComponentModel; - -// ReSharper disable once CheckNamespace -namespace LibHac.Fs; - -public readonly struct Buffer : IEquatable -{ - public static Buffer Empty => default; - public Memory Memory { get; } - public Span Span => Memory.Span; - public int Length => Memory.Length; - public bool IsNull => Memory.IsEmpty; - - public Buffer(Memory buffer) - { - Memory = buffer; - } - - public static bool operator ==(Buffer left, Buffer right) => left.Memory.Equals(right.Memory); - public static bool operator !=(Buffer left, Buffer right) => !(left == right); - - [EditorBrowsable(EditorBrowsableState.Never)] - public override bool Equals(object obj) => obj is Buffer other && Equals(other); - public bool Equals(Buffer other) => Memory.Equals(other.Memory); - public override int GetHashCode() => Memory.GetHashCode(); -} diff --git a/src/LibHac/Fs/Buffers/IBufferManager.cs b/src/LibHac/Fs/Buffers/IBufferManager.cs index dcc3e0f9..27c94fc8 100644 --- a/src/LibHac/Fs/Buffers/IBufferManager.cs +++ b/src/LibHac/Fs/Buffers/IBufferManager.cs @@ -1,5 +1,5 @@ using System; - +using Buffer = LibHac.Mem.Buffer; using CacheHandle = System.Int64; // ReSharper disable once CheckNamespace diff --git a/src/LibHac/FsSystem/Buffers/BufferManagerUtility.cs b/src/LibHac/FsSystem/Buffers/BufferManagerUtility.cs index e9d4e759..fcaf91ee 100644 --- a/src/LibHac/FsSystem/Buffers/BufferManagerUtility.cs +++ b/src/LibHac/FsSystem/Buffers/BufferManagerUtility.cs @@ -3,7 +3,7 @@ using System.Runtime.CompilerServices; using System.Threading; using LibHac.Diag; using LibHac.Fs; -using Buffer = LibHac.Fs.Buffer; +using Buffer = LibHac.Mem.Buffer; namespace LibHac.FsSystem.Buffers; diff --git a/src/LibHac/FsSystem/Buffers/FileSystemBuddyHeap.cs b/src/LibHac/FsSystem/Buffers/FileSystemBuddyHeap.cs index fff6df7f..5c88d26f 100644 --- a/src/LibHac/FsSystem/Buffers/FileSystemBuddyHeap.cs +++ b/src/LibHac/FsSystem/Buffers/FileSystemBuddyHeap.cs @@ -5,7 +5,7 @@ using System.Runtime.InteropServices; using LibHac.Diag; using LibHac.Fs; using LibHac.Util; -using Buffer = LibHac.Fs.Buffer; +using Buffer = LibHac.Mem.Buffer; // ReSharper disable once CheckNamespace namespace LibHac.FsSystem; diff --git a/src/LibHac/FsSystem/Buffers/FileSystemBufferManager.cs b/src/LibHac/FsSystem/Buffers/FileSystemBufferManager.cs index a2297083..b405c64f 100644 --- a/src/LibHac/FsSystem/Buffers/FileSystemBufferManager.cs +++ b/src/LibHac/FsSystem/Buffers/FileSystemBufferManager.cs @@ -6,7 +6,7 @@ using LibHac.Common; using LibHac.Diag; using LibHac.Fs; using LibHac.Util; -using Buffer = LibHac.Fs.Buffer; +using Buffer = LibHac.Mem.Buffer; using CacheHandle = System.Int64; // ReSharper disable once CheckNamespace diff --git a/src/LibHac/FsSystem/RomFs/RomFsDictionary.cs b/src/LibHac/FsSystem/RomFs/RomFsDictionary.cs index 93543799..ea02f261 100644 --- a/src/LibHac/FsSystem/RomFs/RomFsDictionary.cs +++ b/src/LibHac/FsSystem/RomFs/RomFsDictionary.cs @@ -192,7 +192,7 @@ internal class RomFsDictionary where T : unmanaged if (value != _capacity) { byte[] newBuffer = new byte[value]; - System.Buffer.BlockCopy(Entries, 0, newBuffer, 0, _length); + Buffer.BlockCopy(Entries, 0, newBuffer, 0, _length); Entries = newBuffer; _capacity = value; diff --git a/src/LibHac/FsSystem/Save/BufferedStorage.cs b/src/LibHac/FsSystem/Save/BufferedStorage.cs index 91d10d16..b7ba1d1a 100644 --- a/src/LibHac/FsSystem/Save/BufferedStorage.cs +++ b/src/LibHac/FsSystem/Save/BufferedStorage.cs @@ -7,7 +7,7 @@ using LibHac.Diag; using LibHac.Fs; using LibHac.FsSystem.Buffers; using LibHac.Util; -using Buffer = LibHac.Fs.Buffer; +using Buffer = LibHac.Mem.Buffer; using CacheHandle = System.Int64; namespace LibHac.FsSystem.Save; diff --git a/src/LibHac/Kvdb/AutoBuffer.cs b/src/LibHac/Kvdb/AutoBuffer.cs index f7c00164..48012ae6 100644 --- a/src/LibHac/Kvdb/AutoBuffer.cs +++ b/src/LibHac/Kvdb/AutoBuffer.cs @@ -1,4 +1,5 @@ using System; +using Buffer = LibHac.Mem.Buffer; namespace LibHac.Kvdb; @@ -7,15 +8,15 @@ internal struct AutoBuffer : IDisposable private const int Alignment = 0x10; private MemoryResource _memoryResource; - private MemoryResource.Buffer _buffer; + private Buffer _buffer; - public Span Get() => _buffer.Get(); + public Span Get() => _buffer.Span; public int GetSize() => _buffer.Length; public Result Initialize(long size, MemoryResource memoryResource) { - MemoryResource.Buffer buffer = memoryResource.Allocate(size, Alignment); - if (!buffer.IsValid) + Buffer buffer = memoryResource.Allocate(size, Alignment); + if (buffer.IsNull) return ResultKvdb.AllocationFailed.Log(); _memoryResource = memoryResource; @@ -26,7 +27,7 @@ internal struct AutoBuffer : IDisposable public void Dispose() { - if (_buffer.IsValid) + if (!_buffer.IsNull) { _memoryResource.Deallocate(ref _buffer, Alignment); } diff --git a/src/LibHac/Kvdb/FlatMapKeyValueStore.cs b/src/LibHac/Kvdb/FlatMapKeyValueStore.cs index 2924a54d..185405f3 100644 --- a/src/LibHac/Kvdb/FlatMapKeyValueStore.cs +++ b/src/LibHac/Kvdb/FlatMapKeyValueStore.cs @@ -5,6 +5,7 @@ using LibHac.Common; using LibHac.Diag; using LibHac.Fs; using LibHac.Fs.Fsa; +using Buffer = LibHac.Mem.Buffer; namespace LibHac.Kvdb; @@ -284,8 +285,8 @@ public class FlatMapKeyValueStore : IDisposable where TKey : unmanaged, IE if (rc.IsFailure()) return rc; // Allocate memory for value. - MemoryResource.Buffer newValue = _memoryResource.Allocate(valueSize, Alignment); - if (!newValue.IsValid) + Buffer newValue = _memoryResource.Allocate(valueSize, Alignment); + if (newValue.IsNull) return ResultKvdb.AllocationFailed.Log(); bool success = false; @@ -294,7 +295,7 @@ public class FlatMapKeyValueStore : IDisposable where TKey : unmanaged, IE // Read key and value. Unsafe.SkipInit(out TKey key); - rc = reader.ReadKeyValue(SpanHelpers.AsByteSpan(ref key), newValue.Get()); + rc = reader.ReadKeyValue(SpanHelpers.AsByteSpan(ref key), newValue.Span); if (rc.IsFailure()) return rc; rc = _index.AppendUnsafe(in key, newValue); @@ -376,9 +377,9 @@ public class FlatMapKeyValueStore : IDisposable where TKey : unmanaged, IE public struct KeyValue { public TKey Key; - public MemoryResource.Buffer Value; + public Buffer Value; - public KeyValue(in TKey key, MemoryResource.Buffer value) + public KeyValue(in TKey key, Buffer value) { Key = key; Value = value; @@ -459,11 +460,11 @@ public class FlatMapKeyValueStore : IDisposable where TKey : unmanaged, IE } // Allocate new value. - MemoryResource.Buffer newValue = _memoryResource.Allocate(value.Length, Alignment); - if (!newValue.IsValid) + Buffer newValue = _memoryResource.Allocate(value.Length, Alignment); + if (newValue.IsNull) return ResultKvdb.AllocationFailed.Log(); - value.CopyTo(newValue.Get()); + value.CopyTo(newValue.Span); // Add the new entry to the list. _entries[index] = new KeyValue(in key, newValue); @@ -479,7 +480,7 @@ public class FlatMapKeyValueStore : IDisposable where TKey : unmanaged, IE /// The key to add. /// The value to add. /// The of the operation. - public Result AppendUnsafe(in TKey key, MemoryResource.Buffer value) + public Result AppendUnsafe(in TKey key, Buffer value) { if (_count >= _capacity) return ResultKvdb.OutOfKeyResource.Log(); @@ -647,11 +648,11 @@ public class FlatMapKeyValueStore : IDisposable where TKey : unmanaged, IE } public ref KeyValue Get() => ref _entries[_index]; - public Span GetValue() => _entries[_index].Value.Get(); + public Span GetValue() => _entries[_index].Value.Span; public ref T GetValue() where T : unmanaged { - return ref SpanHelpers.AsStruct(_entries[_index].Value.Get()); + return ref SpanHelpers.AsStruct(_entries[_index].Value.Span); } public void Next() => _index++; @@ -720,7 +721,7 @@ public class FlatMapKeyValueStore : IDisposable where TKey : unmanaged, IE } public ref readonly KeyValue Get() => ref _entries[_index]; - public ReadOnlySpan GetValue() => _entries[_index].Value.Get(); + public ReadOnlySpan GetValue() => _entries[_index].Value.Span; public void Next() => _index++; public bool IsEnd() => _index == _length; diff --git a/src/LibHac/Mem/Buffer.cs b/src/LibHac/Mem/Buffer.cs new file mode 100644 index 00000000..a2070ad9 --- /dev/null +++ b/src/LibHac/Mem/Buffer.cs @@ -0,0 +1,48 @@ +using System; +using System.ComponentModel; + +namespace LibHac.Mem; + +/// +/// Represents a region of memory allocated by a . +/// +public struct Buffer : IEquatable +{ + private Memory _memory; + + public static Buffer Empty => default; + + /// + /// A field where implementers can store info about the . + /// + internal object Extra { get; } + + /// + /// The length of the buffer in bytes. + /// + public readonly int Length => _memory.Length; + + /// + /// Gets a from the . + /// + public readonly Span Span => _memory.Span; + + /// + /// Returns if the is not valid. + /// + public readonly bool IsNull => _memory.IsEmpty; + + internal Buffer(Memory memory, object extra = null) + { + _memory = memory; + Extra = extra; + } + + public static bool operator ==(Buffer left, Buffer right) => left._memory.Equals(right._memory); + public static bool operator !=(Buffer left, Buffer right) => !(left == right); + + [EditorBrowsable(EditorBrowsableState.Never)] + public override bool Equals(object obj) => obj is Buffer other && Equals(other); + public bool Equals(Buffer other) => _memory.Equals(other._memory); + public override int GetHashCode() => _memory.GetHashCode(); +} \ No newline at end of file diff --git a/src/LibHac/MemoryResource.cs b/src/LibHac/MemoryResource.cs index 568457ff..842731f8 100644 --- a/src/LibHac/MemoryResource.cs +++ b/src/LibHac/MemoryResource.cs @@ -1,6 +1,7 @@ using System; using System.Buffers; using System.Runtime.CompilerServices; +using Buffer = LibHac.Mem.Buffer; namespace LibHac; @@ -28,40 +29,6 @@ public abstract class MemoryResource protected abstract Buffer DoAllocate(long size, int alignment); protected abstract void DoDeallocate(Buffer buffer, int alignment); protected abstract bool DoIsEqual(MemoryResource other); - - /// - /// Represents a region of memory allocated by a . - /// - public struct Buffer - { - private Memory _memory; - - /// - /// A field where implementers can store info about the . - /// - internal object Extra { get; } - - /// - /// The length of the buffer in bytes. - /// - public readonly int Length => _memory.Length; - - /// - /// Gets a span from the . - /// - public readonly Span Get() => _memory.Span; - - /// - /// Returns if the is valid. - /// - public readonly bool IsValid => !_memory.Equals(default); - - internal Buffer(Memory memory, object extra = null) - { - _memory = memory; - Extra = extra; - } - } } public class ArrayPoolMemoryResource : MemoryResource diff --git a/tests/LibHac.Tests/FsSystem/FileSystemBufferManagerTests.cs b/tests/LibHac.Tests/FsSystem/FileSystemBufferManagerTests.cs index 4860bd93..e98150f6 100644 --- a/tests/LibHac.Tests/FsSystem/FileSystemBufferManagerTests.cs +++ b/tests/LibHac.Tests/FsSystem/FileSystemBufferManagerTests.cs @@ -1,5 +1,6 @@ using LibHac.Fs; using LibHac.FsSystem; +using LibHac.Mem; using Xunit; namespace LibHac.Tests.FsSystem; diff --git a/tests/LibHac.Tests/Kvdb/FlatMapKeyValueStoreTests.cs b/tests/LibHac.Tests/Kvdb/FlatMapKeyValueStoreTests.cs index f3832948..c19f88c1 100644 --- a/tests/LibHac.Tests/Kvdb/FlatMapKeyValueStoreTests.cs +++ b/tests/LibHac.Tests/Kvdb/FlatMapKeyValueStoreTests.cs @@ -142,7 +142,7 @@ public class FlatMapKeyValueStoreTests ref FlatMapKeyValueStore.KeyValue kv = ref iterator.Get(); Assert.Equal(expectedKey, kv.Key); - Assert.Equal(expectedValue, kv.Value.Get().ToArray()); + Assert.Equal(expectedValue, kv.Value.Span.ToArray()); iterator.Next(); } @@ -299,7 +299,7 @@ public class FlatMapKeyValueStoreTests ref FlatMapKeyValueStore.KeyValue kv = ref iterator.Get(); Assert.Equal(expectedKey, kv.Key); - Assert.Equal(expectedValue, kv.Value.Get().ToArray()); + Assert.Equal(expectedValue, kv.Value.Span.ToArray()); iterator.Next(); } @@ -576,7 +576,7 @@ public class FlatMapKeyValueStoreTests ref FlatMapKeyValueStore.KeyValue kv = ref iterator.Get(); Assert.Equal(expectedKey, kv.Key); - Assert.Equal(expectedValue, kv.Value.Get().ToArray()); + Assert.Equal(expectedValue, kv.Value.Span.ToArray()); iterator.Next(); }