我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用mmap.MAP_SHARED。
def remoteSceneChanged(self, data): w, h, size, newfile = data #self._sizeHint = (whint, hhint) if self.shm is None or self.shm.size != size: if self.shm is not None: self.shm.close() if sys.platform.startswith('win'): self.shmtag = newfile ## on windows, we create a new tag for every resize self.shm = mmap.mmap(-1, size, self.shmtag) ## can't use tmpfile on windows because the file can only be opened once. elif sys.platform == 'darwin': self.shmFile.close() self.shmFile = open(self._view.shmFileName(), 'r') self.shm = mmap.mmap(self.shmFile.fileno(), size, mmap.MAP_SHARED, mmap.PROT_READ) else: self.shm = mmap.mmap(self.shmFile.fileno(), size, mmap.MAP_SHARED, mmap.PROT_READ) self.shm.seek(0) data = self.shm.read(w*h*4) self._img = QtGui.QImage(data, w, h, QtGui.QImage.Format_ARGB32) self._img.data = data # data must be kept alive or PySide 1.2.1 (and probably earlier) will crash. self.update()
def __init__(self, *args, **kwds): ## Create shared memory for rendered image #pg.dbg(namespace={'r': self}) if sys.platform.startswith('win'): self.shmtag = "pyqtgraph_shmem_" + ''.join([chr((random.getrandbits(20)%25) + 97) for i in range(20)]) self.shm = mmap.mmap(-1, mmap.PAGESIZE, self.shmtag) # use anonymous mmap on windows else: self.shmFile = tempfile.NamedTemporaryFile(prefix='pyqtgraph_shmem_') self.shmFile.write(b'\x00' * (mmap.PAGESIZE+1)) fd = self.shmFile.fileno() self.shm = mmap.mmap(fd, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_WRITE) atexit.register(self.close) GraphicsView.__init__(self, *args, **kwds) self.scene().changed.connect(self.update) self.img = None self.renderTimer = QtCore.QTimer() self.renderTimer.timeout.connect(self.renderView) self.renderTimer.start(16)
def wipe(self): filter_bitmap_fd = os.open("/dev/shm/kafl_filter0", os.O_RDWR | os.O_SYNC | os.O_CREAT) os.ftruncate(filter_bitmap_fd, self.config.config_values['BITMAP_SHM_SIZE']) filter_bitmap = mmap.mmap(filter_bitmap_fd, self.config.config_values['BITMAP_SHM_SIZE'], mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ) for i in range(self.config.config_values['BITMAP_SHM_SIZE']): filter_bitmap[i] = '\x00' filter_bitmap.close() os.close(filter_bitmap_fd) filter_bitmap_fd = os.open("/dev/shm/kafl_tfilter", os.O_RDWR | os.O_SYNC | os.O_CREAT) os.ftruncate(filter_bitmap_fd, 0x1000000) filter_bitmap = mmap.mmap(filter_bitmap_fd, 0x1000000, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ) for i in range(0x1000000): filter_bitmap[i] = '\x00' filter_bitmap.close() os.close(filter_bitmap_fd)
def __set_binary(self, filename, binaryfile, max_size): shm_fd = os.open(filename, os.O_RDWR | os.O_SYNC | os.O_CREAT) os.ftruncate(shm_fd, max_size) shm = mmap.mmap(shm_fd, max_size, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ) shm.seek(0x0) shm.write('\x00' * max_size) shm.seek(0x0) f = open(binaryfile, "rb") bytes = f.read(1024) if bytes: shm.write(bytes) while bytes != "": bytes = f.read(1024) if bytes: shm.write(bytes) f.close() shm.close() os.close(shm_fd)
def init(self): self.control = socket.socket(socket.AF_UNIX) while True: try: self.control.connect(self.control_filename) #self.control.connect(self.control_filename) break except socket_error: pass #time.sleep(0.01) self.kafl_shm_f = os.open(self.bitmap_filename, os.O_RDWR | os.O_SYNC | os.O_CREAT) self.fs_shm_f = os.open(self.payload_filename, os.O_RDWR | os.O_SYNC | os.O_CREAT) #argv_fd = os.open(self.argv_filename, os.O_RDWR | os.O_SYNC | os.O_CREAT) os.ftruncate(self.kafl_shm_f, self.bitmap_size) os.ftruncate(self.fs_shm_f, (128 << 10)) #os.ftruncate(argv_fd, (4 << 10)) self.kafl_shm = mmap.mmap(self.kafl_shm_f, self.bitmap_size, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ) self.fs_shm = mmap.mmap(self.fs_shm_f, (128 << 10), mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ) return True
def get(self,request): n=12 try: size = os.path.getsize(ACTIVITY_LOG) with open(ACTIVITY_LOG, "rb") as f: # for Windows the mmap parameters are different fm = mmap.mmap(f.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ) for i in xrange(size - 1, -1, -1): if fm[i] == '\n': n -= 1 if n == -1: break lines = fm[i + 1 if i else 0:].splitlines() return JsonResponse({'status': "success", 'log': lines}) except Exception as err: return JsonResponse({'status' : "error",'messagge' : err}) finally: try: fm.close() except (UnboundLocalError, TypeError): return JsonResponse({'status':"error", 'message': "Activity log file is empty"}) #index
def __init__(self, ha_api_url, ble_adapter_name, mac_address, components): super().__init__() self.components = [] self.active_component = None component_instances = get_component_instances(components, mac_address) self.set_components(component_instances) self.manager = None self.ble_adapter_name = ble_adapter_name self.controller = None self.mac_address = mac_address self.battery_level = None # memory map using mmap to store nuimo battery level fd = os.open('/tmp/' + self.mac_address.replace(':', '-'), os.O_CREAT | os.O_TRUNC | os.O_RDWR) assert os.write(fd, b'\x00' * mmap.PAGESIZE) == mmap.PAGESIZE buf = mmap.mmap(fd, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_WRITE) self.bl = ctypes.c_int.from_buffer(buf) self.bl.value = 0 offset = struct.calcsize(self.bl._type_) assert buf[offset] == 0
def __init__(self, transfer_size): fd, self.filename = tempfile.mkstemp() os.ftruncate(fd, 20) self.buf = mmap.mmap(fd, 20, mmap.MAP_SHARED, mmap.PROT_WRITE) os.close(fd) self.total_bytes = ctypes.c_uint64.from_buffer(self.buf) self.total_bytes.value = 0 self.average_time = ctypes.c_double.from_buffer(self.buf, 8) self.average_time.value = 0.0 self.transfer_size = ctypes.c_uint32.from_buffer(self.buf, 16) self.transfer_size.value = transfer_size
def __get_shm(self, type_id, slave_id): if slave_id in self.tmp_shm[type_id]: shm = self.tmp_shm[type_id][slave_id] else: shm_fd = os.open(self.files[type_id] + str(slave_id), os.O_RDWR | os.O_SYNC) shm = mmap.mmap(shm_fd, self.sizes[type_id]*self.tasks_per_requests, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ) self.tmp_shm[type_id][slave_id] = shm return shm
def open_global_bitmap(self): self.global_bitmap_fd = os.open(self.config.argument_values['work_dir'] + "/bitmap", os.O_RDWR | os.O_SYNC | os.O_CREAT) os.ftruncate(self.global_bitmap_fd, self.bitmap_size) self.global_bitmap = mmap.mmap(self.global_bitmap_fd, self.bitmap_size, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)
def resize(self, width, height): # Drop previous buffer and shm data if necessary if self.buffer: self.buffer.destroy() self.shm_data.close() wl_shm_format, cairo_shm_format = self._w.shm_formats[0] stride = cairo.ImageSurface.format_stride_for_width( cairo_shm_format, width) size = stride * height with AnonymousFile(size) as fd: self.shm_data = mmap.mmap( fd, size, prot=mmap.PROT_READ | mmap.PROT_WRITE, flags=mmap.MAP_SHARED) pool = self._w.shm.create_pool(fd, size) self.buffer = pool.create_buffer( 0, width, height, stride, wl_shm_format) pool.destroy() self.s = cairo.ImageSurface(cairo_shm_format, width, height, data=self.shm_data, stride=stride) self.surface.attach(self.buffer, 0, 0) self.width = width self.height = height if self.redraw_func: self.redraw_func(self) self.surface.commit()
def keyboard_keymap(self, keyboard, format_, fd, size): print("keyboard_keymap {} {} {}".format(format_, fd, size)) keymap_data = mmap.mmap( fd, 0, prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) os.close(fd) # The provided keymap appears to have a terminating NULL which # xkbcommon chokes on. Specify length=size-1 to remove it. keymap = self._c.xkb_context.keymap_new_from_buffer( keymap_data, length=size - 1) keymap_data.close() self.keyboard_state = keymap.state_new()
def __init__(self, knowledgebase): if _sanity_check: ExprGenerator.setup() State.ExtraInfoGen = ExprGenerator Perceptron.model = Model() Perceptron.indepKB = IndepKnowledgeBase() Perceptron.KB = knowledgebase knowledgebase.postedit_indepkb(Perceptron.indepKB) Perceptron.c = 0 Perceptron.iter = FLAGS.iter Perceptron.beamsize = FLAGS.beam Perceptron.parser = Parser(Perceptron.indepKB, Perceptron.KB, Perceptron.model, State) Perceptron.ncpus = FLAGS.ncpus Perceptron.ontheflyfd = FLAGS.ontheflyfd Perceptron.single_gold = FLAGS.singlegold Perceptron.output_prefix = FLAGS.outputprefix Perceptron.fdbeamsize = FLAGS.fdbeam if Perceptron.ncpus > 0: Perceptron.shared_memo_size = int(1024 * 1024 * 1024) # 1G shared memory Perceptron.shared_memo = mmap.mmap(-1, Perceptron.shared_memo_size, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE) Perceptron.ref_beams = {} if FLAGS.ref: print >> LOGS, "loading refs", hgs = pickle.load(open(FLAGS.ref)) self.load_hgs(hgs) if FLAGS.extraref: print >> LOGS, "loading extra refs", hgs = pickle.load(open(FLAGS.extraref)) self.load_hgs(hgs)
def __open_qqwry(self): """Open qqwry.dat. """ with open(self.path, "rb") as f: self.file_obj = mmap.mmap( f.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ)
def __init__(self, filename): """Read the given TrueType file. :Parameters: `filename` The name of any Windows, OS2 or Macintosh Truetype file. The object must be closed (see `close`) after use. An exception will be raised if the file does not exist or cannot be read. """ if not filename: filename = '' len = os.stat(filename).st_size self._fileno = os.open(filename, os.O_RDONLY) if hasattr(mmap, 'MAP_SHARED'): self._data = mmap.mmap(self._fileno, len, mmap.MAP_SHARED, mmap.PROT_READ) else: self._data = mmap.mmap(self._fileno, len, None, mmap.ACCESS_READ) offsets = _read_offset_table(self._data, 0) self._tables = {} for table in _read_table_directory_entry.array(self._data, offsets.size, offsets.num_tables): self._tables[table.tag] = table self._names = None self._horizontal_metrics = None self._character_advances = None self._character_kernings = None self._glyph_kernings = None self._character_map = None self._glyph_map = None self._font_selection_flags = None self.header = \ _read_head_table(self._data, self._tables['head'].offset) self.horizontal_header = \ _read_horizontal_header(self._data, self._tables['hhea'].offset)
def _restore_dict(self, path, read_only, cache_size): # ====== already exist ====== # if os.path.exists(path): if os.path.getsize(path) == 0: if read_only: raise Exception('File at path:"%s" has zero size, no data ' 'found in (read-only mode).' % path) file = open(str(path), mode='rb+') if file.read(len(MmapDict.HEADER)) != MmapDict.HEADER: raise Exception('Given file is not in the right format ' 'for MmapDict.') # 48 bytes for the file size max_position = int(file.read(MmapDict.SIZE_BYTES)) # length of pickled indices dictionary dict_size = int(file.read(MmapDict.SIZE_BYTES)) # read dictionary file.seek(max_position) pickled_indices = file.read(dict_size) self._indices_dict = async(lambda: cPickle.loads(pickled_indices))() # ====== create new file from scratch ====== # else: file = open(str(path), mode='wb+') file.write(MmapDict.HEADER) # just write the header header = ('%' + str(MmapDict.SIZE_BYTES) + 'd') % \ (len(MmapDict.HEADER) + MmapDict.SIZE_BYTES * 2) file.write(header.encode()) # write the length of Pickled indices dictionary data_size = ('%' + str(MmapDict.SIZE_BYTES) + 'd') % 0 file.write(data_size.encode()) file.flush() # init indices dict self._indices_dict = {} # ====== create Mmap from offset file ====== # self._file = file self._mmap = mmap.mmap(file.fileno(), length=0, offset=0, flags=mmap.MAP_SHARED) self._increased_indices_size = 0. # in MB # store all the (key, value) recently added self._cache_dict = {}
def create_function_from_shellcode(shell_code, restype=ctypes.c_int64, argtypes=()): mm = mmap.mmap(-1, len(shell_code), flags=mmap.MAP_SHARED | mmap.MAP_ANONYMOUS, prot=mmap.PROT_WRITE | mmap.PROT_READ | mmap.PROT_EXEC) mm.write(shell_code) ctypes_buffer = ctypes.c_int.from_buffer(mm) function = ctypes.CFUNCTYPE(restype, *argtypes)(ctypes.addressof(ctypes_buffer)) function._avoid_gc_for_mmap = mm return function
def from_ShellCode(cls, shell_code, restype=ctypes.c_int64, *argtypes): if not isinstance(shell_code, bytes): shell_code = bytes(shell_code, 'utf-8') mm = mmap.mmap(-1, len(shell_code), flags=mmap.MAP_SHARED | mmap.MAP_ANONYMOUS, prot=mmap.PROT_WRITE | mmap.PROT_READ | mmap.PROT_EXEC) mm.write(shell_code) return cls(mm, restype, *argtypes)
def __init__(self, phys_address, size=PAGE_SIZE): """ Create object which maps physical memory to Python's mmap object. :param phys_address: based address of physical memory """ self._size = size phys_address -= phys_address % PAGE_SIZE fd = self._open_dev("/dev/mem") self._memmap = mmap.mmap(fd, size, flags=mmap.MAP_SHARED, prot=mmap.PROT_READ | mmap.PROT_WRITE, offset=phys_address) self._close_dev(fd) atexit.register(self.cleanup)
def find_strs(self, file_path, start_offset): """ Load input file to shared memory, and execute string extractors Args: file_path: start_offset: Returns: """ self._populate_conf_dict(file_path, start_offset) self._populate_shm_objects_dict() if platform == 'win32': with open(self.conf_d['file_path'], 'rb') as reader: shm = mmap.mmap(reader.fileno(), 0, tagname=self.shm_objectes_d['tagname'], access=mmap.ACCESS_READ) else: with open(self.conf_d['file_path'], 'rb') as fd: self.shm_objectes_d['shm'] = mmap.mmap(fd.fileno(), 0, mmap.MAP_SHARED, access=mmap.ACCESS_READ) shm_reader_func = mem_reader.read_chunks_from_shm for (found_str, str_offset, str_encoding) in mem_ext.extract_strs( self.conf_d, self.shm_objectes_d, shm_reader_func): yield found_str, str_offset, str_encoding
def get_nuimo_battery_level(mac_address): # pragma: no cover, try: fd = os.open('/tmp/' + mac_address.replace(':', '-'), os.O_RDONLY) except FileNotFoundError: logger.error("No memory mapped file named %s, return None battery level", mac_address.replace(':', '-')) return None buf = mmap.mmap(fd, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_READ) result, = struct.unpack('i', buf[:4]) return result
def renderView(self): if self.img is None: ## make sure shm is large enough and get its address if self.width() == 0 or self.height() == 0: return size = self.width() * self.height() * 4 if size > self.shm.size(): if sys.platform.startswith('win'): ## windows says "WindowsError: [Error 87] the parameter is incorrect" if we try to resize the mmap self.shm.close() ## it also says (sometimes) 'access is denied' if we try to reuse the tag. self.shmtag = "pyqtgraph_shmem_" + ''.join([chr((random.getrandbits(20)%25) + 97) for i in range(20)]) self.shm = mmap.mmap(-1, size, self.shmtag) elif sys.platform == 'darwin': self.shm.close() self.shmFile.close() self.shmFile = tempfile.NamedTemporaryFile(prefix='pyqtgraph_shmem_') self.shmFile.write(b'\x00' * (size + 1)) self.shmFile.flush() self.shm = mmap.mmap(self.shmFile.fileno(), size, mmap.MAP_SHARED, mmap.PROT_WRITE) else: self.shm.resize(size) ## render the scene directly to shared memory if USE_PYSIDE: ch = ctypes.c_char.from_buffer(self.shm, 0) #ch = ctypes.c_char_p(address) self.img = QtGui.QImage(ch, self.width(), self.height(), QtGui.QImage.Format_ARGB32) else: address = ctypes.addressof(ctypes.c_char.from_buffer(self.shm, 0)) # different versions of pyqt have different requirements here.. try: self.img = QtGui.QImage(sip.voidptr(address), self.width(), self.height(), QtGui.QImage.Format_ARGB32) except TypeError: try: self.img = QtGui.QImage(memoryview(buffer(self.shm)), self.width(), self.height(), QtGui.QImage.Format_ARGB32) except TypeError: # Works on PyQt 4.9.6 self.img = QtGui.QImage(address, self.width(), self.height(), QtGui.QImage.Format_ARGB32) self.img.fill(0xffffffff) p = QtGui.QPainter(self.img) self.render(p, self.viewRect(), self.rect()) p.end() self.sceneRendered.emit((self.width(), self.height(), self.shm.size(), self.shmFileName()))
def _flush(self, save_all=False): """ Parameters ---------- save_indices: bool force the indices dictionary to be saved, even though, its increased hasn't reach the maximum. """ # check if closed or in read only mode if self.is_closed or self.read_only: return # ====== write new data ====== # # get old position file = self._file # start from header (i.e. "mmapdict") file.seek(len(MmapDict.HEADER)) max_position = int(file.read(MmapDict.SIZE_BYTES)) # ====== serialize the data ====== # # start from old_max_position, append new values file.seek(max_position) for key, value in self._cache_dict.items(): try: value = marshal.dumps(value) except ValueError: raise RuntimeError("Cannot marshal.dump %s" % str(value)) self.indices[key] = (max_position, len(value)) max_position += len(value) file.write(value) # increase indices size (in MegaBytes) self._increased_indices_size += (8 + 8 + len(key)) / 1024. / 1024. # ====== write the dumped indices ====== # indices_length = 0 if save_all or \ self._increased_indices_size > MmapDict.MAX_INDICES_SIZE: indices_dump = cPickle.dumps(self.indices, protocol=cPickle.HIGHEST_PROTOCOL) indices_length = len(indices_dump) file.write(indices_dump) self._increased_indices_size = 0. # ====== update the position ====== # # write new max size file.seek(len(MmapDict.HEADER)) max_position = ('%' + str(MmapDict.SIZE_BYTES) + 'd') % max_position file.write(max_position.encode()) # update length of pickled indices dictionary if indices_length > 0: indices_length = ('%' + str(MmapDict.SIZE_BYTES) + 'd') % indices_length file.write(indices_length.encode()) # flush everything file.flush() # upate the mmap self._mmap.close(); del self._mmap self._mmap = mmap.mmap(file.fileno(), length=0, offset=0, flags=mmap.MAP_SHARED) # reset some values del self._cache_dict self._cache_dict = {} # ==================== I/O methods ==================== #