Changeset c410e3a4
- Timestamp:
- Aug 7, 2009 10:30:22 AM (15 years ago)
- Branches:
- axis_example, compt_changes, info-ops, master, version-2.00, version-3.01, version-3.02
- Children:
- 19a3e06
- Parents:
- 1fda364
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fedd/federation/ip_allocator.py
r1fda364 rc410e3a4 4 4 def __init__(self, base=0, size=4294967296, min_alloc=256): 5 5 6 7 8 6 self.base = base 7 self.size = size 8 self.min_alloc = min_alloc 9 9 10 11 12 13 14 15 16 17 18 10 i = self.min_alloc 11 self.free = { } 12 self.allocated = { } 13 while i < self.size: 14 self.free[i] = set() 15 self.allocated[i] = set() 16 i *= 2 17 self.free[self.size] = set([self.base]) 18 self.allocated[self.size] = set() 19 19 20 20 def split(self, base, size): 21 21 return ( base, base + size) 22 22 23 23 def make_chunk(self, size): 24 25 26 27 28 29 30 31 24 if not self.free[size * 2]: 25 if size < self.size: 26 if not self.make_chunk(size * 2): 27 return False 28 else: 29 return False 30 base = min(self.free[size*2]) 31 self.free[size*2].discard(base) 32 32 33 34 35 33 for b in self.split(base, size): 34 self.free[size].add(b) 35 return True 36 36 37 37 def allocate(self, size): 38 39 40 41 42 43 44 45 46 47 48 38 # Make sure size is a power of 2 at least as big as minalloc 39 ns = self.min_alloc 40 while ns < self.size: 41 if ns == size: 42 break 43 if ns > size: 44 size = ns 45 break 46 ns *= 2 47 else: 48 size = self.size 49 49 50 51 52 53 54 55 56 50 if self.free[size] or self.make_chunk(size): 51 rv = min(self.free[size]) 52 self.free[size].discard(rv) 53 self.allocated[size].add(rv) 54 return (rv, size) 55 else: 56 return None 57 57 58 58 def deallocate(self, base, size): 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 59 if base not in self.allocated[size]: 60 raise KeyError("Not allocated") 61 if base % (size * 2) == 0: 62 if base + size in self.free[size]: 63 self.allocated[size].discard(base) 64 self.allocated[size * 2].add(base) 65 self.free[size].discard(base+size) 66 self.deallocate(base, size *2) 67 else: 68 self.allocated[size].discard(base) 69 self.free[size].add(base) 70 else: 71 if base - size in self.free[size]: 72 self.allocated[size].discard(base) 73 self.allocated[size * 2].add(base - size) 74 self.free[size].discard(base - size) 75 self.deallocate(base-size, size * 2) 76 else: 77 self.allocated[size].discard(base) 78 self.free[size].add(base) 79 return True 80 80 81 81 def __str__(self): 82 83 84 85 86 87 82 rv = "" 83 i=self.size 84 while i >= self.min_alloc: 85 rv += "%d: %s\n" % (i, self.free[i]) 86 i /= 2 87 return rv
Note: See TracChangeset
for help on using the changeset viewer.