@@ -45,6 +45,7 @@ def __init__(self, start: int, size: int, protect: MemoryProtect = MemoryProtect
4545 self .protect = protect
4646 self .type = type
4747 self .info = info
48+ self .commit_count = 0
4849
4950 @property
5051 def end (self ):
@@ -193,11 +194,14 @@ def release(self, start: int):
193194 self ._page_manager .decommit (parent_region .start , parent_region .size )
194195 for page in parent_region .pages ():
195196 del self ._committed [page ]
197+ parent_region .commit_count -= 1
196198 else :
197199 for page in parent_region .pages ():
198200 if page in self ._committed :
199201 self ._page_manager .decommit (page , PAGE_SIZE )
200202 del self ._committed [page ]
203+ parent_region .commit_count -= 1
204+ assert parent_region .commit_count == 0
201205 self ._regions .remove (parent_region )
202206
203207 def commit (self , start : int , size : int , protect : MemoryProtect = MemoryProtect .UNDEFINED ):
@@ -212,10 +216,12 @@ def commit(self, start: int, size: int, protect: MemoryProtect = MemoryProtect.U
212216 if protect == MemoryProtect .UNDEFINED :
213217 protect = parent_region .protect
214218
215- if all ([page not in self ._committed for page in region .pages ()]):
219+ if parent_region .commit_count == 0 :
220+ assert all ([page not in self ._committed for page in region .pages ()])
216221 self ._page_manager .commit (region .start , region .size , protect )
217222 for page in region .pages ():
218223 self ._committed [page ] = MemoryRegion (page , PAGE_SIZE , protect , parent_region .type )
224+ parent_region .commit_count += 1
219225 else :
220226 for page in region .pages ():
221227 if page in self ._committed :
@@ -226,6 +232,7 @@ def commit(self, start: int, size: int, protect: MemoryProtect = MemoryProtect.U
226232 else :
227233 self ._page_manager .commit (page , PAGE_SIZE , protect )
228234 self ._committed [page ] = MemoryRegion (page , PAGE_SIZE , protect , parent_region .type )
235+ parent_region .commit_count += 1
229236
230237 def decommit (self , start : int , size : int ):
231238 assert size > 0 and self .align_page (size ) == size
@@ -239,11 +246,13 @@ def decommit(self, start: int, size: int):
239246 self ._page_manager .decommit (region .start , region .size )
240247 for page in self ._committed :
241248 del self ._committed [page ]
249+ parent_region .commit_count -= 1
242250 else :
243251 for page in region .pages ():
244252 if page in self ._committed :
245253 self ._page_manager .decommit (page , PAGE_SIZE )
246254 del self ._committed [page ]
255+ parent_region .commit_count -= 1
247256
248257 def protect (self , start : int , size : int , protect : MemoryProtect ):
249258 assert isinstance (protect , MemoryProtect )
@@ -308,6 +317,10 @@ def query(self, start: int):
308317 result .state = MemoryState .MEM_RESERVE
309318 result .protect = MemoryProtect .UNDEFINED
310319 result .type = parent_region .type
320+ # If no pages are commited in this parent region we can bail early
321+ if parent_region .commit_count == 0 :
322+ result .region_size = parent_region .size
323+ break
311324 else :
312325 commited_page = self ._committed .get (page , None )
313326 if result .state == MemoryState .MEM_RESERVE :
0 commit comments