def test_fakeadr_eq(): S = lltype.GcStruct("S", ("x", lltype.Signed), ("y", lltype.Signed)) s = lltype.malloc(S) assert cast_ptr_to_adr(s) == cast_ptr_to_adr(s) adr1 = cast_ptr_to_adr(s) + FieldOffset(S, "x") adr2 = cast_ptr_to_adr(s) + FieldOffset(S, "y") adr3 = cast_ptr_to_adr(s) + FieldOffset(S, "y") assert adr1 != adr2 assert adr2 == adr3 A = lltype.GcArray(lltype.Char) a = lltype.malloc(A, 5) adr1 = cast_ptr_to_adr(a) + ArrayLengthOffset(A) adr2 = cast_ptr_to_adr(a) + ArrayLengthOffset(A) assert adr1 == adr2 adr1 = cast_ptr_to_adr(a) + ArrayItemsOffset(A) adr2 = cast_ptr_to_adr(a) + ArrayItemsOffset(A) assert adr1 == adr2 adr2 += ItemOffset(lltype.Char, 0) assert adr1 == adr2 adr1 += ItemOffset(lltype.Char, 2) adr2 += ItemOffset(lltype.Char, 3) assert adr1 != adr2 adr2 += ItemOffset(lltype.Char, -1) assert adr1 == adr2
def xxx_test_later_along_link(): S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True}) s1 = lltype.malloc(S1) s1.x = 123 s2 = lltype.malloc(S1) s2.x = 60 def fn(x, y): if x: x = s1.x else: x = s2.x y *= 2 return (x+1) - y graph, t = get_graph(fn, [int, int]) assert summary(graph) == {'int_is_true': 1, 'getfield': 2, 'int_mul': 1, 'int_add': 1, 'int_sub': 1} constant_fold_graph(graph) assert summary(graph) == {'int_is_true': 1, 'int_mul': 1, 'int_sub': 1} check_graph(graph, [-1], 124, t) check_graph(graph, [0], 61, t)
def do_poll(self, space, timeout): from pypy.module._multiprocessing.interp_win32 import ( _PeekNamedPipe, _GetTickCount, _Sleep) from rpython.rlib import rwin32 from pypy.interpreter.error import wrap_windowserror bytes_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor='raw') try: if not _PeekNamedPipe(self.handle, rffi.NULL, 0, lltype.nullptr(rwin32.LPDWORD.TO), bytes_ptr, lltype.nullptr(rwin32.LPDWORD.TO)): raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) bytes = bytes_ptr[0] finally: lltype.free(bytes_ptr, flavor='raw') if timeout == 0.0: return bytes > 0 block = timeout < 0 if not block: # XXX does not check for overflow deadline = intmask(_GetTickCount()) + int(1000 * timeout + 0.5) else: deadline = 0 _Sleep(0) delay = 1 while True: bytes_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor='raw') try: if not _PeekNamedPipe(self.handle, rffi.NULL, 0, lltype.nullptr(rwin32.LPDWORD.TO), bytes_ptr, lltype.nullptr(rwin32.LPDWORD.TO)): raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) bytes = bytes_ptr[0] finally: lltype.free(bytes_ptr, flavor='raw') if bytes > 0: return True if not block: now = intmask(_GetTickCount()) if now > deadline: return False diff = deadline - now if delay > diff: delay = diff else: delay += 1 if delay >= 20: delay = 20 _Sleep(delay)
def time_time_llimpl(): void = lltype.nullptr(rffi.VOIDP.TO) result = -1.0 if self.HAVE_GETTIMEOFDAY: t = lltype.malloc(self.TIMEVAL, flavor='raw') errcode = -1 if self.GETTIMEOFDAY_NO_TZ: errcode = c_gettimeofday(t) else: errcode = c_gettimeofday(t, void) if rffi.cast(rffi.LONG, errcode) == 0: result = decode_timeval(t) lltype.free(t, flavor='raw') if result != -1: return result else: # assume using ftime(3) t = lltype.malloc(self.TIMEB, flavor='raw') c_ftime(t) result = (float(intmask(t.c_time)) + float(intmask(t.c_millitm)) * 0.001) lltype.free(t, flavor='raw') return result return float(c_time(void))
def f(): t.p = lltype.malloc(S) t.p.x = 43 for i in range(2500000): s = lltype.malloc(S) s.x = i return t.p.x
def test_multiple_incoming_links(): S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True}) s1 = lltype.malloc(S1) s1.x = 123 s2 = lltype.malloc(S1) s2.x = 60 s3 = lltype.malloc(S1) s3.x = 15 def fn(x): y = x * 10 if x == 1: x = s1.x elif x == 2: x = s2.x elif x == 3: x = s3.x y = s1.x return (x+1) + y graph, t = get_graph(fn, [int]) constant_fold_graph(graph) assert summary(graph) == {'int_mul': 1, 'int_eq': 3, 'int_add': 2} for link in graph.iterlinks(): if Constant(139) in link.args: break else: raise AssertionError("139 not found in the graph as a constant") for i in range(4): check_graph(graph, [i], fn(i), t)
def test_guard(self): ops = ''' [i0, p0, i1, p1] p3 = getfield_gc(p0, descr=fielddescr) guard_true(i0) [p0, i1, p1, p3] ''' s1 = lltype.malloc(self.S) s2 = lltype.malloc(self.S) s1.field = s2 self.interpret(ops, [0, s1, 1, s2]) frame = lltype.cast_opaque_ptr(jitframe.JITFRAMEPTR, self.deadframe) # p0 and p3 should be in registers, p1 not so much assert self.getptr(0, lltype.Ptr(self.S)) == s1 # the gcmap should contain three things, p0, p1 and p3 # p3 stays in a register # while p0 and p1 are on the frame b = getmap(frame) nos = [len(b) - 1 - i.start() for i in re.finditer('1', b)] nos.reverse() if self.cpu.backend_name.startswith('x86'): if self.cpu.IS_64_BIT: assert nos == [0, 1, 31] else: assert nos == [0, 1, 25] elif self.cpu.backend_name.startswith('arm'): assert nos == [0, 1, 47] else: raise Exception("write the data here") assert frame.jf_frame[nos[0]] assert frame.jf_frame[nos[1]] assert frame.jf_frame[nos[2]]
def _(obj): if isinstance(obj, space.Integer): return Uint8Array(lltype.malloc(rffi.UCHARP.TO, obj.value, flavor='raw'), obj.value) if isinstance(obj, space.List): length = len(obj.contents) array = Uint8Array(lltype.malloc(rffi.UCHARP.TO, length, flavor='raw'), length) for i in range(0, length): x = obj.contents[i] if isinstance(x, space.Integer): array.uint8data[i] = rffi.r_uchar(x.value) else: raise space.OldError(u"Value of incorrect type: " + x.repr()) return array it = obj.iter() out = [] try: while True: x = it.callattr(u"next", []) if isinstance(x, space.Integer): out.append(rffi.r_uchar(x.value)) else: raise space.OldError(u"Value of incorrect type: " + x.repr()) except StopIteration as stop: pass length = len(out) uint8data = lltype.malloc(rffi.UCHARP.TO, length, flavor='raw') for i in range(0, length): uint8data[i] = out[i] return Uint8Array(uint8data, length)
def detect_floatformat(): from rpython.rtyper.lltypesystem import rffi, lltype buf = lltype.malloc(rffi.CCHARP.TO, 8, flavor='raw') rffi.cast(rffi.DOUBLEP, buf)[0] = 9006104071832581.0 packed = rffi.charpsize2str(buf, 8) if packed == "\x43\x3f\xff\x01\x02\x03\x04\x05": double_format = 'IEEE, big-endian' elif packed == "\x05\x04\x03\x02\x01\xff\x3f\x43": double_format = 'IEEE, little-endian' else: double_format = 'unknown' lltype.free(buf, flavor='raw') # buf = lltype.malloc(rffi.CCHARP.TO, 4, flavor='raw') rffi.cast(rffi.FLOATP, buf)[0] = rarithmetic.r_singlefloat(16711938.0) packed = rffi.charpsize2str(buf, 4) if packed == "\x4b\x7f\x01\x02": float_format = 'IEEE, big-endian' elif packed == "\x02\x01\x7f\x4b": float_format = 'IEEE, little-endian' else: float_format = 'unknown' lltype.free(buf, flavor='raw') return double_format, float_format
def strxfrm(space, s): "string -> string. Returns a string that behaves for cmp locale-aware." n1 = len(s) + 1 buf = lltype.malloc(rffi.CCHARP.TO, n1, flavor="raw", zero=True) s_c = rffi.str2charp(s) try: n2 = _strxfrm(buf, s_c, n1) + 1 finally: rffi.free_charp(s_c) if n2 > n1: # more space needed lltype.free(buf, flavor="raw") buf = lltype.malloc(rffi.CCHARP.TO, intmask(n2), flavor="raw", zero=True) s_c = rffi.str2charp(s) try: _strxfrm(buf, s_c, n2) finally: rffi.free_charp(s_c) val = rffi.charp2str(buf) lltype.free(buf, flavor="raw") return space.wrap(val)
def test_getfield_pure(): S1 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed)) S2 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed), hints={'immutable': True}) accessor = rclass.FieldListAccessor() # s1 = lltype.malloc(S1); s1.x = 45 py.test.raises(TypeError, llop.getfield, lltype.Signed, s1, 'x') s2 = lltype.malloc(S2); s2.x = 45 assert llop.getfield(lltype.Signed, s2, 'x') == 45 # py.test.raises(TypeError, llop.getinteriorfield, lltype.Signed, s1, 'x') assert llop.getinteriorfield(lltype.Signed, s2, 'x') == 45 # for kind in [rclass.IR_MUTABLE, rclass.IR_IMMUTABLE, rclass.IR_IMMUTABLE_ARRAY, rclass.IR_QUASIIMMUTABLE, rclass.IR_QUASIIMMUTABLE_ARRAY]: # S3 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed), hints={'immutable_fields': accessor}) accessor.initialize(S3, {'x': kind}) s3 = lltype.malloc(S3); s3.x = 46; s3.y = 47 if kind in [rclass.IR_IMMUTABLE, rclass.IR_IMMUTABLE_ARRAY]: assert llop.getfield(lltype.Signed, s3, 'x') == 46 assert llop.getinteriorfield(lltype.Signed, s3, 'x') == 46 else: py.test.raises(TypeError, llop.getfield, lltype.Signed, s3, 'x') py.test.raises(TypeError, llop.getinteriorfield, lltype.Signed, s3, 'x') py.test.raises(TypeError, llop.getfield, lltype.Signed, s3, 'y') py.test.raises(TypeError, llop.getinteriorfield, lltype.Signed, s3, 'y')
def get_type_id(self, TYPE): try: return self.id_of_type[TYPE] except KeyError: assert self.can_add_new_types assert isinstance(TYPE, (lltype.GcStruct, lltype.GcArray)) # Record the new type_id description as a TYPE_INFO structure. # build the TYPE_INFO structure if not TYPE._is_varsize(): fullinfo = lltype.malloc(GCData.TYPE_INFO, immortal=True, zero=True) info = fullinfo else: fullinfo = lltype.malloc(GCData.VARSIZE_TYPE_INFO, immortal=True, zero=True) info = fullinfo.header type_id = self.type_info_group.add_member(fullinfo) if self.can_encode_type_shape: encode_type_shape(self, info, TYPE, type_id.index) else: self._pending_type_shapes.append((info, TYPE, type_id.index)) # store it self.id_of_type[TYPE] = type_id self.add_vtable_after_typeinfo(TYPE) return type_id
def f(d): va = lltype.malloc(T, d, flavor='raw', zero=True) vb = lltype.malloc(T, d, flavor='raw', zero=True) for j in range(d): va[j] = rffi.r_int(j) vb[j] = rffi.r_int(j) i = 0 while i < d: myjitdriver.jit_merge_point() if i < 0: raise IndexError if i >= d: raise IndexError a = va[i] if i < 0: raise IndexError if i >= d: raise IndexError b = vb[i] ec = intmask(a)+intmask(b) if i < 0: raise IndexError if i >= d: raise IndexError va[i] = rffi.r_int(ec) i += 1 lltype.free(va, flavor='raw') lltype.free(vb, flavor='raw') return 0
def __init__(self, name, argtypes, restype, flags=FUNCFLAG_CDECL): self.name = name self.argtypes = argtypes self.restype = restype self.flags = flags argnum = len(argtypes) self.ll_argtypes = lltype.malloc(FFI_TYPE_PP.TO, argnum, flavor='raw', track_allocation=False) # freed by the __del__ for i in range(argnum): self.ll_argtypes[i] = argtypes[i] self.ll_cif = lltype.malloc(FFI_CIFP.TO, flavor='raw', track_allocation=False) # freed by the __del__ if _MSVC: # This little trick works correctly with MSVC. # It returns small structures in registers if intmask(restype.c_type) == FFI_TYPE_STRUCT: if restype.c_size <= 4: restype = ffi_type_sint32 elif restype.c_size <= 8: restype = ffi_type_sint64 res = c_ffi_prep_cif(self.ll_cif, rffi.cast(rffi.USHORT, get_call_conv(flags,False)), rffi.cast(rffi.UINT, argnum), restype, self.ll_argtypes) if not res == FFI_OK: raise LibFFIError
def f(n): xy2 = self.setup2() xy2.inst_x = 10 xy2.inst_l1 = lltype.malloc(ARRAY, 1) xy2.inst_l1[0] = 1982731 xy2.inst_l2 = lltype.malloc(ARRAY, 1) xy2.inst_l2[0] = 10000 other = self.setup2() other.inst_x = 15 other.inst_l1 = lltype.malloc(ARRAY, 2) other.inst_l1[0] = 189182 other.inst_l1[1] = 58421 other.inst_l2 = lltype.malloc(ARRAY, 2) other.inst_l2[0] = 181 other.inst_l2[1] = 189 while n > 0: myjitdriver.can_enter_jit(xy2=xy2, n=n, other=other) myjitdriver.jit_merge_point(xy2=xy2, n=n, other=other) promote_virtualizable(other, 'inst_l2') length = len(other.inst_l2) # getfield_gc/arraylen_gc value = other.inst_l2[0] # getfield_gc/getarrayitem_gc other.inst_l2[0] = value + length # getfield_gc/setarrayitem_gc promote_virtualizable(xy2, 'inst_l2') xy2.inst_l2[0] = value + 100 # virtualized away n -= 1 promote_virtualizable(xy2, 'inst_l2') return xy2.inst_l2[0]
def test_iterkeys(self, space, api): w_dict = space.sys.getdict(space) py_dict = make_ref(space, w_dict) ppos = lltype.malloc(Py_ssize_tP.TO, 1, flavor='raw') pkey = lltype.malloc(PyObjectP.TO, 1, flavor='raw') pvalue = lltype.malloc(PyObjectP.TO, 1, flavor='raw') keys_w = [] values_w = [] try: ppos[0] = 0 while api.PyDict_Next(w_dict, ppos, pkey, None): w_key = from_ref(space, pkey[0]) keys_w.append(w_key) ppos[0] = 0 while api.PyDict_Next(w_dict, ppos, None, pvalue): w_value = from_ref(space, pvalue[0]) values_w.append(w_value) finally: lltype.free(ppos, flavor='raw') lltype.free(pkey, flavor='raw') lltype.free(pvalue, flavor='raw') api.Py_DecRef(py_dict) # release borrowed references assert space.eq_w(space.newlist(keys_w), space.call_method(w_dict, "keys")) assert space.eq_w(space.newlist(values_w), space.call_method(w_dict, "values"))
def test_prebuilt_list_of_addresses(self): from rpython.rtyper.lltypesystem import llmemory TP = lltype.Struct('x', ('y', lltype.Signed)) a = lltype.malloc(TP, flavor='raw', immortal=True) b = lltype.malloc(TP, flavor='raw', immortal=True) c = lltype.malloc(TP, flavor='raw', immortal=True) a_a = llmemory.cast_ptr_to_adr(a) a0 = llmemory.cast_ptr_to_adr(a) assert a_a is not a0 assert a_a == a0 a_b = llmemory.cast_ptr_to_adr(b) a_c = llmemory.cast_ptr_to_adr(c) d = {a_a: 3, a_b: 4, a_c: 5} d[a0] = 8 def func(i): if i == 0: ptr = a else: ptr = b return d[llmemory.cast_ptr_to_adr(ptr)] py.test.raises(TypeError, self.interpret, func, [0])
def walk_stack_from(self): curframe = lltype.malloc(WALKFRAME, flavor='raw') otherframe = lltype.malloc(WALKFRAME, flavor='raw') # Walk over all the pieces of stack. They are in a circular linked # list of structures of 7 words, the 2 first words being prev/next. # The anchor of this linked list is: anchor = llmemory.cast_ptr_to_adr(gcrootanchor) initialframedata = anchor.address[1] stackscount = 0 while initialframedata != anchor: # while we have not looped back self.fill_initial_frame(curframe, initialframedata) # Loop over all the frames in the stack while self.walk_to_parent_frame(curframe, otherframe): swap = curframe curframe = otherframe # caller becomes callee otherframe = swap # Then proceed to the next piece of stack initialframedata = initialframedata.address[1] stackscount += 1 # expected = rffi.stackcounter.stacks_counter if NonConstant(0): rffi.stackcounter.stacks_counter += 42 # hack to force it ll_assert(not (stackscount < expected), "non-closed stacks around") ll_assert(not (stackscount > expected), "stacks counter corruption?") lltype.free(otherframe, flavor='raw') lltype.free(curframe, flavor='raw')
def walk_stack_from(self): curframe = lltype.malloc(WALKFRAME, flavor='raw') otherframe = lltype.malloc(WALKFRAME, flavor='raw') # Walk over all the pieces of stack. They are in a circular linked # list of structures of 7 words, the 2 first words being prev/next. # The anchor of this linked list is: anchor = llmemory.cast_ptr_to_adr(gcrootanchor) initialframedata = anchor.address[1] stackscount = 0 while initialframedata != anchor: # while we have not looped back self.walk_frames(curframe, otherframe, initialframedata) # Then proceed to the next piece of stack initialframedata = initialframedata.address[1] stackscount += 1 # # for the JIT: rpy_fastgil may contain an extra framedata rpy_fastgil = rgil.gil_fetch_fastgil().signed[0] if rpy_fastgil != 1: ll_assert(rpy_fastgil != 0, "walk_stack_from doesn't have the GIL") initialframedata = rffi.cast(llmemory.Address, rpy_fastgil) self.walk_frames(curframe, otherframe, initialframedata) stackscount += 1 # expected = rffi.stackcounter.stacks_counter if NonConstant(0): rffi.stackcounter.stacks_counter += 42 # hack to force it ll_assert(not (stackscount < expected), "non-closed stacks around") ll_assert(not (stackscount > expected), "stacks counter corruption?") lltype.free(otherframe, flavor='raw') lltype.free(curframe, flavor='raw')
def f(n): while n > 0: myjitdriver.can_enter_jit(n=n) myjitdriver.jit_merge_point(n=n) xy = XY() xy.next1 = lltype.malloc(A, 0) xy.next2 = lltype.malloc(A, 0) xy.next3 = lltype.malloc(A, 0) xy.next4 = lltype.malloc(A, 0) xy.next5 = lltype.malloc(A, 0) xy.n = n exctx.topframeref = vref = virtual_ref(xy) if n % 6 == 0: xy.next1 = lltype.nullptr(A) xy.next2 = lltype.nullptr(A) xy.next3 = lltype.nullptr(A) externalfn(n) n -= 1 exctx.topframeref = vref_None xy.next1 = lltype.nullptr(A) xy.next2 = lltype.nullptr(A) xy.next3 = lltype.nullptr(A) xy.next4 = lltype.nullptr(A) xy.next5 = lltype.nullptr(A) virtual_ref_finish(vref, xy) return exctx.m
def setlen(self, size, zero=False, overallocate=True): if size > 0: if size > self.allocated or size < self.allocated / 2: if overallocate: if size < 9: some = 3 else: some = 6 some += size >> 3 else: some = 0 self.allocated = size + some if zero: new_buffer = lltype.malloc( mytype.arraytype, self.allocated, flavor='raw', add_memory_pressure=True, zero=True) else: new_buffer = lltype.malloc( mytype.arraytype, self.allocated, flavor='raw', add_memory_pressure=True) for i in range(min(size, self.len)): new_buffer[i] = self.buffer[i] else: self.len = size return else: assert size == 0 self.allocated = 0 new_buffer = lltype.nullptr(mytype.arraytype) if self.buffer: lltype.free(self.buffer, flavor='raw') self.buffer = new_buffer self.len = size
def rawallocate(self, ctypefunc): space = ctypefunc.space self.space = space # compute the total size needed in the CIF_DESCRIPTION buffer self.nb_bytes = 0 self.bufferp = lltype.nullptr(rffi.CCHARP.TO) self.fb_build() # allocate the buffer if we_are_translated(): rawmem = lltype.malloc(rffi.CCHARP.TO, self.nb_bytes, flavor="raw") rawmem = rffi.cast(CIF_DESCRIPTION_P, rawmem) else: # gross overestimation of the length below, but too bad rawmem = lltype.malloc(CIF_DESCRIPTION_P.TO, self.nb_bytes, flavor="raw") # the buffer is automatically managed from the W_CTypeFunc instance ctypefunc.cif_descr = rawmem # call again fb_build() to really build the libffi data structures self.bufferp = rffi.cast(rffi.CCHARP, rawmem) self.fb_build() assert self.bufferp == rffi.ptradd(rffi.cast(rffi.CCHARP, rawmem), self.nb_bytes) # fill in the 'exchange_*' fields self.fb_build_exchange(rawmem) # fill in the extra fields self.fb_extra_fields(rawmem) # call libffi's ffi_prep_cif() function res = jit_libffi.jit_ffi_prep_cif(rawmem) if res != clibffi.FFI_OK: raise OperationError(space.w_SystemError, space.wrap("libffi failed to build this function type"))
def getdefaultlocale(): encoding = "cp%d" % GetACP() BUFSIZE = 50 buf_lang = lltype.malloc(rffi.CCHARP.TO, BUFSIZE, flavor='raw') buf_country = lltype.malloc(rffi.CCHARP.TO, BUFSIZE, flavor='raw') try: if (GetLocaleInfo(cConfig.LOCALE_USER_DEFAULT, cConfig.LOCALE_SISO639LANGNAME, buf_lang, BUFSIZE) and GetLocaleInfo(cConfig.LOCALE_USER_DEFAULT, cConfig.LOCALE_SISO3166CTRYNAME, buf_country, BUFSIZE)): lang = rffi.charp2str(buf_lang) country = rffi.charp2str(buf_country) language = "%s_%s" % (lang, country) # If we end up here, this windows version didn't know about # ISO639/ISO3166 names (it's probably Windows 95). Return the # Windows language identifier instead (a hexadecimal number) elif GetLocaleInfo(cConfig.LOCALE_USER_DEFAULT, cConfig.LOCALE_IDEFAULTLANGUAGE, buf_lang, BUFSIZE): lang = rffi.charp2str(buf_lang) language = "0x%s" % (lang,) else: language = None finally: lltype.free(buf_lang, flavor='raw') lltype.free(buf_country, flavor='raw') return language, encoding
def test_jit_ffi_call(): cd = lltype.malloc(CIF_DESCRIPTION, 1, flavor='raw') cd.abi = clibffi.FFI_DEFAULT_ABI cd.nargs = 1 cd.rtype = clibffi.cast_type_to_ffitype(rffi.DOUBLE) atypes = lltype.malloc(clibffi.FFI_TYPE_PP.TO, 1, flavor='raw') atypes[0] = clibffi.cast_type_to_ffitype(rffi.DOUBLE) cd.atypes = atypes cd.exchange_size = 64 # 64 bytes of exchange data cd.exchange_result = 24 cd.exchange_args[0] = 16 # jit_ffi_prep_cif(cd) # assert rffi.sizeof(rffi.DOUBLE) == 8 exb = lltype.malloc(rffi.DOUBLEP.TO, 8, flavor='raw') exb[2] = 1.23 jit_ffi_call(cd, math_sin, rffi.cast(rffi.CCHARP, exb)) res = exb[3] lltype.free(exb, flavor='raw') # lltype.free(atypes, flavor='raw') lltype.free(cd, flavor='raw') # assert res == math.sin(1.23)
def get_darwin_sysctl_signed(sysctl_name): rval_p = lltype.malloc(rffi.LONGLONGP.TO, 1, flavor='raw') try: len_p = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw') try: size = rffi.sizeof(rffi.LONGLONG) rval_p[0] = rffi.cast(rffi.LONGLONG, 0) len_p[0] = rffi.cast(rffi.SIZE_T, size) # XXX a hack for llhelper not being robust-enough result = sysctlbyname(sysctl_name, rffi.cast(rffi.VOIDP, rval_p), len_p, lltype.nullptr(rffi.VOIDP.TO), rffi.cast(rffi.SIZE_T, 0)) rval = 0 if (rffi.cast(lltype.Signed, result) == 0 and rffi.cast(lltype.Signed, len_p[0]) == size): rval = rffi.cast(lltype.Signed, rval_p[0]) if rffi.cast(rffi.LONGLONG, rval) != rval_p[0]: rval = 0 # overflow! return rval finally: lltype.free(len_p, flavor='raw') finally: lltype.free(rval_p, flavor='raw')
def test_gc_pointers_inside(): from rpython.rtyper import rclass PT = lltype.Ptr(lltype.GcStruct('T')) S1 = lltype.GcStruct('S', ('x', PT), ('y', PT)) S2 = lltype.GcStruct('S', ('x', PT), ('y', PT), hints={'immutable': True}) accessor = rclass.FieldListAccessor() S3 = lltype.GcStruct('S', ('x', PT), ('y', PT), hints={'immutable_fields': accessor}) accessor.initialize(S3, {'x': IR_IMMUTABLE, 'y': IR_QUASIIMMUTABLE}) # s1 = lltype.malloc(S1) adr = llmemory.cast_ptr_to_adr(s1) lst = list(gc_pointers_inside(s1._obj, adr, mutable_only=True)) expected = [adr + llmemory.offsetof(S1, 'x'), adr + llmemory.offsetof(S1, 'y')] assert lst == expected or lst == expected[::-1] # s2 = lltype.malloc(S2) adr = llmemory.cast_ptr_to_adr(s2) lst = list(gc_pointers_inside(s2._obj, adr, mutable_only=True)) assert lst == [] # s3 = lltype.malloc(S3) adr = llmemory.cast_ptr_to_adr(s3) lst = list(gc_pointers_inside(s3._obj, adr, mutable_only=True)) assert lst == [adr + llmemory.offsetof(S3, 'y')]
def date_sunrise_sunset(func_name, interp, num_args, timestamp, return_format, latitude, longitude, zenith, gmt_offset): sunrise = False if func_name == "date_sunrise": sunrise = True if return_format not in [SUNFUNCS_RET_TIMESTAMP, SUNFUNCS_RET_STRING, SUNFUNCS_RET_DOUBLE]: interp.space.ec.warn( "%s(): Wrong return format given, pick one of " "SUNFUNCS_RET_TIMESTAMP, SUNFUNCS_RET_STRING or SUNFUNCS_RET_DOUBLE" % func_name) return interp.space.w_False altitude = 90 - zenith timelib_time = timelib.timelib_time_ctor() timelib_timezone = interp.get_default_timezone("date").timelib_timezone timelib_time.c_tz_info = timelib_timezone timelib_time.c_zone_type = timelib.TIMELIB_ZONETYPE_ID timelib.timelib_unixtime2local(timelib_time, timestamp) c_h_rise = lltype.malloc(rffi.CArrayPtr(lltype.Float).TO, 1, flavor='raw') c_h_set = lltype.malloc(rffi.CArrayPtr(lltype.Float).TO, 1, flavor='raw') c_rise = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw') c_set = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw') c_transit = lltype.malloc(rffi.CArrayPtr(rffi.LONGLONG).TO, 1, flavor='raw') rs = timelib.timelib_astro_rise_set_altitude( timelib_time, longitude, latitude, altitude, 1, c_h_rise, c_h_set, c_rise, c_set, c_transit ) if num_args <= 5: gmt_offset = float(timelib.timelib_get_current_offset(timelib_time) / 3600) timelib.timelib_time_dtor(timelib_time) if rs != 0: return interp.space.w_False if return_format == 0: return interp.space.wrap(c_rise[0] if sunrise else c_set[0]) N = (c_h_rise[0] if sunrise else c_h_set[0]) + gmt_offset if N > 24 or N < 0: N -= math.floor(N / 24) * 24 if return_format == 1: return interp.space.wrap("%s:%s" % ( timelib.format_to(2, int(math.floor(N))), timelib.format_to(2, int(math.floor(60 * (N - int(N))))) )) elif return_format == 2: return interp.space.wrap(N)
def parse(input): OUTPUT_SIZE = 100 out = lltype.malloc(rffi.VOIDPP.TO, OUTPUT_SIZE, flavor='raw', track_allocation=False) info = lltype.malloc(parse_c_type.PINFO.TO, flavor='raw', zero=True, track_allocation=False) info.c_ctx = ctx info.c_output = out rffi.setintfield(info, 'c_output_size', OUTPUT_SIZE) for j in range(OUTPUT_SIZE): out[j] = rffi.cast(rffi.VOIDP, -424242) res = parse_c_type.parse_c_type(info, input.encode('ascii')) if res < 0: raise ParseError(rffi.charp2str(info.c_error_message).decode('ascii'), rffi.getintfield(info, 'c_error_location')) assert 0 <= res < OUTPUT_SIZE result = [] for j in range(OUTPUT_SIZE): if out[j] == rffi.cast(rffi.VOIDP, -424242): assert res < j break i = rffi.cast(rffi.SIGNED, out[j]) if j == res: result.append('->') result.append(i) return result
def setup(): rgc.register_custom_trace_hook(S, lambda_customtrace) tx = lltype.malloc(T) tx.z = 4243 s1 = lltype.malloc(S) s1.x = llmemory.cast_ptr_to_adr(tx) return s1
def os_utime_llimpl(path, tp): hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, 0, None, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, rwin32.NULL_HANDLE) if hFile == rwin32.INVALID_HANDLE_VALUE: raise rwin32.lastWindowsError() ctime = lltype.nullptr(rwin32.FILETIME) atime = lltype.malloc(rwin32.FILETIME, flavor='raw') mtime = lltype.malloc(rwin32.FILETIME, flavor='raw') try: if tp is None: now = lltype.malloc(rwin32.SYSTEMTIME, flavor='raw') try: GetSystemTime(now) if (not SystemTimeToFileTime(now, atime) or not SystemTimeToFileTime(now, mtime)): raise rwin32.lastWindowsError() finally: lltype.free(now, flavor='raw') else: actime, modtime = tp time_t_to_FILE_TIME(actime, atime) time_t_to_FILE_TIME(modtime, mtime) if not SetFileTime(hFile, ctime, atime, mtime): raise rwin32.lastWindowsError() finally: rwin32.CloseHandle(hFile) lltype.free(atime, flavor='raw') lltype.free(mtime, flavor='raw')
def f(): array = lltype.malloc(A, 5) array[2] = 5 return array[2] + len(array)
def g(): return lltype.malloc(S)
def f(): t1 = malloc(T, flavor='raw') t2 = malloc(T, flavor='raw') free(t1, flavor='raw') free(t2, flavor='raw')
def do_poll(self, space, timeout): from pypy.module._multiprocessing.interp_win32 import (_PeekNamedPipe, _GetTickCount, _Sleep) from rpython.rlib import rwin32 from pypy.interpreter.error import wrap_windowserror bytes_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor='raw') try: if not _PeekNamedPipe(self.handle, rffi.NULL, 0, lltype.nullptr(rwin32.LPDWORD.TO), bytes_ptr, lltype.nullptr(rwin32.LPDWORD.TO)): raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) bytes = r_uint(bytes_ptr[0]) finally: lltype.free(bytes_ptr, flavor='raw') if timeout == 0.0: return bytes > 0 block = timeout < 0 if not block: # XXX does not check for overflow deadline = intmask(_GetTickCount()) + int(1000 * timeout + 0.5) else: deadline = 0 _Sleep(0) delay = 1 while True: bytes_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor='raw') try: if not _PeekNamedPipe(self.handle, rffi.NULL, 0, lltype.nullptr(rwin32.LPDWORD.TO), bytes_ptr, lltype.nullptr(rwin32.LPDWORD.TO)): raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) bytes = r_uint(bytes_ptr[0]) finally: lltype.free(bytes_ptr, flavor='raw') if bytes > 0: return True if not block: now = intmask(_GetTickCount()) if now > deadline: return False diff = deadline - now if delay > diff: delay = diff else: delay += 1 if delay >= 20: delay = 20 _Sleep(delay)
def _init_timezone(space): timezone = daylight = altzone = 0 tzname = ["", ""] if _WIN: c_tzset() timezone = c_get_timezone() altzone = timezone - 3600 daylight = c_get_daylight() for i in [0, 1]: blen = c_get_tzname(0, i, None) with rffi.scoped_alloc_buffer(blen) as buf: s = c_get_tzname(blen, i, buf.raw) tzname[i] = buf.str(s - 1) if _POSIX: if _CYGWIN: YEAR = (365 * 24 + 6) * 3600 # about January 11th t = (((c_time(lltype.nullptr(rffi.TIME_TP.TO))) / YEAR) * YEAR + 10 * 24 * 3600) # we cannot have reference to stack variable, put it on the heap t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw') t_ref[0] = rffi.cast(rffi.TIME_T, t) p = c_localtime(t_ref) q = c_gmtime(t_ref) janzone = (p.c_tm_hour + 24 * p.c_tm_mday) - (q.c_tm_hour + 24 * q.c_tm_mday) if janzone < -12: janname = " " elif janzone > 14: janname = " " else: janname = _time_zones[janzone - 12] janzone = janzone * 3600 # about July 11th tt = t + YEAR / 2 t_ref[0] = rffi.cast(rffi.TIME_T, tt) p = c_localtime(t_ref) q = c_gmtime(t_ref) julyzone = (p.c_tm_hour + 24 * p.c_tm_mday) - (q.c_tm_hour + 24 * q.c_tm_mday) if julyzone < -12: julyname = " " elif julyzone > 14: julyname = " " else: julyname = _time_zones[julyzone - 12] julyzone = julyzone * 3600 lltype.free(t_ref, flavor='raw') if janzone < julyzone: # DST is reversed in the southern hemisphere timezone = julyzone altzone = janzone daylight = int(janzone != julyzone) tzname = [julyname, janname] else: timezone = janzone altzone = julyzone daylight = int(janzone != julyzone) tzname = [janname, julyname] else: YEAR = (365 * 24 + 6) * 3600 t = (((c_time(lltype.nullptr(rffi.TIME_TP.TO))) / YEAR) * YEAR) # we cannot have reference to stack variable, put it on the heap t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw') t_ref[0] = rffi.cast(rffi.TIME_T, t) p = c_localtime(t_ref) janzone = -p.c_tm_gmtoff tm_zone = rffi.charp2str(p.c_tm_zone) janname = [" ", tm_zone][bool(tm_zone)] tt = t + YEAR / 2 t_ref[0] = rffi.cast(rffi.TIME_T, tt) p = c_localtime(t_ref) lltype.free(t_ref, flavor='raw') tm_zone = rffi.charp2str(p.c_tm_zone) julyzone = -p.c_tm_gmtoff julyname = [" ", tm_zone][bool(tm_zone)] if janzone < julyzone: # DST is reversed in the southern hemisphere timezone = julyzone altzone = janzone daylight = int(janzone != julyzone) tzname = [julyname, janname] else: timezone = janzone altzone = julyzone daylight = int(janzone != julyzone) tzname = [janname, julyname] _set_module_object(space, "timezone", space.newint(timezone)) _set_module_object(space, 'daylight', space.newint(daylight)) tzname_w = [space.newtext(tzname[0]), space.newtext(tzname[1])] _set_module_object(space, 'tzname', space.newtuple(tzname_w)) _set_module_object(space, 'altzone', space.newint(altzone))
def PyObject_Malloc(space, size): # returns non-zero-initialized memory, like CPython return lltype.malloc(rffi.VOIDP.TO, size, flavor='raw', add_memory_pressure=True)
def g(): lltype.malloc(S, zero=True)
def runs_setupterm(): null = lltype.nullptr(rffi.CCHARP.TO) p_errret = lltype.malloc(rffi.INTP.TO, 1, flavor='raw') errval = fficurses.setupterm(null, 1, p_errret)
def strftime(space, format, w_tup=None): """strftime(format[, tuple]) -> string Convert a time tuple to a string according to a format specification. See the library reference manual for formatting codes. When the time tuple is not present, current time as returned by localtime() is used.""" buf_value = _gettmarg(space, w_tup) # Checks added to make sure strftime() does not crash Python by # indexing blindly into some array for a textual representation # by some bad index (fixes bug #897625). # No check for year since handled in gettmarg(). if rffi.getintfield(buf_value, 'c_tm_mon') < 0 or rffi.getintfield( buf_value, 'c_tm_mon') > 11: raise oefmt(space.w_ValueError, "month out of range") if rffi.getintfield(buf_value, 'c_tm_mday') < 1 or rffi.getintfield( buf_value, 'c_tm_mday') > 31: raise oefmt(space.w_ValueError, "day of month out of range") if rffi.getintfield(buf_value, 'c_tm_hour') < 0 or rffi.getintfield( buf_value, 'c_tm_hour') > 23: raise oefmt(space.w_ValueError, "hour out of range") if rffi.getintfield(buf_value, 'c_tm_min') < 0 or rffi.getintfield( buf_value, 'c_tm_min') > 59: raise oefmt(space.w_ValueError, "minute out of range") if rffi.getintfield(buf_value, 'c_tm_sec') < 0 or rffi.getintfield( buf_value, 'c_tm_sec') > 61: raise oefmt(space.w_ValueError, "seconds out of range") if rffi.getintfield(buf_value, 'c_tm_yday') < 0 or rffi.getintfield( buf_value, 'c_tm_yday') > 365: raise oefmt(space.w_ValueError, "day of year out of range") if rffi.getintfield(buf_value, 'c_tm_isdst') < -1 or rffi.getintfield( buf_value, 'c_tm_isdst') > 1: raise oefmt(space.w_ValueError, "daylight savings flag out of range") if _WIN: # check that the format string contains only valid directives length = len(format) i = 0 while i < length: if format[i] == '%': i += 1 if i < length and format[i] == '#': # not documented by python i += 1 if i >= length or format[i] not in "aAbBcdHIjmMpSUwWxXyYzZ%": raise oefmt(space.w_ValueError, "invalid format string") i += 1 i = 1024 while True: outbuf = lltype.malloc(rffi.CCHARP.TO, i, flavor='raw') try: buflen = c_strftime(outbuf, i, format, buf_value) if buflen > 0 or i >= 256 * len(format): # if the buffer is 256 times as long as the format, # it's probably not failing for lack of room! # More likely, the format yields an empty result, # e.g. an empty format, or %Z when the timezone # is unknown. result = rffi.charp2strn(outbuf, intmask(buflen)) return space.newtext(result) finally: lltype.free(outbuf, flavor='raw') i += i
def _gettmarg(space, w_tup, allowNone=True): if space.is_none(w_tup): if not allowNone: raise oefmt(space.w_TypeError, "tuple expected") # default to the current local time tt = rffi.r_time_t(int(pytime.time())) t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw') t_ref[0] = tt pbuf = c_localtime(t_ref) lltype.free(t_ref, flavor='raw') if not pbuf: raise OperationError(space.w_ValueError, space.newtext(_get_error_msg())) return pbuf tup_w = space.fixedview(w_tup) if len(tup_w) != 9: raise oefmt(space.w_TypeError, "argument must be sequence of length 9, not %d", len(tup_w)) y = space.int_w(tup_w[0]) tm_mon = space.int_w(tup_w[1]) if tm_mon == 0: tm_mon = 1 tm_mday = space.int_w(tup_w[2]) if tm_mday == 0: tm_mday = 1 tm_yday = space.int_w(tup_w[7]) if tm_yday == 0: tm_yday = 1 rffi.setintfield(glob_buf, 'c_tm_mon', tm_mon) rffi.setintfield(glob_buf, 'c_tm_mday', tm_mday) rffi.setintfield(glob_buf, 'c_tm_hour', space.int_w(tup_w[3])) rffi.setintfield(glob_buf, 'c_tm_min', space.int_w(tup_w[4])) rffi.setintfield(glob_buf, 'c_tm_sec', space.int_w(tup_w[5])) rffi.setintfield(glob_buf, 'c_tm_wday', space.int_w(tup_w[6])) rffi.setintfield(glob_buf, 'c_tm_yday', tm_yday) rffi.setintfield(glob_buf, 'c_tm_isdst', space.int_w(tup_w[8])) if _POSIX: if _CYGWIN: pass else: # actually never happens, but makes annotator happy glob_buf.c_tm_zone = lltype.nullptr(rffi.CCHARP.TO) rffi.setintfield(glob_buf, 'c_tm_gmtoff', 0) if y < 1900: w_accept2dyear = _get_module_object(space, "accept2dyear") accept2dyear = space.int_w(w_accept2dyear) if not accept2dyear: raise oefmt(space.w_ValueError, "year >= 1900 required") if 69 <= y <= 99: y += 1900 elif 0 <= y <= 68: y += 2000 else: raise oefmt(space.w_ValueError, "year out of range") # tm_wday does not need checking of its upper-bound since taking "% # 7" in gettmarg() automatically restricts the range. if rffi.getintfield(glob_buf, 'c_tm_wday') < -1: raise oefmt(space.w_ValueError, "day of week out of range") rffi.setintfield(glob_buf, 'c_tm_year', y - 1900) rffi.setintfield(glob_buf, 'c_tm_mon', rffi.getintfield(glob_buf, 'c_tm_mon') - 1) rffi.setintfield(glob_buf, 'c_tm_wday', (rffi.getintfield(glob_buf, 'c_tm_wday') + 1) % 7) rffi.setintfield(glob_buf, 'c_tm_yday', rffi.getintfield(glob_buf, 'c_tm_yday') - 1) return glob_buf
def g(x): a = lltype.malloc(A, 1) a[0].y = 3 return f(a, x)
args, result, compilation_info=eci, calling_conv=_calling_conv, releasegil=False, **kwds) if _POSIX: cConfig.timeval.__name__ = "_timeval" timeval = cConfig.timeval CLOCKS_PER_SEC = cConfig.CLOCKS_PER_SEC clock_t = cConfig.clock_t tm = cConfig.tm glob_buf = lltype.malloc(tm, flavor='raw', zero=True, immortal=True) if cConfig.has_gettimeofday: c_gettimeofday = external('gettimeofday', [rffi.VOIDP, rffi.VOIDP], rffi.INT) TM_P = lltype.Ptr(tm) c_time = external('time', [rffi.TIME_TP], rffi.TIME_T) c_ctime = external('ctime', [rffi.TIME_TP], rffi.CCHARP) c_gmtime = external('gmtime', [rffi.TIME_TP], TM_P, save_err=rffi.RFFI_SAVE_ERRNO) c_mktime = external('mktime', [TM_P], rffi.TIME_T) c_asctime = external('asctime', [TM_P], rffi.CCHARP) c_localtime = external('localtime', [rffi.TIME_TP], TM_P, save_err=rffi.RFFI_SAVE_ERRNO)
def create_instance(self): return malloc(self.object_type, flavor=self.gcflavor, immortal=True)
def f(x, y, z): p = lltype.malloc(S) return g(p, x, y, z)
def f(): ll_stuff = lltype.malloc(STUFFP.TO, flavor='raw') result = ll_get(ll_stuff) lltype.free(ll_stuff, flavor='raw') return result
def init_vtable(self): self.vtable = malloc(self.vtable_type, immortal=True) self.fill_vtable_root(self.vtable)
def __init__(self, space): self.interpreter_state = lltype.malloc(PyInterpreterState.TO, flavor='raw', zero=True, immortal=True)
def __init__(self, space): self.space = space if space is not None: self.memory = lltype.malloc(T, flavor=flavor) else: self.memory = lltype.nullptr(T)
def convert_to_regdata(space, w_value, typ): buf = None if typ == rwinreg.REG_DWORD: if space.is_none(w_value) or (space.isinstance_w(w_value, space.w_int) or space.isinstance_w( w_value, space.w_long)): if space.is_none(w_value): value = r_uint(0) else: value = space.c_uint_w(w_value) buflen = rffi.sizeof(rwin32.DWORD) buf1 = lltype.malloc(rffi.CArray(rwin32.DWORD), 1, flavor='raw') buf1[0] = value buf = rffi.cast(rffi.CCHARP, buf1) elif typ == rwinreg.REG_SZ or typ == rwinreg.REG_EXPAND_SZ: if space.is_w(w_value, space.w_None): buflen = 1 buf = lltype.malloc(rffi.CCHARP.TO, buflen, flavor='raw') buf[0] = '\0' else: if space.isinstance_w(w_value, space.w_unicode): w_value = space.call_method(w_value, 'encode', space.newtext('mbcs')) buf = rffi.str2charp(space.text_w(w_value)) buflen = space.len_w(w_value) + 1 elif typ == rwinreg.REG_MULTI_SZ: if space.is_w(w_value, space.w_None): buflen = 1 buf = lltype.malloc(rffi.CCHARP.TO, buflen, flavor='raw') buf[0] = '\0' elif space.isinstance_w(w_value, space.w_list): strings = [] buflen = 0 # unwrap strings and compute total size w_iter = space.iter(w_value) while True: try: w_item = space.next(w_iter) if space.isinstance_w(w_item, space.w_unicode): w_item = space.call_method(w_item, 'encode', space.newtext('mbcs')) item = space.bytes_w(w_item) strings.append(item) buflen += len(item) + 1 except OperationError as e: if not e.match(space, space.w_StopIteration): raise # re-raise other app-level exceptions break buflen += 1 buf = lltype.malloc(rffi.CCHARP.TO, buflen, flavor='raw') # Now copy data buflen = 0 for string in strings: for i in range(len(string)): buf[buflen + i] = string[i] buflen += len(string) + 1 buf[buflen - 1] = '\0' buflen += 1 buf[buflen - 1] = '\0' else: # REG_BINARY and ALL unknown data types. if space.is_w(w_value, space.w_None): buflen = 0 buf = lltype.malloc(rffi.CCHARP.TO, 1, flavor='raw') buf[0] = '\0' else: try: value = w_value.readbuf_w(space) except BufferInterfaceNotFound: raise oefmt( space.w_TypeError, "Objects of type '%T' can not be used as binary " "registry values", w_value) else: value = value.as_str() buflen = len(value) buf = rffi.str2charp(value) if buf is not None: return rffi.cast(rffi.CCHARP, buf), buflen raise oefmt(space.w_ValueError, "Could not convert the data to the specified type")
def f(n): a = lltype.malloc(TP, n, flavor='raw') a[0] = n res = a[0] lltype.free(a, flavor='raw') return res
def allocate(self, SHADOWSTACKREF): """Allocate an empty SHADOWSTACKREF object.""" return lltype.malloc(SHADOWSTACKREF, zero=True)
def builtin_ffi_function(ctx): # parameter validation assert len(ctx.params) == 4 and \ ctx.params[0].type == 'str' and \ ctx.params[1].type == 'str' \ and ctx.params[2].type == 'str' \ and ctx.params[3].type == 'array' for e in ctx.params[3].arrayvalue: assert e.type == 'str' # extract parameters libname = ctx.params[0].strvalue funcname = ctx.params[1].strvalue rtype = ctx.params[2].strvalue atypes = [e.strvalue for e in ctx.params[3].arrayvalue] # validate if we defined before if (libname, funcname) in ctx.machine.space.ffi_functions: ctx.machine.error = ctx.machine.space.newstr( 'cannot define %s twice in %s.' % (funcname, libname)) return # setup cif argc = len(atypes) cif = lltype.malloc(jit_libffi.CIF_DESCRIPTION, argc, flavor='raw') cif.abi = clibffi.FFI_DEFAULT_ABI cif.nargs = argc cif.rtype = _cast_aotype_to_ffitype(rtype) cif.atypes = lltype.malloc(clibffi.FFI_TYPE_PP.TO, argc, flavor='raw') # create room for an array of nargs pointers exchange_offset = rffi.sizeof(rffi.VOIDP) * argc exchange_offset = _align(exchange_offset) cif.exchange_result = exchange_offset # create room for return value, roundup to sizeof(ffi-arg) exchange_offset += max(rffi.getintfield(cif.rtype, 'c_size'), jit_libffi.SIZE_OF_FFI_ARG) # set size for each arg for i in range(argc): atype = _cast_aotype_to_ffitype(atypes[i]) cif.atypes[i] = atype exchange_offset = _align(exchange_offset) cif.exchange_args[i] = exchange_offset exchange_offset += rffi.getintfield(atype, 'c_size') # set total size of args + retval cif.exchange_size = exchange_offset # prepare cif code = jit_libffi.jit_ffi_prep_cif(cif) if code != clibffi.FFI_OK: ctx.machine.error = ctx.machine.space.newstr( 'failed to build function %s for lib %s.' % (funcname, libname)) return # cache ffi ffi = ctx.machine.space.newforeignfunction(libname, funcname, rtype, atypes, cif) ctx.machine.space.ffi_functions[(libname, funcname)] = ffi ctx.tos.push(ctx.machine.space.null)
def test_ret_struct_val(self): from rpython.translator.tool.cbuild import ExternalCompilationInfo from rpython.translator.platform import platform from rpython.tool.udir import udir c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") c_file.write( py.code.Source(''' #include "src/precommondefs.h" #include <stdlib.h> #include <stdio.h> struct s2h { short x; short y; }; RPY_EXPORTED struct s2h give(short x, short y) { struct s2h out; out.x = x; out.y = y; return out; } RPY_EXPORTED struct s2h perturb(struct s2h inp) { inp.x *= 2; inp.y *= 3; return inp; } ''')) eci = ExternalCompilationInfo(include_dirs=[cdir]) lib_name = str(platform.compile([c_file], eci, 'x', standalone=False)) lib = CDLL(lib_name) size = ffi_type_sshort.c_size * 2 alignment = ffi_type_sshort.c_alignment tpe = make_struct_ffitype_e(size, alignment, [ffi_type_sshort] * 2) give = lib.getrawpointer('give', [ffi_type_sshort, ffi_type_sshort], tpe.ffistruct) inbuffer = lltype.malloc(rffi.SHORTP.TO, 2, flavor='raw') inbuffer[0] = rffi.cast(rffi.SHORT, 40) inbuffer[1] = rffi.cast(rffi.SHORT, 72) outbuffer = lltype.malloc(rffi.SHORTP.TO, 2, flavor='raw') give.call([ rffi.cast(rffi.VOIDP, inbuffer), rffi.cast(rffi.VOIDP, rffi.ptradd(inbuffer, 1)) ], rffi.cast(rffi.VOIDP, outbuffer)) assert outbuffer[0] == 40 assert outbuffer[1] == 72 perturb = lib.getrawpointer('perturb', [tpe.ffistruct], tpe.ffistruct) inbuffer[0] = rffi.cast(rffi.SHORT, 7) inbuffer[1] = rffi.cast(rffi.SHORT, 11) perturb.call([rffi.cast(rffi.VOIDP, inbuffer)], rffi.cast(rffi.VOIDP, outbuffer)) assert inbuffer[0] == 7 assert inbuffer[1] == 11 assert outbuffer[0] == 14 assert outbuffer[1] == 33 lltype.free(outbuffer, flavor='raw') lltype.free(inbuffer, flavor='raw') del give del perturb lltype.free(tpe, flavor='raw') del lib assert not ALLOCATED
def _prepare(self): ll_args = lltype.malloc(rffi.VOIDPP.TO, len(self.argtypes), flavor='raw') return ll_args
def __init__(self, space, size, ctype): cdata = lltype.malloc(rffi.CCHARP.TO, size, flavor='raw', zero=True) W_CData.__init__(self, space, cdata, ctype)
def get_all_loop_runs(self): # not implemented return lltype.malloc(LOOP_RUN_CONTAINER, 0)
def setup_once(self): # the address of the function called by 'new' gc_ll_descr = self.cpu.gc_ll_descr gc_ll_descr.initialize() if hasattr(gc_ll_descr, 'minimal_size_in_nursery'): self.gc_minimal_size_in_nursery = gc_ll_descr.minimal_size_in_nursery else: self.gc_minimal_size_in_nursery = 0 if hasattr(gc_ll_descr, 'gcheaderbuilder'): self.gc_size_of_header = gc_ll_descr.gcheaderbuilder.size_gc_header else: self.gc_size_of_header = WORD # for tests self.memcpy_addr = rffi.cast(lltype.Signed, memcpy_fn) self.memset_addr = rffi.cast(lltype.Signed, memset_fn) self._build_failure_recovery(False, withfloats=False) self._build_failure_recovery(True, withfloats=False) self._build_wb_slowpath(False) self._build_wb_slowpath(True) self._build_wb_slowpath(False, for_frame=True) # only one of those self.build_frame_realloc_slowpath() if self.cpu.supports_floats: self._build_failure_recovery(False, withfloats=True) self._build_failure_recovery(True, withfloats=True) self._build_wb_slowpath(False, withfloats=True) self._build_wb_slowpath(True, withfloats=True) self._build_propagate_exception_path() if gc_ll_descr.get_malloc_slowpath_addr is not None: # generate few slowpaths for various cases self.malloc_slowpath = self._build_malloc_slowpath(kind='fixed') self.malloc_slowpath_varsize = self._build_malloc_slowpath( kind='var') if hasattr(gc_ll_descr, 'malloc_str'): self.malloc_slowpath_str = self._build_malloc_slowpath(kind='str') else: self.malloc_slowpath_str = None if hasattr(gc_ll_descr, 'malloc_unicode'): self.malloc_slowpath_unicode = self._build_malloc_slowpath( kind='unicode') else: self.malloc_slowpath_unicode = None lst = [0, 0, 0, 0] lst[0] = self._build_cond_call_slowpath(False, False) lst[1] = self._build_cond_call_slowpath(False, True) if self.cpu.supports_floats: lst[2] = self._build_cond_call_slowpath(True, False) lst[3] = self._build_cond_call_slowpath(True, True) self.cond_call_slowpath = lst self._build_stack_check_slowpath() self._build_release_gil(gc_ll_descr.gcrootmap) # do not rely on the attribute _debug for jitlog if not self._debug: # if self._debug is already set it means that someone called # set_debug by hand before initializing the assembler. Leave it # as it is should_debug = have_debug_prints_for('jit-backend-counts') self.set_debug(should_debug) # when finishing, we only have one value at [0], the rest dies self.gcmap_for_finish = lltype.malloc(jitframe.GCMAP, 1, flavor='raw', track_allocation=False) self.gcmap_for_finish[0] = r_uint(1)
def f(x): t = malloc(T, flavor='raw') if x: free(t, flavor='raw')
def h(x): lltype.malloc(S, zero=True)
def __init__(self, space, flags): self.flags = flags self.buffer = lltype.malloc(rffi.CCHARP.TO, self.BUFFER_SIZE, flavor='raw') self.register_finalizer(space)