//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="HLSFAllocator.Alloc"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Diagnostics.Tracing;
using System.Numerics;
using System.Runtime.InteropServices;
using hlsfHandle = StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
namespace StgSharp.HighPerformance.Memory
{
public unsafe partial class HybridLayerSegregatedFitAllocator
{
public hlsfHandle Alloc(uint size)
{
if (size > 32U * 1024U * 1024U)
{
Entry* e = (Entry*)_entries.Allocate();
e->Size = size;
e->Position = (nuint)NativeMemory.AlignedAlloc(size, (nuint)_align);
e->State = EntryState.Large;
return new hlsfHandle(e, size);
}
int level = GetLevelFromSize(size);
int segmentIndex = DetermineSegmentIndex(size, level);
int i = level, s = segmentIndex;
Entry* handle;
while (!TryPopLevelForAllocation(i, s, out handle))
{
if (++s >= 3)
{
s = 0;
i++;
}
if (i >= levelSizeArray.Length)
{
if (TryAllocateNew16MBBlock(out handle))
{
break;
}
throw new OverflowException("Out of memory: Unable to allocate new 16MB block or find suitable bucket for allocation.");
}
}
if (handle != null)
{
int requestedOffset = (segmentIndex + 1) * levelSizeArray[level];
if (requestedOffset > handle->Size) {
throw new OverflowException("Out of memory: insufficient block size returned from spare memory.");
}
long remainder = handle->Size - requestedOffset;
if (remainder > 0)
{
Entry* extra = (Entry*)_entries.Allocate();
extra->Position = handle->Position + (nuint)requestedOffset;
extra->Size = (uint)remainder;
extra->State = EntryState.Empty;
extra->NextNear = handle->NextNear;
extra->PreviousNear = handle;
extra->NextNear->PreviousNear = extra;
extra->PreviousNear->NextNear = extra;
SliceMemory(extra);
}
handle->State = EntryState.Alloc;
handle->Size = requestedOffset;
handle->Level = level;
}
return new hlsfHandle(handle, size);
}
///
/// Slice the memory block into smaller blocks and add them to the appropriate buckets
/// Notice: this method is not responsible for freeing the original BucketNode
///
///
///
///
private void SliceMemory(Entry* e)
{
Entry* cur = e->PreviousNear;
Entry* next = e->NextNear;
long remain = e->Size;
long offset = (long)e->Position - (long)m_Buffer;
long end = offset + remain;
_entries.Free((nuint)e);
int i = (BitOperations.TrailingZeroCount(remain) - 6) / 2;
i = int.Min(10, i);
for (; i < levelSizeArray.Length; i++)
{
int size = levelSizeArray[i];
int upper = size * 4;
long edge = (offset + upper - 1) / upper * upper;
int count = (int)((long.Min(edge, end) - offset) / size);
if (count > 0)
{
int newSize = count * size;
Entry* newEntry = (Entry*)_entries.Allocate();
newEntry->PreviousNear = cur;
newEntry->NextNear = next;
cur->NextNear = newEntry;
next->PreviousNear = newEntry;
newEntry->Position = (nuint)offset + (nuint)m_Buffer;
newEntry->State = EntryState.Empty;
newEntry->Size = (uint)newSize;
newEntry->Level = i;
BucketNode* b = (BucketNode*)newEntry->Position;
b->EntryRef = newEntry;
PushLevel(i, count - 1, b);
offset += newSize;
remain -= (long)newSize;
cur = newEntry;
}
if (remain <= 0) {
return;
}
}
if (i > MaxLevel)
{
int size = levelSizeArray[^1];
int upper = size * 4;
long edge = end / upper * upper;
int count = (int)((long.Min(edge, end) - offset) / upper);
if (count > 0)
{
int newSize = count * upper;
Entry* newEntry = (Entry*)_entries.Allocate();
newEntry->PreviousNear = cur;
newEntry->NextNear = next;
cur->NextNear = newEntry;
next->PreviousNear = newEntry;
newEntry->Position = (nuint)offset + (nuint)m_Buffer;
newEntry->State = EntryState.Empty;
newEntry->Size = (uint)newSize;
newEntry->Level = i;
BucketNode* b = (BucketNode*)newEntry->Position;
b->EntryRef = newEntry;
PushLevel(i, count - 1, b);
cur = newEntry;
offset += newSize;
remain -= (long)newSize;
if (remain <= 0) {
return;
}
}
}
for (i--; i >= 0; i--)
{
int size = levelSizeArray[i];
long edge = end / size * size;
int count = (int)((long.Min(edge, end) - offset) / size);
if (count > 0)
{
int newSize = count * size;
Entry* newEntry = (Entry*)_entries.Allocate();
newEntry->PreviousNear = cur;
newEntry->NextNear = next;
cur->NextNear = newEntry;
next->PreviousNear = newEntry;
newEntry->Position = (nuint)offset + (nuint)m_Buffer;
newEntry->State = EntryState.Empty;
newEntry->Size = (uint)newSize;
newEntry->Level = i;
BucketNode* b = (BucketNode*)newEntry->Position;
b->EntryRef = newEntry;
b->NextLevel = null;
b->PreviousLevel = null;
PushLevel(i, count - 1, b);
cur = newEntry;
offset += newSize;
remain -= (long)newSize;
}
if (remain <= 0) {
return;
}
}
}
///
/// Try to allocate a new 16MB block when all buckets are empty
///
///
/// Output handle to the allocated Entry
///
///
/// True if allocation succeeded
///
private bool TryAllocateNew16MBBlock(out Entry* handle)
{
nuint pos = _spareMemory->Position;
long remain = _spareMemory->Size;
if (remain <= 0)
{
handle = null;
return false;
}
long size = remain > 16 * 1024 * 1024L ? 16 * 1024 * 1024L : remain;
// build new entry
Entry* e = (Entry*)_entries.Allocate();
e->Position = pos;
e->Size = size;
Entry* prev = _spareMemory->PreviousNear;
e->NextNear = _spareMemory;
e->PreviousNear = prev;
prev->NextNear = e;
_spareMemory->PreviousNear = e;
e->State = EntryState.Empty;
e->Level = GetLevelFromSize(size);
// set fields of spare memory
_spareMemory->Size -= size;
_spareMemory->Position += (nuint)size;
handle = e;
return true;
}
///
/// Helper method for allocation - converts BucketNode to Entry
///
private bool TryPopLevelForAllocation(int levelIndex, int sizeIndex, out Entry* entry)
{
if (TryPopLevel(levelIndex, sizeIndex, out BucketNode* bucketNode))
{
entry = bucketNode->EntryRef;
return entry != null;
}
entry = null;
return false;
}
}
}