Skip to content

Commit 9f16040

Browse files
committed
Fixed some mapping issues on windows. Fixed some tests to deal with the very different granularity
1 parent abf5640 commit 9f16040

5 files changed

Lines changed: 24 additions & 23 deletions

File tree

smmap/mman.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
MappedRegion,
55
MappedRegionList,
66
is_64_bit,
7-
PAGESIZE
87
)
98

109
from exc import RegionCollectionError
@@ -446,10 +445,6 @@ def max_mapped_memory_size(self):
446445
""":return: maximum amount of memory we may allocate"""
447446
return self._max_memory_size
448447

449-
def page_size(self):
450-
""":return: size of a single memory page in bytes"""
451-
return PAGESIZE
452-
453448
#} END interface
454449

455450
#{ Special Purpose Interface

smmap/test/test_buf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def test_basics(self):
9797
assert manager.num_file_handles()
9898
assert manager.collect()
9999
assert manager.num_file_handles() == 0
100-
elapsed = time() - st
100+
elapsed = max(time() - st, 0.001) # prevent zero division errors on windows
101101
mb = float(1000*1000)
102102
mode_str = (access_mode and "slice") or "single byte"
103103
sys.stderr.write("%s: Made %i random %s accesses to buffer created from %s reading a total of %f mb in %f s (%f mb/s)\n"

smmap/test/test_mman.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from smmap.mman import *
44
from smmap.mman import MemoryCursor
5-
from smmap.util import PAGESIZE, align_to_page
5+
from smmap.util import align_to_mmap
66
from smmap.exc import RegionCollectionError
77

88
from random import randint
@@ -52,7 +52,6 @@ def test_memory_manager(self):
5252
assert man.window_size() > 0
5353
assert man.mapped_memory_size() == 0
5454
assert man.max_mapped_memory_size() > 0
55-
assert man.page_size() == PAGESIZE
5655

5756
# collection doesn't raise in 'any' mode
5857
man._collect_lru_region(0)
@@ -102,7 +101,7 @@ def test_memman_operation(self):
102101
assert c.size() == size
103102
assert c.ofs_begin() == base_offset
104103
assert rr().ofs_begin() == 0 # it was aligned and expanded
105-
assert rr().size() == align_to_page(man.window_size(), True) # but isn't larger than the max window (aligned)
104+
assert rr().size() == align_to_mmap(man.window_size(), True) # but isn't larger than the max window (aligned)
106105

107106
assert c.buffer()[:] == data[base_offset:base_offset+size]
108107

@@ -164,7 +163,7 @@ def test_memman_operation(self):
164163
assert includes_ofs(base_offset+csize-1)
165164
assert not includes_ofs(base_offset+csize)
166165
# END while we should do an access
167-
elapsed = time() - st
166+
elapsed = max(time() - st, 0.001) # prevent zero divison errors on windows
168167
mb = float(1000 * 1000)
169168
sys.stderr.write("Read %i mb of memory with %i random on cursor initialized with %s accesses in %fs (%f mb/s)\n"
170169
% (memory_read/mb, max_random_accesses, type(item), elapsed, (memory_read/mb)/elapsed))

smmap/test/test_util.py

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

33
from smmap.util import *
4+
from mmap import ALLOCATIONGRANULARITY
45

56
import os
67
import sys
@@ -50,12 +51,12 @@ def test_window(self):
5051
assert wr.ofs == wc2.ofs_end()
5152

5253
wc.align()
53-
assert wc.ofs == 0 and wc.size == PAGESIZE*2
54+
assert wc.ofs == 0 and wc.size == align_to_mmap(wc.size, True)
5455

5556
def test_region(self):
5657
fc = FileCreator(self.k_window_test_size, "window_test")
5758
half_size = fc.size / 2
58-
rofs = align_to_page(4200, False)
59+
rofs = align_to_mmap(4200, False)
5960
rfull = MappedRegion(fc.path, 0, fc.size)
6061
rhalfofs = MappedRegion(fc.path, rofs, fc.size)
6162
rhalfsize = MappedRegion(fc.path, 0, half_size)
@@ -69,7 +70,13 @@ def test_region(self):
6970

7071
assert rfull.includes_ofs(0) and rfull.includes_ofs(fc.size-1) and rfull.includes_ofs(half_size)
7172
assert not rfull.includes_ofs(-1) and not rfull.includes_ofs(sys.maxint)
72-
assert rhalfofs.includes_ofs(rofs) and not rhalfofs.includes_ofs(0)
73+
# with the values we have, this test only works on windows where an alignment
74+
# size of 4096 is assumed.
75+
if sys.platform == 'win32':
76+
assert rhalfofs.includes_ofs(rofs) and rhalfofs.includes_ofs(0)
77+
else:
78+
assert rhalfofs.includes_ofs(rofs) and not rhalfofs.includes_ofs(0)
79+
#END handle platforms
7380

7481
# auto-refcount
7582
assert rfull.client_count() == 1
@@ -102,6 +109,6 @@ def test_region_list(self):
102109

103110
def test_util(self):
104111
assert isinstance(is_64_bit(), bool) # just call it
105-
assert align_to_page(1, False) == 0
106-
assert align_to_page(1, True) == PAGESIZE
112+
assert align_to_mmap(1, False) == 0
113+
assert align_to_mmap(1, True) == ALLOCATIONGRANULARITY
107114

smmap/util.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
import sys
44
import mmap
55

6-
from mmap import PAGESIZE, mmap, ACCESS_READ
6+
from mmap import ALLOCATIONGRANULARITY, mmap, ACCESS_READ
77
from sys import getrefcount
88

9-
__all__ = [ "align_to_page", "is_64_bit",
10-
"MemoryWindow", "MappedRegion", "MappedRegionList", "PAGESIZE"]
9+
__all__ = [ "align_to_mmap", "is_64_bit",
10+
"MemoryWindow", "MappedRegion", "MappedRegionList", "ALLOCATIONGRANULARITY"]
1111

1212
#{ Utilities
1313

14-
def align_to_page(num, round_up):
14+
def align_to_mmap(num, round_up):
1515
"""Align the given integer number to the closest page offset, which usually is 4096 bytes.
1616
:param round_up: if True, the next higher multiple of page size is used, otherwise
1717
the lower page_size will be used (i.e. if True, 1 becomes 4096, otherwise it becomes 0)
1818
:return: num rounded to closest page"""
19-
res = (num / PAGESIZE) * PAGESIZE;
19+
res = (num / ALLOCATIONGRANULARITY) * ALLOCATIONGRANULARITY;
2020
if round_up and (res != num):
21-
res += PAGESIZE;
21+
res += ALLOCATIONGRANULARITY
2222
#END handle size
2323
return res;
2424

@@ -55,10 +55,10 @@ def ofs_end(self):
5555

5656
def align(self):
5757
"""Assures the previous window area is contained in the new one"""
58-
nofs = align_to_page(self.ofs, 0)
58+
nofs = align_to_mmap(self.ofs, 0)
5959
self.size += self.ofs - nofs # keep size constant
6060
self.ofs = nofs
61-
self.size = align_to_page(self.size, 1)
61+
self.size = align_to_mmap(self.size, 1)
6262

6363
def extend_left_to(self, window, max_size):
6464
"""Adjust the offset to start where the given window on our left ends if possible,

0 commit comments

Comments
 (0)