Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
RamViz allocation visualizer
# RamViz - a RAM-allocation algorithm visualizer
# Designed to allow people writing kernels, memory managers, etc to
# visually inspect the operation of their algorithms.
# Operates with 64 blocks of RAM by default
# Call "Memory.alloc(block, type)" and "Memory.dealloc(memory)" to change the
# state of blocks of memory.
# Using __is_alloc() in your own scripts is cheating.
import sys
import random
import colorama
from colorama import Fore, Back, Style
colorama.init()
class chars:
lightshade = "" # 1
mediumshade = "" # 2
darkshade = "" # 3
fullblock = "" # 4
dualquad = "" # 5
class colors:
red = Fore.RED
green = Fore.GREEN
yellow = Fore.YELLOW
blue = Fore.BLUE
magenta = Fore.MAGENTA
cyan = Fore.CYAN
white = Fore.WHITE
class atypes:
UNALLOCATED = (colors.white, chars.fullblock)
KERNEL01 = (colors.red, chars.fullblock)
KERNEL02 = (colors.green, chars.fullblock)
KERNEL03 = (colors.yellow, chars.fullblock)
KERNEL04 = (colors.blue, chars.fullblock)
USER01 = (colors.red, chars.dualquad)
USER02 = (colors.green, chars.dualquad)
USER03 = (colors.yellow, chars.dualquad)
USER04 = (colors.blue, chars.dualquad)
atypes_list = [atypes.UNALLOCATED, atypes.KERNEL01, atypes.KERNEL02, atypes.KERNEL03,\
atypes.KERNEL04, atypes.USER01, atypes.USER02, atypes.USER03, atypes.USER04]
def reset_color():
"""Reset the coloration of all text printed after the call."""
sys.stdout.write(Style.RESET_ALL)
class Memory:
def __init__(self, size = 64):
self.size = size
self.alloctable = [Block(x) for x in range(0, size)]
self.alloc(0, atypes.KERNEL01)
def __is_alloc(self, location):
'Check if a location is allocated already.'
if location > self.size - 1:
raise ValueError("Tried to check a location that was out of bounds!")
return self.alloctable[location] is atypes.UNALLOCATED
def alloc(self, location, atype):
'Allocate a new portion of memory.'
if location > self.size - 1:
raise ValueError("Tried to alloc a location that was out of bounds!")
if self.__is_alloc(location):
raise ValueError("Tried to allocate " + str(location) + " which is already allocated.")
self.alloctable[location].allocate(atype)
def dealloc(self, location):
if location > self.size - 1:
raise ValueError("Tried to dealloc a location that was out of bounds!")
self.alloctable[location].alloc(atypes.UNALLOCATED)
def report(self):
print("\t REPORT FOR MEMORY OF SIZE " + str(self.size))
print("\t+00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15")
for i in range(0, int(self.size / 16)):
# We need i here because we need to do things per every 16 blocks
# Beginning of a new line.
sys.stdout.write("\n" + str(16 * i) + "\t ")
for j in range(0, 16):
sys.stdout.write(str(self.alloctable[i + j]) + " ")
reset_color()
class Block:
def __init__(self, number):
self.number = number
self.atype = atypes.UNALLOCATED
def allocate(self, atype):
self.atype = atype
def __repr__(self):
color, char = self.atype
return color + char + Style.RESET_ALL
def test():
ram = Memory(256) # Make an empty memory
random.seed() # Set up randomness
i = 0 # Set up for loop
while i < 256: # Run through our RAM
r = atypes_list[random.randint(0,8)] # Pick something to allocate the block as
n = random.randint(1,8) # Pick the size of the block
for j in range(0,n):
ram.alloc((i+j) % 255, r) # Allocate one peice of the block; the % 255 keeps us in bounds
i = i + n
ram.report()
if __name__ == "__main__":
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment