Skip to content

Commit 011343f

Browse files
committed
Cursor can now take additional flags when opening the file handle for mapping. Renamed stream to buf, as the first item will be a buffer which uses the cursor underneath. We should add a stream for good measure though
1 parent 04991e9 commit 011343f

4 files changed

Lines changed: 12 additions & 7 deletions

File tree

File renamed without changes.

smmap/mman.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ def assign(self, rhs):
7878
self._destroy()
7979
self._copy_from(rhs)
8080

81-
def use_region(self, offset, size, _is_recursive=False):
81+
def use_region(self, offset, size, flags = 0, _is_recursive=False):
8282
"""Assure we point to a window which allows access to the given offset into the file
8383
:param offset: absolute offset in bytes into the file
8484
:param size: amount of bytes to map
85+
:param flags: additional flags to be given to os.open in case a file handle is initially opened
86+
for mapping. Has no effect if a region can actually be reused.
8587
:return: this instance - it should be queried for whether it points to a valid memory region.
8688
This is not the case if the mapping failed becaues we reached the end of the file
8789
:note: The size actually mapped may be smaller than the given size. If that is the case,
@@ -106,6 +108,8 @@ def use_region(self, offset, size, _is_recursive=False):
106108
return self
107109
# END handle offset too large
108110

111+
# bisect to find an existing region. The c++ implementation cannot
112+
# do that as it uses a linked list for regions.
109113
existing_region = None
110114
a = self._rlist
111115
lo = 0
@@ -181,7 +185,7 @@ def use_region(self, offset, size, _is_recursive=False):
181185
if man._handle_count >= man._max_handle_count:
182186
raise Exception
183187
#END assert own imposed max file handles
184-
self._region = MappedRegion(a.path(), mid.ofs, mid.size)
188+
self._region = MappedRegion(a.path(), mid.ofs, mid.size, flags)
185189
except Exception:
186190
# apparently we are out of system resources or hit a limit
187191
# As many more operations are likely to fail in that condition (
@@ -195,7 +199,7 @@ def use_region(self, offset, size, _is_recursive=False):
195199
raise
196200
#END handle existing recursion
197201
man._collect_lru_region(0)
198-
return self.use_region(offset, size, True)
202+
return self.use_region(offset, size, flags, True)
199203
#END handle exceptions
200204

201205
man._handle_count += 1
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from lib import TestBase
22

3-
from smmap.stream import *
3+
from smmap.buf import *
44

5-
class TestStream(TestBase):
5+
class TestBuf(TestBase):
66
def test_basics(self):
77
assert False

smmap/util.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,19 @@ class MappedRegion(object):
9393
#END handle additional slot
9494

9595

96-
def __init__(self, path, ofs, size):
96+
def __init__(self, path, ofs, size, flags = 0):
9797
"""Initialize a region, allocate the memory map
9898
:param path: path to the file to map
9999
:param ofs: **aligned** offset into the file to be mapped
100100
:param size: if size is larger then the file on disk, the whole file will be
101101
allocated the the size automatically adjusted
102+
:param flags: additional flags to be given when opening the file.
102103
:raise Exception: if no memory can be allocated"""
103104
self._b = ofs
104105
self._size = 0
105106
self._uc = 0
106107

107-
fd = os.open(path, os.O_RDONLY|getattr(os, 'O_BINARY', 0))
108+
fd = os.open(path, os.O_RDONLY|getattr(os, 'O_BINARY', 0)|flags)
108109
try:
109110
kwargs = dict(access=ACCESS_READ, offset=ofs)
110111
corrected_size = size

0 commit comments

Comments
 (0)