diff --git a/src/IAllocator.cs b/src/IAllocator.cs index 291f4fb..838e43c 100644 --- a/src/IAllocator.cs +++ b/src/IAllocator.cs @@ -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. namespace SharpAllocators; @@ -6,10 +6,6 @@ namespace SharpAllocators; public unsafe interface IAllocator { public MemorySlice Allocate(nuint elementCount) where T : unmanaged; - public void Free(MemorySlice memorySlice) where T : unmanaged; + public void Free(T* pointer) where T : unmanaged; public MemorySlice Reallocate(T* pointer, nuint elementCount) where T : unmanaged; - public MemorySlice Reallocate(MemorySlice memorySlice) where T : unmanaged - { - return Reallocate(memorySlice.Pointer, memorySlice.Length); - } -} \ No newline at end of file +} diff --git a/src/IMemorySlice.cs b/src/IMemorySlice.cs new file mode 100644 index 0000000..4211b95 --- /dev/null +++ b/src/IMemorySlice.cs @@ -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 + where TElement : unmanaged + where TLength : unmanaged +{ + public TElement* Pointer { get; } + public TLength Length { get; } +} diff --git a/src/LongSpan.cs b/src/LongSpan.cs new file mode 100644 index 0000000..f9a6259 --- /dev/null +++ b/src/LongSpan.cs @@ -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 +{ + private readonly ref T _reference; + public readonly long Length { get; } + public readonly bool IsEmpty => Length is 0; +} diff --git a/src/MemorySlice.cs b/src/MemorySlice.cs index 052c4a7..da042cb 100644 --- a/src/MemorySlice.cs +++ b/src/MemorySlice.cs @@ -3,7 +3,8 @@ namespace SharpAllocators; -public readonly unsafe struct MemorySlice where T : unmanaged +public readonly unsafe struct MemorySlice : IMemorySlice + where T : unmanaged { public T* Pointer { get; } public nuint Length { get; } diff --git a/src/NativeAllocator.cs b/src/NativeAllocator.cs index 952f910..1860997 100644 --- a/src/NativeAllocator.cs +++ b/src/NativeAllocator.cs @@ -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; @@ -17,16 +17,16 @@ public MemorySlice Allocate(nuint elementCount) where T : unmanaged } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Free(MemorySlice memorySlice) where T : unmanaged + public void Free(T* pointer) where T : unmanaged { - NativeMemory.Free(memorySlice.Pointer); + NativeMemory.Free(pointer); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public MemorySlice Reallocate(T* pointer, nuint elementCount) where T : unmanaged + public MemorySlice Reallocate(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); } }