@@ -45,6 +45,7 @@ def __init__(self, start: int, size: int, protect: MemoryProtect = MemoryProtect
45
45
self .protect = protect
46
46
self .type = type
47
47
self .info = info
48
+ self .commit_count = 0
48
49
49
50
@property
50
51
def end (self ):
@@ -193,11 +194,14 @@ def release(self, start: int):
193
194
self ._page_manager .decommit (parent_region .start , parent_region .size )
194
195
for page in parent_region .pages ():
195
196
del self ._committed [page ]
197
+ parent_region .commit_count -= 1
196
198
else :
197
199
for page in parent_region .pages ():
198
200
if page in self ._committed :
199
201
self ._page_manager .decommit (page , PAGE_SIZE )
200
202
del self ._committed [page ]
203
+ parent_region .commit_count -= 1
204
+ assert parent_region .commit_count == 0
201
205
self ._regions .remove (parent_region )
202
206
203
207
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
212
216
if protect == MemoryProtect .UNDEFINED :
213
217
protect = parent_region .protect
214
218
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 ()])
216
221
self ._page_manager .commit (region .start , region .size , protect )
217
222
for page in region .pages ():
218
223
self ._committed [page ] = MemoryRegion (page , PAGE_SIZE , protect , parent_region .type )
224
+ parent_region .commit_count += 1
219
225
else :
220
226
for page in region .pages ():
221
227
if page in self ._committed :
@@ -226,6 +232,7 @@ def commit(self, start: int, size: int, protect: MemoryProtect = MemoryProtect.U
226
232
else :
227
233
self ._page_manager .commit (page , PAGE_SIZE , protect )
228
234
self ._committed [page ] = MemoryRegion (page , PAGE_SIZE , protect , parent_region .type )
235
+ parent_region .commit_count += 1
229
236
230
237
def decommit (self , start : int , size : int ):
231
238
assert size > 0 and self .align_page (size ) == size
@@ -239,11 +246,13 @@ def decommit(self, start: int, size: int):
239
246
self ._page_manager .decommit (region .start , region .size )
240
247
for page in self ._committed :
241
248
del self ._committed [page ]
249
+ parent_region .commit_count -= 1
242
250
else :
243
251
for page in region .pages ():
244
252
if page in self ._committed :
245
253
self ._page_manager .decommit (page , PAGE_SIZE )
246
254
del self ._committed [page ]
255
+ parent_region .commit_count -= 1
247
256
248
257
def protect (self , start : int , size : int , protect : MemoryProtect ):
249
258
assert isinstance (protect , MemoryProtect )
@@ -308,6 +317,10 @@ def query(self, start: int):
308
317
result .state = MemoryState .MEM_RESERVE
309
318
result .protect = MemoryProtect .UNDEFINED
310
319
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
311
324
else :
312
325
commited_page = self ._committed .get (page , None )
313
326
if result .state == MemoryState .MEM_RESERVE :
0 commit comments