Skip to content

Commit 9dc4a8d

Browse files
committed
Changed names to be more descriptive, hopefully. This opens op the option to implement such a manager differently, without the sliding window mechanics, which would be quite simple and not much better than a map of mmaps in the end
1 parent d09158f commit 9dc4a8d

6 files changed

Lines changed: 53 additions & 53 deletions

File tree

smmap/buf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""Module with a simple buffer implementation using the memory manager"""
2-
from mman import MemoryCursor
2+
from mman import SlidingCursor
33

44
import sys
55

6-
__all__ = ["MappedMemoryBuffer"]
6+
__all__ = ["SlidingWindowMapBuffer"]
77

8-
class MappedMemoryBuffer(object):
8+
class SlidingWindowMapBuffer(object):
99
"""A buffer like object which allows direct byte-wise object and slicing into
1010
memory of a mapped file. The mapping is controlled by the provided cursor.
1111

smmap/mman.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
"""Module containnig a memory memory manager which provides a sliding window on a number of memory mapped files"""
22
from util import (
3-
MemoryWindow,
4-
MappedRegion,
5-
MappedRegionList,
3+
MapWindow,
4+
MapRegion,
5+
MapRegionList,
66
is_64_bit,
77
)
88

99
from exc import RegionCollectionError
1010
from weakref import ref
1111
import sys
1212

13-
__all__ = ["MappedMemoryManager"]
13+
__all__ = ["SlidingWindowMapManager"]
1414
#{ Utilities
1515

1616
#}END utilities
1717

18-
class MemoryCursor(object):
18+
class SlidingCursor(object):
1919
"""Pointer into the mapped region of the memory manager, keeping the current window
2020
alive until it is destroyed.
2121
22-
Cursors should not be created manually, but are instead returned by the MappedMemoryManager"""
22+
Cursors should not be created manually, but are instead returned by the SlidingWindowMapManager"""
2323
__slots__ = (
2424
'_manager', # the manger keeping all file regions
2525
'_rlist', # a regions list with regions for our file
@@ -29,8 +29,8 @@ class MemoryCursor(object):
2929
)
3030

3131
#{ Configuration
32-
MemoryWindowCls = MemoryWindow
33-
MappedRegionCls = MappedRegion
32+
MapWindowCls = MapWindow
33+
MapRegionCls = MapRegion
3434
#} END configuration
3535

3636
def __init__(self, manager = None, regions = None):
@@ -133,9 +133,9 @@ def use_region(self, offset, size, flags = 0, _is_recursive=False):
133133
#END while bisecting
134134

135135
if existing_region is None:
136-
left = self.MemoryWindowCls(0, 0)
137-
mid = self.MemoryWindowCls(offset, size)
138-
right = self.MemoryWindowCls(self.file_size(), 0)
136+
left = self.MapWindowCls(0, 0)
137+
mid = self.MapWindowCls(offset, size)
138+
right = self.MapWindowCls(self.file_size(), 0)
139139

140140
# we want to honor the max memory size, and assure we have anough
141141
# memory available
@@ -166,13 +166,13 @@ def use_region(self, offset, size, flags = 0, _is_recursive=False):
166166
# possible mapping
167167
if insert_pos == 0:
168168
if len_regions:
169-
right = self.MemoryWindowCls.from_region(a[insert_pos])
169+
right = self.MapWindowCls.from_region(a[insert_pos])
170170
#END adjust right side
171171
else:
172172
if insert_pos != len_regions:
173-
right = self.MemoryWindowCls.from_region(a[insert_pos])
173+
right = self.MapWindowCls.from_region(a[insert_pos])
174174
# END adjust right window
175-
left = self.MemoryWindowCls.from_region(a[insert_pos - 1])
175+
left = self.MapWindowCls.from_region(a[insert_pos - 1])
176176
#END adjust surrounding windows
177177

178178
mid.extend_left_to(left, window_size)
@@ -189,7 +189,7 @@ def use_region(self, offset, size, flags = 0, _is_recursive=False):
189189
if man._handle_count >= man._max_handle_count:
190190
raise Exception
191191
#END assert own imposed max file handles
192-
self._region = self.MappedRegionCls(a.path_or_fd(), mid.ofs, mid.size, flags)
192+
self._region = self.MapRegionCls(a.path_or_fd(), mid.ofs, mid.size, flags)
193193
except Exception:
194194
# apparently we are out of system resources or hit a limit
195195
# As many more operations are likely to fail in that condition (
@@ -301,7 +301,7 @@ def fd(self):
301301
#} END interface
302302

303303

304-
class MappedMemoryManager(object):
304+
class SlidingWindowMapManager(object):
305305
"""Maintains a list of ranges of mapped memory regions in one or more files and allows to easily
306306
obtain additional regions assuring there is no overlap.
307307
Once a certain memory limit is reached globally, or if there cannot be more open file handles
@@ -315,7 +315,7 @@ class MappedMemoryManager(object):
315315
space is full."""
316316

317317
__slots__ = [
318-
'_fdict', # mapping of path -> MappedRegionList
318+
'_fdict', # mapping of path -> MapRegionList
319319
'_window_size', # maximum size of a window
320320
'_max_memory_size', # maximum amount ofmemory we may allocate
321321
'_max_handle_count', # maximum amount of handles to keep open
@@ -324,7 +324,7 @@ class MappedMemoryManager(object):
324324
]
325325

326326
#{ Configuration
327-
MappedRegionListCls = MappedRegionList
327+
MapRegionListCls = MapRegionList
328328
#} END configuration
329329

330330
_MB_in_bytes = 1024 * 1024
@@ -411,10 +411,10 @@ def make_cursor(self, path_or_fd):
411411
prevents the file to be opened again just for the purpose of mapping it."""
412412
regions = self._fdict.get(path_or_fd)
413413
if regions is None:
414-
regions = self.MappedRegionListCls(path_or_fd)
414+
regions = self.MapRegionListCls(path_or_fd)
415415
self._fdict[path_or_fd] = regions
416416
# END obtain region for path
417-
return MemoryCursor(self, regions)
417+
return SlidingCursor(self, regions)
418418

