solution to length unfixed cacheline in l4
This commit is contained in:
@@ -1,27 +1,30 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HLSFAllocator"
|
||||
// file="HLSFAllocator.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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.
|
||||
// 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.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
@@ -30,7 +33,7 @@ using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using hlsfHandle = StgSharp.Mathematics.Memory.HybridLayerSegregatedFitAllocationHandle;
|
||||
using hlsfHandle = StgSharp.Mathematics.Memory.hlsfHandle;
|
||||
|
||||
namespace StgSharp.Mathematics.Memory
|
||||
{
|
||||
@@ -43,47 +46,41 @@ namespace StgSharp.Mathematics.Memory
|
||||
// _spareMemory is also the beginning position of allocator
|
||||
private Entry *_spareMemory;
|
||||
private readonly byte* m_Buffer;
|
||||
|
||||
private readonly Action<nuint> _freeHandle;
|
||||
private bool disposedValue;
|
||||
private readonly int _align;
|
||||
private readonly long _maxSize;
|
||||
private readonly nuint _size;
|
||||
private readonly SlabAllocator<Entry> _entries;
|
||||
|
||||
public HybridLayerSegregatedFitAllocator(
|
||||
nuint byteSize
|
||||
)
|
||||
internal HybridLayerSegregatedFitAllocator(
|
||||
nuint byteSize,
|
||||
int align,
|
||||
long maxBlockSize,
|
||||
Func<nuint, int, nuint> allocator,
|
||||
Action<nuint> freeHandle,
|
||||
SlabAllocator<Entry>? entryManager)
|
||||
: this(byteSize, align, maxBlockSize, (void*)allocator(byteSize, align), freeHandle, entryManager) { }
|
||||
|
||||
internal HybridLayerSegregatedFitAllocator(
|
||||
nuint byteSize,
|
||||
int align,
|
||||
long maxBlockSize,
|
||||
void* externalPointer,
|
||||
Action<nuint> freeHandle,
|
||||
SlabAllocator<Entry>? entryManager = null)
|
||||
{
|
||||
if (byteSize < 64) {
|
||||
throw new InvalidOperationException();
|
||||
if (align < 16 || (align & (align - 1)) != 0) {
|
||||
throw new ArgumentException(
|
||||
"Alignment must be a power of two and at least 16 bytes.");
|
||||
}
|
||||
_entries = SlabAllocator.Create<Entry>(64, SlabBufferLayout.Chunked, false);
|
||||
_size = byteSize;
|
||||
m_Buffer = (byte *)NativeMemory.AlignedAlloc(byteSize, 16);
|
||||
_align = 16;
|
||||
|
||||
_bucketHeads = new BucketNode*[_levelSizeArray.Length * 3 + 1];
|
||||
|
||||
_spareMemory = (Entry *)_entries.Allocate();
|
||||
_spareMemory->State = EntryState.Spare;
|
||||
_spareMemory->PreviousNear = _spareMemory;
|
||||
_spareMemory->NextNear = _spareMemory;
|
||||
_spareMemory->Position = (nuint)m_Buffer;
|
||||
_spareMemory->Size = (long)byteSize;
|
||||
|
||||
_maxSize = 32U * 1024U * 1024U;
|
||||
}
|
||||
|
||||
public HybridLayerSegregatedFitAllocator(
|
||||
nuint byteSize,
|
||||
uint maxBlockSize
|
||||
)
|
||||
{
|
||||
_entries = SlabAllocator.Create<Entry>(64, SlabBufferLayout.Chunked, false);
|
||||
m_Buffer = (byte *)NativeMemory.AlignedAlloc(byteSize, 16);
|
||||
_align = 16;
|
||||
_entries = entryManager ?? SlabAllocator.Create<Entry>(64, SlabBufferLayout.Chunked, false);
|
||||
m_Buffer = (byte*)externalPointer;
|
||||
_align = align;
|
||||
_size = byteSize;
|
||||
|
||||
_spareMemory = (Entry *)_entries.Allocate();
|
||||
_spareMemory = (Entry*)_entries.Allocate();
|
||||
_spareMemory->State = EntryState.Empty;
|
||||
_spareMemory->PreviousNear = _spareMemory;
|
||||
_spareMemory->NextNear = _spareMemory;
|
||||
@@ -93,7 +90,7 @@ namespace StgSharp.Mathematics.Memory
|
||||
int levelCountOrigin = _levelSizeArray.Length;
|
||||
int levelCount = GetLevelFromSize(maxBlockSize) + 1;
|
||||
Array.Resize(ref _levelSizeArray, levelCount);
|
||||
_bucketHeads = new BucketNode *[(levelCount * 3) + 1];
|
||||
_bucketHeads = new BucketNode*[(levelCount * 3) + 1];
|
||||
int size = _levelSizeArray[^1];
|
||||
for (int i = levelCountOrigin; i < levelCount; i++)
|
||||
{
|
||||
@@ -101,72 +98,25 @@ namespace StgSharp.Mathematics.Memory
|
||||
_levelSizeArray[i] = size;
|
||||
}
|
||||
_maxSize = maxBlockSize;
|
||||
_freeHandle = freeHandle ?? (static _ => { });
|
||||
}
|
||||
|
||||
public HybridLayerSegregatedFitAllocator(
|
||||
nuint byteSize,
|
||||
int align
|
||||
)
|
||||
{
|
||||
if (align < 16 || (align & (align - 1)) != 0) {
|
||||
throw new ArgumentException(
|
||||
"Alignment must be a power of two and at least 16 bytes.");
|
||||
}
|
||||
_entries = SlabAllocator.Create<Entry>(64, SlabBufferLayout.Chunked, false);
|
||||
m_Buffer = (byte *)NativeMemory.AlignedAlloc(byteSize, (nuint)align);
|
||||
_align = align;
|
||||
_size = byteSize;
|
||||
public HybridLayerSegregatedFitAllocator(nuint byteSize)
|
||||
: this(
|
||||
byteSize, 16, 32U * 1024U * 1024U, NativeMemory.AlignedAlloc(byteSize, 16), ptr => NativeMemory.AlignedFree((void*)ptr)) { }
|
||||
|
||||
_bucketHeads = new BucketNode *[_levelSizeArray.Length * 3 + 1];
|
||||
public HybridLayerSegregatedFitAllocator(nuint byteSize, uint maxBlockSize)
|
||||
: this(
|
||||
byteSize, 16, maxBlockSize, NativeMemory.AlignedAlloc(byteSize, 16), ptr => NativeMemory.AlignedFree((void*)ptr)) { }
|
||||
|
||||
_spareMemory = (Entry *)_entries.Allocate();
|
||||
_spareMemory->State = EntryState.Empty;
|
||||
_spareMemory->PreviousNear = _spareMemory;
|
||||
_spareMemory->NextNear = _spareMemory;
|
||||
_spareMemory->Position = (nuint)m_Buffer;
|
||||
_spareMemory->Size = (long)byteSize;
|
||||
public HybridLayerSegregatedFitAllocator(nuint byteSize, int align)
|
||||
: this(
|
||||
byteSize, align, 32U * 1024U * 1024U, NativeMemory.AlignedAlloc(byteSize, 16), ptr => NativeMemory.AlignedFree((void*)ptr)) { }
|
||||
|
||||
_maxSize = 32U * 1024U * 1024U;
|
||||
}
|
||||
public HybridLayerSegregatedFitAllocator(nuint byteSize, int align, long maxBlockSize)
|
||||
: this(byteSize, align, maxBlockSize, NativeMemory.AlignedAlloc(byteSize, 16), ptr => NativeMemory.AlignedFree((void*)ptr)) { }
|
||||
|
||||
public HybridLayerSegregatedFitAllocator(
|
||||
nuint byteSize,
|
||||
int align,
|
||||
long maxBlockSize
|
||||
)
|
||||
{
|
||||
if (align < 16 || (align & (align - 1)) != 0) {
|
||||
throw new ArgumentException(
|
||||
"Alignment must be a power of two and at least 16 bytes.");
|
||||
}
|
||||
_entries = SlabAllocator.Create<Entry>(64, SlabBufferLayout.Chunked, false);
|
||||
m_Buffer = (byte*)NativeMemory.AlignedAlloc(byteSize, (nuint)align);
|
||||
_align = align;
|
||||
_size = byteSize;
|
||||
|
||||
_spareMemory = (Entry *)_entries.Allocate();
|
||||
_spareMemory->State = EntryState.Empty;
|
||||
_spareMemory->PreviousNear = _spareMemory;
|
||||
_spareMemory->NextNear = _spareMemory;
|
||||
_spareMemory->Position = (nuint)m_Buffer;
|
||||
_spareMemory->Size = (long)byteSize;
|
||||
|
||||
int levelCountOrigin = _levelSizeArray.Length;
|
||||
int levelCount = GetLevelFromSize(maxBlockSize) + 1;
|
||||
Array.Resize(ref _levelSizeArray, levelCount);
|
||||
_bucketHeads = new BucketNode *[levelCount * 3 + 1];
|
||||
int size = _levelSizeArray[^1];
|
||||
for (int i = levelCountOrigin; i < levelCount; i++)
|
||||
{
|
||||
size *= 4;
|
||||
_levelSizeArray[i] = size;
|
||||
}
|
||||
_maxSize = maxBlockSize;
|
||||
}
|
||||
|
||||
public partial hlsfHandle Alloc(
|
||||
long size
|
||||
);
|
||||
public partial hlsfHandle Alloc(long size);
|
||||
|
||||
public partial void Collect();
|
||||
|
||||
@@ -176,16 +126,14 @@ namespace StgSharp.Mathematics.Memory
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(
|
||||
bool disposing
|
||||
)
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
if (disposing) {
|
||||
_entries.Dispose();
|
||||
}
|
||||
NativeMemory.AlignedFree(m_Buffer);
|
||||
_freeHandle((nuint)m_Buffer);
|
||||
disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user