ListMemory<L, T>
ListMemory<L, T> creates List<T> as memory.
// Create memory
IMemory<byte> memory = new ListMemory<List<byte>, byte>();
ListMemory<L, T> adapts IList<T> as memory.
// Create list
List<byte> list = new List<byte>();
// Adapt memory access
IMemory<byte> memory = new ListMemory<IList<byte>, byte>(list);
Array can be adapted as fixed-size memory.
// Create list
byte[] list = new byte[1024];
// Create fixed-size memory access
IMemory<byte> memory = new ListMemory<byte[], byte>(list);
// Write
memory[0] = 0;
Array can be adapted as incremental variable-size memory.
// Create list
byte[] list = new byte[1024];
// Create incremental memory access to 'list'
var memory = new Slice<ListMemory<byte[], byte>, byte>(new ListMemory<byte[], byte>(list), 0, 0, list.Length);
// Insert
memory.Add((byte)0);
// Print status
Console.WriteLine(memory.Count); // 1
Console.WriteLine(memory.MaxCount); // 1024
StructList can be allocated from stack and adapted to memory with zero heap allocation.
// Create memory on stack
var memory = new ListMemory<StructList32<byte>, byte>();
Concurrency
.RWLocked(lock?) decorates for concurrent thread use.
// Create memory access
IMemory<byte> memory = new ListMemory<List<byte>, byte>(new());
// Decorate with lock
memory = memory.RWLocked(@lock: default);
// Write data
Parallel.For(0, 1024, (int i) => memory.Add((byte)i));
// Print
Console.WriteLine(memory.Count); // 1024
Full Example
Full example
using Avalanche.Memory;
using Avalanche.Utilities;
public class listmemory
{
public static void Run()
{
{
// <00>
// Create memory
IMemory<byte> memory = new ListMemory<List<byte>, byte>();
// </00>
}
{
// <01>
// Create list
List<byte> list = new List<byte>();
// Adapt memory access
IMemory<byte> memory = new ListMemory<IList<byte>, byte>(list);
// </01>
}
{
// <02>
// Create list
byte[] list = new byte[1024];
// Create fixed-size memory access
IMemory<byte> memory = new ListMemory<byte[], byte>(list);
// Write
memory[0] = 0;
// </02>
}
{
// <03>
// Create list
byte[] list = new byte[1024];
// Create incremental memory access to 'list'
var memory = new Slice<ListMemory<byte[], byte>, byte>(new ListMemory<byte[], byte>(list), 0, 0, list.Length);
// Insert
memory.Add((byte)0);
// Print status
Console.WriteLine(memory.Count); // 1
Console.WriteLine(memory.MaxCount); // 1024
// </03>
}
{
// <04>
// Create memory on stack
var memory = new ListMemory<StructList32<byte>, byte>();
// </04>
}
{
// <11>
// Create memory access
IMemory<byte> memory = new ListMemory<List<byte>, byte>(new());
// Decorate with lock
memory = memory.RWLocked(@lock: default);
// Write data
Parallel.For(0, 1024, (int i) => memory.Add((byte)i));
// Print
Console.WriteLine(memory.Count); // 1024
// </11>
}
}
}