Skip to content

Commit 5fdf08e

Browse files
authored
Merge pull request #38 from mrexodia/allocation-alignment
Align the result of memory.find_free to be suitable for allocation per default
2 parents 6ea68e1 + b74f330 commit 5fdf08e

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/dumpulator/dumpulator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ def call(self, addr, args: List[int] = [], regs: dict = {}, count=0):
681681
def allocate(self, size, page_align=False):
682682
if not self._allocate_ptr:
683683
self._allocate_base = self.memory.find_free(self._allocate_size)
684+
assert self._allocate_base is not None, "Failed to find free memory"
684685
self.memory.reserve(
685686
start=self._allocate_base,
686687
size=self._allocate_size,

src/dumpulator/memory.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,21 @@ def align_allocation(self, addr: int):
152152
mask = self._granularity - 1
153153
return (addr + mask) & ~mask
154154

155-
def find_free(self, size: int):
155+
def find_free(self, size: int, allocation_align=True):
156156
assert size > 0 and self.align_page(size) == size
157157
base = self._minimum
158158
while base < self._maximum:
159159
info = self.query(base)
160160
assert info.base == base
161-
if info.state == MemoryState.MEM_FREE and info.region_size >= size and self.align_allocation(base) == base:
162-
return info.base
163161
base += info.region_size
162+
if info.state == MemoryState.MEM_FREE:
163+
if allocation_align:
164+
aligned_base = self.align_allocation(info.base)
165+
diff = aligned_base - info.base
166+
info.base = aligned_base
167+
info.region_size -= diff
168+
if info.region_size >= size:
169+
return info.base
164170
return None
165171

166172
def reserve(self, start: int, size: int, protect: MemoryProtect, type: MemoryType = MemoryType.MEM_PRIVATE, info: Any = None):

0 commit comments

Comments
 (0)