//----------------------------------------------------------------------- // ----------------------------------------------------------------------- // file="HLSFAllocator.Level.cs" // Project: StgSharp // 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. // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace StgSharp.HighPerformance.Memory { public unsafe partial class HybridLayerSegregatedFitAllocator { private const int MaxLevel = 9; private static readonly int[] levelSize = [ 64, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216 ]; public int AlignOfLevel(int bucketLevel) { int blockSize = levelSize[bucketLevel]; return int.Min(blockSize, _align); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private int GetLevelFromSize(uint size) { // Ensure minimum size if (size <= 64) { return 0; } // Use binary operations to find the appropriate level // Level sizes are powers of 2: 64(2^6), 256(2^8), 1024(2^10), etc. // We can use bit operations to efficiently determine the level // Find the position of the highest set bit (equivalent to log2) int msb = 31 - System.Numerics.BitOperations.LeadingZeroCount(size); // Map bit position to level index // Level 0: 64 (2^6) -> msb = 6 // Level 1: 256 (2^8) -> msb = 8 // Level 2: 1024 (2^10) -> msb = 10 // Pattern: level = (msb - 6) / 2, but we need to handle boundaries if (msb <= 6) { return 0; // size <= 64 } if (msb <= 7) { return 0; // size <= 128, still fits in level 0 (64-256) } if (msb <= 8) { return 1; // size <= 256 } if (msb <= 9) { return 1; // size <= 512, still fits in level 1 (256-1024) } if (msb <= 10) { return 2; // size <= 1024 } if (msb <= 11) { return 2; // size <= 2048, still fits in level 2 (1024-4096) } if (msb <= 12) { return 3; // size <= 4096 } if (msb <= 13) { return 3; // size <= 8192, still fits in level 3 (4096-16384) } if (msb <= 14) { return 4; // size <= 16384 } if (msb <= 15) { return 4; // size <= 32768, still fits in level 4 (16384-65536) } if (msb <= 16) { return 5; // size <= 65536 } if (msb <= 17) { return 5; // size <= 131072, still fits in level 5 (65536-262144) } if (msb <= 18) { return 6; // size <= 262144 } if (msb <= 19) { return 6; // size <= 524288, still fits in level 6 (262144-1048576) } if (msb <= 20) { return 7; // size <= 1048576 } if (msb <= 21) { return 7; // size <= 2097152, still fits in level 7 (1048576-4194304) } if (msb <= 22) { return 8; // size <= 4194304 } if (msb <= 23) { return 8; // size <= 8388608, still fits in level 8 (4194304-16777216) } // If size is larger than all predefined levels, use the last level return levelSize.Length - 1; } } public enum CommonMemoryAlign : int { XMMLevel = 16, YMMLevel = 32, ZMMLevel = 64, ArmNEONLevel = 16, } }