Example #1
0
        public static void ConsumeForward(HeapAllocation *root)
        {
            ////_logger.LogDebug($"  ConsumeForward {*root}");
            var ptr = root->Next;

            while (ptr != null)
            {
                ////_logger.LogDebug($"  Walked forward {*ptr}");
                Assert.EqualTo(ptr->MagicSignature, MagicChecksum);
                if (!ptr->IsFree)
                {
                    break;
                }

                root->Blocks++;
                root->Blocks += ptr->Blocks;
                var next = ptr->Next;

                *ptr = default;

                ptr        = next;
                root->Next = ptr;
            }

            if (root->Next != null)
            {
                root->Next->Previous = root;
            }

            //Assert(ptr->MagicSignature == MagicChecksum);
        }
Example #2
0
        public static void Split(HeapAllocation *ptr, uint blocks)
        {
            //var blocks = (size + (HeapSize - 1)) >> 5;
            Assert.GreatherThan(blocks, 0);//make sure we are taking at least one block
            Assert.EqualTo(ptr->MagicSignature, MagicChecksum);

            var remainingBlocks = ptr->Blocks - blocks;

            //Assert.LessThan(remainingBlocks, ptr->Blocks);
            if (remainingBlocks > 1)
            {
                var newBlock = &ptr[blocks + 1];
                *   newBlock = default;
                newBlock->Blocks         = remainingBlocks - 1;
                newBlock->Previous       = ptr;
                newBlock->MagicSignature = MagicChecksum;

                if (ptr->Next != null)
                {
                    Assert.EqualTo(ptr->MagicSignature, MagicChecksum);
                    newBlock->Next      = ptr->Next;
                    ptr->Next->Previous = newBlock;
                }

                ptr->Next       = newBlock;
                remainingBlocks = 0;
            }
            ptr->Flags |= 1;
            ptr->Blocks = blocks + remainingBlocks;
        }
Example #3
0
 public HeapAllocation(int size)
 {
     Blocks         = (((uint)(size + (HeapSize - 1))) >> 5) - 1;
     Flags          = 0;
     Checksum       = 0;
     MagicSignature = MagicChecksum;
     Previous       = null;
     Next           = null;
 }
Example #4
0
        internal static void Validate(HeapAllocation *root)
        {
            var ptr = root;

            while (ptr != null)
            {
                //         _logger.LogDebug(ptr->ToString());
                ptr = ptr->Next;
            }
        }
Example #5
0
        public static void Free(HeapAllocation *root)
        {
            Assert.EqualTo(root->IsFree, false);
            Assert.EqualTo(root->MagicSignature, MagicChecksum);
            root->Flags ^= 1;

            var ptr = FindFreeBackwards(root);

            ConsumeForward(ptr);
        }
Example #6
0
            public HeapPage(ILoggerFactory logFactory, IAllocator allocator, int size)
            {
                _logger    = logFactory.CreateLogger <HeapPage>();
                _allocator = allocator;
                _handle    = allocator.Take((int)size);
                Size       = size;
                _heap      = (HeapAllocation *)_handle.Address;
                *_heap = new HeapAllocation(size);

                // _enableLogging = enableLogging;
                // if (_enableLogging)
                //     _stackTrace = new string[_heap->Blocks];
            }
Example #7
0
        public static uint CountFreeBlocks(HeapAllocation *root)
        {
            var ptr = root;

            var blocks = 0u;

            while (ptr != null)
            {
                if (ptr->IsFree)
                {
                    blocks += ptr->Blocks;
                }
                ptr = ptr->Next;
            }
            return(blocks);
        }
Example #8
0
        public static HeapAllocation *FindFreeBackwards(HeapAllocation *ptr)
        {
            ////_logger.LogDebug($"  FindFreeBackwards {*ptr}");
            while (ptr->Previous != null)
            {
                Assert.EqualTo(ptr->MagicSignature, MagicChecksum);
                if (!ptr->Previous->IsFree)
                {
                    break;
                }

                ptr = ptr->Previous;
                ////_logger.LogDebug($"    Walked back {*ptr}");
            }

            Assert.EqualTo(ptr->MagicSignature, MagicChecksum);
            return(ptr);
        }
Example #9
0
        public static uint CountUsedBlocks(HeapAllocation *root, out int allocations)
        {
            allocations = 0;

            var ptr    = root;
            var blocks = 0u;

            while (ptr != null)
            {
                blocks++;
                if (!ptr->IsFree)
                {
                    blocks += ptr->Blocks;
                    allocations++;
                }
                ptr = ptr->Next;
            }

            return(blocks);
        }