Skip to content

Commit 0bf7387

Browse files
committed
Fixed bug in test case as it didn't properly align its offset to a page
1 parent 66a78db commit 0bf7387

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

smmap/mman.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import mmap
88

99
from mmap import PAGESIZE
10+
from sys import getrefcount
1011

1112
#{ Utilities
1213

@@ -71,7 +72,6 @@ class MappedRegion(object):
7172
__slots__ = [
7273
'_b' , # beginning of mapping
7374
'_mf', # mapped memory chunk (as returned by mmap)
74-
'_nc', # number of clients using this region
7575
'_uc', # total amount of usages
7676
'_ms' # actual size of the mapping
7777
]
@@ -90,24 +90,24 @@ def __init__(self, path, ofs, size):
9090
allocated the the size automatically adjusted
9191
:raise Exception: if no memory can be allocated"""
9292
self._b = ofs
93-
self._nc = 0
9493
self._uc = 0
9594

9695
fd = os.open(path, os.O_RDONLY|getattr(os, 'O_BINARY', 0))
9796
try:
9897
kwargs = dict(access=mmap.ACCESS_READ, offset=ofs)
9998
corrected_size = size
99+
sizeofs = ofs
100100
if self._need_compat_layer:
101101
del(kwargs['offset'])
102102
corrected_size += ofs
103+
sizeofs = 0
103104
# END handle python not supporting offset ! Arg
104105

105106
# have to correct size, otherwise (instead of the c version) it will
106107
# bark that the size is too large ... many extra file accesses because
107108
# if this ... argh !
108-
self._mf = mmap.mmap(fd, min(os.fstat(fd).st_size, corrected_size), **kwargs)
109+
self._mf = mmap.mmap(fd, min(os.fstat(fd).st_size - sizeofs, corrected_size - sizeofs), **kwargs)
109110

110-
print len(self._mf)
111111
if self._need_compat_layer:
112112
self._mfb = buffer(self._mf, ofs, size)
113113
#END handle buffer wrapping
@@ -133,7 +133,8 @@ def includes_ofs(self, ofs):
133133

134134
def client_count(self):
135135
""":return: number of clients currently using this region"""
136-
return self._nc
136+
# -1: self on stack, -1 self in this method, -1 self in getrefcount
137+
return getrefcount(self)-3
137138

138139
def adjust_client_count(self, ofs):
139140
"""Adjust the client count by the given positive or negative offset"""
@@ -157,6 +158,15 @@ def ofs_end(self):
157158
#END handle compat layer
158159

159160

161+
class Cursor(object):
162+
"""Pointer into the mapped region of the memory manager, keeping the current window
163+
alive until it is destroyed"""
164+
165+
166+
class MappedRegionList(list):
167+
"""List of MappedRegion instances with specific functionality"""
168+
169+
160170
class MappedMemoryManager(object):
161171
"""Maintains a list of ranges of mapped memory regions in one or more files and allows to easily
162172
obtain additional regions assuring there is no overlap.

smmap/test/test_mman.py

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

33
from smmap.mman import *
4-
from smmap.mman import MappedRegion
4+
from smmap.mman import align_to_page
55
from smmap.mman import Window
6+
from smmap.mman import MappedRegion
7+
from smmap.mman import MappedRegionList
8+
from smmap.mman import Cursor
69

710
import sys
811
import mmap
@@ -56,11 +59,10 @@ def test_window(self):
5659
wc.align()
5760
assert wc.ofs == 0 and wc.size == mmap.PAGESIZE*2
5861

59-
6062
def test_region(self):
6163
fc = FileCreator(self._window_test_size, "window_test")
6264
half_size = fc.size / 2
63-
rofs = 4000
65+
rofs = align_to_page(4200, False)
6466
rfull = MappedRegion(fc.path, 0, fc.size)
6567
rhalfofs = MappedRegion(fc.path, rofs, fc.size)
6668
rhalfsize = MappedRegion(fc.path, 0, half_size)
@@ -76,10 +78,20 @@ def test_region(self):
7678
assert not rfull.includes_ofs(-1) and not rfull.includes_ofs(sys.maxint)
7779
assert rhalfofs.includes_ofs(rofs) and not rhalfofs.includes_ofs(0)
7880

81+
# auto-refcount
82+
assert rfull.client_count() == 1
83+
rfull2 = rfull
84+
assert rfull.client_count() == 2
85+
7986
# window constructor
8087
w = Window.from_region(rfull)
8188
assert w.ofs == rfull.ofs_begin() and w.ofs_end() == rfull.ofs_end()
8289

90+
def test_region_list(self):
91+
pass
92+
93+
def test_cursor(self):
94+
pass
8395

8496
def test_basics(self):
8597
pass

0 commit comments

Comments
 (0)