Exemplo n.º 1
0
        public static void Setup()
        {
            BitMapIndexTable = new BitMapIndexTable(BootPageAllocator.AllocatePage());

            var maximumMemoryAddress = BootMemoryMap.GetMaximumAddress();

            TotalPages = (uint)(maximumMemoryAddress.ToInt64() / Page.Size);
            var bitMapCount = TotalPages / (Page.Size * 8);

            for (uint i = 0; i < bitMapCount; i++)
            {
                var bitmap = BootPageAllocator.AllocatePage();
                Page.ClearPage(bitmap);

                BitMapIndexTable.AddBitMapEntry(i, bitmap);
            }

            var entries = BootMemoryMap.GetBootMemoryMapEntryCount();

            // pass 0 - mark available pages
            for (uint i = 0; i < entries; i++)
            {
                var entry = BootMemoryMap.GetBootMemoryMapEntry(i);

                if (!entry.IsAvailable)
                {
                    continue;
                }

                var start = Alignment.AlignUp(entry.StartAddress.ToInt64(), Page.Size);
                var end   = Alignment.AlignDown(entry.EndAddress.ToInt64() + 1, Page.Size);

                var pages = (uint)(end - start) / Page.Size;

                if (pages <= 0)
                {
                    continue;
                }

                var startPage = (uint)(start / Page.Shift);
                var endPage   = startPage + pages - 1;

                MinimumAvailablePage = Math.Min(MinimumAvailablePage, startPage);
                MaximumAvailablePage = Math.Max(MaximumAvailablePage, endPage);

                SetPageBitMapEntry(startPage, pages, entry.IsAvailable);
            }

            // pass 1 - unmark reserved pages
            for (uint i = 0; i < entries; i++)
            {
                var entry = BootMemoryMap.GetBootMemoryMapEntry(i);

                if (entry.IsAvailable)
                {
                    continue;
                }

                var start = Alignment.AlignUp(entry.StartAddress.ToInt64(), Page.Size);
                var end   = Alignment.AlignDown(entry.EndAddress.ToInt64() + 1, Page.Size);

                var pages = (uint)(end - start) / Page.Size;

                if (pages <= 0)
                {
                    continue;
                }

                var startPage = (uint)(start / Page.Shift);
                var endPage   = startPage + pages - 1;

                MinimumReservedPage = Math.Min(MinimumReservedPage, startPage);
                MaximumReservedPage = Math.Max(MaximumReservedPage, endPage);

                if (MinimumAvailablePage >= startPage && MinimumAvailablePage <= endPage)
                {
                    MinimumAvailablePage = endPage + 1;
                }

                if (MaximumAvailablePage >= startPage && MaximumAvailablePage <= endPage)
                {
                    MaximumAvailablePage = startPage - 1;
                }

                SetPageBitMapEntry(startPage, pages, entry.IsAvailable);
            }

            SearchNextStartPage = MinimumAvailablePage;
        }