def _import(cls): prefix = 'ffi_type_' for key, value in clibffi.__dict__.iteritems(): if key.startswith(prefix): name = key[len(prefix):] setattr(cls, name, value) cls.slong = clibffi.cast_type_to_ffitype(rffi.LONG) cls.ulong = clibffi.cast_type_to_ffitype(rffi.ULONG) cls.slonglong = clibffi.cast_type_to_ffitype(rffi.LONGLONG) cls.ulonglong = clibffi.cast_type_to_ffitype(rffi.ULONGLONG) cls.wchar_t = clibffi.cast_type_to_ffitype(lltype.UniChar) del cls._import
def _read(self, storage, width, i, offset): if we_are_translated(): res = libffi.array_getitem(clibffi.cast_type_to_ffitype(self.T), width, storage, i, offset) else: res = libffi.array_getitem_T(self.T, width, storage, i, offset) return byteswap(res)
def _write(self, storage, width, i, offset, value): #value = byteswap(value) XXX if we_are_translated(): libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T), width, storage, i, offset, value) else: libffi.array_setitem_T(self.T, width, storage, i, offset, value)
def define_callback_with_collect(cls): from pypy.rlib.clibffi import ffi_type_pointer, cast_type_to_ffitype,\ CDLL, ffi_type_void, CallbackFuncPtr, ffi_type_sint from pypy.rpython.lltypesystem import rffi, ll2ctypes import gc ffi_size_t = cast_type_to_ffitype(rffi.SIZE_T) from pypy.rlib.clibffi import get_libc_name def callback(ll_args, ll_res, stuff): gc.collect() p_a1 = rffi.cast(rffi.VOIDPP, ll_args[0])[0] p_a2 = rffi.cast(rffi.VOIDPP, ll_args[1])[0] a1 = rffi.cast(rffi.LONGP, p_a1)[0] a2 = rffi.cast(rffi.LONGP, p_a2)[0] res = rffi.cast(rffi.INTP, ll_res) if a1 > a2: res[0] = rffi.cast(rffi.INT, 1) else: res[0] = rffi.cast(rffi.INT, -1) def f(): libc = CDLL(get_libc_name()) qsort = libc.getpointer( 'qsort', [ffi_type_pointer, ffi_size_t, ffi_size_t, ffi_type_pointer], ffi_type_void) ptr = CallbackFuncPtr([ffi_type_pointer, ffi_type_pointer], ffi_type_sint, callback) TP = rffi.CArray(rffi.LONG) to_sort = lltype.malloc(TP, 4, flavor='raw') to_sort[0] = 4 to_sort[1] = 3 to_sort[2] = 1 to_sort[3] = 2 qsort.push_arg(rffi.cast(rffi.VOIDP, to_sort)) qsort.push_arg(rffi.cast(rffi.SIZE_T, 4)) qsort.push_arg(rffi.cast(rffi.SIZE_T, rffi.sizeof(rffi.LONG))) qsort.push_arg(rffi.cast(rffi.VOIDP, ptr.ll_closure)) qsort.call(lltype.Void) result = [to_sort[i] for i in range(4)] == [1, 2, 3, 4] lltype.free(to_sort, flavor='raw') keepalive_until_here(ptr) return int(result) return f
def define_callback_with_collect(cls): from pypy.rlib.clibffi import ffi_type_pointer, cast_type_to_ffitype,\ CDLL, ffi_type_void, CallbackFuncPtr, ffi_type_sint from pypy.rpython.lltypesystem import rffi, ll2ctypes import gc ffi_size_t = cast_type_to_ffitype(rffi.SIZE_T) from pypy.rlib.clibffi import get_libc_name def callback(ll_args, ll_res, stuff): gc.collect() p_a1 = rffi.cast(rffi.VOIDPP, ll_args[0])[0] p_a2 = rffi.cast(rffi.VOIDPP, ll_args[1])[0] a1 = rffi.cast(rffi.LONGP, p_a1)[0] a2 = rffi.cast(rffi.LONGP, p_a2)[0] res = rffi.cast(rffi.INTP, ll_res) if a1 > a2: res[0] = rffi.cast(rffi.INT, 1) else: res[0] = rffi.cast(rffi.INT, -1) def f(): libc = CDLL(get_libc_name()) qsort = libc.getpointer('qsort', [ffi_type_pointer, ffi_size_t, ffi_size_t, ffi_type_pointer], ffi_type_void) ptr = CallbackFuncPtr([ffi_type_pointer, ffi_type_pointer], ffi_type_sint, callback) TP = rffi.CArray(rffi.LONG) to_sort = lltype.malloc(TP, 4, flavor='raw') to_sort[0] = 4 to_sort[1] = 3 to_sort[2] = 1 to_sort[3] = 2 qsort.push_arg(rffi.cast(rffi.VOIDP, to_sort)) qsort.push_arg(rffi.cast(rffi.SIZE_T, 4)) qsort.push_arg(rffi.cast(rffi.SIZE_T, rffi.sizeof(rffi.LONG))) qsort.push_arg(rffi.cast(rffi.VOIDP, ptr.ll_closure)) qsort.call(lltype.Void) result = [to_sort[i] for i in range(4)] == [1,2,3,4] lltype.free(to_sort, flavor='raw') keepalive_until_here(ptr) return int(result) return f
def read(self, storage, width, i, offset): return self.box(libffi.array_getitem(clibffi.cast_type_to_ffitype(self.T), width, storage, i, offset ))
def _setup(): # compute alignment for tp in globals().values(): if isinstance(tp, type) and hasattr(tp, 'T'): tp.alignment = clibffi.cast_type_to_ffitype(tp.T).c_alignment
def store(self, storage, width, i, offset, box): value = self.unbox(box) libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T), width, storage, i, offset, value )
def fill(self, storage, width, box, start, stop, offset): value = self.unbox(box) for i in xrange(start, stop): libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T), width, storage, i, offset, value )
def read_bool(self, storage, width, i, offset): return bool(self.for_computation( libffi.array_getitem(clibffi.cast_type_to_ffitype(self.T), width, storage, i, offset)))
def store(self, storage, width, i, offset, box): value = self.unbox(box) libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T), width, storage, i, offset, value)
def fill(self, storage, width, box, start, stop, offset): value = self.unbox(box) for i in xrange(start, stop): libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T), width, storage, i, offset, value)
def read(self, storage, width, i, offset): return self.box( libffi.array_getitem(clibffi.cast_type_to_ffitype(self.T), width, storage, i, offset))