Advertisement
LeonardCHoo

buddy

May 13th, 2018
1,113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.66 KB | None | 0 0
  1. """
  2. @Buddy System Implementation
  3.    CHOO SANGBUEM
  4.    71675619
  5. """
  6. """
  7.     level: 0 1 2 3 4  5  |6
  8.     size:  1 2 4 8 16 32 |64
  9.  
  10.     Level 6 can only belong to the actual memory itself
  11.    So we can start at var level = 5
  12.     thus, partition size = 2^level, where every time partition happens, level -= 1
  13.  
  14.     Two blocks are said to be companion buddies if they resulted from
  15.    the split of the same direct parent block
  16.  
  17.    parent = a number gets assigned every time partition happens.
  18. """
  19.  
  20. class Partition:
  21.     def __init__(self, data, start, size, parent, state="free"):
  22.         self.data = data
  23.         self.start = start
  24.         self.size = size
  25.         self.parent = parent
  26.         self.state = state
  27.  
  28.         self.prev = None
  29.         self.next = None
  30.  
  31. class Memory(): #doubly linked list implementation
  32.  
  33.     memory_size = 64
  34.     parent = 1
  35.  
  36.     def __init__(self):
  37.         self.head = self.tail = Partition(0, 0, self.memory_size, 0)
  38.  
  39.     # Show the memory state
  40.     # def showMem(self):
  41.     #     #show graphically
  42.  
  43.     def makePartition(self, currPart):
  44.         #halve current partition size
  45.         currPart.size = currPart.size / 2
  46.         #make new partition object starting from second half
  47.         newStart = currPart.start + currPart.size
  48.         newPart = Partition(0, newStart, currPart.size, 0)
  49.         #assign parent number
  50.         currPart.parent = newPart.parent = self.parent
  51.         #increase the parent number
  52.         self.parent += 1
  53.  
  54.         #insert the newPart in a doubly linked list
  55.         newPart.next = currPart.next
  56.         currPart.next = newPart
  57.         if newPart.next is not None:
  58.             newPart.next.prev = newPart
  59.             newPart.prev = currPart
  60.  
  61.         #head will always stay the same since we are making new partition to the right of original
  62.         #set tail
  63.         while newPart.next is not None:
  64.             newPart = newPart.next
  65.         self.tail = newPart
  66.        
  67.         print(newPart.size)
  68.  
  69.         return currPart # newly halved one
  70.  
  71.     def allocate(self, input_size):
  72.        
  73.         # find the smallest possible free partition by traversing backward,
  74.         currPart = self.tail
  75.        
  76.         while currPart.prev:
  77.             if(currPart.prev.state is "free" and currPart.prev.size >= input_size):
  78.                 currPart = currPart.prev
  79.        
  80.         #check first partition
  81.         if currPart.state is "free" and currPart.size >= input_size:
  82.                 pass
  83.         # in case nothing new found, check the current partition's state
  84.         elif currPart.state is not "free":
  85.             print("ALl memory is used.")
  86.             return
  87.         elif currPart.state is "free" and currPart.size < input_size:
  88.             print("Not enough free space.")
  89.             return
  90.                
  91.  
  92.         # Now with the free memory found
  93.         # Make more partitions while input is smaller than currPart/2
  94.         while input_size  <= (currPart.size/2):
  95.             currPart = self.makePartition(currPart) #returns first half of the new partition
  96.             #this repeats until the new partition /2 is less than the input
  97.  
  98.         #finally when all conditions are met, allocate the memory
  99.         currPart.data = input_size
  100.         currPart.state = "allocated"
  101.  
  102.  
  103.     def free(self, address):
  104.         #find the partition
  105.         currPart = self.head
  106.         while currPart.next:
  107.             if currPart.start == address:
  108.                 #free the memory
  109.                 currPart.data = 0
  110.                 currPart.state = "free"
  111.                 self.merge(currPart)
  112.                 return
  113.             currPart = currPart.next
  114.         #last partition
  115.         if currPart.start == address:
  116.             currPart.data = 0
  117.             currPart.state = "free"            
  118.             self.merge(currPart)
  119.             return
  120.        
  121.         print()
  122.  
  123.     def checkBuddy(self, p1, p2):
  124.         # do they have same parent
  125.         if p1.parent is p2.parent:
  126.             return True
  127.         else:
  128.             return False
  129.  
  130.     def merge(self, reqPart):
  131.         buddy = None
  132.         #check partitions nearby to see if free
  133.         if reqPart.prev:
  134.             if reqPart.prev.state is "free":
  135.                 if self.checkBuddy(reqPart, reqPart.prev):
  136.                     buddy = reqPart.prev
  137.         if reqPart.next:
  138.             if reqPart.next.state is "free":
  139.                 if self.checkBuddy(reqPart, reqPart.next):
  140.                     buddy = reqPart.next
  141.         # if buddy found, merge
  142.         if buddy:
  143.             self.delete(buddy)
  144.             reqPart.size = reqPart.size * 2            
  145.             #call recursively
  146.             self.merge(reqPart)            
  147.  
  148.         return
  149.  
  150.  
  151.     def delete(self, partDel):
  152.         if self.head is partDel:
  153.             self.head = self.head.next
  154.             self.head.prev = None
  155.  
  156.         elif self.tail == partDel:
  157.             self.tail = self.tail.prev
  158.             self.tail.next = None
  159.  
  160.         else:
  161.             partDel.prev.next = partDel.next
  162.             partDel.next.prev = partDel.prev
  163.  
  164.         return
  165.  
  166.  
  167.  
  168. while True:
  169.  
  170.     # Main
  171.     memory = Memory()
  172.    
  173.     # Take user input
  174.    
  175.     # inputs:  [command,num]
  176.     # allocate: a [number of blocks]
  177.     # free:     f [first block number of the allocated chunk]
  178.     # quit:     q
  179.    
  180.     #split and store
  181.     inputs = input("command, num: ").split(" ")
  182.    
  183.     if inputs[0] is "a":
  184.         memory.allocate(int(inputs[1]))
  185.         print(memory.head.size)
  186.    
  187.    
  188.     elif inputs[0] is "f":
  189.         memory.free(inputs[1])
  190.    
  191.    
  192.     elif inputs[0] is "q":
  193.         exit()
  194.    
  195.     else:
  196.         print("Invalid input")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement