def ref(self, firstitemptr): A = lltype.typeOf(firstitemptr).TO if A == self.TYPE: # for array of containers parent, index = lltype.parentlink(firstitemptr._obj) assert parent, "%r is not within a container" % (firstitemptr, ) assert isinstance( lltype.typeOf(parent), (lltype.Array, lltype.FixedSizeArray)), ("%r is not within an array" % (firstitemptr, )) if isinstance(index, str): assert index.startswith('item') # itemN => N index = int(index[4:]) index += self.repeat if index == parent.getlength(): # for references exactly to the end of the array try: endmarker = _end_markers[parent] except KeyError: endmarker = _endmarker_struct(A, parent=parent, parentindex=index) _end_markers[parent] = endmarker return endmarker._as_ptr() else: return parent.getitem(index)._as_ptr() elif ( (isinstance(A, lltype.FixedSizeArray) or (isinstance(A, lltype.Array) and A._hints.get('nolength', False))) and array_item_type_match(A.OF, self.TYPE)): # for array of primitives or pointers return lltype.direct_ptradd(firstitemptr, self.repeat) else: raise TypeError('got %r, expected %r' % (A, self.TYPE))
def walk_roots(self, collect_stack_root, collect_static_in_prebuilt_nongc, collect_static_in_prebuilt_gc, is_minor=False): gc = self.tester.gc layoutbuilder = self.tester.layoutbuilder if collect_static_in_prebuilt_gc: for addrofaddr in layoutbuilder.addresses_of_static_ptrs: if addrofaddr.address[0]: collect_static_in_prebuilt_gc(gc, addrofaddr) if collect_static_in_prebuilt_nongc: for addrofaddr in layoutbuilder.addresses_of_static_ptrs_in_nongc: if addrofaddr.address[0]: collect_static_in_prebuilt_nongc(gc, addrofaddr) if collect_stack_root: stackroots = self.tester.stackroots a = lltype.malloc(ADDR_ARRAY, len(stackroots), flavor='raw') for i in range(len(a)): a[i] = llmemory.cast_ptr_to_adr(stackroots[i]) a_base = lltype.direct_arrayitems(a) for i in range(len(a)): ai = lltype.direct_ptradd(a_base, i) collect_stack_root(gc, llmemory.cast_ptr_to_adr(ai)) for i in range(len(a)): PTRTYPE = lltype.typeOf(stackroots[i]) stackroots[i] = llmemory.cast_adr_to_ptr(a[i], PTRTYPE) lltype.free(a, flavor='raw')
def ref(self, firstitemptr): A = lltype.typeOf(firstitemptr).TO if A == self.TYPE: # for array of containers parent, index = lltype.parentlink(firstitemptr._obj) assert parent, "%r is not within a container" % (firstitemptr,) assert isinstance(lltype.typeOf(parent), (lltype.Array, lltype.FixedSizeArray)), ( "%r is not within an array" % (firstitemptr,)) if isinstance(index, str): assert index.startswith('item') # itemN => N index = int(index[4:]) index += self.repeat if index == parent.getlength(): # for references exactly to the end of the array try: endmarker = _end_markers[parent] except KeyError: endmarker = _endmarker_struct(A, parent=parent, parentindex=index) _end_markers[parent] = endmarker return endmarker._as_ptr() else: return parent.getitem(index)._as_ptr() elif ((isinstance(A, lltype.FixedSizeArray) or (isinstance(A, lltype.Array) and A._hints.get('nolength', False))) and array_item_type_match(A.OF, self.TYPE)): # for array of primitives or pointers return lltype.direct_ptradd(firstitemptr, self.repeat) else: raise TypeError('got %r, expected %r' % (A, self.TYPE))
def addr_add_bytes(addr, ofs): if (isinstance(ofs, int) and getattr(addr.adr.ptr._TYPE.TO, 'OF', None) == lltype.Char): return addr + ofs ptr = rffi.cast(rffi.CCHARP, addr.adr) ptr = lltype.direct_ptradd(ptr, ofs) return cast_to_int(ptr)
def finalize_call(self, args, args_w, call_local): stride = capi.c_function_arg_sizeof(self.space) for i in range(len(args_w)): conv = self.converters[i] arg_i = lltype.direct_ptradd(rffi.cast(rffi.CCHARP, args), i*stride) loc_i = self._address_from_local_buffer(call_local, i) conv.finalize_call(self.space, args_w[i], loc_i) conv.free_argument(self.space, rffi.cast(capi.C_OBJECT, arg_i), loc_i) capi.c_deallocate_function_args(self.space, args)
def writeall_not_sandboxed(fd, buf, length): while length > 0: size = rffi.cast(rffi.SIZE_T, length) count = rffi.cast(lltype.Signed, ll_write_not_sandboxed(fd, buf, size)) if count <= 0: raise IOError length -= count buf = lltype.direct_ptradd(lltype.direct_arrayitems(buf), count) buf = rffi.cast(rffi.CCHARP, buf)
def op_direct_ptradd(obj, index): checkptr(obj) assert is_valid_int(index) if not obj: raise AssertionError("direct_ptradd on null pointer") ## assert isinstance(index, int) ## assert not (0 <= index < 4096) ## from rpython.rtyper.lltypesystem import rffi ## return rffi.cast(lltype.typeOf(obj), index) return lltype.direct_ptradd(obj, index)
def prepare_arguments(self, args_w, call_local): args = capi.c_allocate_function_args(self.space, len(args_w)) stride = capi.c_function_arg_sizeof(self.space) for i in range(len(args_w)): conv = self.converters[i] w_arg = args_w[i] try: arg_i = lltype.direct_ptradd(rffi.cast(rffi.CCHARP, args), i*stride) loc_i = self._address_from_local_buffer(call_local, i) conv.convert_argument(self.space, w_arg, rffi.cast(capi.C_OBJECT, arg_i), loc_i) except: # fun :-( for j in range(i): conv = self.converters[j] arg_j = lltype.direct_ptradd(rffi.cast(rffi.CCHARP, args), j*stride) loc_j = self._address_from_local_buffer(call_local, j) conv.free_argument(self.space, rffi.cast(capi.C_OBJECT, arg_j), loc_j) capi.c_deallocate_function_args(self.space, args) raise return args
def descr_next(self, space): if self.w_seq is None: raise OperationError(space.w_StopIteration, space.w_None) if self.len <= self.index: self.w_seq = None raise OperationError(space.w_StopIteration, space.w_None) offset = lltype.direct_ptradd( self.data, rffi.cast(rffi.SIZE_T, self.index * self.stride)) w_item = self.converter.from_memory(space, space.w_None, rffi.cast(rffi.LONG, offset)) self.index += 1 return w_item
def _unwrap_object(self, space, w_value): utf8, length = space.utf8_len_w(space.unicode_from_object(w_value)) if length != 1: raise oefmt(space.w_ValueError, "char32_t expected, got string of size %d", length) utf32 = utf8_encode_utf_32(utf8, 'strict') rawstr = rffi.str2charp(utf32) value = rffi.cast(self.c_ptrtype, lltype.direct_ptradd(rawstr, 4))[0] # adjust BOM lltype.free(rawstr, flavor='raw') return value
def op_direct_ptradd(obj, index): checkptr(obj) assert is_valid_int(index) return lltype.direct_ptradd(obj, index)
def direct_ptradd(ptr, offset): offset = rffi.cast(rffi.SIZE_T, offset) jit.promote(offset) assert lltype.typeOf(ptr) == C_OBJECT address = rffi.cast(rffi.CCHARP, ptr) return rffi.cast(C_OBJECT, lltype.direct_ptradd(address, offset))
def _opaque_direct_ptradd(ptr, offset): address = rffi.cast(rffi.CCHARP, ptr) return rffi.cast(capi.C_OBJECT, lltype.direct_ptradd(address, offset))
def fn(): p1 = lltype.direct_arrayitems(a1) p2 = lltype.direct_ptradd(p1, 6) return p2[0]
def __getitem__(self, index): ptr = self.addr.ref() if index != 0: ptr = lltype.direct_ptradd(ptr, index) return self.read_from_ptr(ptr)
def __setitem__(self, index, value): assert lltype.typeOf(value) == self.TYPE ptr = self.addr.ref() if index != 0: ptr = lltype.direct_ptradd(ptr, index) self.write_into_ptr(ptr, value)
def _address_from_local_buffer(self, call_local, idx): if not call_local: return call_local stride = 2*rffi.sizeof(rffi.VOIDP) loc_idx = lltype.direct_ptradd(rffi.cast(rffi.CCHARP, call_local), idx*stride) return rffi.cast(rffi.VOIDP, loc_idx)
def f(p, n): return lltype.direct_ptradd(p, n)
def f(p, n): return lltype.direct_ptradd(p, n + 2)