Variable-sized memory
ListMemory<List<T>, T>, BlockMemory<T> and StreamMemory<T> are variable-sized memories.
IMemory<byte> listMemory = new ListMemory<List<byte>, byte>(new());
IMemory<byte> blockMemory = new BlockMemory<byte>();
IMemory<byte> streamMemory = new StreamMemory(new MemoryStream());
The IList.IsFixedSize returns 'False'.
WriteLine(listMemory.IsFixedSize); // 'False'
WriteLine(blockMemory.IsFixedSize); // 'False'
WriteLine(streamMemory.IsFixedSize); // 'False'
Count changing methods, Insert, InsertAt, InsertFrom, InsertElementsAt, RemoveElementsAt, Clear can be used.
listMemory.Add(item: 10);
listMemory.Insert(0L, 9);
listMemory.Count = 5;
listMemory.Clear();
Fixed-size memory
ListMemory<T[], T>, MemoryMemory<T>, PointerMemory<T> and MMFMemory<T> are fixed-size memories.
IMemory<byte> arrayMemory = new ListMemory<byte[], byte>(new byte[50]);
IMemory<byte> memoryMemory = new MemoryMemory<byte>( (Memory<byte>) new byte[50] );
byte[] data = new byte[50];
fixed (byte* ptr = data)
{
IMemory<byte> pointerMemory = new PointerMemory<byte>(ptr, data.Length);
}
using IMemory<byte> mmfMemory = MMFMemory<byte>.CreateNew(null, 50);
The IList.IsFixedSize returns 'True'.
WriteLine(arrayMemory.IsFixedSize); // 'True'
WriteLine(memoryMemory.IsFixedSize); // 'True'
WriteLine(mmfMemory.IsFixedSize); // 'True'
The IList.MaxCount returns allocation count.
WriteLine(arrayMemory.MaxCount); // '50'
WriteLine(memoryMemory.MaxCount); // '50'
WriteLine(mmfMemory.MaxCount); // '4096'
Count changing methods Insert, InsertAt, InsertFrom, InsertElementsAt, RemoveElementsAt, Clear throw NotSupportedException.
try
{
arrayMemory.Add(item: 10);
arrayMemory.Insert(0L, 9);
arrayMemory.Count = 5;
arrayMemory.Clear();
} catch (NotSupportedException)
{
// Cannot add elements to byte[].
}
Fixed-size to Variable-sized
Slice<M, T> adapts to variable-sized, up until allocation limit.
// Create fixed-sized memory
IMemory<byte> arrayMemory = new ListMemory<byte[], byte>(new byte[50]);
// Adapt to variable-sized
IMemory<byte> arrayMemoryAllocation = new Slice<IMemory<byte>, byte>(
decoree: arrayMemory,
offset: 10L,
count: 9L,
allocation: 20L);
decoree: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
slice: offset → ▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░
↑ count ↑
↑ allocation ↑
Elements can be added up until allocation number of elements.
WriteLine(arrayMemory.IsFixedSize); // 'True'
WriteLine(arrayMemoryAllocation.IsFixedSize); // 'False'
WriteLine(arrayMemoryAllocation.Count); // '9'
WriteLine(arrayMemoryAllocation.MaxCount); // '20'
// Modify content
arrayMemoryAllocation.Add(item: 10);
arrayMemoryAllocation.Insert(0L, 9);
arrayMemoryAllocation.Count = 20L;
arrayMemoryAllocation.Count = 5L;
// Clear
arrayMemoryAllocation.Clear();
WriteLine(arrayMemoryAllocation.Count); // '0'
WriteLine(arrayMemoryAllocation.MaxCount); // '20'
Full Example
Full example
using Avalanche.Memory;
using Avalanche.Utilities;
using static System.Console;
public class sizevariability
{
public unsafe static void Run()
{
{
// <01>
IMemory<byte> listMemory = new ListMemory<List<byte>, byte>(new());
IMemory<byte> blockMemory = new BlockMemory<byte>();
IMemory<byte> streamMemory = new StreamMemory(new MemoryStream());
// </01>
// <02>
WriteLine(listMemory.IsFixedSize); // 'False'
WriteLine(blockMemory.IsFixedSize); // 'False'
WriteLine(streamMemory.IsFixedSize); // 'False'
// </02>
// <03>
listMemory.Add(item: 10);
listMemory.Insert(0L, 9);
listMemory.Count = 5;
listMemory.Clear();
// </03>
}
unsafe {
// <11>
IMemory<byte> arrayMemory = new ListMemory<byte[], byte>(new byte[50]);
IMemory<byte> memoryMemory = new MemoryMemory<byte>( (Memory<byte>) new byte[50] );
byte[] data = new byte[50];
fixed (byte* ptr = data)
{
IMemory<byte> pointerMemory = new PointerMemory<byte>(ptr, data.Length);
}
using IMemory<byte> mmfMemory = MMFMemory<byte>.CreateNew(null, 50);
// </11>
// <12>
WriteLine(arrayMemory.IsFixedSize); // 'True'
WriteLine(memoryMemory.IsFixedSize); // 'True'
WriteLine(mmfMemory.IsFixedSize); // 'True'
// </12>
// <13>
WriteLine(arrayMemory.MaxCount); // '50'
WriteLine(memoryMemory.MaxCount); // '50'
WriteLine(mmfMemory.MaxCount); // '4096'
// </13>
// <14>
try
{
arrayMemory.Add(item: 10);
arrayMemory.Insert(0L, 9);
arrayMemory.Count = 5;
arrayMemory.Clear();
} catch (NotSupportedException)
{
// Cannot add elements to byte[].
}
// </14>
}
{
// <21>
// Create fixed-sized memory
IMemory<byte> arrayMemory = new ListMemory<byte[], byte>(new byte[50]);
// Adapt to variable-sized
IMemory<byte> arrayMemoryAllocation = new Slice<IMemory<byte>, byte>(
decoree: arrayMemory,
offset: 10L,
count: 9L,
allocation: 20L);
// </21>
// <22>
WriteLine(arrayMemory.IsFixedSize); // 'True'
WriteLine(arrayMemoryAllocation.IsFixedSize); // 'False'
WriteLine(arrayMemoryAllocation.Count); // '9'
WriteLine(arrayMemoryAllocation.MaxCount); // '20'
// Modify content
arrayMemoryAllocation.Add(item: 10);
arrayMemoryAllocation.Insert(0L, 9);
arrayMemoryAllocation.Count = 20L;
arrayMemoryAllocation.Count = 5L;
// Clear
arrayMemoryAllocation.Clear();
WriteLine(arrayMemoryAllocation.Count); // '0'
WriteLine(arrayMemoryAllocation.MaxCount); // '20'
// </22>
}
}
}