def advance(self): objects = rffi.cast(TYPE_BFG_OBJECT_ARRAY_PTR, self.thetape.c_objects) self.thetape.c_index += 1 if self.thetape.c_length <= self.thetape.c_index: if self.thetape.c_alloc_length <= self.thetape.c_length: # Double allocated length self.thetape.c_alloc_length *= 2 temp = lltype.malloc(TYPE_BFG_OBJECT_ARRAY, self.thetape.c_alloc_length, flavor='raw') rffi.c_memset(rffi.cast(rffi.VOIDP, temp), 0, SIZE_BFG_OBJECT*self.thetape.c_alloc_length) rffi.c_memcpy(rffi.cast(rffi.VOIDP, temp), rffi.cast(rffi.VOIDP, self.thetape.c_objects), SIZE_BFG_OBJECT*self.thetape.c_length) # Free the old memory, not sure if this works yet... # Does it free alloc'd memory from the C-bindings? lltype.free(self.thetape.c_objects, flavor='raw') self.thetape.c_objects = rffi.cast(TYPE_BFG_OBJECT_PTR, temp) self.thetape.c_length += 1 else: self.thetape.c_length += 1
def Uint8Data_memcpy(self, src, size): size = src.length if size is None else size.value if size > self.length or size > src.length: raise space.unwind(space.LError(u"memcpy range error")) rffi.c_memcpy(rffi.cast(rffi.VOIDP, self.uint8data), rffi.cast(rffi.VOIDP, src.uint8data), size) return space.null
def delitem(self, space, i, j): if i < 0: i += self.len if i < 0: i = 0 if j < 0: j += self.len if j < 0: j = 0 if j > self.len: j = self.len if i >= j: return None oldbuffer = self._buffer self._buffer = lltype.malloc(rffi.CCHARP.TO, (self.len - (j - i)) * self.itemsize, flavor='raw') # Issue #2913: don't pass add_memory_pressure here, otherwise # memory pressure grows but actual raw memory usage doesn't---we # are freeing the old buffer at the end of this function. if i: rffi.c_memcpy(rffi.cast(rffi.VOIDP, self._buffer), rffi.cast(rffi.VOIDP, oldbuffer), i * self.itemsize) if j < self.len: rffi.c_memcpy( rffi.cast(rffi.VOIDP, rffi.ptradd(self._buffer, i * self.itemsize)), rffi.cast(rffi.VOIDP, rffi.ptradd(oldbuffer, j * self.itemsize)), (self.len - j) * self.itemsize) self.len -= j - i self.allocated = self.len if oldbuffer: lltype.free(oldbuffer, flavor='raw')
def delitem(self, space, i, j): if i < 0: i += self.len if i < 0: i = 0 if j < 0: j += self.len if j < 0: j = 0 if j > self.len: j = self.len if i >= j: return None oldbuffer = self._buffer self._buffer = lltype.malloc(rffi.CCHARP.TO, (self.len - (j - i)) * self.itemsize, flavor='raw', add_memory_pressure=True) if i: rffi.c_memcpy(rffi.cast(rffi.VOIDP, self._buffer), rffi.cast(rffi.VOIDP, oldbuffer), i * self.itemsize) if j < self.len: rffi.c_memcpy( rffi.cast(rffi.VOIDP, rffi.ptradd(self._buffer, i * self.itemsize)), rffi.cast(rffi.VOIDP, rffi.ptradd(oldbuffer, j * self.itemsize)), (self.len - j) * self.itemsize) self.len -= j - i self.allocated = self.len if oldbuffer: lltype.free(oldbuffer, flavor='raw')
def _(a, b): c = alloc_uint8array(a.length + b.length) rffi.c_memcpy(rffi.cast(rffi.VOIDP, c.uint8data), rffi.cast(rffi.VOIDP, a.uint8data), a.length) rffi.c_memcpy(rffi.cast(rffi.VOIDP, rffi.ptradd(c.uint8data, a.length)), rffi.cast(rffi.VOIDP, b.uint8data), b.length) return c
def load(self, offset, copy): if copy: pointer = lltype.malloc(rffi.VOIDP.TO, self.size, flavor='raw') rffi.c_memcpy(pointer, offset, sizeof(self)) return AutoMem(Pointer(self), pointer, 1) else: return Mem(Pointer(self), offset, 1)
def Uint8Builder_build(self): if self.array and self.array.length == self.total_capacity: return self.array # Folding buffers together is only necessary if more # stuff was appanded in the behind. length = self.total_capacity - self.avail base = lltype.malloc(rffi.VOIDP.TO, length, flavor='raw') remaining = length current = base if self.array is not None: rffi.c_memcpy(current, rffi.cast(rffi.VOIDP, self.array.uint8data), self.array.length) current = rffi.ptradd(current, self.array.length) remaining -= self.array.length for buf, sz in self.buffers: sz = min(sz, remaining) rffi.c_memcpy(current, buf, sz) current = rffi.ptradd(current, sz) remaining -= sz lltype.free(buf, flavor='raw') self.buffers = [] base = rffi.cast(rffi.UCHARP, base) self.array = Uint8Array(base, length) self.total_capacity = length self.avail = 0 return self.array
def delitem(self, space, i, j): if i < 0: i += self.len if i < 0: i = 0 if j < 0: j += self.len if j < 0: j = 0 if j > self.len: j = self.len if i >= j: return None oldbuffer = self.buffer self.buffer = lltype.malloc(mytype.arraytype, max(self.len - (j - i), 0), flavor='raw', add_memory_pressure=True) if i: rffi.c_memcpy(rffi.cast(rffi.VOIDP, self.buffer), rffi.cast(rffi.VOIDP, oldbuffer), i * mytype.bytes) if j < self.len: rffi.c_memcpy( rffi.cast(rffi.VOIDP, rffi.ptradd(self.buffer, i)), rffi.cast(rffi.VOIDP, rffi.ptradd(oldbuffer, j)), (self.len - j) * mytype.bytes) self.len -= j - i self.allocated = self.len if oldbuffer: lltype.free(oldbuffer, flavor='raw')
def delitem(self, space, i, j): if i < 0: i += self.len if i < 0: i = 0 if j < 0: j += self.len if j < 0: j = 0 if j > self.len: j = self.len if i >= j: return None oldbuffer = self.buffer self.buffer = lltype.malloc( mytype.arraytype, max(self.len - (j - i), 0), flavor='raw', add_memory_pressure=True) if i: rffi.c_memcpy( rffi.cast(rffi.VOIDP, self.buffer), rffi.cast(rffi.VOIDP, oldbuffer), i * mytype.bytes ) if j < self.len: rffi.c_memcpy( rffi.cast(rffi.VOIDP, rffi.ptradd(self.buffer, i)), rffi.cast(rffi.VOIDP, rffi.ptradd(oldbuffer, j)), (self.len - j) * mytype.bytes ) self.len -= j - i self.allocated = self.len if oldbuffer: lltype.free(oldbuffer, flavor='raw')
def store(self, pool, offset, value): if isinstance(value, Mem): ctype = value.ctype if isinstance(ctype, Pointer) and ctype.to is self: rffi.c_memcpy(offset, value.pointer, sizeof(self)) return value if isinstance(value, Dict): # If we set using this technique, we expect that the memory is clean # for fields that we didn't set. # This causes us to clean a field twice in some situations, # So it is not the perfect solution. # TODO: figure out something better. rffi.c_memset(offset, 0, sizeof(self)) for key, obj in value.data.iteritems(): if not isinstance(key, String): raise unwind( LTypeError( u"dictionary fields must be string fields for struct.store to work" )) try: x, ctype = self.namespace[key.string] except KeyError as k: raise unwind( LTypeError(u"%s not in %s" % (key.repr(), self.repr()))) ctype.store(pool, rffi.ptradd(offset, x), obj) return value raise unwind(LTypeError(u"cannot struct store " + value.repr()))
def method_put_bytes(self, space, start, string, str_offset, nbytes): with rffi.scoped_view_charp(string) as cstring: rffi.c_memcpy( rffi.ptradd(self.ptr, start), rffi.cast(rffi.VOIDP, rffi.ptradd(cstring, str_offset)), nbytes )
def force_words(self, start, stop): assert start > 0 and stop > 0 and self.size() >= stop and self.pixelbuffer_words >= stop and stop >= start pixbuf = rffi.ptradd(self.display().get_pixelbuffer(), start) realbuf = rffi.ptradd(self._real_depth_buffer, start) rffi.c_memcpy( rffi.cast(rffi.VOIDP, pixbuf), rffi.cast(rffi.VOIDP, realbuf), (stop - start) * constants.BYTES_PER_WORD) # VOIDP is char*, we want to copy word*
def timelib_time_modify(timelib_time, modifier, tzi): error = '' ll_s = rffi.str2charp(modifier) error_c = lltype.malloc(timelib_error_containerP.TO, 1, flavor='raw') tmp_timelib_time = timelib_strtotime(ll_s, len(modifier), error_c, timelib_builtin_db(), tzinfo_callback) error_count = rffi.cast(lltype.Signed, error_c[0].c_error_count) if error_count: position = int(error_c[0].c_error_messages[0].c_position) message = rffi.charp2str(error_c[0].c_error_messages[0].c_message) char = error_c[0].c_error_messages[0].c_character error = "Failed to parse time string (%s) at position %s (%s): %s" % ( modifier, position, char, message) lltype.free(error_c, flavor='raw') rffi.c_memcpy(rffi.cast(rffi.VOIDP, timelib_time.c_relative), rffi.cast(rffi.VOIDP, tmp_timelib_time.c_relative), rffi.sizeof(timelib_rel_time)) timelib_time.c_have_relative = tmp_timelib_time.c_have_relative timelib_time.c_sse_uptodate = rffi.cast(rffi.UINT, 0) if intmask(tmp_timelib_time.c_y) != -99999: timelib_time.c_y = tmp_timelib_time.c_y if intmask(tmp_timelib_time.c_m) != -99999: timelib_time.c_m = tmp_timelib_time.c_m if intmask(tmp_timelib_time.c_d) != -99999: timelib_time.c_d = tmp_timelib_time.c_d if intmask(tmp_timelib_time.c_h) != -99999: timelib_time.c_h = tmp_timelib_time.c_h if intmask(tmp_timelib_time.c_i) != -99999: timelib_time.c_i = tmp_timelib_time.c_i if intmask(tmp_timelib_time.c_s) != -99999: timelib_time.c_s = tmp_timelib_time.c_s else: timelib_time.c_s = rffi.cast(lltype.Signed, 0) else: timelib_time.c_i = rffi.cast(lltype.Signed, 0) timelib_time.c_s = rffi.cast(lltype.Signed, 0) timelib_time_dtor(tmp_timelib_time) timelib_update_ts(timelib_time, lltype.nullptr(timelib_tzinfo.TO)) timelib_update_from_sse(timelib_time) timelib_time.c_have_relative = rffi.cast(rffi.UINT, 0) return timelib_time, error
def memcpy(dst, src, count): if isinstance(src, Mem): src_pointer = src.pointer elif isinstance(src, Uint8Array): src_pointer = rffi.cast(rffi.VOIDP, src.uint8data) else: raise unwind(LTypeError(u"expected mem or array")) rffi.c_memcpy(dst.pointer, src_pointer, count.value) return dst
def memcpy(dst, src, count): if isinstance(src, Mem): src_pointer = src.pointer elif isinstance(src, Uint8Data): src_pointer = rffi.cast(rffi.VOIDP, src.uint8data) else: raise unwind(LTypeError(u"expected mem or array")) rffi.c_memcpy(dst.pointer, src_pointer, count.value) return dst
def allocate_ctxobj(src_ctx): p = lltype.malloc(PCTXOBJ.TO, flavor='raw', zero=True) if src_ctx: rffi.c_memcpy(rffi.cast(rffi.VOIDP, p.ctx), rffi.cast(rffi.VOIDP, src_ctx), rffi.cast(rffi.SIZE_T, rffi.sizeof(PCTX.TO))) p.info.c_ctx = p.ctx p.info.c_output = internal_output rffi.setintfield(p.info, 'c_output_size', FFI_COMPLEXITY_OUTPUT) return p
def copy_pixels(self, pixels, start, stop): offset = start * self.bpp assert offset >= 0 remaining_size = (self.width * self.height * self.bpp) - offset if remaining_size <= 0 or start >= stop: return nbytes = rffi.r_size_t(min((stop - start) * self.bpp, remaining_size)) pixbuf = rffi.ptradd(PIXELVOIDPP[0], offset) surfacebuf = rffi.ptradd(rffi.cast(rffi.VOIDP, pixels), offset) rffi.c_memcpy(pixbuf, surfacebuf, nbytes)
def convert_from_object(self, cdata, w_ob): if isinstance(w_ob, cdataobj.W_CData) and w_ob.ctype is self: length = w_ob.get_array_length() with w_ob as source: source = rffi.cast(rffi.VOIDP, source) target = rffi.cast(rffi.VOIDP, cdata) size = rffi.cast(rffi.SIZE_T, self.ctitem.size * length) rffi.c_memcpy(target, source, size) else: self.convert_array_from_object(cdata, w_ob)
def descr_copy(self, space): """ copy(array) Return a copy of the array. """ w_a = self.constructor(self.space) w_a.setlen(self.len, overallocate=False) rffi.c_memcpy(rffi.cast(rffi.VOIDP, w_a._buffer_as_unsigned()), rffi.cast(rffi.VOIDP, self._buffer_as_unsigned()), self.len * self.itemsize) return w_a
def descr_inplace_add(self, space, w_other): if not isinstance(w_other, W_Array): return space.w_NotImplemented oldlen = self.len otherlen = w_other.len self.setlen(oldlen + otherlen) if otherlen: rffi.c_memcpy( rffi.cast(rffi.VOIDP, rffi.ptradd(self.buffer, oldlen)), rffi.cast(rffi.VOIDP, w_other.buffer), otherlen * mytype.bytes) return self
def descr_inplace_add(self, space, w_other): if not isinstance(w_other, W_Array): return space.w_NotImplemented oldlen = self.len otherlen = w_other.len self.setlen(oldlen + otherlen) if otherlen: rffi.c_memcpy( rffi.cast(rffi.VOIDP, rffi.ptradd(self.buffer, oldlen)), rffi.cast(rffi.VOIDP, w_other.buffer), otherlen * mytype.bytes ) return self
def descr_copy(self, space): """ copy(array) Return a copy of the array. """ w_a = self.constructor(self.space) w_a.setlen(self.len, overallocate=False) rffi.c_memcpy( rffi.cast(rffi.VOIDP, w_a._buffer_as_unsigned()), rffi.cast(rffi.VOIDP, self._buffer_as_unsigned()), self.len * self.itemsize ) return w_a
def msg_send(socket, parts): for i, part in enumerate(parts): msg = rffi.lltype.malloc(rzmq.msg_t.TO, flavor='raw') rc = rzmq.msg_init_size(msg, len(part)) assert rc == 0 rffi.c_memcpy(rzmq.msg_data(msg), part, len(part)) if i < len(parts) - 1: msg_size = rzmq.msg_send(msg, socket, rzmq.SNDMORE) else: msg_size = rzmq.msg_send(msg, socket, 0) assert msg_size == len(part)
def _sqlite3VdbeSerialPut_with_length(self, buf, serial_type, length): flags = self.get_flags() if flags & CConfig.MEM_Null: return 0 if flags & (CConfig.MEM_Int | CConfig.MEM_Real): if flags & CConfig.MEM_Int: i = self.get_u_i() else: i = longlong2float.float2longlong(self.get_u_r()) _write_int_to_buf(buf, i, length) return length else: rffi.c_memcpy(rffi.cast(rffi.VOIDP, buf), rffi.cast(rffi.VOIDP, self.get_z()), length) return length
def _mul_helper(self, space, w_repeat, is_inplace): try: repeat = space.getindex_w(w_repeat, space.w_OverflowError) except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise if is_inplace: a = self start = 1 else: a = self.constructor(space) start = 0 if repeat <= start: if repeat <= 0: a.setlen(0, overallocate=False) return a oldlen = self.len try: newlen = ovfcheck(oldlen * repeat) except OverflowError: raise MemoryError # srcbuf = self._buffer srcsize = self.len * self.itemsize for i in range(srcsize): if srcbuf[i] != '\x00': break else: # the source is entirely zero: initialize the target # with zeroes too a.setlen(newlen, zero=True, overallocate=False) return a # a.setlen(newlen, overallocate=False) srcbuf = self._buffer # reload this, in case self is a if oldlen == 1: self._repeat_single_item(a, start, repeat) else: dstbuf = a._buffer if start == 1: dstbuf = rffi.ptradd(dstbuf, srcsize) for r in range(start, repeat): rffi.c_memcpy(rffi.cast(rffi.VOIDP, dstbuf), rffi.cast(rffi.VOIDP, srcbuf), srcsize) dstbuf = rffi.ptradd(dstbuf, srcsize) keepalive_until_here(self) keepalive_until_here(a) return a
def timelib_time_modify(timelib_time, modifier, tzi): ll_s = rffi.str2charp(modifier) error_c = lltype.malloc(timelib_error_containerP.TO, 1, flavor='raw') tmp_timelib_time = timelib_strtotime( ll_s, len(modifier), error_c, timelib_builtin_db(), tzinfo_callback ) lltype.free(error_c, flavor='raw') rffi.c_memcpy( rffi.cast(rffi.VOIDP, timelib_time.c_relative), rffi.cast(rffi.VOIDP, tmp_timelib_time.c_relative), rffi.sizeof(timelib_rel_time) ) timelib_time.c_have_relative = tmp_timelib_time.c_have_relative timelib_time.c_sse_uptodate = rffi.cast(rffi.UINT, 0) if intmask(tmp_timelib_time.c_y) != -99999: timelib_time.c_y = tmp_timelib_time.c_y if intmask(tmp_timelib_time.c_m) != -99999: timelib_time.c_m = tmp_timelib_time.c_m if intmask(tmp_timelib_time.c_d) != -99999: timelib_time.c_d = tmp_timelib_time.c_d if intmask(tmp_timelib_time.c_h) != -99999: timelib_time.c_h = tmp_timelib_time.c_h if intmask(tmp_timelib_time.c_i) != -99999: timelib_time.c_i = tmp_timelib_time.c_i if intmask(tmp_timelib_time.c_s) != -99999: timelib_time.c_s = tmp_timelib_time.c_s else: timelib_time.c_s = rffi.cast(lltype.Signed, 0) else: timelib_time.c_i = rffi.cast(lltype.Signed, 0) timelib_time.c_s = rffi.cast(lltype.Signed, 0) timelib_time_dtor(tmp_timelib_time) timelib_update_ts(timelib_time, lltype.nullptr(timelib_tzinfo.TO)) timelib_update_from_sse(timelib_time) timelib_time.c_have_relative = rffi.cast(rffi.UINT, 0) return timelib_time
def descr_add(self, space, w_other): if not isinstance(w_other, W_Array): return space.w_NotImplemented a = mytype.w_class(space) a.setlen(self.len + w_other.len, overallocate=False) if self.len: rffi.c_memcpy(rffi.cast(rffi.VOIDP, a.buffer), rffi.cast(rffi.VOIDP, self.buffer), self.len * mytype.bytes) if w_other.len: rffi.c_memcpy( rffi.cast(rffi.VOIDP, rffi.ptradd(a.buffer, self.len)), rffi.cast(rffi.VOIDP, w_other.buffer), w_other.len * mytype.bytes) return a
def setlen(self, size, zero=False, overallocate=True): if self._buffer: delta_memory_pressure = -self.allocated * self.itemsize else: delta_memory_pressure = 0 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 byte_size = self.allocated * self.itemsize delta_memory_pressure += byte_size if zero: new_buffer = lltype.malloc(rffi.CCHARP.TO, byte_size, flavor='raw', zero=True) else: new_buffer = lltype.malloc(rffi.CCHARP.TO, byte_size, flavor='raw') copy_bytes = min(size, self.len) * self.itemsize rffi.c_memcpy(rffi.cast(rffi.VOIDP, new_buffer), rffi.cast(rffi.VOIDP, self._buffer), copy_bytes) else: self.len = size return else: assert size == 0 self.allocated = 0 new_buffer = lltype.nullptr(rffi.CCHARP.TO) if self._buffer: lltype.free(self._buffer, flavor='raw') self._buffer = new_buffer self.len = size # adds the difference between the old and the new raw-malloced # size. If setlen() is called a lot on the same array object, # it is important to take into account the fact that we also do # lltype.free() above. rgc.add_memory_pressure(delta_memory_pressure)
def raw_storage_getitem_unaligned(TP, storage, index): if misaligned_is_fine: if we_are_translated(): return raw_storage_getitem(TP, storage, index) else: return _raw_storage_getitem_unchecked(TP, storage, index) mask = _get_alignment_mask(TP) if (index & mask) == 0: if we_are_translated(): return raw_storage_getitem(TP, storage, index) else: return _raw_storage_getitem_unchecked(TP, storage, index) ptr = rffi.ptradd(storage, index) with lltype.scoped_alloc(rffi.CArray(TP), 1) as s_array: rffi.c_memcpy(rffi.cast(rffi.VOIDP, s_array), rffi.cast(rffi.VOIDP, ptr), rffi.sizeof(TP)) return rffi.cast(rffi.CArrayPtr(TP), s_array)[0]
def descr_inplace_add(self, space, w_other): if (not isinstance(w_other, W_ArrayBase) or w_other.typecode != self.typecode): return space.w_NotImplemented oldlen = self.len otherlen = w_other.len self.setlen(oldlen + otherlen) if otherlen: rffi.c_memcpy( rffi.cast(rffi.VOIDP, rffi.ptradd(self._buffer, oldlen * self.itemsize)), rffi.cast(rffi.VOIDP, w_other._buffer), otherlen * self.itemsize) keepalive_until_here(self) keepalive_until_here(w_other) return self
def bytes_attach(space, py_obj, w_obj): """ Copy RPython string object contents to a PyBytesObject. The c_ob_sval must not be modified. """ py_str = rffi.cast(PyBytesObject, py_obj) s = space.str_w(w_obj) if py_str.c_ob_size < len(s): raise oefmt(space.w_ValueError, "bytes_attach called on object with ob_size %d but trying to store %d", py_str.c_ob_size, len(s)) with rffi.scoped_nonmovingbuffer(s) as s_ptr: rffi.c_memcpy(py_str.c_ob_sval, s_ptr, len(s)) py_str.c_ob_sval[len(s)] = '\0' py_str.c_ob_shash = space.hash_w(w_obj) py_str.c_ob_sstate = rffi.cast(rffi.INT, 1) # SSTATE_INTERNED_MORTAL
def timelib_time_modify(timelib_time, modifier, tzi): ll_s = rffi.str2charp(modifier) error_c = lltype.malloc(timelib_error_containerP.TO, 1, flavor='raw') tmp_timelib_time = timelib_strtotime(ll_s, len(modifier), error_c, timelib_builtin_db(), tzinfo_callback) lltype.free(error_c, flavor='raw') rffi.c_memcpy(rffi.cast(rffi.VOIDP, timelib_time.c_relative), rffi.cast(rffi.VOIDP, tmp_timelib_time.c_relative), rffi.sizeof(timelib_rel_time)) timelib_time.c_have_relative = tmp_timelib_time.c_have_relative timelib_time.c_sse_uptodate = rffi.cast(rffi.UINT, 0) if intmask(tmp_timelib_time.c_y) != -99999: timelib_time.c_y = tmp_timelib_time.c_y if intmask(tmp_timelib_time.c_m) != -99999: timelib_time.c_m = tmp_timelib_time.c_m if intmask(tmp_timelib_time.c_d) != -99999: timelib_time.c_d = tmp_timelib_time.c_d if intmask(tmp_timelib_time.c_h) != -99999: timelib_time.c_h = tmp_timelib_time.c_h if intmask(tmp_timelib_time.c_i) != -99999: timelib_time.c_i = tmp_timelib_time.c_i if intmask(tmp_timelib_time.c_s) != -99999: timelib_time.c_s = tmp_timelib_time.c_s else: timelib_time.c_s = rffi.cast(lltype.Signed, 0) else: timelib_time.c_i = rffi.cast(lltype.Signed, 0) timelib_time.c_s = rffi.cast(lltype.Signed, 0) timelib_time_dtor(tmp_timelib_time) timelib_update_ts(timelib_time, lltype.nullptr(timelib_tzinfo.TO)) timelib_update_from_sse(timelib_time) timelib_time.c_have_relative = rffi.cast(rffi.UINT, 0) return timelib_time
def read_ptr(ptr, ofs, TP): T = lltype.Ptr(rffi.CArray(TP)) for c in unroll_letters_for_floats: # Note: if we are on ARM and have a float-ish value that is not word # aligned accessing it directly causes a SIGBUS. Instead we use memcpy # to avoid the problem if (_ARM and LL_TYPEMAP[c] is TP and rffi.cast(lltype.Signed, ptr) & 3 != 0): if ofs != 0: ptr = rffi.ptradd(ptr, ofs * rffi.sizeof(TP)) with lltype.scoped_alloc(T.TO, 1) as t_array: rffi.c_memcpy(rffi.cast(rffi.VOIDP, t_array), rffi.cast(rffi.VOIDP, ptr), rffi.sizeof(TP)) ptr_val = t_array[0] return ptr_val else: return rffi.cast(T, ptr)[ofs]
def store(self, pool, offset, value): if isinstance(value, Mem): ctype = value.ctype if isinstance(ctype, Pointer) and ctype.to is self: rffi.c_memcpy(offset, value.pointer, sizeof(self)) return value if isinstance(value, Dict): for key, obj in value.data.iteritems(): if not isinstance(key, String): raise unwind(LTypeError(u"dictionary fields must be string fields for union.store to work")) try: x, ctype = self.namespace[key.string] except KeyError as k: raise unwind(LTypeError(u"%s not in %s" % (key.repr(), self.repr()))) ctype.store(pool, rffi.ptradd(offset, x), obj) return value raise unwind(LTypeError(u"cannot union store " + value.repr()))
def _do_setslice(self, w_slice, w_value, ptr): ctptr, start, length = self._do_getslicearg(w_slice) ctitem = ctptr.ctitem ctitemsize = ctitem.size target = rffi.ptradd(ptr, start * ctitemsize) # if isinstance(w_value, W_CData): from pypy.module._cffi_backend import ctypearray ctv = w_value.ctype if (isinstance(ctv, ctypearray.W_CTypeArray) and ctv.ctitem is ctitem and w_value.get_array_length() == length): # fast path: copying from exactly the correct type with w_value as source: source = rffi.cast(rffi.VOIDP, source) target = rffi.cast(rffi.VOIDP, target) size = rffi.cast(rffi.SIZE_T, ctitemsize * length) rffi.c_memcpy(target, source, size) return # # A fast path for <char[]>[0:N] = "somestring" or some bytearray. from pypy.module._cffi_backend import ctypeprim space = self.space if isinstance(ctitem, ctypeprim.W_CTypePrimitive) and ctitem.size == 1: if space.isinstance_w(w_value, space.w_bytes): from rpython.rtyper.annlowlevel import llstr from rpython.rtyper.lltypesystem.rstr import copy_string_to_raw value = space.bytes_w(w_value) if len(value) != length: raise oefmt(space.w_ValueError, "need a string of length %d, got %d", length, len(value)) copy_string_to_raw(llstr(value), target, 0, length) return if space.isinstance_w(w_value, space.w_bytearray): value = w_value.bytearray_list_of_chars_w(space) if len(value) != length: raise oefmt(space.w_ValueError, "need a bytearray of length %d, got %d", length, len(value)) self._copy_list_of_chars_to_raw(value, target, length) return # self._do_setslice_iterate(space, ctitem, w_value, target, ctitemsize, length)
def _do_setslice(self, w_slice, w_value, ptr): ctptr, start, length = self._do_getslicearg(w_slice) ctitem = ctptr.ctitem ctitemsize = ctitem.size target = rffi.ptradd(ptr, start * ctitemsize) # if isinstance(w_value, W_CData): from pypy.module._cffi_backend import ctypearray ctv = w_value.ctype if ( isinstance(ctv, ctypearray.W_CTypeArray) and ctv.ctitem is ctitem and w_value.get_array_length() == length ): # fast path: copying from exactly the correct type with w_value as source: source = rffi.cast(rffi.VOIDP, source) target = rffi.cast(rffi.VOIDP, target) size = rffi.cast(rffi.SIZE_T, ctitemsize * length) rffi.c_memcpy(target, source, size) return # # A fast path for <char[]>[0:N] = "somestring" or some bytearray. from pypy.module._cffi_backend import ctypeprim space = self.space if isinstance(ctitem, ctypeprim.W_CTypePrimitive) and ctitem.size == 1: if space.isinstance_w(w_value, space.w_str): from rpython.rtyper.annlowlevel import llstr from rpython.rtyper.lltypesystem.rstr import copy_string_to_raw value = space.str_w(w_value) if len(value) != length: raise oefmt(space.w_ValueError, "need a string of length %d, got %d", length, len(value)) copy_string_to_raw(llstr(value), target, 0, length) return if space.isinstance_w(w_value, space.w_bytearray): value = w_value.bytearray_list_of_chars_w(space) if len(value) != length: raise oefmt(space.w_ValueError, "need a bytearray of length %d, got %d", length, len(value)) self._copy_list_of_chars_to_raw(value, target, length) return # self._do_setslice_iterate(space, ctitem, w_value, target, ctitemsize, length)
def descr_add(self, space, w_other): if not isinstance(w_other, W_Array): return space.w_NotImplemented a = mytype.w_class(space) a.setlen(self.len + w_other.len, overallocate=False) if self.len: rffi.c_memcpy( rffi.cast(rffi.VOIDP, a.buffer), rffi.cast(rffi.VOIDP, self.buffer), self.len * mytype.bytes ) if w_other.len: rffi.c_memcpy( rffi.cast(rffi.VOIDP, rffi.ptradd(a.buffer, self.len)), rffi.cast(rffi.VOIDP, w_other.buffer), w_other.len * mytype.bytes ) return a
def _do_setslice(self, w_slice, w_value): ctptr, start, length = self._do_getslicearg(w_slice) ctitem = ctptr.ctitem ctitemsize = ctitem.size cdata = rffi.ptradd(self._cdata, start * ctitemsize) # if isinstance(w_value, W_CData): from pypy.module._cffi_backend import ctypearray ctv = w_value.ctype if (isinstance(ctv, ctypearray.W_CTypeArray) and ctv.ctitem is ctitem and w_value.get_array_length() == length): # fast path: copying from exactly the correct type s = w_value._cdata rffi.c_memcpy(cdata, s, ctitemsize * length) keepalive_until_here(w_value) return # # A fast path for <char[]>[0:N] = "somestring". from pypy.module._cffi_backend import ctypeprim space = self.space if (space.isinstance_w(w_value, space.w_str) and isinstance(ctitem, ctypeprim.W_CTypePrimitiveChar)): from rpython.rtyper.annlowlevel import llstr from rpython.rtyper.lltypesystem.rstr import copy_string_to_raw value = space.str_w(w_value) if len(value) != length: raise oefmt(space.w_ValueError, "need a string of length %d, got %d", length, len(value)) copy_string_to_raw(llstr(value), cdata, 0, length) return # w_iter = space.iter(w_value) for i in range(length): try: w_item = space.next(w_iter) except OperationError, e: if not e.match(space, space.w_StopIteration): raise raise oefmt(space.w_ValueError, "need %d values to unpack, got %d", length, i) ctitem.convert_from_object(cdata, w_item) cdata = rffi.ptradd(cdata, ctitemsize)
def read_ptr(ptr, ofs, TP): T = lltype.Ptr(rffi.CArray(TP)) for c in unroll_letters_for_floats: # Note: if we are on ARM and have a float-ish value that is not word # aligned accessing it directly causes a SIGBUS. Instead we use memcpy # to avoid the problem if (_ARM and LL_TYPEMAP[c] is TP and rffi.cast(lltype.Signed, ptr) & 3 != 0): if ofs != 0: ptr = rffi.ptradd(ptr, ofs*rffi.sizeof(TP)) with lltype.scoped_alloc(T.TO, 1) as t_array: rffi.c_memcpy( rffi.cast(rffi.VOIDP, t_array), rffi.cast(rffi.VOIDP, ptr), rffi.sizeof(TP)) ptr_val = t_array[0] return ptr_val else: return rffi.cast(T, ptr)[ofs]
def descr_add(self, space, w_other): if (not isinstance(w_other, W_ArrayBase) or w_other.typecode != self.typecode): return space.w_NotImplemented a = self.constructor(space) a.setlen(self.len + w_other.len, overallocate=False) if self.len: rffi.c_memcpy(rffi.cast(rffi.VOIDP, a._buffer), rffi.cast(rffi.VOIDP, self._buffer), self.len * self.itemsize) if w_other.len: rffi.c_memcpy( rffi.cast(rffi.VOIDP, rffi.ptradd(a._buffer, self.len * self.itemsize)), rffi.cast(rffi.VOIDP, w_other._buffer), w_other.len * self.itemsize) keepalive_until_here(self) keepalive_until_here(a) return a
def bytes_attach(space, py_obj, w_obj, w_userdata=None): """ Copy RPython string object contents to a PyBytesObject. The c_ob_sval must not be modified. """ py_str = rffi.cast(PyBytesObject, py_obj) s = space.bytes_w(w_obj) len_s = len(s) if py_str.c_ob_size < len_s: raise oefmt( space.w_ValueError, "bytes_attach called on object with ob_size %d but trying to store %d", py_str.c_ob_size, len_s) with rffi.scoped_nonmovingbuffer(s) as s_ptr: rffi.c_memcpy(py_str.c_ob_sval, s_ptr, len_s) py_str.c_ob_sval[len_s] = '\0' # if py_obj has a tp_hash, this will try to call it, but the objects are # not fully linked yet #py_str.c_ob_shash = space.hash_w(w_obj) py_str.c_ob_shash = space.hash_w(space.newbytes(s)) py_str.c_ob_sstate = rffi.cast(rffi.INT, 1) # SSTATE_INTERNED_MORTAL
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 byte_size = self.allocated * self.itemsize if zero: new_buffer = lltype.malloc(rffi.CCHARP.TO, byte_size, flavor='raw', add_memory_pressure=True, zero=True) else: new_buffer = lltype.malloc(rffi.CCHARP.TO, byte_size, flavor='raw', add_memory_pressure=True) copy_bytes = min(size, self.len) * self.itemsize rffi.c_memcpy(rffi.cast(rffi.VOIDP, new_buffer), rffi.cast(rffi.VOIDP, self._buffer), copy_bytes) else: self.len = size return else: assert size == 0 self.allocated = 0 new_buffer = lltype.nullptr(rffi.CCHARP.TO) if self._buffer: lltype.free(self._buffer, flavor='raw') self._buffer = new_buffer self.len = size
def _memcpy_full_hidden(self, from_): rffi.c_memcpy(rffi.cast(rffi.VOIDP, self.pMem), rffi.cast(rffi.VOIDP, from_.pMem), rffi.sizeof(capi.MEM))
def handle_struct(self, w_ffitype, w_structinstance): rawmem = rffi.cast(rffi.CCHARP, self.rawmem) dst = rffi.cast(rffi.VOIDP, rffi.ptradd(rawmem, self.offset)) src = w_structinstance.rawmem length = w_ffitype.sizeof() rffi.c_memcpy(dst, src, length)
def copy(self): new = HAVAL256_5Hash() rffi.c_memcpy(rffi.cast(rffi.VOIDP, new.ctx), rffi.cast(rffi.VOIDP, self.ctx), rffi.sizeof(chaval.HAVAL_CTX_PTR.TO)) return new
def copy(self): new = TIGER192_4Hash() rffi.c_memcpy(rffi.cast(rffi.VOIDP, new.ctx), rffi.cast(rffi.VOIDP, self.ctx), rffi.sizeof(ctiger.PTR_TIGER_CTX.TO)) return new
def copy(self): new = MD4Hash() rffi.c_memcpy(rffi.cast(rffi.VOIDP, new.ctx), rffi.cast(rffi.VOIDP, self.ctx), rffi.sizeof(cmd.PTR_MD4_CTX.TO)) return new
def copy(self): new = SHA512Hash() rffi.c_memcpy(rffi.cast(rffi.VOIDP, new.ctx), rffi.cast(rffi.VOIDP, self.ctx), rffi.sizeof(csha.PTR_SHA512_CTX.TO)) return new
def copy(self): new = SNEFRU256Hash() rffi.c_memcpy(rffi.cast(rffi.VOIDP, new.ctx), rffi.cast(rffi.VOIDP, self.ctx), rffi.sizeof(csnefru.PTR_SNEFRU_CTX.TO)) return new
def _memcpy_partial_hidden(self, from_): MEMCELLSIZE = rffi.offsetof(capi.MEM, 'zMalloc') rffi.c_memcpy(rffi.cast(rffi.VOIDP, self.pMem), rffi.cast(rffi.VOIDP, from_.pMem), MEMCELLSIZE)
def copy(self): new = JOAATHash() rffi.c_memcpy(rffi.cast(rffi.VOIDP, new.ctx), rffi.cast(rffi.VOIDP, self.ctx), rffi.sizeof(cjoaat.PTR_JOAAT_CTX.TO)) return new
def copy(self): new = GOSTHash() rffi.c_memcpy(rffi.cast(rffi.VOIDP, new.ctx), rffi.cast(rffi.VOIDP, self.ctx), rffi.sizeof(cgost.PTR_GOST_CTX.TO)) return new
def timelib_time_modify(timelib_time, modifier, tzi): error = '' ll_s = rffi.str2charp(modifier) error_c = lltype.malloc(timelib_error_containerP.TO, 1, flavor='raw') tmp_timelib_time = timelib_strtotime( ll_s, len(modifier), error_c, timelib_builtin_db(), tzinfo_callback ) error_count = rffi.cast(lltype.Signed, error_c[0].c_error_count) if error_count: position = int(error_c[0].c_error_messages[0].c_position) message = rffi.charp2str(error_c[0].c_error_messages[0].c_message) char = error_c[0].c_error_messages[0].c_character error = "Failed to parse time string (%s) at position %s (%s): %s" % ( modifier, position, char, message ) lltype.free(error_c, flavor='raw') rffi.c_memcpy( rffi.cast(rffi.VOIDP, timelib_time.c_relative), rffi.cast(rffi.VOIDP, tmp_timelib_time.c_relative), rffi.sizeof(timelib_rel_time) ) timelib_time.c_have_relative = tmp_timelib_time.c_have_relative timelib_time.c_sse_uptodate = rffi.cast(rffi.UINT, 0) if intmask(tmp_timelib_time.c_y) != -99999: timelib_time.c_y = tmp_timelib_time.c_y if intmask(tmp_timelib_time.c_m) != -99999: timelib_time.c_m = tmp_timelib_time.c_m if intmask(tmp_timelib_time.c_d) != -99999: timelib_time.c_d = tmp_timelib_time.c_d if intmask(tmp_timelib_time.c_h) != -99999: timelib_time.c_h = tmp_timelib_time.c_h if intmask(tmp_timelib_time.c_i) != -99999: timelib_time.c_i = tmp_timelib_time.c_i if intmask(tmp_timelib_time.c_s) != -99999: timelib_time.c_s = tmp_timelib_time.c_s else: timelib_time.c_s = rffi.cast(lltype.Signed, 0) else: timelib_time.c_i = rffi.cast(lltype.Signed, 0) timelib_time.c_s = rffi.cast(lltype.Signed, 0) timelib_time_dtor(tmp_timelib_time) timelib_update_ts(timelib_time, lltype.nullptr(timelib_tzinfo.TO)) timelib_update_from_sse(timelib_time) timelib_time.c_have_relative = rffi.cast(rffi.UINT, 0) return timelib_time, error