API Reference

Mapped Memory Managers

Module containing a memory memory manager which provides a sliding window on a number of memory mapped files

class smmap.mman.StaticWindowMapManager(window_size=0, max_memory_size=0, max_open_handles=9223372036854775807)

Provides a manager which will produce single size cursors that are allowed to always map the whole file.

Clients must be written to specifically know that they are accessing their data through a StaticWindowMapManager, as they otherwise have to deal with their window size.

These clients would have to use a SlidingWindowMapBuffer to hide this fact.

This type will always use a maximum window size, and optimize certain methods to accommodate this fact


alias of smmap.util.MapRegion


alias of smmap.util.MapRegionList


alias of smmap.util.MapWindow


alias of WindowCursor


Collect all available free-to-collect mapped regions :return: Amount of freed handles


ONLY AVAILABLE ON WINDOWS On windows removing files is not allowed if anybody still has it opened. If this process is ourselves, and if the whole process uses this memory manager (as far as the parent framework is concerned) we can enforce closing all memory maps whose path matches the given base path to allow the respective operation after all. The respective system must NOT access the closed memory regions anymore ! This really may only be used if you know that the items which keep the cursors alive will not be using it anymore. They need to be recreated ! :return: Amount of closed handles

Note: does nothing on non-windows platforms

Returns:a cursor pointing to the given path or file descriptor. It can be used to map new regions of the file into memory

Note: if a file descriptor is given, it is assumed to be open and valid, but may be closed afterwards. To refer to the same file, you may reuse your existing file descriptor, but keep in mind that new windows can only be mapped as long as it stays valid. This is why the using actual file paths are preferred unless you plan to keep the file descriptor open.

Note: file descriptors are problematic as they are not necessarily unique, as two different files opened and closed in succession might have the same file descriptor id.

Note: Using file descriptors directly is faster once new windows are mapped as it prevents the file to be opened again just for the purpose of mapping it.

Returns:amount of bytes currently mapped in total
Returns:maximum amount of handles we may have opened
Returns:maximum amount of memory we may allocate
Returns:amount of file handles in use. Each mapped region uses one file handle

Amount of opened files in the system

Returns:size of each window when allocating new regions
class smmap.mman.SlidingWindowMapManager(window_size=-1, max_memory_size=0, max_open_handles=9223372036854775807)

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. Once a certain memory limit is reached globally, or if there cannot be more open file handles which result from each mmap call, the least recently used, and currently unused mapped regions are unloaded automatically.

Note: currently not thread-safe !

Note: in the current implementation, we will automatically unload windows if we either cannot
create more memory maps (as the open file handles limit is hit) or if we have allocated more than a safe amount of memory already, which would possibly cause memory allocations to fail as our address space is full.
class smmap.mman.WindowCursor(manager=None, regions=None)

Pointer into the mapped region of the memory manager, keeping the map alive until it is destroyed and no other client uses it.

Cursors should not be created manually, but are instead returned by the SlidingWindowMapManager

Note:: The current implementation is suited for static and sliding window managers, but it also means that it must be suited for the somewhat quite different sliding manager. It could be improved, but I see no real need to do so.


Assign rhs to this instance. This is required in order to get a real copy. Alternatively, you can copy an existing instance using the copy module


Return a buffer object which allows access to our memory region from our offset to the window size. Please note that it might be smaller than you requested when calling use_region()

Note: You can only obtain a buffer if this instance is_valid() !

Note: buffers should not be cached passed the duration of your access as it will prevent resources from being freed even though they might not be accounted for anymore !

Returns:file descriptor used to create the underlying mapping.

Note: it is not required to be valid anymore :raise ValueError: if the mapping was not created by a file descriptor

Returns:size of the underlying file
Returns:True if the given absolute offset is contained in the cursors current region

Note: cursor must be valid for this to work

Returns:True if we are associated with a specific file already
Returns:True if we have a valid and usable region
Returns:the underlying raw memory map. Please not that the offset and size is likely to be different to what you set as offset and size. Use it only if you are sure about the region it maps, which is the whole file in case of StaticWindowMapManager
Returns:offset to the first byte pointed to by our cursor

Note: only if is_valid() is True

Returns:offset to one past the last available byte
Returns:path of the underlying mapped file
Raises:ValueError – if attached path is not a path
Returns:path or file descriptor of the underlying mapped file
Returns:our mapped region, or None if nothing is mapped yet
Raises:AssertionError – if we have no current region. This is only useful for debugging
Returns:amount of bytes we point to

Unuse the current region. Does nothing if we have no current region

Note: the cursor unuses the region automatically upon destruction. It is recommended to un-use the region once you are done reading from it in persistent cursors as it helps to free up resource more quickly

use_region(offset=0, size=0, flags=0)

Assure we point to a window which allows access to the given offset into the file

  • offset – absolute offset in bytes into the file
  • size – amount of bytes to map. If 0, all available bytes will be mapped
  • flags – additional flags to be given to os.open in case a file handle is initially opened for mapping. Has no effect if a region can actually be reused.

this instance - it should be queried for whether it points to a valid memory region. This is not the case if the mapping failed because we reached the end of the file

Note:: The size actually mapped may be smaller than the given size. If that is the case, either the file has reached its end, or the map was created between two existing regions


Module with a simple buffer implementation using the memory manager

class smmap.buf.SlidingWindowMapBuffer(cursor=None, offset=0, size=9223372036854775807, flags=0)

A buffer like object which allows direct byte-wise object and slicing into memory of a mapped file. The mapping is controlled by the provided cursor.

The buffer is relative, that is if you map an offset, index 0 will map to the first byte at the offset you used during initialization or begin_access

Note: Although this type effectively hides the fact that there are mapped windows underneath, it can unfortunately not be used in any non-pure python method which needs a buffer or string

begin_access(cursor=None, offset=0, size=9223372036854775807, flags=0)

Call this before the first use of this instance. The method was already called by the constructor in case sufficient information was provided.

For more information no the parameters, see the __init__ method :param path: if cursor is None the existing one will be used. :return: True if the buffer can be used

Returns:the currently set cursor which provides access to the data

Call this method once you are done using the instance. It is automatically called on destruction, and should be called just in time to allow system resources to be freed.

Once you called end_access, you must call begin access before reusing this instance!


Module containing a memory memory manager which provides a sliding window on a number of memory mapped files

smmap.util.align_to_mmap(num, round_up)

Align the given integer number to the closest page offset, which usually is 4096 bytes.

Parameters:round_up – if True, the next higher multiple of page size is used, otherwise the lower page_size will be used (i.e. if True, 1 becomes 4096, otherwise it becomes 0)
Returns:num rounded to closest page
Returns:True if the system is 64 bit. Otherwise it can be assumed to be 32 bit
class smmap.util.MapWindow(offset, size)

Utility type which is used to snap windows towards each other, and to adjust their size


Assures the previous window area is contained in the new one

extend_left_to(window, max_size)

Adjust the offset to start where the given window on our left ends if possible, but don’t make yourself larger than max_size. The resize will assure that the new window still contains the old window area

extend_right_to(window, max_size)

Adjust the size to make our window end where the right window begins, but don’t get larger than max_size

classmethod from_region(region)
Returns:new window from a region
class smmap.util.MapRegion(path_or_fd, ofs, size, flags=0)

Defines a mapped region of memory, aligned to pagesizes

Note: deallocates used region automatically on destruction

Returns:a buffer containing the memory
Returns:number of clients currently using this region
Returns:True if the given offset can be read in our mapped region

Adjust the usage count by the given positive or negative offset. If usage count equals 0, we will auto-release our resources :return: True if we released resources, False otherwise. In the latter case, we can still be used

Returns:a memory map containing the memory
Returns:absolute byte offset to the first byte of the mapping
Returns:Absolute offset to one byte beyond the mapping into the file

Release all resources this instance might hold. Must only be called if there usage_count() is zero

Returns:total size of the mapped region in bytes
class smmap.util.MapRegionList(path_or_fd)

List of MapRegion instances associating a path with a list of regions.

Returns:size of file we manager
Returns:path or file descriptor we are attached to