1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| import numpy as np
class PartitionNode: """ a node of idle partition chain """ def __init__(self, size, loc): """ initialize the node :param size: the size of this partition """ self.prior = None self.part_size = size self.loc = loc self.status = 0 self.next = None
class PartitionChain: def __init__(self, num): """ initialize a free partition chain :param num: """ self.partition_chain = PartitionNode(np.random.randint(50, 151), 0) self.num = num self.size = self.partition_chain.part_size self.free = 0 self.__init_chain() self.min_remain = 25
def __init_chain(self): """ initialize the partition charin :return: """ tmp = self.partition_chain for time in range(1, self.num): tmp.next = PartitionNode(np.random.randint(50, 151), self.size) self.size += tmp.next.part_size tmp.next.prior = tmp tmp = tmp.next self.free = self.size
def print_chain(self): """ print the infomation of partition chain :return: """ tmp = self.partition_chain self.free = 0 while tmp: self.free += tmp.part_size tmp = tmp.next tmp = self.partition_chain print("空闲分区链(分区数-%d 大小-%d 空闲-%d):" % (self.num, self.size, self.free), end=" ") while tmp: if tmp.next: print("<AT-%d, S-%d>" % (tmp.loc, tmp.part_size), end='···') else: print("<AT-%d, S-%d>" % (tmp.loc, tmp.part_size)) tmp = tmp.next
def alloc_memory(self, method, m): """ the algorithm of allocate memory :param method: first fit, next fit, best fit, worst fit :param m: the memory of application :return: """ print('**需求**:', m) if method == 'first': tmp = self.partition_chain while tmp: if m <= tmp.part_size and tmp.status == 0: if tmp.part_size - m <= self.min_remain: self.num -= 1 if tmp.prior is None: self.partition_chain = tmp.next self.partition_chain.prior = None elif tmp.next is None: tmp.prior.next = None else: tmp.next.prior = tmp.prior tmp.prior.next = tmp.next print("**Info**: 分配成功^_^, 地址-%d 大小-%d" % (tmp.loc, tmp.part_size)) return {'Address': tmp.loc, 'Size': tmp.part_size} else: tmp.part_size -= m print("**Info**: 分配成功^_^, 地址-%d 大小-%d" % (tmp.loc, m)) return {'Address': tmp.loc, 'Size': m} else: tmp = tmp.next print('**Info**: 分配失败x_x') elif method == 'next': pass elif method == 'best': pass elif method == 'fit': pass
def dealloc_memory(self, m_dict): """ recycle the memeory :param m_dict: the dict of deallocated memory :return: """ if m_dict is None: print("**Info**: 回收失败x_x") return loc = m_dict.get('Address') size = m_dict.get('Size') tmp = self.partition_chain while tmp: if tmp.prior is None: if loc <= tmp.loc: if loc == tmp.loc: tmp.part_size += size else: new_part = PartitionNode(size, loc) new_part.next = tmp tmp.prior = new_part self.partition_chain = new_part self.num += 1 print("**Info**: 大小:%d 回收成功^_^" % size) break elif (tmp.next is None) and (loc >= tmp.loc): if tmp.loc == loc: tmp.part_size += size else: new_part = PartitionNode(size, loc) tmp.next = new_part new_part.prior = tmp self.num += 1 print("**Info**: 大小:%d 回收成功^_^" % size) break else: if loc <= tmp.loc: if ((loc + size) == tmp.loc) | (loc == tmp.loc): if loc == tmp.loc: tmp.part_size += size elif (tmp.prior.loc + tmp.prior.part_size) == loc: next_part = tmp.next if next_part: next_part.prior = tmp.prior tmp.prior.part_size += size + tmp.part_size tmp.prior.next = next_part else: tmp.part_size += size print("**Info**: 大小:%d 回收成功^_^" % size) break else: if loc == tmp.prior.loc: tmp.prior.part_size += size elif (tmp.prior.loc + tmp.prior.part_size) == loc: tmp.prior.part_size += size else: new_part = PartitionNode(size, loc) new_part.next = tmp new_part.prior = tmp.prior tmp.prior.next = new_part tmp.prior = new_part self.num += 1 print("**Info**: 大小:%d 回收成功^_^" % size) break tmp = tmp.next
if __name__ == "__main__": my_chain = PartitionChain(5) my_chain.print_chain() my_apply = [] for i in range(3): rnd = np.random.randint(80, 130) my_apply.append(my_chain.alloc_memory('first', rnd)) my_chain.print_chain() print('已经请求的分区:', my_apply) for applys in my_apply: my_chain.dealloc_memory(applys) my_chain.print_chain()
|