Skip to content

Commit d5d697c

Browse files
committed
Refactor mem is_available
1 parent 99129aa commit d5d697c

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

qiling/os/memory.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -342,23 +342,20 @@ def unmap_all(self):
342342
if begin and end:
343343
self.unmap(begin, end - begin + 1)
344344

345-
def is_available(self, addr, size):
345+
def is_available(self, addr: int, size: int) -> bool:
346+
'''Query whether the memory range starting at `addr` and is of length of `size` bytes
347+
can be allocated.
348+
349+
Returns: True if it can be allocated, False otherwise
346350
'''
347-
The main function of is_available is to determine
348-
whether the memory starting with addr and having a size of length can be used for allocation.
349351

350-
If it can be allocated, returns True.
352+
assert size > 0, 'expected a positive size value'
351353

352-
If it cannot be allocated, it returns False.
353-
'''
354-
try:
355-
self.map(addr, addr)
356-
except:
357-
return False
358-
359-
self.unmap(addr, addr)
360-
return True
354+
begin = addr
355+
end = addr + size
361356

357+
# make sure neither begin nor end are enclosed within a mapped range
358+
return not any((lbound <= begin < ubound) or (lbound < end <= ubound) for lbound, ubound, _, _ in self.map_info)
362359

363360
def is_mapped(self, address, size):
364361
'''

0 commit comments

Comments
 (0)