<commit_before>﻿using System;

namespace Cosmos.Kernel {

    public static class Heap
    {
        private static uint mStart;
        private static uint mStartAddress;        
        private static uint mLength;
        private static uint mEndOfRam;

        private static void Initialize(uint aStartAddress, uint aEndOfRam)
        {
            mStart = mStartAddress = aStartAddress + (4 - (aStartAddress % 4));
            mLength = aEndOfRam - aStartAddress;
            mLength = (mLength / 4) * 4;
            mStartAddress += 1024;
            mEndOfRam = aEndOfRam;
            mStartAddress = (mStartAddress / 4) * 4;
            mLength -= 1024;
            ClearMemory(aStartAddress, mLength);
            UpdateDebugDisplay();
        }

        private static void WriteNumber(uint aNumber, byte aBits)
        {
            uint xValue = aNumber;
            byte xCurrentBits = aBits;
            Console.Write("0x");
            while (xCurrentBits >= 4) {
                xCurrentBits -= 4;
                byte xCurrentDigit = (byte)((xValue >> xCurrentBits) & 0xF);
                string xDigitString = null;
                switch (xCurrentDigit) {
                    case 0:
                        xDigitString = "0";
                        goto default;
                    case 1:
                        xDigitString = "1";
                        goto default;
                    case 2:
                        xDigitString = "2";
                        goto default;
                    case 3:
                        xDigitString = "3";
                        goto default;
                    case 4:
                        xDigitString = "4";
                        goto default;
                    case 5:
                        xDigitString = "5";
                        goto default;
                    case 6:
                        xDigitString = "6";
                        goto default;
                    case 7:
                        xDigitString = "7";
                        goto default;
                    case 8:
                        xDigitString = "8";
                        goto default;
                    case 9:
                        xDigitString = "9";
                        goto default;
                    case 10:
                        xDigitString = "A";
                        goto default;
                    case 11:
                        xDigitString = "B";
                        goto default;
                    case 12:
                        xDigitString = "C";
                        goto default;
                    case 13:
                        xDigitString = "D";
                        goto default;
                    case 14:
                        xDigitString = "E";
                        goto default;
                    case 15:
                        xDigitString = "F";
                        goto default;
                    default:
                        if (xDigitString == null) {
                            System.Diagnostics.Debugger.Break();
                        }
                        Console.Write(xDigitString);
                        break;
                }
            }
        }

        private static bool mDebugDisplayInitialized = false;

        // this method displays the used/total memory of the heap on the first line of the text screen
        private static void UpdateDebugDisplay() {
            if (!mDebugDisplayInitialized) {
                mDebugDisplayInitialized = true;
                int xOldPositionLeft = Console.CursorLeft;
                int xOldPositionTop = Console.CursorTop;
                Console.CursorLeft = 0;
                Console.CursorTop = 0;
                Console.Write("[Heap Usage: ");
                WriteNumber(mStartAddress,
                            32);
                Console.Write("/");
                WriteNumber(mEndOfRam,
                            32);
                Console.Write("] bytes");
                while(Console.CursorLeft < (Console.WindowWidth-1)) {
                    Console.Write(" ");
                }
                Console.CursorLeft = xOldPositionLeft;
                Console.CursorTop = xOldPositionTop;
            }else {
                int xOldPositionLeft = Console.CursorLeft;
                int xOldPositionTop = Console.CursorTop;
                Console.CursorLeft = 13;
                Console.CursorTop = 0;
                WriteNumber(mStartAddress,
                            32);
                Console.CursorLeft = xOldPositionLeft;
                Console.CursorTop = xOldPositionTop;
            }
        }

        private static bool mInited;
        public static void Init()
        {
            if (!mInited)
            {
                mInited = true;
                Initialize(CPU.EndOfKernel, (CPU.AmountOfMemory * 1024 * 1024) - 1024);
            }
        }
        private static void ClearMemory(uint aStartAddress, uint aLength)
        {
            CPU.ZeroFill(aStartAddress, aLength);
        }

