Browse Source

IManagedByteBuffer

af/merge-core
Anton Firszov 8 years ago
parent
commit
d8ebe2f03d
  1. 12
      src/ImageSharp/Memory/ArrayPoolMemoryManager.cs
  2. 13
      src/ImageSharp/Memory/IManagedByteBuffer.cs
  3. 10
      src/ImageSharp/Memory/ManagedByteBuffer.cs
  4. 2
      src/ImageSharp/Memory/MemoryManager.cs
  5. 17
      src/ImageSharp/Memory/MemoryManagerExtensions.cs
  6. 5
      src/ImageSharp/Memory/SimpleManagedMemoryManager.cs

12
src/ImageSharp/Memory/ArrayPoolMemoryManager.cs

@ -53,6 +53,18 @@ namespace SixLabors.ImageSharp.Memory
return buffer; 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;
}
/// <inheritdoc /> /// <inheritdoc />
internal override void Release<T>(Buffer<T> buffer) internal override void Release<T>(Buffer<T> buffer)
{ {

13
src/ImageSharp/Memory/IManagedByteBuffer.cs

@ -0,0 +1,13 @@
namespace SixLabors.ImageSharp.Memory
{
/// <summary>
/// Represents a byte buffer backed by a managed array.
/// </summary>
internal interface IManagedByteBuffer : IBuffer<byte>
{
/// <summary>
/// Gets the managed array backing this buffer instance.
/// </summary>
byte[] Array { get; }
}
}

10
src/ImageSharp/Memory/ManagedByteBuffer.cs

@ -0,0 +1,10 @@
namespace SixLabors.ImageSharp.Memory
{
internal class ManagedByteBuffer : Buffer<byte>, IManagedByteBuffer
{
internal ManagedByteBuffer(byte[] array, int length, MemoryManager memoryManager)
: base(array, length, memoryManager)
{
}
}
}

2
src/ImageSharp/Memory/MemoryManager.cs

@ -22,6 +22,8 @@ namespace SixLabors.ImageSharp.Memory
internal abstract Buffer<T> Allocate<T>(int length, bool clear) internal abstract Buffer<T> Allocate<T>(int length, bool clear)
where T : struct; where T : struct;
internal abstract IManagedByteBuffer AllocateManagedByteBuffer(int length, bool clear);
/// <summary> /// <summary>
/// Releases the memory allocated for <paramref name="buffer"/>. After this, the buffer /// Releases the memory allocated for <paramref name="buffer"/>. After this, the buffer
/// is no longer usable. /// is no longer usable.

17
src/ImageSharp/Memory/MemoryManagerExtensions.cs

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

5
src/ImageSharp/Memory/SimpleManagedMemoryManager.cs

@ -11,6 +11,11 @@
return new Buffer<T>(new T[length], length); return new Buffer<T>(new T[length], length);
} }
internal override IManagedByteBuffer AllocateManagedByteBuffer(int length, bool clear)
{
return new ManagedByteBuffer(new byte[length], length, this);
}
/// <inheritdoc /> /// <inheritdoc />
internal override void Release<T>(Buffer<T> buffer) internal override void Release<T>(Buffer<T> buffer)
{ {

Loading…
Cancel
Save