| 1 |
import random |
| 2 |
|
| 3 |
from pyglet.graphics import allocation |
| 4 |
|
| 5 |
|
| 6 |
class Region: |
| 7 |
def __init__(self, start, size): |
| 8 |
self.start = start |
| 9 |
self.size = size |
| 10 |
|
| 11 |
@property |
| 12 |
def end(self): |
| 13 |
return self.start + self.size |
| 14 |
|
| 15 |
def __repr__(self): |
| 16 |
return 'Region(%r, %r)' % (self.start, self.size) |
| 17 |
|
| 18 |
|
| 19 |
class RegionAllocator: |
| 20 |
def __init__(self, capacity): |
| 21 |
self.allocator = allocation.Allocator(capacity) |
| 22 |
self.regions = [] |
| 23 |
|
| 24 |
@property |
| 25 |
def capacity(self): |
| 26 |
return self.allocator.capacity |
| 27 |
|
| 28 |
def check_region(self, region): |
| 29 |
for other in self.regions: |
| 30 |
if other is region: |
| 31 |
continue |
| 32 |
|
| 33 |
if (other.start < region.start < other.end) or (other.start < region.end < other.end): |
| 34 |
raise Exception('%r overlaps with %r' % (region, other)) |
| 35 |
|
| 36 |
def check_coverage(self): |
| 37 |
starts, sizes = self.allocator.get_allocated_regions() |
| 38 |
|
| 39 |
if len(starts) != len(sizes): |
| 40 |
raise Exception('Length of starts (%d) does not match sizes (%d)' % (len(starts), len(sizes))) |
| 41 |
|
| 42 |
if not starts and not self.regions: |
| 43 |
return |
| 44 |
|
| 45 |
self.regions.sort(key=lambda r: r.start) |
| 46 |
regions = iter(self.regions) |
| 47 |
|
| 48 |
blocks = iter(zip(starts, sizes)) |
| 49 |
block_start, block_size = next(blocks) |
| 50 |
block_used = False |
| 51 |
try: |
| 52 |
while True: |
| 53 |
region = next(regions) |
| 54 |
block_used = True |
| 55 |
if region.start < block_start: |
| 56 |
raise Exception(f'Start of {region!r} was not covered at {block_start}') |
| 57 |
|
| 58 |
|
| 59 |
block_start += region.size |
| 60 |
block_size -= region.size |
| 61 |
if block_size < 0: |
| 62 |
raise Exception('%r extended past end of block by %d' % (region, -block_size)) |
| 63 |
elif block_size == 0: |
| 64 |
block_start, block_size = next(blocks) |
| 65 |
block_used = False |
| 66 |
except StopIteration: |
| 67 |
pass |
| 68 |
|
| 69 |
if not block_used: |
| 70 |
raise Exception(f'Uncovered block(s) from {block_start}') |
| 71 |
|
| 72 |
try: |
| 73 |
block_start, block_size = next(blocks) |
| 74 |
raise Exception(f'Uncovered block(s) from {block_start}') |
| 75 |
except StopIteration: |
| 76 |
pass |
| 77 |
|
| 78 |
try: |
| 79 |
region = next(regions) |
| 80 |
raise Exception(f'{region!r} was not covered') |
| 81 |
except StopIteration: |
| 82 |
pass |
| 83 |
|
| 84 |
def check_redundancy(self): |
| 85 |
|
| 86 |
starts, sizes = self.allocator.get_allocated_regions() |
| 87 |
|
| 88 |
last = -1 |
| 89 |
for start, size in zip(starts, sizes): |
| 90 |
if start < last: |
| 91 |
raise Exception(f'Block at {start} is out of order') |
| 92 |
if start == last: |
| 93 |
raise Exception(f'Block at {start} is redundant') |
| 94 |
last = start + size |
| 95 |
|
| 96 |
def alloc(self, size): |
| 97 |
start = self.allocator.alloc(size) |
| 98 |
region = Region(start, size) |
| 99 |
self.check_region(region) |
| 100 |
self.regions.append(region) |
| 101 |
self.check_coverage() |
| 102 |
self.check_redundancy() |
| 103 |
return region |
| 104 |
|
| 105 |
def dealloc(self, region): |
| 106 |
assert region in self.regions |
| 107 |
self.allocator.dealloc(region.start, region.size) |
| 108 |
self.regions.remove(region) |
| 109 |
self.check_coverage() |
| 110 |
self.check_redundancy() |
| 111 |
|
| 112 |
def realloc(self, region, size): |
| 113 |
assert region in self.regions |
| 114 |
region.start = self.allocator.realloc(region.start, region.size, size) |
| 115 |
region.size = size |
| 116 |
self.check_region(region) |
| 117 |
self.check_coverage() |
| 118 |
self.check_redundancy() |
| 119 |
|
| 120 |
def force_alloc(self, size): |
| 121 |
try: |
| 122 |
return self.alloc(size) |
| 123 |
except allocation.AllocatorMemoryException as e: |
| 124 |
self.allocator.set_capacity(e.requested_capacity) |
| 125 |
return self.alloc(size) |
| 126 |
|
| 127 |
def force_realloc(self, region, size): |
| 128 |
try: |
| 129 |
self.realloc(region, size) |
| 130 |
except allocation.AllocatorMemoryException as e: |
| 131 |
self.allocator.set_capacity(e.requested_capacity) |
| 132 |
self.realloc(region, size) |
| 133 |
|
| 134 |
def get_free_size(self): |
| 135 |
return self.allocator.get_free_size() |
| 136 |
|
| 137 |
|
| 138 |
def test_alloc1(): |
| 139 |
capacity = 10 |
| 140 |
allocator = RegionAllocator(capacity) |
| 141 |
for i in range(capacity): |
| 142 |
allocator.alloc(1) |
| 143 |
|
| 144 |
|
| 145 |
def test_alloc2(): |
| 146 |
capacity = 10 |
| 147 |
allocator = RegionAllocator(capacity) |
| 148 |
for i in range(capacity // 2): |
| 149 |
allocator.alloc(2) |
| 150 |
|
| 151 |
|
| 152 |
def test_alloc3(): |
| 153 |
capacity = 10 |
| 154 |
allocator = RegionAllocator(capacity) |
| 155 |
for i in range(capacity // 3): |
| 156 |
allocator.alloc(3) |
| 157 |
|
| 158 |
|
| 159 |
def test_alloc_mix1_2(): |
| 160 |
allocs = [1, 2] * 5 |
| 161 |
capacity = sum(allocs) |
| 162 |
allocator = RegionAllocator(capacity) |
| 163 |
for alloc in allocs: |
| 164 |
allocator.alloc(alloc) |
| 165 |
|
| 166 |
|
| 167 |
def test_alloc_mix5_3_7(): |
| 168 |
allocs = [5, 3, 7] * 5 |
| 169 |
capacity = sum(allocs) |
| 170 |
allocator = RegionAllocator(capacity) |
| 171 |
for alloc in allocs: |
| 172 |
allocator.alloc(alloc) |
| 173 |
|
| 174 |
|
| 175 |
def test_dealloc_1_order_all(): |
| 176 |
capacity = 10 |
| 177 |
allocator = RegionAllocator(capacity) |
| 178 |
regions = [] |
| 179 |
for i in range(capacity): |
| 180 |
regions.append(allocator.alloc(1)) |
| 181 |
for region in regions: |
| 182 |
allocator.dealloc(region) |
| 183 |
assert allocator.get_free_size() == allocator.capacity |
| 184 |
|
| 185 |
|
| 186 |
def test_dealloc_1_order(): |
| 187 |
capacity = 15 |
| 188 |
allocator = RegionAllocator(capacity) |
| 189 |
regions = [] |
| 190 |
for i in range(10): |
| 191 |
regions.append(allocator.alloc(1)) |
| 192 |
for region in regions: |
| 193 |
allocator.dealloc(region) |
| 194 |
|
| 195 |
|
| 196 |
def test_dealloc_1_reverse_all(): |
| 197 |
capacity = 10 |
| 198 |
allocator = RegionAllocator(capacity) |
| 199 |
regions = [] |
| 200 |
for i in range(capacity): |
| 201 |
regions.append(allocator.alloc(1)) |
| 202 |
for region in regions[::-1]: |
| 203 |
allocator.dealloc(region) |
| 204 |
assert allocator.get_free_size() == allocator.capacity |
| 205 |
|
| 206 |
|
| 207 |
def test_dealloc_1_reverse(): |
| 208 |
capacity = 15 |
| 209 |
allocator = RegionAllocator(capacity) |
| 210 |
regions = [] |
| 211 |
for i in range(10): |
| 212 |
regions.append(allocator.alloc(1)) |
| 213 |
for region in regions[::-1]: |
| 214 |
allocator.dealloc(region) |
| 215 |
|
| 216 |
|
| 217 |
def test_dealloc_mix1_2_order(): |
| 218 |
allocs = [1, 2] * 5 |
| 219 |
capacity = sum(allocs) |
| 220 |
allocator = RegionAllocator(capacity) |
| 221 |
regions = [] |
| 222 |
for alloc in allocs: |
| 223 |
regions.append(allocator.alloc(alloc)) |
| 224 |
for region in regions: |
| 225 |
allocator.dealloc(region) |
| 226 |
assert allocator.get_free_size() == allocator.capacity |
| 227 |
|
| 228 |
|
| 229 |
def test_dealloc_mix5_3_7_order(): |
| 230 |
allocs = [5, 3, 7] * 5 |
| 231 |
capacity = sum(allocs) |
| 232 |
allocator = RegionAllocator(capacity) |
| 233 |
regions = [] |
| 234 |
for alloc in allocs: |
| 235 |
regions.append(allocator.alloc(alloc)) |
| 236 |
for region in regions: |
| 237 |
allocator.dealloc(region) |
| 238 |
assert allocator.get_free_size() == allocator.capacity |
| 239 |
|
| 240 |
|
| 241 |
def test_dealloc_1_outoforder(): |
| 242 |
random.seed(1) |
| 243 |
capacity = 15 |
| 244 |
allocator = RegionAllocator(capacity) |
| 245 |
regions = [] |
| 246 |
for i in range(capacity): |
| 247 |
regions.append(allocator.alloc(1)) |
| 248 |
random.shuffle(regions) |
| 249 |
for region in regions: |
| 250 |
allocator.dealloc(region) |
| 251 |
assert allocator.get_free_size() == allocator.capacity |
| 252 |
|
| 253 |
|
| 254 |
def test_dealloc_mix1_2_outoforder(): |
| 255 |
random.seed(1) |
| 256 |
allocs = [1, 2] * 5 |
| 257 |
capacity = sum(allocs) |
| 258 |
allocator = RegionAllocator(capacity) |
| 259 |
regions = [] |
| 260 |
for alloc in allocs: |
| 261 |
regions.append(allocator.alloc(alloc)) |
| 262 |
random.shuffle(regions) |
| 263 |
for region in regions: |
| 264 |
allocator.dealloc(region) |
| 265 |
assert allocator.get_free_size() == allocator.capacity |
| 266 |
|
| 267 |
|
| 268 |
def test_dealloc_mix5_3_7_outoforder(): |
| 269 |
random.seed(1) |
| 270 |
allocs = [5, 3, 7] * 5 |
| 271 |
capacity = sum(allocs) |
| 272 |
allocator = RegionAllocator(capacity) |
| 273 |
regions = [] |
| 274 |
for alloc in allocs: |
| 275 |
regions.append(allocator.alloc(alloc)) |
| 276 |
random.shuffle(regions) |
| 277 |
for region in regions: |
| 278 |
allocator.dealloc(region) |
| 279 |
assert allocator.get_free_size() == allocator.capacity |
| 280 |
|
| 281 |
|
| 282 |
def mixed_alloc_dealloc_list(choices, count=10, seed=1): |
| 283 |
random.seed(seed) |
| 284 |
allocs = [] |
| 285 |
live = [] |
| 286 |
j = 0 |
| 287 |
for i in range(count): |
| 288 |
if live: |
| 289 |
if random.random() < .5: |
| 290 |
allocs.append(random.choice(choices)) |
| 291 |
live.append(j) |
| 292 |
j += 1 |
| 293 |
else: |
| 294 |
k = random.choice(live) |
| 295 |
live.remove(k) |
| 296 |
allocs.append(-k) |
| 297 |
else: |
| 298 |
allocs.append(random.choice(choices)) |
| 299 |
live.append(j) |
| 300 |
j += 1 |
| 301 |
for j in live: |
| 302 |
allocs.append(-j) |
| 303 |
return allocs |
| 304 |
|
| 305 |
|
| 306 |
def test_mix_alloc_dealloc1(): |
| 307 |
allocs = mixed_alloc_dealloc_list([1]) |
| 308 |
|
| 309 |
capacity = sum([a for a in allocs if a > 0]) |
| 310 |
allocator = RegionAllocator(capacity) |
| 311 |
regions = [] |
| 312 |
for alloc in allocs: |
| 313 |
if alloc > 0: |
| 314 |
regions.append(allocator.alloc(alloc)) |
| 315 |
else: |
| 316 |
region = regions[abs(alloc)] |
| 317 |
allocator.dealloc(region) |
| 318 |
assert allocator.get_free_size() == allocator.capacity |
| 319 |
|
| 320 |
|
| 321 |
def test_mix_alloc_dealloc5_3_7(): |
| 322 |
allocs = mixed_alloc_dealloc_list([5, 3, 7], count=50) |
| 323 |
|
| 324 |
capacity = sum([a for a in allocs if a > 0]) |
| 325 |
allocator = RegionAllocator(capacity) |
| 326 |
regions = [] |
| 327 |
for alloc in allocs: |
| 328 |
if alloc > 0: |
| 329 |
regions.append(allocator.alloc(alloc)) |
| 330 |
else: |
| 331 |
region = regions[abs(alloc)] |
| 332 |
allocator.dealloc(region) |
| 333 |
assert allocator.get_free_size() == allocator.capacity |
| 334 |
|
| 335 |
|
| 336 |
def test_realloc1_2(): |
| 337 |
allocator = RegionAllocator(30) |
| 338 |
regions = [] |
| 339 |
for i in range(10): |
| 340 |
regions.append(allocator.alloc(1)) |
| 341 |
for region in regions: |
| 342 |
allocator.realloc(region, 2) |
| 343 |
for region in regions: |
| 344 |
allocator.dealloc(region) |
| 345 |
|
| 346 |
|
| 347 |
def test_realloc2_1(): |
| 348 |
allocator = RegionAllocator(20) |
| 349 |
regions = [] |
| 350 |
for i in range(10): |
| 351 |
regions.append(allocator.alloc(2)) |
| 352 |
for region in regions: |
| 353 |
allocator.realloc(region, 1) |
| 354 |
for region in regions: |
| 355 |
allocator.dealloc(region) |
| 356 |
assert allocator.get_free_size() == allocator.capacity |
| 357 |
|
| 358 |
|
| 359 |
def test_realloc_2_1_2(): |
| 360 |
allocator = RegionAllocator(30) |
| 361 |
regions = [] |
| 362 |
for i in range(10): |
| 363 |
regions.append(allocator.alloc(2)) |
| 364 |
for region in regions: |
| 365 |
allocator.realloc(region, 1) |
| 366 |
for region in regions: |
| 367 |
allocator.realloc(region, 2) |
| 368 |
for region in regions: |
| 369 |
allocator.dealloc(region) |
| 370 |
assert allocator.get_free_size() == allocator.capacity |
| 371 |
|
| 372 |
|
| 373 |
def test_realloc_3_1_5_4_6(): |
| 374 |
allocator = RegionAllocator(1000) |
| 375 |
regions = [] |
| 376 |
for i in range(10): |
| 377 |
regions.append(allocator.alloc(3)) |
| 378 |
for region in regions: |
| 379 |
allocator.realloc(region, 1) |
| 380 |
for region in regions: |
| 381 |
allocator.realloc(region, 5) |
| 382 |
for region in regions: |
| 383 |
allocator.realloc(region, 4) |
| 384 |
for region in regions: |
| 385 |
allocator.realloc(region, 6) |
| 386 |
for region in regions: |
| 387 |
allocator.dealloc(region) |
| 388 |
assert allocator.get_free_size() == allocator.capacity |
| 389 |
|
| 390 |
|
| 391 |
def test_realloc_3_1_5_4_6_sequential(): |
| 392 |
allocator = RegionAllocator(1000) |
| 393 |
regions = [] |
| 394 |
for i in range(10): |
| 395 |
regions.append(allocator.alloc(3)) |
| 396 |
for region in regions: |
| 397 |
allocator.realloc(region, 1) |
| 398 |
allocator.realloc(region, 5) |
| 399 |
allocator.realloc(region, 4) |
| 400 |
allocator.realloc(region, 6) |
| 401 |
for region in regions: |
| 402 |
allocator.dealloc(region) |
| 403 |
assert allocator.get_free_size() == allocator.capacity |
| 404 |
|
| 405 |
|
| 406 |
def test_resize1(): |
| 407 |
allocator = RegionAllocator(1) |
| 408 |
regions = [] |
| 409 |
for i in range(10): |
| 410 |
regions.append(allocator.force_alloc(3)) |
| 411 |
for region in regions: |
| 412 |
allocator.dealloc(region) |
| 413 |
assert allocator.get_free_size() == allocator.capacity |
| 414 |
|
| 415 |
|
| 416 |
def test_mix_resize(): |
| 417 |
|
| 418 |
allocator = RegionAllocator(1) |
| 419 |
regions = [] |
| 420 |
for i in range(10): |
| 421 |
regions.append(allocator.force_alloc(3)) |
| 422 |
for region in regions[:5]: |
| 423 |
|
| 424 |
allocator.force_realloc(region, 8) |
| 425 |
for i in range(10): |
| 426 |
regions.append(allocator.force_alloc(i + 1)) |
| 427 |
for region in regions[5:15]: |
| 428 |
allocator.force_realloc(region, 5) |
| 429 |
for region in regions[3:18:2]: |
| 430 |
allocator.dealloc(region) |
| 431 |
regions.remove(region) |
| 432 |
for i in range(5): |
| 433 |
regions.append(allocator.force_alloc(3)) |
| 434 |
for region in regions[-10:]: |
| 435 |
allocator.force_realloc(region, 6) |
| 436 |
for region in regions: |
| 437 |
allocator.dealloc(region) |
| 438 |
assert allocator.get_free_size() == allocator.capacity |