        public static uint MemAlloc(uint aLength)
        {
            Init();
            uint xTemp = mStartAddress;
            if ((xTemp + aLength) > (mStart + mLength))
            {
                Console.WriteLine("Too large memory block allocated!");
                System.Diagnostics.Debugger.Break();<commit_msg>Added simple boolean property (EnableDebug) which disables the heap consumption status line if set to false.<commit_after>﻿using System;

namespace Cosmos.Kernel
{
    public static class Heap
    {
        public static bool EnableDebug = true;
        private static uint mStart;
        private static uint mStartAddress;        
        private static uint mLength;
        private static uint mEndOfRam;

        private static void Initialize(uint aStartAddress, uint aEndOfRam)
        {
            mStart = mStartAddress = aStartAddress + (4 - (aStartAddress % 4));
            mLength = aEndOfRam - aStartAddress;
            mLength = (mLength / 4) * 4;
            mStartAddress += 1024;
            mEndOfRam = aEndOfRam;
            mStartAddress = (mStartAddress / 4) * 4;
            mLength -= 1024;
            ClearMemory(aStartAddress, mLength);
            UpdateDebugDisplay();
        }

        private static void WriteNumber(uint aNumber, byte aBits)
        {
            uint xValue = aNumber;
            byte xCurrentBits = aBits;
            Console.Write("0x");
            while (xCurrentBits >= 4) {
                xCurrentBits -= 4;
                byte xCurrentDigit = (byte)((xValue >> xCurrentBits) & 0xF);
                string xDigitString = null;
                switch (xCurrentDigit) {
                    case 0:
                        xDigitString = "0";
                        goto default;
                    case 1:
                        xDigitString = "1";
                        goto default;
                    case 2:
                        xDigitString = "2";
                        goto default;
                    case 3:
                        xDigitString = "3";
                        goto default;
                    case 4:
                        xDigitString = "4";
                        goto default;
                    case 5:
                        xDigitString = "5";
                        goto default;
                    case 6:
                        xDigitString = "6";
                        goto default;
                    case 7:
                        xDigitString = "7";
                        goto default;
                    case 8:
                        xDigitString = "8";
                        goto default;
                    case 9:
                        xDigitString = "9";
                        goto default;
                    case 10:
                        xDigitString = "A";
                        goto default;
                    case 11:
                        xDigitString = "B";
                        goto default;
                    case 12:
                        xDigitString = "C";
                        goto default;
                    case 13:
                        xDigitString = "D";
                        goto default;
                    case 14:
                        xDigitString = "E";
                        goto default;
                    case 15:
                        xDigitString = "F";
                        goto default;
                    default:
                        if (xDigitString == null) {
                            System.Diagnostics.Debugger.Break();
                        }
                        Console.Write(xDigitString);
                        break;
                }
            }
        }

        private static bool mDebugDisplayInitialized = false;

        // this method displays the used/total memory of the heap on the first line of the text screen
        private static void UpdateDebugDisplay()
        {
            if (EnableDebug)
            {
                if (!mDebugDisplayInitialized)
                {
                    mDebugDisplayInitialized = true;
                    int xOldPositionLeft = Console.CursorLeft;
                    int xOldPositionTop = Console.CursorTop;
                    Console.CursorLeft = 0;
                    Console.CursorTop = 0;
                    Console.Write("[Heap Usage: ");
                    WriteNumber(mStartAddress,
                                32);
                    Console.Write("/");
                    WriteNumber(mEndOfRam,
                                32);
                    Console.Write("] bytes");
                    while (Console.CursorLeft < (Console.WindowWidth-1))
                    {
                        Console.Write(" ");
                    }
                    Console.CursorLeft = xOldPositionLeft;
                    Console.CursorTop = xOldPositionTop;
                }
                else
                {
                    int xOldPositionLeft = Console.CursorLeft;
                    int xOldPositionTop = Console.CursorTop;
                    Console.CursorLeft = 13;
                    Console.CursorTop = 0;
                    WriteNumber(mStartAddress,
                                32);
                    Console.CursorLeft = xOldPositionLeft;
                    Console.CursorTop = xOldPositionTop;
                }
            }
        }

        private static bool mInited;
        public static void Init()
        {
            if (!mInited)
            {
                mInited = true;
                Initialize(CPU.EndOfKernel, (CPU.AmountOfMemory * 1024 * 1024) - 1024);
            }
        }
        private static void ClearMemory(uint aStartAddress, uint aLength)
        {
            CPU.ZeroFill(aStartAddress, aLength);
        }

        public static uint MemAlloc(uint aLength)
        {
            Init();
            uint xTemp = mStartAddress;
            if ((xTemp + aLength) > (mStart + mLength))
            {
                Console.WriteLine("Too large memory block allocated!");
                System.Diagnostics.Debugger.Break();
{'fraction_non_alphanumeric': 0.068489490559316, 'fraction_numerical': 0.013181332383327396, 'mean_word_length': 0.9373706004140787, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5268427', 'n_tokens_mistral': 2708, 'n_tokens_neox': 2411, 'n_words': 638}