Browse Source

moving common MemoryManager logic into extension methods

af/merge-core
Anton Firszov 8 years ago
parent
commit
efc5d6f306
  1. 2
      src/ImageSharp/Image/ImageFrame{TPixel}.cs
  2. 8
      src/ImageSharp/Memory/ArrayPoolMemoryManager.cs
  3. 19
      src/ImageSharp/Memory/MemoryManager.cs
  4. 45
      src/ImageSharp/Memory/MemoryManagerExtensions.cs
  5. 8
      src/ImageSharp/Memory/SimpleManagedMemoryManager.cs
  6. 2
      src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs

2
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<TPixel>(width, height, true);
this.pixelBuffer = memoryManager.AllocateClean2D<TPixel>(width, height);
this.MetaData = metaData;
}

8
src/ImageSharp/Memory/ArrayPoolMemoryManager.cs

@ -28,8 +28,8 @@ namespace SixLabors.ImageSharp.Memory
/// <summary>
/// Initializes a new instance of the <see cref="ArrayPoolMemoryManager"/> class.
/// <param name="maxPoolSizeInBytes">The maximum size of pooled arrays. Arrays over the thershold are gonna be always allocated.</param>
/// </summary>
/// <param name="maxPoolSizeInBytes">The maximum size of pooled arrays. Arrays over the thershold are gonna be always allocated.</param>
public ArrayPoolMemoryManager(int maxPoolSizeInBytes)
{
Guard.MustBeGreaterThan(maxPoolSizeInBytes, 0, nameof(maxPoolSizeInBytes));
@ -37,12 +37,6 @@ namespace SixLabors.ImageSharp.Memory
this.pool = ArrayPool<byte>.Create(maxPoolSizeInBytes, 50);
}
/// <inheritdoc />
internal override Buffer<T> Allocate<T>(int itemCount)
{
return this.Allocate<T>(itemCount, false);
}
/// <inheritdoc />
internal override Buffer<T> Allocate<T>(int itemCount, bool clear)
{

19
src/ImageSharp/Memory/MemoryManager.cs

@ -11,17 +11,6 @@ namespace SixLabors.ImageSharp.Memory
/// </summary>
public abstract class MemoryManager
{
/// <summary>
/// Allocates a <see cref="Buffer{T}"/> of size <paramref name="size"/>.
/// Note: Depending on the implementation, the buffer may not cleared before
/// returning, so it may contain data from an earlier use.
/// </summary>
/// <typeparam name="T">Type of the data stored in the buffer</typeparam>
/// <param name="size">Size of the buffer to allocate</param>
/// <returns>A buffer of values of type <typeparamref name="T"/>.</returns>
internal abstract Buffer<T> Allocate<T>(int size)
where T : struct;
/// <summary>
/// Allocates a <see cref="Buffer{T}"/> of size <paramref name="size"/>, optionally
/// clearing the buffer before it gets returned.
@ -41,13 +30,5 @@ namespace SixLabors.ImageSharp.Memory
/// <param name="buffer">The buffer to release</param>
internal abstract void Release<T>(Buffer<T> buffer)
where T : struct;
internal Buffer2D<T> Allocate2D<T>(int width, int height, bool clear = false)
where T : struct
{
Buffer<T> buffer = this.Allocate<T>(width * height, clear);
return new Buffer2D<T>(buffer, width, height);
}
}
}

45
src/ImageSharp/Memory/MemoryManagerExtensions.cs

@ -0,0 +1,45 @@
namespace SixLabors.ImageSharp.Memory
{
/// <summary>
/// Extension methods for <see cref="MemoryManager"/>.
/// </summary>
internal static class MemoryManagerExtensions
{
/// <summary>
/// Allocates a <see cref="Buffer{T}"/> of size <paramref name="size"/>.
/// Note: Depending on the implementation, the buffer may not cleared before
/// returning, so it may contain data from an earlier use.
/// </summary>
/// <typeparam name="T">Type of the data stored in the buffer</typeparam>
/// <param name="memoryManager">The <see cref="MemoryManager"/></param>
/// <param name="size">Size of the buffer to allocate</param>
/// <returns>A buffer of values of type <typeparamref name="T"/>.</returns>
public static Buffer<T> Allocate<T>(this MemoryManager memoryManager, int size)
where T : struct
{
return memoryManager.Allocate<T>(size, false);
}
public static Buffer<T> AllocateClean<T>(this MemoryManager memoryManager, int size)
where T : struct
{
return memoryManager.Allocate<T>(size, true);
}
public static Buffer2D<T> Allocate2D<T>(this MemoryManager memoryManager, int width, int height, bool clear)
where T : struct
{
Buffer<T> buffer = memoryManager.Allocate<T>(width * height, clear);
return new Buffer2D<T>(buffer, width, height);
}
public static Buffer2D<T> Allocate2D<T>(this MemoryManager memoryManager, int width, int height)
where T : struct =>
Allocate2D<T>(memoryManager, width, height, false);
public static Buffer2D<T> AllocateClean2D<T>(this MemoryManager memoryManager, int width, int height)
where T : struct =>
Allocate2D<T>(memoryManager, width, height, true);
}
}

8
src/ImageSharp/Memory/NullMemoryManager.cs → src/ImageSharp/Memory/SimpleManagedMemoryManager.cs

@ -3,14 +3,8 @@
/// <summary>
/// Implements <see cref="MemoryManager"/> by allocating new buffers on every call.
/// </summary>
public class NullMemoryManager : MemoryManager
public class SimpleManagedMemoryManager : MemoryManager
{
/// <inheritdoc />
internal override Buffer<T> Allocate<T>(int size)
{
return new Buffer<T>(new T[size], size);
}
/// <inheritdoc />
internal override Buffer<T> Allocate<T>(int size, bool clear)
{

2
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<Vector4>(width, source.Height))
using (Buffer2D<Vector4> firstPassPixels = this.MemoryManager.Allocate2D<Vector4>(width, source.Height))
{
firstPassPixels.Buffer.Clear();

Loading…
Cancel
Save