def _new_value(type_): size = ctypes.sizeof(type_) # 对应c语言类型所需内存大小 wrapper = heap.BufferWrapper(size) buf = wrapper.create_memoryview() obj = type_.from_buffer(buf) obj._wrapper = wrapper return obj
def allocate_aligned_shared_mem_c_array( shape_or_count=(), elem_type=None, alignment=32, segment_count=cpu_count(), init=() ): shape_or_count = shape_or_count or shape(init) item_count = product(shape_or_count) item_type = dtype(elem_type or next(imap(type, flat(init)), elem_type)) item_size = item_type.item_size array_size = ((item_size * item_count) + (segment_count * alignment)) wrapper = heap.BufferWrapper(array_size) raw_array = sharedctypes.rebuild_ctype(ctypes.c_byte * array_size, wrapper, None) aligned_address = ctypes.addressof(raw_array) aligned_address += aligned_address % max(alignment, 1) flatten_values = (ctype(item_type) * product(shape(init))).from_address(aligned_address) if init: flatten_values[:] = tuple(flat(init)) # tuple(imap(ctype(item_type), flat(init))) return Array(raw_array, flatten_values, item_type, shape_or_count)
def _new_value(type_): size = ctypes.sizeof(type_) wrapper = heap.BufferWrapper(size) return rebuild_ctype(type_, wrapper, None)