// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Runtime.CompilerServices;
namespace FASTER.core
{
///
/// Heap container to store keys and values when they go pending
///
///
public interface IHeapContainer
{
///
/// Get object
///
///
ref T Get();
///
/// Dispose container
///
void Dispose();
}
///
/// Heap container for standard C# objects (non-variable-length)
///
///
internal class StandardHeapContainer : IHeapContainer
{
private T obj;
public StandardHeapContainer(ref T obj)
{
this.obj = obj;
}
public ref T Get() => ref obj;
public void Dispose() { }
}
///
/// Heap container for variable length structs
///
///
internal class VarLenHeapContainer : IHeapContainer
{
private SectorAlignedMemory mem;
public unsafe VarLenHeapContainer(ref T obj, IVariableLengthStruct varLenStruct, SectorAlignedBufferPool pool)
{
var len = varLenStruct.GetLength(ref obj);
mem = pool.Get(len);
Buffer.MemoryCopy(Unsafe.AsPointer(ref obj), mem.GetValidPointer(), len, len);
}
public unsafe ref T Get()
{
return ref Unsafe.AsRef(mem.GetValidPointer());
}
public void Dispose()
{
mem.Return();
}
}
}