public static void* Alloc(UInt32 size, UInt32 boundary, FlingOops.String caller) { #if HEAP_TRACE BasicConsole.SetTextColour(BasicConsole.warning_colour); BasicConsole.WriteLine("Attempt to alloc mem...."); BasicConsole.SetTextColour(BasicConsole.default_colour); #endif HeapBlock* b; byte* bm; UInt32 bcnt; UInt32 x, y, z; UInt32 bneed; byte nid; #if MIPS if (boundary < 4) { boundary = 4; } #endif if (boundary > 1) { size += (boundary - 1); } /* iterate blocks */ for (b = fblock; (UInt32)b != 0; b = b->next) { /* check if block has enough room */ if (b->size - (b->used * b->bsize) >= size) { bcnt = b->size / b->bsize; bneed = (size / b->bsize) * b->bsize < size ? size / b->bsize + 1 : size / b->bsize; bm = (byte*)&b[1]; for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x != b->lfb; ++x) { /* just wrap around */ if (x >= bcnt) { x = 0; } if (bm[x] == 0) { /* count free blocks */ for (y = 0; bm[x + y] == 0 && y < bneed && (x + y) < bcnt; ++y) ; /* we have enough, now allocate them */ if (y == bneed) { /* find ID that does not match left or right */ nid = GetNID(bm[x - 1], bm[x + y]); /* allocate by setting id */ for (z = 0; z < y; ++z) { bm[x + z] = nid; } /* optimization */ b->lfb = (x + bneed) - 2; /* count used blocks NOT bytes */ b->used += y; void* result = (void*)(x * b->bsize + (UInt32)(&b[1])); if (boundary > 1) { result = (void*)((((UInt32)result) + (boundary - 1)) & ~(boundary - 1)); #if HEAP_TRACE ExitCritical(); BasicConsole.WriteLine(((FlingOops.String)"Allocated address ") + (uint)result + " on boundary " + boundary + " for " + caller); EnterCritical("Alloc:Boundary condition"); #endif } return result; } /* x will be incremented by one ONCE more in our FOR loop */ x += (y - 1); continue; } } } } #if HEAP_TRACE BasicConsole.SetTextColour(BasicConsole.error_colour); BasicConsole.WriteLine("!!Heap out of memory!!"); BasicConsole.SetTextColour(BasicConsole.default_colour); BasicConsole.DelayOutput(2); #endif return null; }
public static void Test_Arg_String(FlingOops.String a) { if (a == "I am a string") { Log.WriteSuccess("Test_Arg_String okay."); } else { Log.WriteError("Test_Arg_String NOT okay."); } }
public static void* AllocZeroedAPB(UInt32 size, UInt32 boundary, FlingOops.String caller) { void* result = null; void* oldValue = null; UInt32 resultAddr; do { oldValue = result; result = AllocZeroed(size, boundary, caller); resultAddr = (UInt32)result; if (oldValue != null) { Free(oldValue); } } while (resultAddr / 0x1000 != (resultAddr + size - 1) / 0x1000); return result; }
public static void* AllocZeroed(UInt32 size, UInt32 boundary, FlingOops.String caller) { void* result = Alloc(size, boundary, caller); if(result == null) { return null; } return Utilities.MemoryUtils.ZeroMem(result, size); }
public static void WriteError(FlingOops.String message) { SetTextColour(error_colour); WriteLine(message); SetTextColour(default_colour); #if x86 DelayOutput(20); #endif }
public static unsafe void Throw(FlingOops.Exception ex) { FlingOops.GC.IncrementRefCount(ex); BasicConsole.WriteLine("Exception thrown"); BasicConsole.WriteLine(ex.Message); if (State->CurrentHandlerPtr->Ex != null) { //GC ref count remains consistent because the Ex pointer below is going to be replaced but // same pointer stored in InnerException. // Result is ref count goes: +1 here, -1 below ex.InnerException = (FlingOops.Exception)Utilities.ObjectUtilities.GetObject(State->CurrentHandlerPtr->Ex); } if (ex.InstructionAddress == 0) { ex.InstructionAddress = *((uint*)BasePointer + 1); } State->CurrentHandlerPtr->Ex = Utilities.ObjectUtilities.GetHandle(ex); State->CurrentHandlerPtr->ExPending = 1; HandleException(); // We never expect to get here... HaltReason = "HandleException returned!"; BasicConsole.WriteLine(HaltReason); // Try to cause fault *((byte*)0xDEADBEEF) = 0; }
public static void DecrementRefCount(FlingOops.Object anObj, bool overrideInside) { if (!Enabled /*|| (InsideGC && !overrideInside)*/ || anObj == null) { return; } if (!overrideInside) { InsideGC = true; } byte* objPtr = (byte*)Utilities.ObjectUtilities.GetHandle(anObj); _DecrementRefCount(objPtr); if (!overrideInside) { InsideGC = false; } }
public static unsafe void WriteLine(FlingOops.String str) { if (!Initialised) return; if (str == null) { return; } #if MIPS //This outputs the string Write(str); Write("\n"); #elif x86 if (PrimaryOutputEnabled) { //This block shifts the video memory up the required number of lines. if (offset == cols * rows) { char* vidMemPtr_Old = vidMemBasePtr; char* vidMemPtr_New = vidMemBasePtr + cols; char* maxVidMemPtr = vidMemBasePtr + (cols * rows); while (vidMemPtr_New < maxVidMemPtr) { vidMemPtr_Old[0] = vidMemPtr_New[0]; vidMemPtr_Old++; vidMemPtr_New++; } offset -= cols; } } //This outputs the string Write(str); if (PrimaryOutputEnabled) { //This block "writes" the new line by filling in the remainder (if any) of the // line with blank characters and correct background colour. int diff = offset; while (diff > cols) { diff -= cols; } diff = cols - diff; char* vidMemPtr = vidMemBasePtr + offset; while (diff > 0) { vidMemPtr[0] = bg_colour; diff--; vidMemPtr++; offset++; } } if (SecondaryOutput != null && SecondaryOutputEnabled) { SecondaryOutput("\r\n"); } #endif }
public static void IncrementRefCount(FlingOops.Object anObj) { if (!Enabled /*|| InsideGC*/ || anObj == null) { return; } InsideGC = true; byte* objPtr = (byte*)Utilities.ObjectUtilities.GetHandle(anObj); _IncrementRefCount(objPtr); InsideGC = false; }
public static void DecrementRefCount(FlingOops.Object anObj) { DecrementRefCount(anObj, false); }
public static void* NewArr(int length, FlingOops.Type elemType) { if (!Enabled) { BasicConsole.SetTextColour(BasicConsole.warning_colour); BasicConsole.WriteLine("Warning! GC returning null pointer because GC not enabled."); BasicConsole.DelayOutput(10); BasicConsole.SetTextColour(BasicConsole.default_colour); return null; } //try { if (length < 0) { BasicConsole.WriteLine("length < 0. Overflow exception."); return null; //ExceptionMethods.Throw_OverflowException(); } InsideGC = true; //Alloc space for GC header that prefixes object data //Alloc space for new array object //Alloc space for new array elems uint totalSize = ((FlingOops.Type)typeof(FlingOops.Array)).Size; if (elemType.IsValueType) { totalSize += elemType.Size * (uint)length; } else { totalSize += elemType.StackSize * (uint)length; } totalSize += (uint)sizeof(GCHeader); GCHeader* newObjPtr = (GCHeader*)Heap.AllocZeroed(totalSize, "GC : NewArray"); if ((UInt32)newObjPtr == 0) { InsideGC = false; BasicConsole.SetTextColour(BasicConsole.error_colour); BasicConsole.WriteLine("Error! GC can't create a new array because the heap returned a null pointer."); BasicConsole.DelayOutput(10); BasicConsole.SetTextColour(BasicConsole.default_colour); return null; } NumObjs++; //Initialise the GCHeader SetSignature(newObjPtr); newObjPtr->RefCount = 1; FlingOops.Array newArr = (FlingOops.Array)Utilities.ObjectUtilities.GetObject(newObjPtr + 1); newArr._Type = (FlingOops.Type)typeof(FlingOops.Array); newArr.length = length; newArr.elemType = elemType; //Move past GCHeader byte* newObjBytePtr = (byte*)(newObjPtr + 1); InsideGC = false; return newObjBytePtr; } //finally { } }
public static void* NewObj(FlingOops.Type theType) { if (!Enabled) { BasicConsole.SetTextColour(BasicConsole.warning_colour); BasicConsole.WriteLine("Warning! GC returning null pointer because GC not enabled."); BasicConsole.DelayOutput(10); BasicConsole.SetTextColour(BasicConsole.default_colour); return null; } //try //{ InsideGC = true; //Alloc space for GC header that prefixes object data //Alloc space for new object uint totalSize = theType.Size; totalSize += (uint)sizeof(GCHeader); GCHeader* newObjPtr = (GCHeader*)Heap.AllocZeroed(totalSize, "GC : NewObject"); if ((UInt32)newObjPtr == 0) { InsideGC = false; BasicConsole.SetTextColour(BasicConsole.error_colour); BasicConsole.WriteLine("Error! GC can't create a new object because the heap returned a null pointer."); BasicConsole.DelayOutput(10); BasicConsole.SetTextColour(BasicConsole.default_colour); return null; } NumObjs++; //Initialise the GCHeader SetSignature(newObjPtr); newObjPtr->RefCount = 1; //Initialise the object _Type field FlingOops.ObjectWithType newObj = (FlingOops.ObjectWithType)Utilities.ObjectUtilities.GetObject(newObjPtr + 1); newObj._Type = theType; //Move past GCHeader byte* newObjBytePtr = (byte*)(newObjPtr + 1); InsideGC = false; return newObjBytePtr; //} //finally //{ //} }
public static void FillString(uint value, int offset, FlingOops.String str) { int end = offset - 8; while (offset > end) { uint rem = value & 0xFu; switch (rem) { case 0: str[offset] = '0'; break; case 1: str[offset] = '1'; break; case 2: str[offset] = '2'; break; case 3: str[offset] = '3'; break; case 4: str[offset] = '4'; break; case 5: str[offset] = '5'; break; case 6: str[offset] = '6'; break; case 7: str[offset] = '7'; break; case 8: str[offset] = '8'; break; case 9: str[offset] = '9'; break; case 10: str[offset] = 'A'; break; case 11: str[offset] = 'B'; break; case 12: str[offset] = 'C'; break; case 13: str[offset] = 'D'; break; case 14: str[offset] = 'E'; break; case 15: str[offset] = 'F'; break; } value >>= 4; offset--; } }
public static void Test_Arg_Param(Int32 sign32, Int64 sign64, UInt32 unsign32, UInt64 unsign64, FlingOops.String str, FlingOops.String str2) { if ((sign32 == 6) && (sign64 == 1441151880758558720) && (unsign32 == 100) && (unsign64 == 10223372036854775807) && (str == "I am a string") && (str2 == "I am a string too")) { Log.WriteSuccess("Test_Arg_Param okay."); } else { Log.WriteError("Test_Arg_Param NOT okay."); } }
public static void Disable(FlingOops.String caller) { //BasicConsole.Write(caller); //BasicConsole.WriteLine(" disabling GC."); //BasicConsole.DelayOutput(2); GC.Enabled = false; }
public static unsafe void Write(FlingOops.String str) { if (!Initialised) return; //If string is null, just don't write anything if (str == null) { //Do not make this throw an exception. The BasicConsole // is largely a debugging tool - it should be reliable, // robust and not throw exceptions. return; } #if MIPS UART.Write(str); #elif x86 if (PrimaryOutputEnabled) { int strLength = str.length; int maxOffset = rows * cols; //This block shifts the video memory up the required number of lines. if (offset + strLength > maxOffset) { int amountToShift = (offset + strLength) - maxOffset; amountToShift = amountToShift + (80 - (amountToShift % 80)); offset -= amountToShift; char* vidMemPtr_Old = vidMemBasePtr; char* vidMemPtr_New = vidMemBasePtr + amountToShift; char* maxVidMemPtr = vidMemBasePtr + (cols * rows); while (vidMemPtr_New < maxVidMemPtr) { vidMemPtr_Old[0] = vidMemPtr_New[0]; vidMemPtr_Old++; vidMemPtr_New++; } } //This block outputs the string in the current foreground / background colours. char* vidMemPtr = vidMemBasePtr + offset; char* strPtr = str.GetCharPointer(); while (strLength > 0) { vidMemPtr[0] = (char)((*strPtr & 0x00FF) | colour); strLength--; vidMemPtr++; strPtr++; offset++; } } if (SecondaryOutput != null && SecondaryOutputEnabled) { SecondaryOutput(str); } #endif }
public static void Write(FlingOops.String str) { for (int i = 0; i < str.length; i++) { Write(str[i]); } }
public static void WriteSuccess(FlingOops.String message) { SetTextColour(success_colour); WriteLine(message); SetTextColour(default_colour); }
public static void* AllocZeroed(UInt32 size, FlingOops.String caller) { return AllocZeroed(size, 1, caller); }
/// <summary> /// Creates a new exception with specified message. /// </summary> /// <param name="aMessage">The exception message.</param> public Exception(FlingOops.String aMessage) : base() { Message = aMessage; }
public static unsafe FlingOops.String Concat(FlingOops.String str1, FlingOops.String str2) { FlingOops.String newStr = New(str1.length + str2.length); for (int i = 0; i < str1.length; i++) { newStr[i] = str1[i]; } for (int i = 0; i < str2.length; i++) { newStr[i + str1.length] = str2[i]; } return newStr; }