当前位置: 首页>>代码示例>>Python>>正文


Python mmap.MAP_PRIVATE属性代码示例

本文整理汇总了Python中mmap.MAP_PRIVATE属性的典型用法代码示例。如果您正苦于以下问题:Python mmap.MAP_PRIVATE属性的具体用法?Python mmap.MAP_PRIVATE怎么用?Python mmap.MAP_PRIVATE使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在mmap的用法示例。


在下文中一共展示了mmap.MAP_PRIVATE属性的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: mmap

# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import MAP_PRIVATE [as 别名]
def mmap(fd, offset, size):
    prot = MMAP.PROT_READ | MMAP.PROT_WRITE
    flags = MMAP.MAP_PRIVATE

    # When trying to map the contents of a file into memory, the offset must be a multiple of the page size (see
    # `man mmap`). So we need to align it before passing it to mmap(). Doing so also increases the size of the memory
    # area needed, so we need to account for that difference.
    aligned_offset = offset & ~0xFFF
    size += offset - aligned_offset

    if size & 0xFFF != 0:
        size = (size & ~0xFFF) + 0x1000
    assert size > 0

    result = mmap_function(0, size, prot, flags, fd, aligned_offset)
    assert result != ctypes.c_void_p(-1).value

    # Now when returning the pointer to the user, we need to skip the corrected offset so that the user doesn't end up
    # with a pointer to another region of the file than the one they requested.
    return ctypes.cast(result + offset - aligned_offset, ctypes.POINTER(ctypes.c_char)) 
开发者ID:trailofbits,项目名称:manticore,代码行数:22,代码来源:mappings.py

示例2: readelf

# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import MAP_PRIVATE [as 别名]
def readelf(path):
    elf = {
        'elf_header': {},
        'sections': [],
        'strtabs': {},
        'dynamic': [],
    }
    f = open(path, 'rb')
    buffer = mmap.mmap(f.fileno(), 0, mmap.MAP_PRIVATE, mmap.PROT_READ)
    read_header(elf, buffer)
    read_section_header(elf, buffer)
    read_strtab(elf, buffer)
    read_dynamic(elf, buffer)
    buffer.close()
    f.close()
    return elf 
开发者ID:mbachry,项目名称:exxo,代码行数:18,代码来源:_exxo_elf.py

示例3: _get_buf

# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import MAP_PRIVATE [as 别名]
def _get_buf(size):
    _buf = mmap.mmap(-1, size, flags=mmap.MAP_PRIVATE|mmap.MAP_ANONYMOUS,
                    prot=mmap.PROT_READ|mmap.PROT_WRITE )
    _buf.seek(0,2) # move seek pointer to end
    return _buf 
开发者ID:kdart,项目名称:pycopia,代码行数:7,代码来源:ringbuffer.py

示例4: get_buffer

# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import MAP_PRIVATE [as 别名]
def get_buffer(size):
    return mmap.mmap(-1, size, flags=mmap.MAP_PRIVATE|mmap.MAP_ANONYMOUS, 
                    prot=mmap.PROT_READ|mmap.PROT_WRITE )

# Also emulate a StringIO object. 
开发者ID:kdart,项目名称:pycopia,代码行数:7,代码来源:charbuffer.py

示例5: mmap_read

# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import MAP_PRIVATE [as 别名]
def mmap_read(f, sz = 0, close=True):
    """Create a read-only memory mapped region on file 'f'.
    If sz is 0, the region will cover the entire file.
    """
    return _mmap_do(f, sz, mmap.MAP_PRIVATE, mmap.PROT_READ, close) 
开发者ID:omererdem,项目名称:honeything,代码行数:7,代码来源:helpers.py

示例6: mmap_readwrite_private

# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import MAP_PRIVATE [as 别名]
def mmap_readwrite_private(f, sz = 0, close=True):
    """Create a read-write memory mapped region on file 'f'.
    If sz is 0, the region will cover the entire file.
    The map is private, which means the changes are never flushed back to the
    file.
    """
    return _mmap_do(f, sz, mmap.MAP_PRIVATE, mmap.PROT_READ|mmap.PROT_WRITE,
                    close) 
开发者ID:omererdem,项目名称:honeything,代码行数:10,代码来源:helpers.py

示例7: open

# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import MAP_PRIVATE [as 别名]
def open(self, mode, typeaccess=0, mmapmode = mmap.MAP_PRIVATE) :
		self.typeaccess = typeaccess
		try :
			self.__fd = open("/dev/mem", mode)
		except IOError :
			print "Mem::open IOErreur"
			sys.exit(-1)
		
		if(self.typeaccess != 0):
			try :
				self.__data = mmap.mmap(self.__fd.fileno(), 100 * 1024 * 1024, mmapmode)
				#print self.__data
			except TypeError :
				print "Mem::open TypeError"
				sys.exit(-1) 
开发者ID:tuwid,项目名称:darkc0de-old-stuff,代码行数:17,代码来源:mem.py

示例8: __init__

# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import MAP_PRIVATE [as 别名]
def __init__(self, file, prune, num_docs, vocab_size, in_memory, gpu):
        self.file = file = open(file, 'rb')
        mmp = mmap.mmap(file.fileno(), 0, flags=mmap.MAP_PRIVATE, prot=mmap.PROT_READ)
        if in_memory:
            # file will be read in in full sequentially
            os.posix_fadvise(file.fileno(), 0, 0, os.POSIX_FADV_SEQUENTIAL)
        else:
            # file will be read randomly as needed
            os.posix_fadvise(file.fileno(), 0, 0, os.POSIX_FADV_RANDOM)
        if prune != 0:
            S_DTYPE = 2
            if in_memory:
                mmp = np.empty((num_docs, prune), dtype='i2'), np.empty((num_docs, prune), dtype='f2')
                for did in logger.pbar(range(num_docs), desc='loading dvecs'):
                    try:
                        mmp[0][did] = np.frombuffer(file.read(prune*S_DTYPE), dtype='i2')
                        mmp[1][did] = np.frombuffer(file.read(prune*S_DTYPE), dtype='f2')
                    except ValueError:
                        pass
                file.close()
            self.lookup = self.dvec_lookup_pruned
        else:
            if in_memory:
                mmp = file.read()
                file.close()
            self.lookup = self.dvec_lookup_unpruned

        self.prune = prune
        self.num_docs = num_docs
        self.vocab_size = vocab_size
        self.mmp = mmp
        self.gpu = gpu
        self.in_memory = in_memory 
开发者ID:Georgetown-IR-Lab,项目名称:OpenNIR,代码行数:35,代码来源:epic_predict.py

示例9: compile

# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import MAP_PRIVATE [as 别名]
def compile(self):
		import ctypes

		machine_code = bytes.join(b'', self.machine_code)
		self.size = ctypes.c_size_t(len(machine_code))

		if is_windows:
			# Allocate a memory segment the size of the machine code, and make it executable
			size = len(machine_code)
			# Alloc at least 1 page to ensure we own all pages that we want to change protection on
			if size < 0x1000: size = 0x1000
			MEM_COMMIT = ctypes.c_ulong(0x1000)
			PAGE_READWRITE = ctypes.c_ulong(0x4)
			pfnVirtualAlloc = ctypes.windll.kernel32.VirtualAlloc
			pfnVirtualAlloc.restype = ctypes.c_void_p
			self.address = pfnVirtualAlloc(None, ctypes.c_size_t(size), MEM_COMMIT, PAGE_READWRITE)
			if not self.address:
				raise Exception("Failed to VirtualAlloc")

			# Copy the machine code into the memory segment
			memmove = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t)(ctypes._memmove_addr)
			if memmove(self.address, machine_code, size) < 0:
				raise Exception("Failed to memmove")

			# Enable execute permissions
			PAGE_EXECUTE = ctypes.c_ulong(0x10)
			old_protect = ctypes.c_ulong(0)
			pfnVirtualProtect = ctypes.windll.kernel32.VirtualProtect
			res = pfnVirtualProtect(ctypes.c_void_p(self.address), ctypes.c_size_t(size), PAGE_EXECUTE, ctypes.byref(old_protect))
			if not res:
				raise Exception("Failed VirtualProtect")

			# Flush Instruction Cache
			# First, get process Handle
			if not self.prochandle:
				pfnGetCurrentProcess = ctypes.windll.kernel32.GetCurrentProcess
				pfnGetCurrentProcess.restype = ctypes.c_void_p
				self.prochandle = ctypes.c_void_p(pfnGetCurrentProcess())
			# Actually flush cache
			res = ctypes.windll.kernel32.FlushInstructionCache(self.prochandle, ctypes.c_void_p(self.address), ctypes.c_size_t(size))
			if not res:
				raise Exception("Failed FlushInstructionCache")
		else:
			from mmap import mmap, MAP_PRIVATE, MAP_ANONYMOUS, PROT_WRITE, PROT_READ, PROT_EXEC

			# Allocate a private and executable memory segment the size of the machine code
			machine_code = bytes.join(b'', self.machine_code)
			self.size = len(machine_code)
			self.mm = mmap(-1, self.size, flags=MAP_PRIVATE | MAP_ANONYMOUS, prot=PROT_WRITE | PROT_READ | PROT_EXEC)

			# Copy the machine code into the memory segment
			self.mm.write(machine_code)
			self.address = ctypes.addressof(ctypes.c_int.from_buffer(self.mm))

		# Cast the memory segment into a function
		functype = ctypes.CFUNCTYPE(self.restype, *self.argtypes)
		self.func = functype(self.address) 
