Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AbandonedList size fix #352

Merged
merged 1 commit into from
Jun 12, 2024
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
62 changes: 62 additions & 0 deletions src/Paprika.Tests/Store/MemoryFencingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using FluentAssertions;
using Paprika.Store;

namespace Paprika.Tests.Store;

/// <summary>
/// Tests ensuring that unmanaged structs do no leak out beyond their sizes.
/// </summary>
public class MemoryFencingTests
{
[Test]
public void Int_for_sanity()
{
Alloc<int>(sizeof(int)).Should().Be(0);
}

[Test]
public void AbandonedPage()
{
ref var list = ref Alloc<AbandonedList>(AbandonedList.Size);
list.IsFullyEmpty.Should().BeTrue();
}

[TearDown]
public unsafe void TearDown()
{
while (_alignedAllocations.TryPop(out var alloc))
{
NativeMemory.AlignedFree(alloc.ToPointer());
}
}

private readonly Stack<UIntPtr> _alignedAllocations = new();

private unsafe ref T Alloc<T>(int size, int alignment = sizeof(int))
where T : struct
{
const int fenceSize = 32;

var sizeTotal = (UIntPtr)size + fenceSize * 2;
var memory = NativeMemory.AlignedAlloc(sizeTotal, (UIntPtr)alignment);
NativeMemory.Clear(memory, sizeTotal);

_alignedAllocations.Push((UIntPtr)memory);

var actualStart = new UIntPtr(memory) + fenceSize;

Fence(memory);
Fence((actualStart + (uint)size).ToPointer());

return ref Unsafe.AsRef<T>(actualStart.ToPointer());

static void Fence(void* ptr)
{
var span = new Span<byte>(ptr, fenceSize);
const byte fenceFilling = 0xFF;
span.Fill(fenceFilling);
}
}
}
25 changes: 19 additions & 6 deletions src/Paprika/Store/AbandonedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ namespace Paprika.Store;
[StructLayout(LayoutKind.Explicit, Size = Size)]
public struct AbandonedList
{
/// <summary>
/// The start for spans of <see cref="BatchIds"/> and <see cref="Addresses"/>.
/// </summary>
private const int EntriesStart = DbAddress.Size + sizeof(uint);

private const int Size = Page.PageSize - PageHeader.Size - RootPage.Payload.AbandonedStart - EntriesStart;
public const int Size = Page.PageSize - PageHeader.Size - RootPage.Payload.AbandonedStart - EntriesStart;
private const int EntrySize = sizeof(uint) + DbAddress.Size;
private const int MaxCount = Size / EntrySize;
private const int MaxCount = (Size - EntriesStart) / EntrySize;

[FieldOffset(0)] private DbAddress Current;

[FieldOffset(4)] private uint EntriesCount;
[FieldOffset(DbAddress.Size)] private uint EntriesCount;

[FieldOffset(EntriesStart)] private uint BatchIdStart;

Expand All @@ -28,8 +30,6 @@ public struct AbandonedList
[FieldOffset(MaxCount * sizeof(uint) + EntriesStart)]
private DbAddress AddressStart;

private const int NotFound = -1;

private Span<DbAddress> Addresses => MemoryMarshal.CreateSpan(ref AddressStart, MaxCount);

/// <summary>
Expand Down Expand Up @@ -229,4 +229,17 @@ public long GatherTotalAbandoned(IPageResolver resolver)

return count;
}

public bool IsFullyEmpty
{
get
{
const int notFound = -1;

return Addresses.IndexOfAnyExcept(DbAddress.Null) == notFound &&
BatchIds.IndexOfAnyExcept(default(uint)) == notFound &&
EntriesCount == 0 &&
Current == DbAddress.Null;
}
}
}
Loading