def map_file(file_name, file_size, flags, mode): """Given a path, this function creates a new read/write mapping for the named file. It will map the file using mmap, but it also takes extra steps to make large page mappings more likely. If creation flags are not supplied, then this function creates a mapping for an existing file. In such case, `file_size` should be zero. The entire file is mapped to memory; its length is used as the length of the mapping. .. seealso:: `NVML libpmem documentation <http://pmem.io/nvml/libpmem/libpm em.3.html>`_. :param file_name: The file name to use. :param file_size: the size to allocate :param flags: The flags argument can be 0 or bitwise OR of one or more of the following file creation flags: :const:`~nvm.pmem.FILE_CREATE`, :const:`~nvm.pmem.FILE_EXCL`, :const:`~nvm.pmem.FILE_TMPFILE`, :const:`~nvm.pmem.FILE_SPARSE`. :return: The mapping, an exception will rise in case of error. """ ret_mappend_len = ffi.new("size_t *") ret_is_pmem = ffi.new("int *") if sys.version_info[0] > 2 and hasattr(file_name, 'encode'): file_name = file_name.encode(errors='surrogateescape') ret = lib.pmem_map_file(file_name, file_size, flags, mode, ret_mappend_len, ret_is_pmem) if ret == ffi.NULL: raise RuntimeError(os.strerror(ffi.errno)) ret_mapped_len = ret_mappend_len[0] ret_is_pmem = bool(ret_is_pmem[0]) cast = ffi.buffer(ret, file_size) return MemoryBuffer(cast, ret_is_pmem, ret_mapped_len)
def read(self, block_num): """This method reads a block from memory pool at specified block number. .. note:: Reading a block that has never been written will return an empty buffer. :return: data at block. """ data = ffi.new("char[]", self.block_size) ret = lib.pmemblk_read(self.block_pool, data, block_num) if ret == -1: raise RuntimeError(ffi.string(ret)) return ffi.string(data)
def map_file(file_name, file_size, flags, mode): """Given a path, this function creates a new read/write mapping for the named file. It will map the file using mmap, but it also takes extra steps to make large page mappings more likely. If creation flags are not supplied, then this function creates a mapping for an existing file. In such case, `file_size` should be zero. The entire file is mapped to memory; its length is used as the length of the mapping. .. seealso:: `NVML libpmem documentation <http://pmem.io/nvml/libpmem/libpm em.3.html>`_. :param file_name: The file name to use. :param file_size: the size to allocate :param flags: The flags argument can be 0 or bitwise OR of one or more of the following file creation flags: :const:`~nvm.pmem.FILE_CREATE`, :const:`~nvm.pmem.FILE_EXCL`, :const:`~nvm.pmem.FILE_TMPFILE`, :const:`~nvm.pmem.FILE_SPARSE`. :return: The mapping, an exception will rise in case of error. """ ret_mappend_len = ffi.new("size_t *") ret_is_pmem = ffi.new("int *") ret = lib.pmem_map_file(file_name, file_size, flags, mode, ret_mappend_len, ret_is_pmem) if ret == ffi.NULL: raise RuntimeError(os.strerror(ffi.errno)) ret_mapped_len = ret_mappend_len[0] ret_is_pmem = bool(ret_is_pmem[0]) cast = ffi.buffer(ret, file_size) return MemoryBuffer(cast, ret_is_pmem, ret_mapped_len)