From 0bf73877f860a86d9cf601154e9a9f76292e63c9 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 8 Jun 2011 21:00:33 +0200 Subject: Fixed bug in test case as it didn't properly align its offset to a page --- smmap/mman.py | 20 +++++++++++++++----- smmap/test/test_mman.py | 18 +++++++++++++++--- 2 files changed, 30 insertions(+), 8 deletions(-) (limited to 'smmap') diff --git a/smmap/mman.py b/smmap/mman.py index c5a3887..27904f5 100644 --- a/smmap/mman.py +++ b/smmap/mman.py @@ -7,6 +7,7 @@ import sys import mmap from mmap import PAGESIZE +from sys import getrefcount #{ Utilities @@ -71,7 +72,6 @@ class MappedRegion(object): __slots__ = [ '_b' , # beginning of mapping '_mf', # mapped memory chunk (as returned by mmap) - '_nc', # number of clients using this region '_uc', # total amount of usages '_ms' # actual size of the mapping ] @@ -90,24 +90,24 @@ class MappedRegion(object): allocated the the size automatically adjusted :raise Exception: if no memory can be allocated""" self._b = ofs - self._nc = 0 self._uc = 0 fd = os.open(path, os.O_RDONLY|getattr(os, 'O_BINARY', 0)) try: kwargs = dict(access=mmap.ACCESS_READ, offset=ofs) corrected_size = size + sizeofs = ofs if self._need_compat_layer: del(kwargs['offset']) corrected_size += ofs + sizeofs = 0 # END handle python not supporting offset ! Arg # have to correct size, otherwise (instead of the c version) it will # bark that the size is too large ... many extra file accesses because # if this ... argh ! - self._mf = mmap.mmap(fd, min(os.fstat(fd).st_size, corrected_size), **kwargs) + self._mf = mmap.mmap(fd, min(os.fstat(fd).st_size - sizeofs, corrected_size - sizeofs), **kwargs) - print len(self._mf) if self._need_compat_layer: self._mfb = buffer(self._mf, ofs, size) #END handle buffer wrapping @@ -133,7 +133,8 @@ class MappedRegion(object): def client_count(self): """:return: number of clients currently using this region""" - return self._nc + # -1: self on stack, -1 self in this method, -1 self in getrefcount + return getrefcount(self)-3 def adjust_client_count(self, ofs): """Adjust the client count by the given positive or negative offset""" @@ -157,6 +158,15 @@ class MappedRegion(object): #END handle compat layer +class Cursor(object): + """Pointer into the mapped region of the memory manager, keeping the current window + alive until it is destroyed""" + + +class MappedRegionList(list): + """List of MappedRegion instances with specific functionality""" + + class MappedMemoryManager(object): """Maintains a list of ranges of mapped memory regions in one or more files and allows to easily obtain additional regions assuring there is no overlap. diff --git a/smmap/test/test_mman.py b/smmap/test/test_mman.py index 482ef92..00c9d90 100644 --- a/smmap/test/test_mman.py +++ b/smmap/test/test_mman.py @@ -1,8 +1,11 @@ from lib import TestBase, FileCreator from smmap.mman import * -from smmap.mman import MappedRegion +from smmap.mman import align_to_page from smmap.mman import Window +from smmap.mman import MappedRegion +from smmap.mman import MappedRegionList +from smmap.mman import Cursor import sys import mmap @@ -56,11 +59,10 @@ class TestMMan(TestBase): wc.align() assert wc.ofs == 0 and wc.size == mmap.PAGESIZE*2 - def test_region(self): fc = FileCreator(self._window_test_size, "window_test") half_size = fc.size / 2 - rofs = 4000 + rofs = align_to_page(4200, False) rfull = MappedRegion(fc.path, 0, fc.size) rhalfofs = MappedRegion(fc.path, rofs, fc.size) rhalfsize = MappedRegion(fc.path, 0, half_size) @@ -76,10 +78,20 @@ class TestMMan(TestBase): assert not rfull.includes_ofs(-1) and not rfull.includes_ofs(sys.maxint) assert rhalfofs.includes_ofs(rofs) and not rhalfofs.includes_ofs(0) + # auto-refcount + assert rfull.client_count() == 1 + rfull2 = rfull + assert rfull.client_count() == 2 + # window constructor w = Window.from_region(rfull) assert w.ofs == rfull.ofs_begin() and w.ofs_end() == rfull.ofs_end() + def test_region_list(self): + pass + + def test_cursor(self): + pass def test_basics(self): pass -- cgit v1.2.1