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
10 changes: 3 additions & 7 deletions src/IAllocator.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
// Copyright (c) 2026 Viktor Stojanović. All rights reserved.
// Copyright (c) 2026 Viktor Stojanović. All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace SharpAllocators;

public unsafe interface IAllocator
{
public MemorySlice<T> Allocate<T>(nuint elementCount) where T : unmanaged;
public void Free<T>(MemorySlice<T> memorySlice) where T : unmanaged;
public void Free<T>(T* pointer) where T : unmanaged;
public MemorySlice<T> Reallocate<T>(T* pointer, nuint elementCount) where T : unmanaged;
public MemorySlice<T> Reallocate<T>(MemorySlice<T> memorySlice) where T : unmanaged
{
return Reallocate(memorySlice.Pointer, memorySlice.Length);
}
}
}
12 changes: 12 additions & 0 deletions src/IMemorySlice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2026 Viktor Stojanović. All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace SharpAllocators;

public unsafe interface IMemorySlice<TElement, TLength>
where TElement : unmanaged
where TLength : unmanaged
{
public TElement* Pointer { get; }
public TLength Length { get; }
}
11 changes: 11 additions & 0 deletions src/LongSpan.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) 2026 Viktor Stojanović. All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace SharpAllocators;

public readonly ref struct LongSpan<T>
{
private readonly ref T _reference;

Check warning on line 8 in src/LongSpan.cs

View workflow job for this annotation

GitHub Actions / build

The field 'LongSpan<T>._reference' is never used

Check warning on line 8 in src/LongSpan.cs

View workflow job for this annotation

GitHub Actions / build

The field 'LongSpan<T>._reference' is never used
public readonly long Length { get; }
public readonly bool IsEmpty => Length is 0;
}
Comment thread
vix127 marked this conversation as resolved.
3 changes: 2 additions & 1 deletion src/MemorySlice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

namespace SharpAllocators;

public readonly unsafe struct MemorySlice<T> where T : unmanaged
public readonly unsafe struct MemorySlice<T> : IMemorySlice<T, nuint>
where T : unmanaged
{
public T* Pointer { get; }
public nuint Length { get; }
Expand Down
12 changes: 6 additions & 6 deletions src/NativeAllocator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2026 Viktor Stojanović. All rights reserved.
// Copyright (c) 2026 Viktor Stojanović. All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using System.Runtime.CompilerServices;
Expand All @@ -17,16 +17,16 @@ public MemorySlice<T> Allocate<T>(nuint elementCount) where T : unmanaged
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Free<T>(MemorySlice<T> memorySlice) where T : unmanaged
public void Free<T>(T* pointer) where T : unmanaged
{
NativeMemory.Free(memorySlice.Pointer);
NativeMemory.Free(pointer);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MemorySlice<T> Reallocate<T>(T* pointer, nuint elementCount) where T : unmanaged
public MemorySlice<T> Reallocate<T>(T* pointer, nuint newLength) where T : unmanaged
{
var reallocatedPointer = (T*)NativeMemory.Realloc(pointer, elementCount);
var reallocatedPointer = (T*)NativeMemory.Realloc(pointer, newLength * (nuint)sizeof(T));

return new(reallocatedPointer, elementCount);
return new(reallocatedPointer, newLength);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
Loading