Last active
October 29, 2017 00:37
-
-
Save lifangda01/735bbcca2197172bae993331ab36c8c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Buddy(object): | |
def __init__(self): | |
super(Buddy, self).__init__() | |
# 1024, 512, 256 | |
self.lists = [[0],[],[]] | |
def get(self, req): | |
print "Requesting %d bytes" % (1024/2**req) | |
print "Lists before request:" | |
print self.lists | |
d = req | |
# Find the deepest level with free memory | |
while len(self.lists[d]) == 0: | |
d -= 1 | |
# Break down from the left | |
while d < req: | |
m = self.lists[d][0] | |
self.lists[d] = self.lists[d][1:] | |
self.lists[d+1] += [m, 1024/(2**(d+1))] | |
d += 1 | |
r = self.lists[d][0] | |
self.lists[d] = self.lists[d][1:] | |
print "Memory found at location %d" % r | |
print "Lists after request:" | |
print self.lists | |
print '' | |
return r | |
def free(self): | |
pass | |
def main(): | |
buddy = Buddy() | |
buddy.get(2) | |
buddy.get(1) | |
buddy.get(2) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment