/********************************************************************** *********************************************************************/ private static void WriteInitialMemoryFragmentationOnCanvas() { if (AllocationStrategiesAlgorithm.GetStatus() == AllocationStrategiesAlgorithm.Status.undefined) { canvas.DrawText("initial memory fragmentation", xPercent(0.5f), yPercent(0.2f), sk_Text); } }
/********************************************************************** *********************************************************************/ private static void DrawRedArrow(float relativeFragmentSize) { // determine position of fragment block int index = AllocationStrategiesAlgorithm.GetCurrentIndex(); //TODO: nach merge könnte der index nicht mehr existieren List <FragmentBlock> allFragmentsList = AllocationStrategiesAlgorithm.GetAllFragmentsList(); float mX1 = 0.1f; // memory bound x1 int stepsSoFar = 0; FragmentBlock fragment = null; for (int i = 0; i <= index; i++) { fragment = allFragmentsList.ElementAt(i); stepsSoFar += fragment.GetSize(); } float fragmentStart = mX1 + (stepsSoFar * relativeFragmentSize) - (fragment.GetSize() * relativeFragmentSize); float fragmentEnd = mX1 + (stepsSoFar * relativeFragmentSize); float xPosition = fragmentEnd - (fragmentEnd - fragmentStart) / 2; // position of text in the middle of fragment // draw the arrow float arrowTip = 0.275f; canvas.DrawLine(xPercent(xPosition), yPercent(arrowTip - 0.125f), xPercent(xPosition), yPercent(arrowTip), sk_ArrowRed); SKPath arrow = new SKPath(); arrow.MoveTo(xPercent(xPosition), yPercent(arrowTip)); arrow.RLineTo(yPercent(0.015f), -yPercent(0.03f)); arrow.RLineTo(-yPercent(0.03f), 0); arrow.RLineTo(yPercent(0.015f), yPercent(0.03f)); arrow.Close(); canvas.DrawPath(arrow, sk_ArrowRed); }
/********************************************************************** *********************************************************************/ private static void DrawGrayArrow(int index, float relativeFragmentSize) { // determine position of fragment block List <FragmentBlock> allFragmentsList = AllocationStrategiesAlgorithm.GetAllFragmentsList(); float mX1 = 0.1f; // memory bound x1 int stepsSoFar = 0; FragmentBlock fragment = null; for (int i = 0; i <= index; i++) { fragment = allFragmentsList.ElementAt(i); stepsSoFar += fragment.GetSize(); } float fragmentStart = mX1 + (stepsSoFar * relativeFragmentSize) - (fragment.GetSize() * relativeFragmentSize); float fragmentEnd = mX1 + (stepsSoFar * relativeFragmentSize); float xPosition = fragmentEnd - (fragmentEnd - fragmentStart) / 2; // position of text in the middle of fragment // draw the arrow float arrowTip = 0.725f; canvas.DrawLine(xPercent(xPosition), yPercent(arrowTip + 0.125f), xPercent(xPosition), yPercent(arrowTip), sk_ArrowGray); SKPath arrow = new SKPath(); arrow.MoveTo(xPercent(xPosition), yPercent(arrowTip)); arrow.RLineTo(-yPercent(0.015f), +yPercent(0.03f)); arrow.RLineTo(+yPercent(0.03f), 0); arrow.RLineTo(-yPercent(0.015f), -yPercent(0.03f)); arrow.Close(); canvas.DrawPath(arrow, sk_ArrowGray); }
/********************************************************************** *********************************************************************/ private static float DrawMemory() { // memory bounds in percent of the canvas float mX1 = 0.1f; float mX2 = 0.9f; float mY1 = 0.3f; float mY2 = 0.7f; float mWidth = mX2 - mX1; float mHeight = mY2 - mY1; int totalMemorySize = AllocationStrategiesAlgorithm.GetTotalMemorySize(); List <FragmentBlock> allFragmentsList = AllocationStrategiesAlgorithm.GetAllFragmentsList(); float relativeFragmentSize = mWidth / totalMemorySize; int stepsSoFar = 0; foreach (FragmentBlock fragment in allFragmentsList) { // fragment size of 1 gets displayed as a . String fragmentSize = fragment.GetSize().ToString(); if (fragmentSize == "1") { fragmentSize = "•"; } stepsSoFar += fragment.GetSize(); float fragmentStart = mX1 + (stepsSoFar * relativeFragmentSize) - (fragment.GetSize() * relativeFragmentSize); float fragmentEnd = mX1 + (stepsSoFar * relativeFragmentSize); float xText = fragmentEnd - (fragmentEnd - fragmentStart) / 2; // position of text in the middle of fragment if (fragment.IsFree()) { // draw size of fragment in the middle of memory canvas.DrawText(fragmentSize, xPercent(xText), yPercent(0.5f), sk_Text); } else { // draw size of fragment beneath the memory //canvas.DrawText(fragmentSize, xPercent(xText), yPercent(0.8f), sk_TextWhite); canvas.DrawText(fragmentSize, xPercent(xText), yPercent(0.8f), sk_Text); // fill in used space SKRect usedSpace = new SKRect(xPercent(fragmentStart), yPercent(mY1), xPercent(fragmentEnd), yPercent(mY2)); //left x1 top y1 right x2 bottom y2 canvas.DrawRect(usedSpace, sk_UsedSpace); } } // draw the outlines of the memory box SKRect sk_memory = new SKRect(xPercent(mX1), yPercent(mY1), xPercent(mX2), yPercent(mY2)); canvas.DrawRect(sk_memory, sk_Black); canvas.DrawText("free", xPercent(0.05f), yPercent(0.5f), sk_Text); //canvas.DrawText("used", xPercent(0.05f), yPercent(0.8f), sk_TextWhite); canvas.DrawText("used", xPercent(0.05f), yPercent(0.8f), sk_Text); return(relativeFragmentSize); }
/********************************************************************** *********************************************************************/ void B_Restart_Clicked(object sender, EventArgs e) { algo = new AllocationStrategiesAlgorithm(strategy, allFragmentsList); CreateContent(); AllocationStrategiesDraw.Paint(); foreach (FragmentBlock fb in allFragmentsList) { //Debug.Write(" |" + fb.IsFree() + " " + fb.GetSize()); } }
/********************************************************************** *********************************************************************** * draw on canvas */ void PaintSurface(object sender, SKPaintSurfaceEventArgs e) { //canvas object info = e.Info; // SKSurface surface = e.Surface; canvas = surface.Canvas; //Important! Otherwise the drawing will look messed up in iOS if (canvas != null) { canvas.Clear(); } CalculateNeededVariables(); MakeSKPaint(); /*********************HERE GOES THE DRAWING************************/ /*important: the coordinate system starts in the upper left corner*/ WriteInitialMemoryFragmentationOnCanvas(); float relativeFragmentSize = DrawMemory(); AllocationStrategiesAlgorithm.Status status = AllocationStrategiesAlgorithm.GetStatus(); if (status == AllocationStrategiesAlgorithm.Status.searching || status == AllocationStrategiesAlgorithm.Status.successfull) { DrawRedArrow(relativeFragmentSize); int promIndex = AllocationStrategiesAlgorithm.GetMostPromisingIndex(); if (promIndex != -1) { DrawGrayArrow(promIndex, relativeFragmentSize); } } //execute all drawing actions canvas.Flush(); }
private static String strategy; // chosen strategy for memory allocation //CONSTRUCTOR public AllocationStrategies(String p_Strategy, List <FragmentBlock> p_AllFragmentsList) { // Help/ info button upper right corner ToolbarItem info = new ToolbarItem(); info.Text = App._sHelpInfoHint; this.ToolbarItems.Add(info); info.Clicked += B_Info_Clicked; // assign variables strategy = p_Strategy; allFragmentsList = new List <FragmentBlock>(p_AllFragmentsList); // copy List without reference to be able to alter it without affecting the original. NOTE: objects in list can still be affected algo = new AllocationStrategiesAlgorithm(strategy, allFragmentsList); Title = "Allocation Strategies: " + p_Strategy; MessagingCenter.Send <object>(this, "Landscape"); // enforce landscape mode // content starts only after notch On <iOS>().SetUseSafeArea(true); CreateContent(); }
/********************************************************************** *********************************************************************/ async void B_Next_Clicked(object sender, EventArgs e) { AllocationStrategiesAlgorithm.Status status = AllocationStrategiesAlgorithm.GetStatus(); // if new memory request was made if (b_Next.Text == "Confirm") { // if the memory request is valid if (ValidateMemoryRequestInput(e_MemoryRequest.Text)) { b_Next.Text = "Next"; // disable memory request entry and indicate that it's accepted e_MemoryRequest.IsEnabled = false; e_MemoryRequest.BackgroundColor = Color.FromRgba(172, 255, 47, 30); // enable restart button b_Restart.IsEnabled = true; // check first if memory is already full before you make a new request if (AllocationStrategiesAlgorithm.MemoryIsFull()) { await DisplayAlert("Alert", "Out of memory! Please restart.", "OK"); b_Next.Text = "Confirm"; e_MemoryRequest.IsEnabled = true; // enable memory request entry e_MemoryRequest.BackgroundColor = Color.White; e_MemoryRequest.Text = ""; } else { AllocationStrategiesAlgorithm.Start(Int32.Parse(e_MemoryRequest.Text)); } } // otherwise wait until a valid memory request is made by the user else { e_MemoryRequest.BackgroundColor = Color.FromRgba(238, 130, 238, 30); } } else { //Debug.WriteLine("next button text"); // start or still searching if (status == AllocationStrategiesAlgorithm.Status.searching || status == AllocationStrategiesAlgorithm.Status.start) { Debug.WriteLine("start or Still searching "); AllocationStrategiesAlgorithm.Next(); status = AllocationStrategiesAlgorithm.GetStatus(); if (status == AllocationStrategiesAlgorithm.Status.successfull) { b_Next.Text = "Allocate"; } else if (status == AllocationStrategiesAlgorithm.Status.unsuccessfull) { b_Next.Text = "Confirm"; e_MemoryRequest.IsEnabled = true; // enable memory request entry e_MemoryRequest.BackgroundColor = Color.White; e_MemoryRequest.Text = ""; await DisplayAlert("Alert", "Memory request was unsuccessfull.", "OK"); } } //sth found else if (status == AllocationStrategiesAlgorithm.Status.successfull) { //Debug.WriteLine("Sth found"); AllocationStrategiesAlgorithm.UpdateAllFragmentsList(); b_Next.Text = "Confirm"; e_MemoryRequest.IsEnabled = true; // enable memory request entry e_MemoryRequest.BackgroundColor = Color.White; e_MemoryRequest.Text = ""; } /* * else if (status == AllocationStrategiesAlgorithm.Status.unsuccessfull) * { * b_Next.Text = "Confirm"; * e_MemoryRequest.IsEnabled = true; // enable memory request entry * e_MemoryRequest.BackgroundColor = Color.White; * e_MemoryRequest.Text = ""; * await DisplayAlert("Alert", "Memory request was unsuccessfull.", "OK"); * * }*/ } AllocationStrategiesDraw.Paint(); }