77
88class MappedMemoryBuffer (object ):
99 """A buffer like object which allows direct byte-wise object and slicing into
10- memory of a mapped file. The mapping is controlled by an underlying memory manager.
11-
12- A buffer, once initialized, stays put on providing access to eactly one path.
13- A custom interface allows you to change paths mid way, and to optimize
14- the resource usage.
15-
16- Please note that this type is only fully usable if you configure it with the
17- MappedMemoryManager to use.
10+ memory of a mapped file. The mapping is controlled by the provided cursor.
1811
1912 The buffer is relative, that is if you map an offset, index 0 will map to the
20- first byte at your given offset. """
13+ first byte at the offset you used during initialization or begin_access """
2114 __slots__ = '_c' # our cursor
2215
23- #{ Configuration
24- # A subclass must provide an instance of a (usually global) MappedMemoryManager
25- manager = None
26- #}END configuration
2716
28- def __init__ (self , path = None , offset = 0 , size = sys .maxint , flags = 0 ):
29- """Initalize the instance to operate on the given path if given .
30- :param path : if not None, the path to the file you want to access
31- If None, you have call begin_access before using the buffer
17+ def __init__ (self , cursor = None , offset = 0 , size = sys .maxint , flags = 0 ):
18+ """Initalize the instance to operate on the given cursor .
19+ :param cursor : if not None, the associated cursor to the file you want to access
20+ If None, you have call begin_access before using the buffer and provide a cursor
3221 :param offset: absolute offset in bytes
3322 :param size: the total size of the mapping. Defaults to the maximum possible size
3423 :param flags: Additional flags to be passed to os.open
3524 :raise ValueError: if the buffer could not achieve a valid state"""
36- self ._c = MemoryCursor (self .manager )
37- assert self .manager is not None , "Require the cls.manager variable to be set in subclass"
38- if path and not self .begin_access (path , offset , size , flags ):
25+ self ._c = cursor
26+ if cursor and not self .begin_access (cursor , offset , size , flags ):
3927 raise ValueError ("Failed to allocate the buffer - probably the given offset is out of bounds" )
4028 # END handle offset
4129
@@ -75,19 +63,19 @@ def __getslice__(self, i, j):
7563 # END fast or slow path
7664 #{ Interface
7765
78- def begin_access (self , path = None , offset = 0 , size = sys .maxint , flags = 0 ):
66+ def begin_access (self , cursor = None , offset = 0 , size = sys .maxint , flags = 0 ):
7967 """Call this before the first use of this instance. The method was already
8068 called by the constructor in case sufficient information was provided.
8169
8270 For more information no the parameters, see the __init__ method
83- :param path: if path is empty or None the existing path will be used if possible .
71+ :param path: if cursor is None the existing one will be used.
8472 :return: True if the buffer can be used"""
85- if path and ( not self . _c . is_associated () or self . _c . path () != path ) :
86- self ._c = self . manager . make_cursor ( path )
87- #END get associated cursor
73+ if cursor :
74+ self ._c = cursor
75+ #END update our cursor
8876
8977 # reuse existing cursors if possible
90- if self ._c .is_associated ():
78+ if self ._c is not None and self . _c .is_associated ():
9179 return self ._c .use_region (offset , size , flags ).is_valid ()
9280 return False
9381
@@ -97,7 +85,9 @@ def end_access(self):
9785 resources to be freed.
9886
9987 Once you called end_access, you must call begin access before reusing this instance!"""
100- self ._c .unuse_region ()
88+ if self ._c is not None :
89+ self ._c .unuse_region ()
90+ #END unuse region
10191
10292 def cursor (self ):
10393 """:return: the currently set cursor which provides access to the data"""
0 commit comments