diff --git a/src/ImageSharp/Memory/ArrayPoolMemoryManager.cs b/src/ImageSharp/Memory/ArrayPoolMemoryManager.cs index 0cb1e38f8..403464334 100644 --- a/src/ImageSharp/Memory/ArrayPoolMemoryManager.cs +++ b/src/ImageSharp/Memory/ArrayPoolMemoryManager.cs @@ -53,6 +53,18 @@ namespace SixLabors.ImageSharp.Memory return buffer; } + internal override IManagedByteBuffer AllocateManagedByteBuffer(int length, bool clear) + { + byte[] array = this.pool.Rent(length); + var buffer = new ManagedByteBuffer(array, length, this); + if (clear) + { + buffer.Clear(); + } + + return buffer; + } + /// internal override void Release(Buffer buffer) { diff --git a/src/ImageSharp/Memory/IManagedByteBuffer.cs b/src/ImageSharp/Memory/IManagedByteBuffer.cs new file mode 100644 index 000000000..541957f42 --- /dev/null +++ b/src/ImageSharp/Memory/IManagedByteBuffer.cs @@ -0,0 +1,13 @@ +namespace SixLabors.ImageSharp.Memory +{ + /// + /// Represents a byte buffer backed by a managed array. + /// + internal interface IManagedByteBuffer : IBuffer + { + /// + /// Gets the managed array backing this buffer instance. + /// + byte[] Array { get; } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Memory/ManagedByteBuffer.cs b/src/ImageSharp/Memory/ManagedByteBuffer.cs new file mode 100644 index 000000000..17fe945d6 --- /dev/null +++ b/src/ImageSharp/Memory/ManagedByteBuffer.cs @@ -0,0 +1,10 @@ +namespace SixLabors.ImageSharp.Memory +{ + internal class ManagedByteBuffer : Buffer, IManagedByteBuffer + { + internal ManagedByteBuffer(byte[] array, int length, MemoryManager memoryManager) + : base(array, length, memoryManager) + { + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Memory/MemoryManager.cs b/src/ImageSharp/Memory/MemoryManager.cs index 6bad01cea..cac9b785b 100644 --- a/src/ImageSharp/Memory/MemoryManager.cs +++ b/src/ImageSharp/Memory/MemoryManager.cs @@ -22,6 +22,8 @@ namespace SixLabors.ImageSharp.Memory internal abstract Buffer Allocate(int length, bool clear) where T : struct; + internal abstract IManagedByteBuffer AllocateManagedByteBuffer(int length, bool clear); + /// /// Releases the memory allocated for . After this, the buffer /// is no longer usable. diff --git a/src/ImageSharp/Memory/MemoryManagerExtensions.cs b/src/ImageSharp/Memory/MemoryManagerExtensions.cs index 8e1aa9850..877230788 100644 --- a/src/ImageSharp/Memory/MemoryManagerExtensions.cs +++ b/src/ImageSharp/Memory/MemoryManagerExtensions.cs @@ -6,24 +6,29 @@ internal static class MemoryManagerExtensions { /// - /// Allocates a of size . + /// Allocates a of size . /// Note: Depending on the implementation, the buffer may not cleared before /// returning, so it may contain data from an earlier use. /// /// Type of the data stored in the buffer /// The - /// Size of the buffer to allocate + /// Size of the buffer to allocate /// A buffer of values of type . - public static Buffer Allocate(this MemoryManager memoryManager, int size) + public static Buffer Allocate(this MemoryManager memoryManager, int length) where T : struct { - return memoryManager.Allocate(size, false); + return memoryManager.Allocate(length, false); } - public static Buffer AllocateClean(this MemoryManager memoryManager, int size) + public static Buffer AllocateClean(this MemoryManager memoryManager, int length) where T : struct { - return memoryManager.Allocate(size, true); + return memoryManager.Allocate(length, true); + } + + public static IManagedByteBuffer AllocateCleanManagedByteBuffer(this MemoryManager memoryManager, int length) + { + return memoryManager.AllocateManagedByteBuffer(length, true); } public static Buffer2D Allocate2D(this MemoryManager memoryManager, int width, int height, bool clear) diff --git a/src/ImageSharp/Memory/SimpleManagedMemoryManager.cs b/src/ImageSharp/Memory/SimpleManagedMemoryManager.cs index 7a92d6c9f..12d8582c7 100644 --- a/src/ImageSharp/Memory/SimpleManagedMemoryManager.cs +++ b/src/ImageSharp/Memory/SimpleManagedMemoryManager.cs @@ -11,6 +11,11 @@ return new Buffer(new T[length], length); } + internal override IManagedByteBuffer AllocateManagedByteBuffer(int length, bool clear) + { + return new ManagedByteBuffer(new byte[length], length, this); + } + /// internal override void Release(Buffer buffer) {