419419
def collect(self):
420420
"""Collect all available free-to-collect mapped regions

smmap/test/test_buf.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from lib import TestBase, FileCreator
22

3-
from smmap.mman import MappedMemoryManager
3+
from smmap.mman import SlidingWindowMapManager
44
from smmap.buf import *
55

66
from random import randint
@@ -9,8 +9,8 @@
99
import os
1010

1111

12-
man_optimal = MappedMemoryManager()
13-
man_worst_case = MappedMemoryManager( window_size=TestBase.k_window_test_size/100,
12+
man_optimal = SlidingWindowMapManager()
13+
man_worst_case = SlidingWindowMapManager( window_size=TestBase.k_window_test_size/100,
1414
max_memory_size=TestBase.k_window_test_size/3,
1515
max_open_handles=15)
1616

@@ -21,10 +21,10 @@ def test_basics(self):
2121

2222
# invalid paths fail upon construction
2323
c = man_optimal.make_cursor(fc.path)
24-
self.failUnlessRaises(ValueError, MappedMemoryBuffer, type(c)()) # invalid cursor
25-
self.failUnlessRaises(ValueError, MappedMemoryBuffer, c, fc.size) # offset too large
24+
self.failUnlessRaises(ValueError, SlidingWindowMapBuffer, type(c)()) # invalid cursor
25+
self.failUnlessRaises(ValueError, SlidingWindowMapBuffer, c, fc.size) # offset too large
2626

27-
buf = MappedMemoryBuffer() # can create uninitailized buffers
27+
buf = SlidingWindowMapBuffer() # can create uninitailized buffers
2828
assert buf.cursor() is None
2929

3030
# can call end access any time
@@ -71,7 +71,7 @@ def test_basics(self):
7171
for item in (fc.path, fd):
7272
for manager, man_id in ( (man_optimal, 'optimal'),
7373
(man_worst_case, 'worst case')):
74-
buf = MappedMemoryBuffer(manager.make_cursor(item))
74+
buf = SlidingWindowMapBuffer(manager.make_cursor(item))
7575
assert manager.num_file_handles() == 1
7676
for access_mode in range(2): # single, multi
7777
num_accesses_left = max_num_accesses

smmap/test/test_mman.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from lib import TestBase, FileCreator
22

33
from smmap.mman import *
4-
from smmap.mman import MemoryCursor
4+
from smmap.mman import SlidingCursor
55
from smmap.util import align_to_mmap
66
from smmap.exc import RegionCollectionError
77

@@ -16,8 +16,8 @@ class TestMMan(TestBase):
1616
def test_cursor(self):
1717
fc = FileCreator(self.k_window_test_size, "cursor_test")
1818

19-
man = MappedMemoryManager()
20-
ci = MemoryCursor(man) # invalid cursor
19+
man = SlidingWindowMapManager()
20+
ci = SlidingCursor(man) # invalid cursor
2121
assert not ci.is_valid()
2222
assert not ci.is_associated()
2323
assert ci.size() == 0 # this is cached, so we can query it in invalid state
@@ -43,10 +43,10 @@ def test_cursor(self):
4343

4444
# destruction is fine (even multiple times)
4545
cv._destroy()
46-
MemoryCursor(man)._destroy()
46+
SlidingCursor(man)._destroy()
4747

4848
def test_memory_manager(self):
49-
man = MappedMemoryManager()
49+
man = SlidingWindowMapManager()
5050
assert man.num_file_handles() == 0
5151
assert man.num_open_files() == 0
5252
assert man.window_size() > 0
@@ -82,7 +82,7 @@ def test_memman_operation(self):
8282

8383
# small windows, a reasonable max memory. Not too many regions at once
8484
max_num_handles = 15
85-
man = MappedMemoryManager(window_size=fc.size / 100, max_memory_size=fc.size / 3, max_open_handles=max_num_handles)
85+
man = SlidingWindowMapManager(window_size=fc.size / 100, max_memory_size=fc.size / 3, max_open_handles=max_num_handles)
8686
c = man.make_cursor(item)
8787

8888
# still empty (more about that is tested in test_memory_manager()

smmap/test/test_util.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
class TestMMan(TestBase):
99

1010
def test_window(self):
11-
wl = MemoryWindow(0, 1) # left
12-
wc = MemoryWindow(1, 1) # center
13-
wc2 = MemoryWindow(10, 5) # another center
14-
wr = MemoryWindow(8000, 50) # right
11+
wl = MapWindow(0, 1) # left
12+
wc = MapWindow(1, 1) # center
13+
wc2 = MapWindow(10, 5) # another center
14+
wr = MapWindow(8000, 50) # right
1515

1616
assert wl.ofs_end() == 1
1717
assert wc.ofs_end() == 2
@@ -56,9 +56,9 @@ def test_region(self):
5656
fc = FileCreator(self.k_window_test_size, "window_test")
5757
half_size = fc.size / 2
5858
rofs = align_to_mmap(4200, False)
59-
rfull = MappedRegion(fc.path, 0, fc.size)
60-
rhalfofs = MappedRegion(fc.path, rofs, fc.size)
61-
rhalfsize = MappedRegion(fc.path, 0, half_size)
59+
rfull = MapRegion(fc.path, 0, fc.size)
60+
rhalfofs = MapRegion(fc.path, rofs, fc.size)
61+
rhalfsize = MapRegion(fc.path, 0, half_size)
6262

6363
# offsets
6464
assert rfull.ofs_begin() == 0 and rfull.size() == fc.size
@@ -88,15 +88,15 @@ def test_region(self):
8888
assert rfull.usage_count() == 1
8989

9090
# window constructor
91-
w = MemoryWindow.from_region(rfull)
91+
w = MapWindow.from_region(rfull)
9292
assert w.ofs == rfull.ofs_begin() and w.ofs_end() == rfull.ofs_end()
9393

9494
def test_region_list(self):
9595
fc = FileCreator(100, "sample_file")
9696

9797
fd = os.open(fc.path, os.O_RDONLY)
9898
for item in (fc.path, fd):
99-
ml = MappedRegionList(item)
99+
ml = MapRegionList(item)
100100

101101
assert ml.client_count() == 1
102102

smmap/util.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from sys import getrefcount
1616

1717
__all__ = [ "align_to_mmap", "is_64_bit",
18-
"MemoryWindow", "MappedRegion", "MappedRegionList", "ALLOCATIONGRANULARITY"]
18+
"MapWindow", "MapRegion", "MapRegionList", "ALLOCATIONGRANULARITY"]
1919

2020
#{ Utilities
2121

@@ -39,7 +39,7 @@ def is_64_bit():
3939

4040
#{ Utility Classes
4141

42-
class MemoryWindow(object):
42+
class MapWindow(object):
4343
"""Utility type which is used to snap windows towards each other, and to adjust their size"""
4444
__slots__ = (
4545
'ofs', # offset into the file in bytes
@@ -51,7 +51,7 @@ def __init__(self, offset, size):
5151
self.size = size
5252

5353
def __repr__(self):
54-
return "MemoryWindow(%i, %i)" % (self.ofs, self.size)
54+
return "MapWindow(%i, %i)" % (self.ofs, self.size)
5555

5656
@classmethod
5757
def from_region(cls, region):
@@ -84,7 +84,7 @@ def extend_right_to(self, window, max_size):
8484
self.size = min(self.size + (window.ofs - self.ofs_end()), max_size)
8585

8686

87-
class MappedRegion(object):
87+
class MapRegion(object):
8888
"""Defines a mapped region of memory, aligned to pagesizes
8989
:note: deallocates used region automatically on destruction"""
9090
__slots__ = [
@@ -145,7 +145,7 @@ def __init__(self, path_or_fd, ofs, size, flags = 0):
145145
#END close file handle
146146

147147
def __repr__(self):
148-
return "MappedRegion<%i, %i>" % (self._b, self.size())
148+
return "MapRegion<%i, %i>" % (self._b, self.size())
149149

150150
#{ Interface
151151

@@ -201,15 +201,15 @@ def includes_ofs(self, ofs):
201201
#} END interface
202202

203203

204-
class MappedRegionList(list):
205-
"""List of MappedRegion instances associating a path with a list of regions."""
204+
class MapRegionList(list):
205+
"""List of MapRegion instances associating a path with a list of regions."""
206206
__slots__ = (
207207
'_path_or_fd', # path or file descriptor which is mapped by all our regions
208208
'_file_size' # total size of the file we map
209209
)
210210

211211
def __new__(cls, path):
212-
return super(MappedRegionList, cls).__new__(cls)
212+
return super(MapRegionList, cls).__new__(cls)
213213

214214
def __init__(self, path_or_fd):
215215
self._path_or_fd = path_or_fd

0 commit comments

Comments
 (0)