//----------------------------------------------------------------------- // ----------------------------------------------------------------------- // file="HLSFAllocator.BucketNode" // 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.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace StgSharp.HighPerformance.Memory { public unsafe partial class HybridLayerSegregatedFitAllocator { #region BucketNode Operations [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool IsNullOrEmpty(BucketNode* node) { return node == null; } #endregion private void PushLevel(int levelIndex, int sizeIndex, BucketNode* node) { if (node == null || node->EntryRef == null) { return; } int index = (3 * levelIndex) + sizeIndex; if (_bucketHeads[index] == EmptyHandle) { _bucketHeads[index] = (nuint)node; node->IsInBucket = true; node->NextLevel = EmptyHandle; node->PreviousLevel = EmptyHandle; // Console.WriteLine($"Push to empty level of {levelIndex} {sizeIndex}, bucket pos {(ulong)node}"); return; } else { ulong head = _bucketHeads[index]; node->NextLevel = head; BucketNode* headNode = (BucketNode*)head; node->IsInBucket = true; // Console.WriteLine($"Push to head of level of {levelIndex} {sizeIndex}, bucket pos {(ulong)node}, previous head is at {head}"); if (headNode != null) { headNode->PreviousLevel = (ulong)node; } return; } } private void RemoveFromBucket(int levelIndex, int sizeIndex, BucketNode* node) { int index = (3 * levelIndex) + sizeIndex; if ((BucketNode*)_bucketHeads[index] == node) { if (TryPopLevel(levelIndex, sizeIndex, out _)) { return; } } ulong p = node->PreviousLevel; ulong n = node->NextLevel; BucketNode* previous = (BucketNode*)p; BucketNode* next = (BucketNode*)n; if (previous != null) { previous->NextLevel = n; } if (next != null) { next->PreviousLevel = p; } node->NextLevel = EmptyHandle; node->PreviousLevel = EmptyHandle; node->IsInBucket = false; return; } private bool TryPopLevel(int levelIndex, int sizeIndex, out BucketNode* node) { int index = (3 * levelIndex) + sizeIndex; ulong h = _bucketHeads[index]; BucketNode* head = (BucketNode*)h; if (head == null) { node = null; return false; } // Console.WriteLine($"level popped from level {levelIndex} {sizeIndex}, node is at {(ulong)head}"); BucketNode* next = (BucketNode*)head->NextLevel; if (next == null) { _bucketHeads[index] = EmptyHandle; node = head; return true; } _bucketHeads[index] = head->NextLevel; node = head; node->IsInBucket = false; node->NextLevel = EmptyHandle; if (next != null) { next->PreviousLevel = EmptyHandle; } return true; } private bool TryRemoveFromBucket(int levelIndex, int sizeIndex, BucketNode* node) { int index = (3 * levelIndex) + sizeIndex; if ((BucketNode*) _bucketHeads[index] == node) { return TryPopLevel(levelIndex, sizeIndex, out _); } ulong p = node->PreviousLevel; ulong n = node->NextLevel; BucketNode* previous = (BucketNode*)p; BucketNode* next = (BucketNode*)n; if (previous != null) { previous->NextLevel = n; } if (next != null) { next->PreviousLevel = p; } node->NextLevel = EmptyHandle; node->PreviousLevel = EmptyHandle; node->IsInBucket = false; return true; } /// /// BucketNode structure - specialized node for bucket linked lists Stored in main /// allocation area (user-overwritable region) /// [StructLayout(LayoutKind.Explicit)] internal struct BucketNode { [FieldOffset(0)] internal Entry* EntryRef; // Pointer to corresponding Entry [FieldOffset(32)] internal bool IsInBucket; // Whether in bucket [FieldOffset(16)] internal ulong LockBuffer; [FieldOffset(8)] internal ulong NextLevel; // Bucket stack linked list: next pointer [FieldOffset(24)] internal ulong PreviousLevel; // Bucket stack linked list: previous pointer } } }