77import mmap
88
99from 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+
160170class 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.
0 commit comments