@@ -11,7 +11,10 @@ class MappedMemoryBuffer(object):
1111
1212 The buffer is relative, that is if you map an offset, index 0 will map to the
1313 first byte at the offset you used during initialization or begin_access"""
14- __slots__ = '_c' # our cursor
14+ __slots__ = (
15+ '_c' , # our cursor
16+ '_size' , # our supposed size
17+ )
1518
1619
1720 def __init__ (self , cursor = None , offset = 0 , size = sys .maxint , flags = 0 ):
@@ -20,6 +23,10 @@ def __init__(self, cursor = None, offset = 0, size = sys.maxint, flags = 0):
2023 If None, you have call begin_access before using the buffer and provide a cursor
2124 :param offset: absolute offset in bytes
2225 :param size: the total size of the mapping. Defaults to the maximum possible size
26+ From that point on, the __len__ of the buffer will be the given size or the file size.
27+ If the size is larger than the mappable area, you can only access the actually available
28+ area, although the length of the buffer is reported to be your given size.
29+ Hence it is in your own interest to provide a proper size !
2330 :param flags: Additional flags to be passed to os.open
2431 :raise ValueError: if the buffer could not achieve a valid state"""
2532 self ._c = cursor
@@ -30,6 +37,9 @@ def __init__(self, cursor = None, offset = 0, size = sys.maxint, flags = 0):
3037 def __del__ (self ):
3138 self .end_access ()
3239
40+ def __len__ (self ):
41+ return self ._size
42+
3343 def __getitem__ (self , i ):
3444 c = self ._c
3545 assert c .is_valid ()
@@ -76,7 +86,18 @@ def begin_access(self, cursor = None, offset = 0, size = sys.maxint, flags = 0):
7686
7787 # reuse existing cursors if possible
7888 if self ._c is not None and self ._c .is_associated ():
79- return self ._c .use_region (offset , size , flags ).is_valid ()
89+ res = self ._c .use_region (offset , size , flags ).is_valid ()
90+ if res :
91+ # if given size is too large or default, we computer a proper size
92+ # If its smaller, we assume the combination between offset and size
93+ # as chosen by the user is correct and use it !
94+ # If not, the user is in trouble.
95+ if size > self ._c .file_size ():
96+ size = self ._c .file_size () - offset
97+ #END handle size
98+ self ._size = size
99+ #END set size
100+ return res
80101 return False
81102
82103 def end_access (self ):
@@ -85,6 +106,7 @@ def end_access(self):
85106 resources to be freed.
86107
87108 Once you called end_access, you must call begin access before reusing this instance!"""
109+ self ._size = 0
88110 if self ._c is not None :
89111 self ._c .unuse_region ()
90112 #END unuse region
0 commit comments