From efc5d6f3067e03f09fd287e69d356efbbc6472f6 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 17 Feb 2018 19:56:40 +0100 Subject: [PATCH] moving common MemoryManager logic into extension methods --- src/ImageSharp/Image/ImageFrame{TPixel}.cs | 2 +- .../Memory/ArrayPoolMemoryManager.cs | 8 +--- src/ImageSharp/Memory/MemoryManager.cs | 19 -------- .../Memory/MemoryManagerExtensions.cs | 45 +++++++++++++++++++ ...nager.cs => SimpleManagedMemoryManager.cs} | 8 +--- .../Processors/Transforms/ResizeProcessor.cs | 2 +- 6 files changed, 49 insertions(+), 35 deletions(-) create mode 100644 src/ImageSharp/Memory/MemoryManagerExtensions.cs rename src/ImageSharp/Memory/{NullMemoryManager.cs => SimpleManagedMemoryManager.cs} (68%) diff --git a/src/ImageSharp/Image/ImageFrame{TPixel}.cs b/src/ImageSharp/Image/ImageFrame{TPixel}.cs index eb34c2673..5170522de 100644 --- a/src/ImageSharp/Image/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/Image/ImageFrame{TPixel}.cs @@ -54,7 +54,7 @@ namespace SixLabors.ImageSharp Guard.NotNull(metaData, nameof(metaData)); this.MemoryManager = memoryManager; - this.pixelBuffer = memoryManager.Allocate2D(width, height, true); + this.pixelBuffer = memoryManager.AllocateClean2D(width, height); this.MetaData = metaData; } diff --git a/src/ImageSharp/Memory/ArrayPoolMemoryManager.cs b/src/ImageSharp/Memory/ArrayPoolMemoryManager.cs index b062df711..86776fd35 100644 --- a/src/ImageSharp/Memory/ArrayPoolMemoryManager.cs +++ b/src/ImageSharp/Memory/ArrayPoolMemoryManager.cs @@ -28,8 +28,8 @@ namespace SixLabors.ImageSharp.Memory /// /// Initializes a new instance of the class. - /// The maximum size of pooled arrays. Arrays over the thershold are gonna be always allocated. /// + /// The maximum size of pooled arrays. Arrays over the thershold are gonna be always allocated. public ArrayPoolMemoryManager(int maxPoolSizeInBytes) { Guard.MustBeGreaterThan(maxPoolSizeInBytes, 0, nameof(maxPoolSizeInBytes)); @@ -37,12 +37,6 @@ namespace SixLabors.ImageSharp.Memory this.pool = ArrayPool.Create(maxPoolSizeInBytes, 50); } - /// - internal override Buffer Allocate(int itemCount) - { - return this.Allocate(itemCount, false); - } - /// internal override Buffer Allocate(int itemCount, bool clear) { diff --git a/src/ImageSharp/Memory/MemoryManager.cs b/src/ImageSharp/Memory/MemoryManager.cs index df1ecbd84..6be7012e6 100644 --- a/src/ImageSharp/Memory/MemoryManager.cs +++ b/src/ImageSharp/Memory/MemoryManager.cs @@ -11,17 +11,6 @@ namespace SixLabors.ImageSharp.Memory /// public abstract class MemoryManager { - /// - /// 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 - /// Size of the buffer to allocate - /// A buffer of values of type . - internal abstract Buffer Allocate(int size) - where T : struct; - /// /// Allocates a of size , optionally /// clearing the buffer before it gets returned. @@ -41,13 +30,5 @@ namespace SixLabors.ImageSharp.Memory /// The buffer to release internal abstract void Release(Buffer buffer) where T : struct; - - internal Buffer2D Allocate2D(int width, int height, bool clear = false) - where T : struct - { - Buffer buffer = this.Allocate(width * height, clear); - - return new Buffer2D(buffer, width, height); - } } } diff --git a/src/ImageSharp/Memory/MemoryManagerExtensions.cs b/src/ImageSharp/Memory/MemoryManagerExtensions.cs new file mode 100644 index 000000000..8e1aa9850 --- /dev/null +++ b/src/ImageSharp/Memory/MemoryManagerExtensions.cs @@ -0,0 +1,45 @@ +namespace SixLabors.ImageSharp.Memory +{ + /// + /// Extension methods for . + /// + internal static class MemoryManagerExtensions + { + /// + /// 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 + /// A buffer of values of type . + public static Buffer Allocate(this MemoryManager memoryManager, int size) + where T : struct + { + return memoryManager.Allocate(size, false); + } + + public static Buffer AllocateClean(this MemoryManager memoryManager, int size) + where T : struct + { + return memoryManager.Allocate(size, true); + } + + public static Buffer2D Allocate2D(this MemoryManager memoryManager, int width, int height, bool clear) + where T : struct + { + Buffer buffer = memoryManager.Allocate(width * height, clear); + + return new Buffer2D(buffer, width, height); + } + + public static Buffer2D Allocate2D(this MemoryManager memoryManager, int width, int height) + where T : struct => + Allocate2D(memoryManager, width, height, false); + + public static Buffer2D AllocateClean2D(this MemoryManager memoryManager, int width, int height) + where T : struct => + Allocate2D(memoryManager, width, height, true); + } +} \ No newline at end of file diff --git a/src/ImageSharp/Memory/NullMemoryManager.cs b/src/ImageSharp/Memory/SimpleManagedMemoryManager.cs similarity index 68% rename from src/ImageSharp/Memory/NullMemoryManager.cs rename to src/ImageSharp/Memory/SimpleManagedMemoryManager.cs index d82f01353..2aefe898f 100644 --- a/src/ImageSharp/Memory/NullMemoryManager.cs +++ b/src/ImageSharp/Memory/SimpleManagedMemoryManager.cs @@ -3,14 +3,8 @@ /// /// Implements by allocating new buffers on every call. /// - public class NullMemoryManager : MemoryManager + public class SimpleManagedMemoryManager : MemoryManager { - /// - internal override Buffer Allocate(int size) - { - return new Buffer(new T[size], size); - } - /// internal override Buffer Allocate(int size, bool clear) { diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs index e8463a266..1fa388da4 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs @@ -124,7 +124,7 @@ namespace SixLabors.ImageSharp.Processing.Processors // First process the columns. Since we are not using multiple threads startY and endY // are the upper and lower bounds of the source rectangle. // TODO: Using a transposed variant of 'firstPassPixels' could eliminate the need for the WeightsWindow.ComputeWeightedColumnSum() method, and improve speed! - using (var firstPassPixels = this.MemoryManager.Allocate2D(width, source.Height)) + using (Buffer2D firstPassPixels = this.MemoryManager.Allocate2D(width, source.Height)) { firstPassPixels.Buffer.Clear();