Skip to content

Commit

Permalink
Breaking change: IMemoryChunkAllocator.FreeChunk using MemoryChunkId …
Browse files Browse the repository at this point in the history
…chunk. Propagates ChunkId to TlfsAllocation.
  • Loading branch information
xoofx committed Jul 6, 2024
1 parent 331649f commit f9be58e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
6 changes: 4 additions & 2 deletions doc/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ public unsafe class BasicChunkAllocator : IMemoryChunkAllocator
return true;
}

public void FreeChunk(in MemoryChunk chunk)
public void FreeChunk(MemoryChunkId chunkId)
{
var index = (int)chunkId.Value;
var chunk = _chunks[index];
NativeMemory.AlignedFree((void*)(ulong)chunk.BaseAddress);
_chunks.Remove((int)chunk.Id.Value);
_chunks.Remove(index);
}
}
```
Expand Down
5 changes: 3 additions & 2 deletions src/XenoAtom.Allocators.Bench/BenchAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ public bool TryAllocateChunk(MemorySize minSize, out MemoryChunk chunk)
return true;
}

public void FreeChunk(in MemoryChunk chunk)
public void FreeChunk(MemoryChunkId chunkId)
{
var chunk = _chunks[(int)chunkId.Value];
NativeMemory.AlignedFree((void*)(ulong)chunk.BaseAddress);
_chunks.Remove((int)chunk.Id.Value);
_chunks.Remove((int)chunkId.Value);
}
}
}
4 changes: 2 additions & 2 deletions src/XenoAtom.Allocators.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,11 +554,11 @@ public bool TryAllocateChunk(MemorySize size, out MemoryChunk chunk)
return true;
}

public void FreeChunk(in MemoryChunk chunk)
public void FreeChunk(MemoryChunkId chunkId)
{
for (var i = 0; i < RequestedChunkAllocations.Count; i++)
{
if (RequestedChunkAllocations[i].Id == chunk.Id)
if (RequestedChunkAllocations[i].Id == chunkId)
{
RequestedChunkAllocations.RemoveAt(i);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/XenoAtom.Allocators/IMemoryChunkAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ public interface IMemoryChunkAllocator
/// Frees a chunk of memory.
/// </summary>
/// <param name="chunk">The original chunk that was allocated.</param>
void FreeChunk(in MemoryChunk chunk);
void FreeChunk(MemoryChunkId chunk);
}
8 changes: 7 additions & 1 deletion src/XenoAtom.Allocators/TlsfAllocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ namespace XenoAtom.Allocators;
/// </summary>
public readonly record struct TlsfAllocation
{
internal TlsfAllocation(TlsfAllocationToken token, MemoryAddress address, MemorySize size)
internal TlsfAllocation(TlsfAllocationToken token, MemoryChunkId chunkId, MemoryAddress address, MemorySize size)
{
Token = token;
ChunkId = chunkId;
Address = address;
Size = size;
}
Expand All @@ -21,6 +22,11 @@ internal TlsfAllocation(TlsfAllocationToken token, MemoryAddress address, Memory
/// </summary>
public readonly TlsfAllocationToken Token;

/// <summary>
/// Gets the chunk id of this allocated block belongs to.
/// </summary>
public readonly MemoryChunkId ChunkId;

/// <summary>
/// Gets the address of the allocated block.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions src/XenoAtom.Allocators/TlsfAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public bool TryAllocate(MemorySize size, out TlsfAllocation allocation)
ref var previousBlock = ref GetBlockAt(freeBlock.PhysicalLink.Previous);
previousBlock.PhysicalLink.Next = usedBlockIndex;
Debug.Assert(previousBlock.OffsetIntoChunk + previousBlock.Size == offsetIntoChunk);
}
}

Debug.Assert(usedBlock.OffsetIntoChunk + size == freeBlock.OffsetIntoChunk);
freeBlock.PhysicalLink.Previous = usedBlockIndex;
Expand All @@ -170,7 +170,7 @@ public bool TryAllocate(MemorySize size, out TlsfAllocation allocation)
InsertBlockIntoFreeList(ref freeBlock, freeBlockIndex, newFirstLevelIndex, newSecondLevelIndex);
}

allocation = new TlsfAllocation(new((uint)usedBlockIndex), (ulong)chunk.Info.BaseAddress + offsetIntoChunk, size);
allocation = new TlsfAllocation(new((uint)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), (ulong)chunk.Info.BaseAddress + offsetIntoChunk, size);
allocation = new TlsfAllocation(new((uint)freeBlockIndex), chunk.Info.Id, (ulong)chunk.Info.BaseAddress + offsetIntoChunk, size);
}

return true;
Expand Down Expand Up @@ -285,7 +285,7 @@ public void Reset()
{
for (int i = 0; i < _chunks.Count; i++)
{
_context.FreeChunk(_chunks[i].Info);
_context.FreeChunk(_chunks[i].Info.Id);
}
_chunks.Clear();
_blockCount = 0;
Expand Down

0 comments on commit f9be58e

Please sign in to comment.