From e9a011bb9d20a9958c1877cffc11654da48f8c0e Mon Sep 17 00:00:00 2001 From: Viktor Stojanovic Date: Wed, 12 Aug 2026 22:17:50 +0200 Subject: [PATCH 1/8] Add IMemorySlice interface --- src/IMemorySlice.cs | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/IMemorySlice.cs diff --git a/src/IMemorySlice.cs b/src/IMemorySlice.cs new file mode 100644 index 0000000..b6ebb88 --- /dev/null +++ b/src/IMemorySlice.cs @@ -0,0 +1,9 @@ +namespace SharpAllocators; + +public unsafe interface IMemorySlice + where TPointer : unmanaged + where TLength : unmanaged +{ + public TPointer* Pointer { get; } + public TLength Length { get; } +} From 9c07a530c5f58064e34191e137c9199fe48cff40 Mon Sep 17 00:00:00 2001 From: Viktor Stojanovic Date: Wed, 12 Aug 2026 22:18:30 +0200 Subject: [PATCH 2/8] Implement IMemorySlice in MemorySlice The MemorySlice struct now implements the IMemorySlice interface to support abstraction and polymorphism. No other changes were made to the struct. --- src/MemorySlice.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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; } From ea5cc219a2719edd6216a25667eca48a9aa0817a Mon Sep 17 00:00:00 2001 From: Viktor Stojanovic Date: Wed, 12 Aug 2026 22:19:38 +0200 Subject: [PATCH 3/8] Update copyright, revise IAllocator Free/Reallocate Updated copyright to reference MIT License and LICENSE.txt. Changed IAllocator::Free to accept T* instead of MemorySlice. Removed Reallocate overload for MemorySlice; only pointer/count version remains. --- src/IAllocator.cs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) 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 +} From bfb16e41307619b6f50e74dfe04e1dd6385dae57 Mon Sep 17 00:00:00 2001 From: Viktor Stojanovic Date: Wed, 12 Aug 2026 22:20:02 +0200 Subject: [PATCH 4/8] Update copyright, Free signature, and Reallocate param Updated copyright to reference MIT License and LICENSE.txt. Changed Free to accept T* instead of MemorySlice. Renamed Reallocate's elementCount to newLength for clarity. --- src/NativeAllocator.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/NativeAllocator.cs b/src/NativeAllocator.cs index 952f910..809e413 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); - return new(reallocatedPointer, elementCount); + return new(reallocatedPointer, newLength); } } From 2b43c729b726cf7b3480733f29365200549010b8 Mon Sep 17 00:00:00 2001 From: Viktor Stojanovic Date: Wed, 12 Aug 2026 22:21:23 +0200 Subject: [PATCH 5/8] Add copyright and license header to IMemorySlice.cs --- src/IMemorySlice.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/IMemorySlice.cs b/src/IMemorySlice.cs index b6ebb88..a0d007e 100644 --- a/src/IMemorySlice.cs +++ b/src/IMemorySlice.cs @@ -1,3 +1,6 @@ +// 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 From 2726a66cdb74be857ab3fb022d34bed2e9f051f6 Mon Sep 17 00:00:00 2001 From: Viktor Stojanovic Date: Thu, 13 Aug 2026 14:19:19 +0200 Subject: [PATCH 6/8] Add LongSpan struct and file header --- src/LongSpan.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/LongSpan.cs 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; +} From 617b576b25da060a44d9f05174919f70ecf3b25d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Stojanovi=C4=87?= Date: Thu, 13 Aug 2026 14:38:22 +0200 Subject: [PATCH 7/8] Update src/IMemorySlice.cs Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- src/IMemorySlice.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/IMemorySlice.cs b/src/IMemorySlice.cs index a0d007e..4211b95 100644 --- a/src/IMemorySlice.cs +++ b/src/IMemorySlice.cs @@ -3,10 +3,10 @@ namespace SharpAllocators; -public unsafe interface IMemorySlice - where TPointer : unmanaged +public unsafe interface IMemorySlice + where TElement : unmanaged where TLength : unmanaged { - public TPointer* Pointer { get; } + public TElement* Pointer { get; } public TLength Length { get; } } From 191036aabce84c6928ef4d88b35b806bd2121bd9 Mon Sep 17 00:00:00 2001 From: Viktor Stojanovic Date: Thu, 13 Aug 2026 15:00:34 +0200 Subject: [PATCH 8/8] Fix Reallocate to use correct byte size for T Previously, NativeMemory.Realloc used newLength as the size, which could under-allocate for types larger than one byte. Now multiplies newLength by sizeof(T) to ensure correct memory allocation and prevent buffer overruns. --- src/NativeAllocator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NativeAllocator.cs b/src/NativeAllocator.cs index 809e413..1860997 100644 --- a/src/NativeAllocator.cs +++ b/src/NativeAllocator.cs @@ -25,7 +25,7 @@ public void Free(T* pointer) where T : unmanaged [MethodImpl(MethodImplOptions.AggressiveInlining)] public MemorySlice Reallocate(T* pointer, nuint newLength) where T : unmanaged { - var reallocatedPointer = (T*)NativeMemory.Realloc(pointer, newLength); + var reallocatedPointer = (T*)NativeMemory.Realloc(pointer, newLength * (nuint)sizeof(T)); return new(reallocatedPointer, newLength); }