开发者ID:workhorsy,项目名称:py-cpuinfo,代码行数:59,代码来源:get_system_info.py

示例10: compile

# 需要导入模块: import mmap [as 别名]
# 或者: from mmap import MAP_PRIVATE [as 别名]
def compile(self):
		machine_code = bytes.join(b'', self.machine_code)
		self.size = ctypes.c_size_t(len(machine_code))

		if DataSource.is_windows:
			# Allocate a memory segment the size of the machine code, and make it executable
			size = len(machine_code)
			# Alloc at least 1 page to ensure we own all pages that we want to change protection on
			if size < 0x1000: size = 0x1000
			MEM_COMMIT = ctypes.c_ulong(0x1000)
			PAGE_READWRITE = ctypes.c_ulong(0x4)
			pfnVirtualAlloc = ctypes.windll.kernel32.VirtualAlloc
			pfnVirtualAlloc.restype = ctypes.c_void_p
			self.address = pfnVirtualAlloc(None, ctypes.c_size_t(size), MEM_COMMIT, PAGE_READWRITE)
			if not self.address:
				raise Exception("Failed to VirtualAlloc")

			# Copy the machine code into the memory segment
			memmove = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t)(ctypes._memmove_addr)
			if memmove(self.address, machine_code, size) < 0:
				raise Exception("Failed to memmove")

			# Enable execute permissions
			PAGE_EXECUTE = ctypes.c_ulong(0x10)
			old_protect = ctypes.c_ulong(0)
			pfnVirtualProtect = ctypes.windll.kernel32.VirtualProtect
			res = pfnVirtualProtect(ctypes.c_void_p(self.address), ctypes.c_size_t(size), PAGE_EXECUTE, ctypes.byref(old_protect))
			if not res:
				raise Exception("Failed VirtualProtect")

			# Flush Instruction Cache
			# First, get process Handle
			if not self.prochandle:
				pfnGetCurrentProcess = ctypes.windll.kernel32.GetCurrentProcess
				pfnGetCurrentProcess.restype = ctypes.c_void_p
				self.prochandle = ctypes.c_void_p(pfnGetCurrentProcess())
			# Actually flush cache
			res = ctypes.windll.kernel32.FlushInstructionCache(self.prochandle, ctypes.c_void_p(self.address), ctypes.c_size_t(size))
			if not res:
				raise Exception("Failed FlushInstructionCache")
		else:
			from mmap import mmap, MAP_PRIVATE, MAP_ANONYMOUS, PROT_WRITE, PROT_READ, PROT_EXEC

			# Allocate a private and executable memory segment the size of the machine code
			machine_code = bytes.join(b'', self.machine_code)
			self.size = len(machine_code)
			self.mm = mmap(-1, self.size, flags=MAP_PRIVATE | MAP_ANONYMOUS, prot=PROT_WRITE | PROT_READ | PROT_EXEC)

			# Copy the machine code into the memory segment
			self.mm.write(machine_code)
			self.address = ctypes.addressof(ctypes.c_int.from_buffer(self.mm))

		# Cast the memory segment into a function
		functype = ctypes.CFUNCTYPE(self.restype, *self.argtypes)
		self.func = functype(self.address) 
开发者ID:workhorsy,项目名称:py-cpuinfo,代码行数:57,代码来源:cpuinfo.py


注:本文中的mmap.MAP_PRIVATE属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。