From ada063f2a766dcc184e8e44d4995645d64945dba Mon Sep 17 00:00:00 2001 From: Viktor Stojanovic Date: Sat, 8 Aug 2026 01:04:05 +0200 Subject: [PATCH 1/3] Add MemorySlice type --- src/MemorySlice.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/MemorySlice.cs diff --git a/src/MemorySlice.cs b/src/MemorySlice.cs new file mode 100644 index 0000000..052c4a7 --- /dev/null +++ b/src/MemorySlice.cs @@ -0,0 +1,23 @@ +// 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 unsafe struct MemorySlice where T : unmanaged +{ + public T* Pointer { get; } + public nuint Length { get; } + public nuint ByteLength => Length * (nuint)sizeof(T); + + public MemorySlice(T* pointer, nuint length) + { + Pointer = pointer; + Length = length; + } + + public void Deconstruct(out T* pointer, out nuint lenght) + { + pointer = Pointer; + lenght = Length; + } +} From 64491b28f4aff850d27682bb88275cff847a8f23 Mon Sep 17 00:00:00 2001 From: Viktor Stojanovic Date: Sat, 8 Aug 2026 01:04:34 +0200 Subject: [PATCH 2/3] Change IAllocator to use MemorySlice --- src/IAllocator.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/IAllocator.cs b/src/IAllocator.cs index 85f610a..291f4fb 100644 --- a/src/IAllocator.cs +++ b/src/IAllocator.cs @@ -5,7 +5,11 @@ namespace SharpAllocators; public unsafe interface IAllocator { - public T* Allocate(nuint elementCount) where T : unmanaged; - public void Free(T* pointer) where T : unmanaged; - public T* Reallocate(T* pointer, nuint elementCount) where T : unmanaged; + public MemorySlice Allocate(nuint elementCount) where T : unmanaged; + public void Free(MemorySlice memorySlice) 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 From e7a233dfd6bcdcab496c5130097cebe2952aa473 Mon Sep 17 00:00:00 2001 From: Viktor Stojanovic Date: Sat, 8 Aug 2026 01:04:54 +0200 Subject: [PATCH 3/3] Change NativeAllocator to use MemorySlice --- src/NativeAllocator.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/NativeAllocator.cs b/src/NativeAllocator.cs index fb2327d..952f910 100644 --- a/src/NativeAllocator.cs +++ b/src/NativeAllocator.cs @@ -9,20 +9,24 @@ namespace SharpAllocators; public readonly unsafe struct NativeAllocator : IAllocator { [MethodImpl(MethodImplOptions.AggressiveInlining)] - public T* Allocate(nuint elementCount) where T : unmanaged + public MemorySlice Allocate(nuint elementCount) where T : unmanaged { - return (T*)NativeMemory.Alloc(elementCount * (nuint)sizeof(T)); + var pointer = (T*)NativeMemory.Alloc(elementCount * (nuint)sizeof(T)); + + return new(pointer, elementCount); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Free(T* pointer) where T : unmanaged + public void Free(MemorySlice memorySlice) where T : unmanaged { - NativeMemory.Free(pointer); + NativeMemory.Free(memorySlice.Pointer); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public T* Reallocate(T* pointer, nuint elementCount) where T : unmanaged + public MemorySlice Reallocate(T* pointer, nuint elementCount) where T : unmanaged { - return (T*)NativeMemory.Realloc(pointer, elementCount); + var reallocatedPointer = (T*)NativeMemory.Realloc(pointer, elementCount); + + return new(reallocatedPointer, elementCount); } }