Skip to content

Commit

Permalink
Allow a token to be valid only when created by the allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Jul 14, 2024
1 parent f9be58e commit 7c03db9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/XenoAtom.Allocators.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;

namespace XenoAtom.Allocators.Tests;
Expand All @@ -10,7 +11,22 @@ public partial class BasicTests
public static MemoryAddress BaseAddress => 0xFE00_1200_0000_0000;

public static MemorySize BaseChunkSize => 65536;

[TestMethod]
public void TestToken()
{
var token = new TlsfAllocationToken();
Assert.IsFalse(token.IsValid);

token = new TlsfAllocationToken(0);
Assert.IsTrue(token.IsValid);
Assert.AreEqual(0, token.BlockIndex);

token = new TlsfAllocationToken(1);
Assert.IsTrue(token.IsValid);
Assert.AreEqual(1, token.BlockIndex);
}

[TestMethod]
public async Task TestAllocate1()
{
Expand Down
48 changes: 44 additions & 4 deletions src/XenoAtom.Allocators/TlsfAllocationToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,60 @@
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using System;
using System.Runtime.CompilerServices;

namespace XenoAtom.Allocators;

/// <summary>
/// A token representing an allocation from a <see cref="TlsfAllocator"/> returned by <see cref="TlsfAllocation.Token"/>.
/// </summary>
public readonly record struct TlsfAllocationToken
public readonly struct TlsfAllocationToken : IEquatable<TlsfAllocationToken>
{
internal TlsfAllocationToken(uint blockIndex)
private readonly int _blockIndex;

internal TlsfAllocationToken(int blockIndex)
{
// Block index is stored with the operator ~ to be able to detect if the token is valid or not.
_blockIndex = ~blockIndex;
}

/// <summary>
/// Returns true if this token is valid.
/// </summary>
public bool IsValid
{
BlockIndex = blockIndex;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _blockIndex < 0;
}

internal readonly uint BlockIndex;
internal int BlockIndex => ~_blockIndex;

/// <inheritdoc />
public override string ToString() => $"TlsfAllocationToken({BlockIndex})";

public bool Equals(TlsfAllocationToken other)
{
return _blockIndex == other._blockIndex;
}

public override bool Equals(object? obj)
{
return obj is TlsfAllocationToken other && Equals(other);
}

public override int GetHashCode()
{
return _blockIndex;
}

public static bool operator ==(TlsfAllocationToken left, TlsfAllocationToken right)
{
return left.Equals(right);
}

public static bool operator !=(TlsfAllocationToken left, TlsfAllocationToken right)
{
return !left.Equals(right);
}
}
4 changes: 2 additions & 2 deletions src/XenoAtom.Allocators/TlsfAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public bool TryAllocate(MemorySize size, out TlsfAllocation allocation)
InsertBlockIntoFreeList(ref freeBlock, freeBlockIndex, newFirstLevelIndex, newSecondLevelIndex);
}

allocation = new TlsfAllocation(new((uint)usedBlockIndex), chunk.Info.Id, (ulong)chunk.Info.BaseAddress + offsetIntoChunk, size);
allocation = new TlsfAllocation(new(usedBlockIndex), chunk.Info.Id, (ulong)chunk.Info.BaseAddress + offsetIntoChunk, size);
}
else
{
Expand All @@ -184,7 +184,7 @@ public bool TryAllocate(MemorySize size, out TlsfAllocation allocation)
chunk.FreeBlockCount--;
Debug.Assert(chunk.FreeBlockCount >= 0);

allocation = new TlsfAllocation(new((uint)freeBlockIndex), chunk.Info.Id, (ulong)chunk.Info.BaseAddress + offsetIntoChunk, size);
allocation = new TlsfAllocation(new(freeBlockIndex), chunk.Info.Id, (ulong)chunk.Info.BaseAddress + offsetIntoChunk, size);
}

return true;
Expand Down
1 change: 1 addition & 0 deletions src/XenoAtom.Allocators/XenoAtom.Allocators.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
<PackageReference Include="MinVer" Version="5.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit 7c03db9

Please sign in to comment.