Memory assignment tweaks

This commit is contained in:
Ivan Malison 2018-12-07 14:22:45 -08:00
parent f17fcae1c5
commit 4162c9ca3b
No known key found for this signature in database
GPG Key ID: 62530EFBE99DC2F8

View File

@ -7,19 +7,17 @@ class MemoryManager(object):
self._allocations = [] self._allocations = []
def malloc(self, size): def malloc(self, size):
if size > 5:
print (size)
next_startpoint = 0 next_startpoint = 0
minimum_size = None minimum_size = None
minimum_start = None minimum_start = None
for (allocation_start_point, size) in self._allocations: for (allocation_start_point, allocation_size) in self._allocations:
current_block_size = allocation_start_point - next_startpoint current_block_size = allocation_start_point - next_startpoint
if (current_block_size > size): if (current_block_size > size):
if (minimum_size is None or minimum_size > current_block_size): if (minimum_size is None or minimum_size > current_block_size):
minimum_size = current_block_size minimum_size = current_block_size
minimum_start = next_startpoint minimum_start = next_startpoint
next_startpoint = allocation_start_point + size - 1 next_startpoint = allocation_start_point + allocation_size - 1
current_block_size = self._memory_size - next_startpoint current_block_size = self._memory_size - next_startpoint
if (current_block_size > size): if (current_block_size > size):
@ -55,7 +53,7 @@ if __name__ == '__main__':
mm.free(allocation) mm.free(allocation)
print("nice", mm.malloc(200)) print("nice", mm.malloc(200))
print("cool", mm.malloc(500)) print("cool", mm.malloc(400))
print(mm._allocations) print(mm._allocations)
# a = mm.malloc(100) # a = mm.malloc(100)
# b = mm.malloc(500) # b = mm.malloc(500)