使用Linked实现内存管理系统

时间:2017-03-24 07:49:20

标签: python memory-management linked-list

目前我正在使用链接列表数据结构同意开发一个操作系统内存管理模拟程序,我的所有场景和oop概念都很好。当我运行我的代码分配时,部分解释器说有一个nonteype属性错误。这让我很困惑。它适用于我的代码中除ishole属性之外的所有部分。任何人都可以告诉我错误吗?

class Node:
    def __init__(self,size,hole_or_not,s_index,e_index,psid):
        self.block_size=size
        self.ishole=hole_or_not
        self.stat_index=s_index
        self.end_index=e_index
        self.ps_id=psid
        self.next=None 

class MemoryManagement:
    def __init__(self,os_size=400,totalsize=2560):
        self.head=Node(os_size,False,0,os_size-1,'os')
        self.head.next=Node(totalsize-os_size,True,os_size,totalsize,'free')
        self.totalfreespace=totalsize-os_size
        self.os_allocated=os_size

    def allocate(self,pid,size):
        current=self.head
        while current.ishole!=False or current.block_size<size:
            if current.ishole and current.block_size==size:
                current.ps_id=pid
                current.ishole=False
            elif current.ishole and current.block_size>size:
                hold_start_memory_address=current.stat_index
                hold_end_memory_address=current.end_index
                hold_block_size_current=current.block_size
                current.ps_id=pid
                current.block_size=size
                current.end_index=hold_start_memory_address+size-1
                current.ishole=False
                remain=Node(hold_block_size_current-size,True,hold_start_memory_address+size,hold_end_memory_address-1,'free')
                remain.next=current.next
                current.next=remain
            current=current.next
        else:
            print('----')

    def printstack(self):
        current=self.head
        while current.next:
            print(current.ps_id,current.block_size,current.stat_index,current.end_index,current.ishole)
            current=current.next

然后我使用了我的后续测试用例

o=MemoryManagement()
o.allocate('p1',100)
o.allocate('p2',500)
o.allocate('p3',1200)
o.allocate('p4',1000)
o.printstack()

然后我得到这样的错误,但我不明白为什么。它仅在属性ishole上发生

  while current.ishole!=False or current.block_size<size:
  AttributeError: 'NoneType' object has no attribute 'ishole'
  [Finished in 0.13s]

1 个答案:

答案 0 :(得分:0)

allocate()函数class MemoryManagement中提出的算法在逻辑中出现了一些缺失。

问题1 - 将while-else循环拆分为一个while循环和一个if-else条件。

  

while循环允许浏览所有现有节点。我摔倒   已检查节点,但未分配pid ==&gt;   “记忆力不够。”

def allocate(self,pid,size):
    current=self.head
    while (current is not None):
        ...
        current=current.next
        # not enough memory to allocate
  

然后,对于每个current节点,检查是否有足够的size可用。   当一个节点允许添加分配的pid时,只需返回。

current=current.next必须在while循环中移动(上图)。

        if current.ishole!=False or current.block_size<size:
            print("current: %s" % (str(current)))
            if (current.ishole):
                if (current.block_size > max_size):
                    max_size = current.block_size
            if current.ishole and current.block_size==size:
                current.ps_id=pid
                current.ishole=False
            elif current.ishole and current.block_size>size:
                hold_start_memory_address=current.stat_index
                hold_end_memory_address=current.end_index
                hold_block_size_current=current.block_size
                current.ps_id=pid
                current.block_size=size
                current.end_index=hold_start_memory_address+size-1
                current.ishole=False
                remain=Node(hold_block_size_current-size,True,hold_start_memory_address+size,hold_end_memory_address-1,'free')
                remain.next=current.next
                current.next=remain
                # successfully allocated then return
                return
            #current=current.next <== replaced by the while-loop above
        else:
            print('----')

Bonus 1 - 从allocate()函数添加布尔值返回值。

  

在上面提出的源代码中,返回可能很有用   False分配失败时(# not enough memory to allocate之后)或分配成功后返回True(仅在# successfully allocated then return之后)。