Example #1
0
        public void initialize(List <MemorySlot> free_slots)
        {
            processes      = new List <Process>();
            freeSlots      = new List <MemorySlot>();
            allocatedSlots = new List <MemorySlot>();

            free_slots.Sort();
            int lastAddress = 0;

            foreach (MemorySlot slot in free_slots)
            {
                if (slot.start > lastAddress)
                {
                    Process    process = new Process("Allocated", slot.start - lastAddress);
                    MemorySlot newSlot = new MemorySlot(lastAddress, slot.start - lastAddress, process);
                    addSlot(newSlot, allocatedSlots);
                    processes.Add(process);
                }
                addFreeSlot(slot);
                lastAddress = slot.start + slot.size;
            }

            if (memorySize - lastAddress > 0)
            {
                Process    process = new Process("Allocated", memorySize - lastAddress);
                MemorySlot newSlot = new MemorySlot(lastAddress, memorySize - lastAddress, process);
                addSlot(newSlot, allocatedSlots);
                processes.Add(process);
            }
        }
Example #2
0
        public bool allocate(String name, int size)
        {
            MemorySlot freeSlot = findSlot(size);

            if (freeSlot != null)
            {
                int allocationStart = freeSlot.start;
                int remainingSize   = freeSlot.size - size;
                int remainingStart  = freeSlot.start + size;

                removeSlot(freeSlot, freeSlots);

                if (remainingSize > 0)
                {
                    addFreeSlot(new MemorySlot(remainingStart, remainingSize));
                }

                Process process = new Process(name, size);
                addSlot(new MemorySlot(allocationStart, size, process), allocatedSlots);
                processes.Add(process);

                return(true);
            }
            return(false);
        }
Example #3
0
        private MemorySlot mergeSlots(MemorySlot slot1, MemorySlot slot2)
        {
            int start1 = slot1.start;
            int end1   = slot1.start + slot1.size;

            int start2 = slot2.start;
            int end2   = slot2.start + slot2.size;

            if (start1 > start2)
            {
                int tmp;

                tmp    = start2;
                start2 = start1;
                start1 = tmp;

                tmp  = end1;
                end1 = end2;
                end2 = tmp;
            }

            if (start2 > end1)
            {
                return(null);
            }

            int start = start1;
            int end   = end1 > end2 ? end1 : end2;

            return(new MemorySlot(start, end - start));
        }
Example #4
0
 private bool isBetter(MemorySlot s1, MemorySlot s2, int algorithm)
 {
     if (algorithm == FIRST_FIT)
     {
         return(s1.start < s2.start);
     }
     else if (algorithm == BEST_FIT)
     {
         return(s1.size < s2.size);
     }
     else if (algorithm == WORST_FIT)
     {
         return(s1.size > s2.size);
     }
     return(false);
 }
Example #5
0
        private void button_add_Click(object sender, EventArgs e)
        {
            int start = int.Parse(textBox_start.Text);
            int size  = int.Parse(textBox_size.Text);
            int end   = start + size;

            MemorySlot slot = new MemorySlot(start, size);

            holes.Add(slot);
            listBox_holes.Items.Add(slot);

            button_Initialize.Enabled = listBox_holes.Items.Count != 0;
            textBox_start.Text        = end.ToString();
            if (end > int.Parse(textBox_memorySize.Text))
            {
                textBox_memorySize.Text = end.ToString();
            }
        }
Example #6
0
 public void addSlot(MemorySlot slot, List <MemorySlot> slots)
 {
     if (slots.Count == 0)
     {
         slots.Add(slot);
     }
     else
     {
         for (int i = 0; i < slots.Count; i++)
         {
             if (isBetter(slot, slots[i], algorithm))
             {
                 slots.Insert(i, slot);
                 return;
             }
         }
         slots.Add(slot);
     }
 }
Example #7
0
        public void deallocate(Process process)
        {
            //Process process = processes[index];
            MemorySlot allocatedSlot = null;

            foreach (MemorySlot slot in allocatedSlots)
            {
                if (slot.process == process)
                {
                    allocatedSlot = slot;
                    break;
                }
            }

            removeSlot(allocatedSlot, allocatedSlots);

            MemorySlot freeSlot = new MemorySlot(allocatedSlot.start, allocatedSlot.size);

            addFreeSlot(freeSlot);

            processes.Remove(process);
        }
Example #8
0
        private void addFreeSlot(MemorySlot slot)
        {
            addSlot(slot, freeSlots);

            List <MemorySlot> mergingList = new List <MemorySlot>();

            // Search for mergable slots
            foreach (MemorySlot s in freeSlots)
            {
                if (mergeSlots(slot, s) != null)
                {
                    mergingList.Add(s);
                }
            }

            foreach (MemorySlot s in mergingList)
            {
                slot = mergeSlots(slot, s);
                removeSlot(s, freeSlots);
            }

            addSlot(slot, freeSlots);
        }
Example #9
0
        private void DrawSlot(Graphics g, Panel panel, MemorySlot slot, int memorySize, Color color)
        {
            int    startAddress = slot.start;
            int    endAddress   = slot.start + slot.size;
            string slotText     = "Free";

            if (slot.process != null)
            {
                slotText = slot.process.name;
            }
            slotText = slotText + " - " + slot.size.ToString() + " B";

            Pen pen = new Pen(Color.Black, 2);

            SolidBrush textBrush    = new SolidBrush(Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B));
            SolidBrush addressBrush = new SolidBrush(Color.Black);

            StringFormat sf = new StringFormat();

            sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;

            int VMargin     = 10;
            int totalHeight = panel.Height - VMargin * 2;

            int slotWidth  = panel.Width * 60 / 100;
            int slotHeight = totalHeight * (endAddress - startAddress) / memorySize;

            Point leftTop     = new Point(panel.Width * 5 / 100, VMargin + totalHeight * startAddress / memorySize);
            Point leftBottom  = new Point(leftTop.X, leftTop.Y + slotHeight);
            Point rightBottom = new Point(leftTop.X + slotWidth, leftTop.Y + slotHeight);
            Point rightTop    = new Point(leftTop.X + slotWidth, leftTop.Y);

            Point centerPoint      = new Point(leftTop.X + slotWidth / 2, leftTop.Y + slotHeight / 2);
            Point slotTextPosition = centerPoint;
            Point addressPosition  = new Point(rightBottom.X + 10, rightBottom.Y - 10);

            // Draw slot borders
            g.DrawLine(pen, leftTop, leftBottom);
            g.DrawLine(pen, rightTop, rightBottom);
            g.DrawLine(pen, leftTop, rightTop);
            g.DrawLine(pen, leftBottom, rightBottom);

            // Fill slot background
            g.FillRectangle(new SolidBrush(color), leftTop.X, leftTop.Y, slotWidth, slotHeight);

            // Print process name
            if (slotHeight > 16)
            {
                slotTextPosition.X += slotText.Count() * 6 / 2;
                slotTextPosition.Y -= 8;
                g.DrawString(slotText, this.Font, textBrush, slotTextPosition, sf);
            }

            // Print address label
            String addressText = "0x" + endAddress.ToString("X");

            if (slotHeight > 8)
            {
                addressPosition.X += addressText.Count() * 6;
                g.DrawString(addressText, this.Font, addressBrush, addressPosition, sf);
            }
        }
Example #10
0
 public void removeSlot(MemorySlot slot, List <MemorySlot> slots)
 {
     slots.Remove(slot);
 }