Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/SharpArena/Allocators/ArenaAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ public ArenaAllocator(
seg->Size = segSize;
seg->Base = mem + sizeof(ArenaSegment);
#if DEBUG
seg->HeadCanary = ArenaSegment.Canary;
seg->TailCanary = ArenaSegment.Canary;
seg->HeadCanary = ArenaSegment.CANARY;
seg->TailCanary = ArenaSegment.CANARY;
#endif
return seg;
}
Expand Down
6 changes: 3 additions & 3 deletions src/SharpArena/Allocators/ArenaSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public unsafe struct ArenaSegment
/// </summary>
public ulong TailCanary;

internal const ulong Canary = 0xDEADBEEFCAFEBABEul;
internal const ulong CANARY = 0xDEADBEEFCAFEBABEul;
#endif

/// <summary>
Expand Down Expand Up @@ -51,8 +51,8 @@ public unsafe struct ArenaSegment
public bool TryAlloc(nuint size, nuint align, out void* ptr)
{
#if DEBUG
Debug.Assert(HeadCanary == Canary, "Arena segment head canary corrupted");
Debug.Assert(TailCanary == Canary, "Arena segment tail canary corrupted");
Debug.Assert(HeadCanary == CANARY, "Arena segment head canary corrupted");
Debug.Assert(TailCanary == CANARY, "Arena segment tail canary corrupted");
#endif
if (align == 0)
{
Expand Down
18 changes: 8 additions & 10 deletions src/SharpArena/Collections/ArenaBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ public ArenaBlockList(ArenaAllocator arena, nuint blockSize = DefaultBlockSize)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void CheckAliveThrowIfNot()
{
private readonly void CheckAliveThrowIfNot() =>
UnsafeHelpers.CheckAliveThrowIfNot(_arena, _generation, nameof(ArenaBlockList<T>));
}

private static ArenaBlock<T>* CreateBlock(ArenaAllocator arena, nuint capacity)
{
Expand Down Expand Up @@ -129,7 +127,7 @@ private void CheckAliveThrowIfNot()
/// <summary>
/// Gets the number of elements stored across all blocks.
/// </summary>
public nuint Count
public readonly nuint Count
{
get
{
Expand All @@ -141,7 +139,7 @@ public nuint Count
/// <summary>
/// Gets the total allocated capacity across all blocks.
/// </summary>
public nuint Capacity
public readonly nuint Capacity
{
get
{
Expand Down Expand Up @@ -192,19 +190,19 @@ public void Add(in T value)
/// </summary>
/// <returns>An enumerator over the list.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Enumerator GetEnumerator()
public readonly Enumerator GetEnumerator()
{
CheckAliveThrowIfNot();
return new Enumerator(_head, _arena);
}

IEnumerator<T> IEnumerable<T>.GetEnumerator()
readonly IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
CheckAliveThrowIfNot();
return new Enumerator(_head, _arena);
}

IEnumerator IEnumerable.GetEnumerator()
readonly IEnumerator IEnumerable.GetEnumerator()
{
CheckAliveThrowIfNot();
return new Enumerator(_head, _arena);
Expand Down Expand Up @@ -235,7 +233,7 @@ internal Enumerator(ArenaBlock<T>* head, ArenaAllocator arena)
/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
/// </summary>
public T Current => _current;
public readonly T Current => _current;

/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
Expand Down Expand Up @@ -299,7 +297,7 @@ public void Dispose()
/// Copies the list contents into a contiguous span allocated from the arena.
/// </summary>
/// <returns>A span that owns a contiguous copy of the stored values.</returns>
public ReadOnlySpan<T> GetSpan()
public readonly ReadOnlySpan<T> GetSpan()
{
CheckAliveThrowIfNot();
var total = Count;
Expand Down
42 changes: 21 additions & 21 deletions src/SharpArena/Collections/ArenaDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void AllocateTable(int capacity)
private readonly void CheckAlive() =>
UnsafeHelpers.CheckAliveThrowIfNot(_arena, _generation, nameof(ArenaDictionary<,>));

private int FindBucket(TKey key)
private readonly int FindBucket(TKey key)
{
uint capacity = (uint)_header->Capacity;
int* buckets = _header->Buckets;
Expand Down Expand Up @@ -110,7 +110,7 @@ public int Count
/// <summary>
/// Gets a value indicating whether the <see cref="ArenaDictionary{TKey, TValue}"/> is read-only.
/// </summary>
public bool IsReadOnly => false;
public readonly bool IsReadOnly => false;

/// <summary>
/// Gets or sets the element with the specified key.
Expand All @@ -120,7 +120,7 @@ public int Count
/// <exception cref="KeyNotFoundException">The property is retrieved and <paramref name="key"/> is not found.</exception>
public TValue this[TKey key]
{
get => TryGetValue(key, out var value) ?
readonly get => TryGetValue(key, out var value) ?
value : throw new KeyNotFoundException();
set => AddOrUpdate(key, value);
}
Expand Down Expand Up @@ -236,7 +236,7 @@ private void Grow()
/// </summary>
/// <param name="key">The key to locate in the <see cref="ArenaDictionary{TKey, TValue}"/>.</param>
/// <returns><see langword="true"/> if the <see cref="ArenaDictionary{TKey, TValue}"/> contains an element with the key; otherwise, <see langword="false"/>.</returns>
public bool ContainsKey(TKey key)
public readonly bool ContainsKey(TKey key)
{
CheckAlive();
int bucketIdx = FindBucket(key);
Expand All @@ -249,7 +249,7 @@ public bool ContainsKey(TKey key)
/// </summary>
/// <param name="key">The key to locate in the <see cref="ArenaDictionary{TKey, TValue}"/>.</param>
/// <returns><see langword="true"/> if the <see cref="ArenaDictionary{TKey, TValue}"/> contains an element with the key; otherwise, <see langword="false"/>.</returns>
public bool ContainsKey(ReadOnlySpan<char> key)
public readonly bool ContainsKey(ReadOnlySpan<char> key)
{
if (typeof(TKey) == typeof(ArenaUtf16String))
{
Expand Down Expand Up @@ -310,7 +310,7 @@ public bool ContainsKey(ReadOnlySpan<char> key)
/// </summary>
/// <param name="key">The key to locate in the <see cref="ArenaDictionary{TKey, TValue}"/>.</param>
/// <returns><see langword="true"/> if the <see cref="ArenaDictionary{TKey, TValue}"/> contains an element with the key; otherwise, <see langword="false"/>.</returns>
public bool ContainsKey(ReadOnlySpan<byte> key)
public readonly bool ContainsKey(ReadOnlySpan<byte> key)
{
if (typeof(TKey) == typeof(ArenaUtf8String))
{
Expand Down Expand Up @@ -371,7 +371,7 @@ public bool ContainsKey(ReadOnlySpan<byte> key)
/// <param name="key">The key whose value to get.</param>
/// <param name="value">When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the <paramref name="value"/> parameter. This parameter is passed uninitialized.</param>
/// <returns><see langword="true"/> if the <see cref="ArenaDictionary{TKey, TValue}"/> contains an element with the specified key; otherwise, <see langword="false"/>.</returns>
public bool TryGetValue(TKey key, out TValue value)
public readonly bool TryGetValue(TKey key, out TValue value)
{
CheckAlive();
int bucketIdx = FindBucket(key);
Expand Down Expand Up @@ -465,7 +465,7 @@ public bool TryGetValue(ReadOnlySpan<char> key, out TValue value)
/// <param name="key">The key whose value to get.</param>
/// <param name="value">When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the <paramref name="value"/> parameter. This parameter is passed uninitialized.</param>
/// <returns><see langword="true"/> if the <see cref="ArenaDictionary{TKey, TValue}"/> contains an element with the specified key; otherwise, <see langword="false"/>.</returns>
public bool TryGetValue(ReadOnlySpan<byte> key, out TValue value)
public readonly bool TryGetValue(ReadOnlySpan<byte> key, out TValue value)
{
if (typeof(TKey) == typeof(ArenaUtf8String))
{
Expand Down Expand Up @@ -590,7 +590,7 @@ public void TrimExcess()
/// </summary>
public bool Remove(TKey key) => throw new NotSupportedException();
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) => Add(item.Key, item.Value);
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
readonly bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
{
if (TryGetValue(item.Key, out var value))
{
Expand Down Expand Up @@ -618,8 +618,8 @@ void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[]
/// Returns an enumerator that iterates through the <see cref="ArenaDictionary{TKey, TValue}"/>.
/// </summary>
/// <returns>A <see cref="IEnumerator{KeyValuePair}"/> for the <see cref="ArenaDictionary{TKey, TValue}"/>.</returns>
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => new Enumerator(this);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public readonly IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => new Enumerator(this);
readonly IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

/// <summary>
/// Enumerates the elements of an <see cref="ArenaDictionary{TKey, TValue}"/>.
Expand Down Expand Up @@ -657,12 +657,12 @@ public bool MoveNext()
/// <summary>
/// Gets the element at the current position of the enumerator.
/// </summary>
public KeyValuePair<TKey, TValue> Current => _current;
public readonly KeyValuePair<TKey, TValue> Current => _current;

/// <summary>
/// Gets the element at the current position of the enumerator.
/// </summary>
object IEnumerator.Current => _current;
readonly object IEnumerator.Current => _current;

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Expand All @@ -679,11 +679,11 @@ private struct KeyCollection : ICollection<TKey>
{
private readonly ArenaDictionary<TKey, TValue> _dict;
public KeyCollection(ArenaDictionary<TKey, TValue> dict) => _dict = dict;
public int Count => _dict.Count;
public bool IsReadOnly => true;
public readonly int Count => _dict.Count;
public readonly bool IsReadOnly => true;
public void Add(TKey item) => throw new NotSupportedException();
public void Clear() => throw new NotSupportedException();
public bool Contains(TKey item) => _dict.ContainsKey(item);
public readonly bool Contains(TKey item) => _dict.ContainsKey(item);
public void CopyTo(TKey[] array, int arrayIndex)
{
int count = _dict.Count;
Expand All @@ -694,10 +694,10 @@ public void CopyTo(TKey[] array, int arrayIndex)
}
}
public bool Remove(TKey item) => throw new NotSupportedException();
public IEnumerator<TKey> GetEnumerator() => new KeyEnumerator(_dict);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public readonly IEnumerator<TKey> GetEnumerator() => new KeyEnumerator(_dict);
readonly IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

private unsafe struct KeyEnumerator : IEnumerator<TKey>
private struct KeyEnumerator : IEnumerator<TKey>
{
private readonly ArenaDictionary<TKey, TValue> _dict;
private int _index;
Expand All @@ -723,8 +723,8 @@ public bool MoveNext()
return false;
}

public TKey Current => _current;
object IEnumerator.Current => _current;
public readonly TKey Current => _current;
readonly object IEnumerator.Current => _current;
public void Dispose() { }
public void Reset() => _index = -1;
}
Expand Down
18 changes: 8 additions & 10 deletions src/SharpArena/Collections/ArenaList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,13 @@ public ArenaList(ArenaAllocator arena, int initialCapacity = 16)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void CheckAliveThrowIfNot()
{
private readonly void CheckAliveThrowIfNot() =>
UnsafeHelpers.CheckAliveThrowIfNot(_arena, _generation, nameof(ArenaList<T>));
}

/// <summary>
/// Gets the number of elements stored in the list.
/// </summary>
public int Length
public readonly int Length
{
get
{
Expand Down Expand Up @@ -121,7 +119,7 @@ public bool IsEmpty
/// </summary>
/// <param name="index">The zero-based index of the item to access.</param>
/// <returns>A reference to the item at the requested index.</returns>
public ref T this[int index]
public readonly ref T this[int index]
{
get
{
Expand Down Expand Up @@ -258,7 +256,7 @@ public void Reset()
/// <summary>
/// Gets a pointer to the raw unmanaged data of the list.
/// </summary>
public T* AsPtr
public readonly T* AsPtr
{
get
{
Expand All @@ -270,13 +268,13 @@ public T* AsPtr
/// <summary>
/// Gets a writable span over the list's contents.
/// </summary>
public Span<T> Span => AsSpan();
public readonly Span<T> Span => AsSpan();

/// <summary>
/// Provides a writable span view of the stored elements.
/// </summary>
/// <returns>A span referencing the list contents.</returns>
public Span<T> AsSpan()
public readonly Span<T> AsSpan()
{
CheckAliveThrowIfNot();
return new Span<T>((T*)_header->Data, _header->Count);
Expand All @@ -286,7 +284,7 @@ public Span<T> AsSpan()
/// Provides a read-only span view of the stored elements.
/// </summary>
/// <returns>A read-only span referencing the list contents.</returns>
public ReadOnlySpan<T> AsReadOnlySpan()
public readonly ReadOnlySpan<T> AsReadOnlySpan()
{
CheckAliveThrowIfNot();
return new ReadOnlySpan<T>((T*)_header->Data, _header->Count);
Expand All @@ -297,7 +295,7 @@ public ReadOnlySpan<T> AsReadOnlySpan()
/// </summary>
/// <param name="item">The object to locate in the list.</param>
/// <returns><see langword="true"/> if <paramref name="item"/> is found; otherwise, <see langword="false"/>.</returns>
public bool Contains(in T item)
public readonly bool Contains(in T item)
{
CheckAliveThrowIfNot();
var span = AsSpan();
Expand Down
Loading
Loading