Skip to content

Commit aafc980

Browse files
committed
Added indirection level to internally used types to allow others to exchange them with their own implementations.
1 parent c30e930 commit aafc980

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

smmap/mman.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class MemoryCursor(object):
2929
'_size' # maximum size we should provide
3030
)
3131

32+
#{ Configuration
33+
MemoryWindowCls = MemoryWindow
34+
MappedRegionCls = MappedRegion
35+
#} END configuration
36+
3237
def __init__(self, manager = None, regions = None):
3338
self._manager = manager
3439
self._rlist = regions
@@ -129,9 +134,9 @@ def use_region(self, offset, size, flags = 0, _is_recursive=False):
129134
#END while bisecting
130135

131136
if existing_region is None:
132-
left = MemoryWindow(0, 0)
133-
mid = MemoryWindow(offset, size)
134-
right = MemoryWindow(self.file_size(), 0)
137+
left = self.MemoryWindowCls(0, 0)
138+
mid = self.MemoryWindowCls(offset, size)
139+
right = self.MemoryWindowCls(self.file_size(), 0)
135140

136141
# we want to honor the max memory size, and assure we have anough
137142
# memory available
@@ -162,13 +167,13 @@ def use_region(self, offset, size, flags = 0, _is_recursive=False):
162167
# possible mapping
163168
if insert_pos == 0:
164169
if len_regions:
165-
right = MemoryWindow.from_region(a[insert_pos])
170+
right = self.MemoryWindowCls.from_region(a[insert_pos])
166171
#END adjust right side
167172
else:
168173
if insert_pos != len_regions:
169-
right = MemoryWindow.from_region(a[insert_pos])
174+
right = self.MemoryWindowCls.from_region(a[insert_pos])
170175
# END adjust right window
171-
left = MemoryWindow.from_region(a[insert_pos - 1])
176+
left = self.MemoryWindowCls.from_region(a[insert_pos - 1])
172177
#END adjust surrounding windows
173178

174179
mid.extend_left_to(left, window_size)
@@ -185,7 +190,7 @@ def use_region(self, offset, size, flags = 0, _is_recursive=False):
185190
if man._handle_count >= man._max_handle_count:
186191
raise Exception
187192
#END assert own imposed max file handles
188-
self._region = MappedRegion(a.path(), mid.ofs, mid.size, flags)
193+
self._region = self.MappedRegionCls(a.path(), mid.ofs, mid.size, flags)
189194
except Exception:
190195
# apparently we are out of system resources or hit a limit
191196
# As many more operations are likely to fail in that condition (
@@ -302,6 +307,10 @@ class MappedMemoryManager(object):
302307
'_handle_count', # amount of currently allocated file handles
303308
]
304309

310+
#{ Configuration
311+
MappedRegionListCls = MappedRegionList
312+
#} END configuration
313+
305314
_MB_in_bytes = 1024 * 1024
306315

307316
def __init__(self, window_size = 0, max_memory_size = 0, max_open_handles = sys.maxint):
@@ -378,7 +387,7 @@ def make_cursor(self, path):
378387
""":return: a cursor pointing to the given path. It can be used to map new regions of the file into memory"""
379388
regions = self._fdict.get(path)
380389
if regions is None:
381-
regions = MappedRegionList(path)
390+
regions = self.MappedRegionListCls(path)
382391
self._fdict[path] = regions
383392
# END obtain region for path
384393
return MemoryCursor(self, regions)

smmap/test/test_buf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_basics(self):
6262
# exagerate the manager's overhead, but measure the buffer overhead
6363
# We do it once with an optimal setting, and with a worse manager which
6464
# will produce small mappings only !
65-
max_num_accesses = 1000
65+
max_num_accesses = 400
6666
for manager, man_id in ( (man_optimal, 'optimal'),
6767
(man_worst_case, 'worst case')):
6868
buf = MappedMemoryBuffer(manager.make_cursor(fc.path))

0 commit comments

Comments
 (0)