def __init__(self, cfunc, callback): self.cfunc = cfunc self.callback = callback pointer = rffi.cast(rffi.VOIDP, clibffi.closureHeap.alloc()) if cfunc.notready: cfunc.prepare_cif() cfunc.notready = False Mem.__init__(self, cfunc, pointer) closure_ptr = rffi.cast(clibffi.FFI_CLOSUREP, self.pointer) # hide the object gcref = rgc.cast_instance_to_gcref(self) raw = rgc.hide_nonmovable_gcref(gcref) unique_id = rffi.cast(rffi.VOIDP, raw) res = clibffi.c_ffi_prep_closure(closure_ptr, cfunc.cif.cif, invoke_callback, unique_id) if rffi.cast(lltype.Signed, res) != clibffi.FFI_OK: raise unwind(LError(u"libffi failed to build this callback")) if closure_ptr.c_user_data != unique_id: raise unwind(LError(u"ffi_prep_closure(): bad user_data")) # The function might be called in separate thread, # so allocate GIL here, just in case. rgil.allocate()
def cast(obj, ctype): ctype = to_type(ctype) if isinstance(obj, Handle): return Handle(obj.library, obj.name, obj.pointer, ctype) if isinstance(obj, Mem): return Mem(ctype, obj.pointer) if isinstance(obj, Uint8Data): return Mem(ctype, rffi.cast(rffi.VOIDP, obj.uint8data)) if isinstance(obj, Integer) and isinstance(ctype, Pointer): return Mem(ctype, rffi.cast(rffi.VOIDP, obj.value)) raise unwind(LTypeError(u"Can cast memory locations only"))
def malloc(argv): ctype = argument(argv, 0, Type) if len(argv) >= 2: n = argument(argv, 1, Integer).value size = simple.sizeof_a(ctype, n) else: n = 1 size = simple.sizeof(ctype) pointer = lltype.malloc(rffi.VOIDP.TO, size, flavor='raw') return Mem(systemv.Pointer(ctype), pointer, n)
def malloc(ctype, count, clear): ctype = to_type(ctype) if count: n = count.value size = simple.sizeof_a(ctype, n) else: n = 1 size = simple.sizeof(ctype) pointer = lltype.malloc(rffi.VOIDP.TO, size, flavor='raw') if is_true(clear): rffi.c_memset(pointer, 0, size) return Mem(systemv.Pointer(ctype), pointer, n)
def __init__(self, library, name, pointer, ctype): Mem.__init__(self, ctype, pointer) self.library = library self.name = name
def cast(obj, ctype): if isinstance(obj, Handle): return Handle(obj.library, obj.name, obj.pointer, ctype) if isinstance(obj, Mem): return Mem(ctype, obj.pointer) raise Error(u"Can cast memory locations only")