def copy_and_convert_to_object(self, source): space = self.space self.check_complete() ptr = lltype.malloc(rffi.CCHARP.TO, self.size, flavor='raw', zero=False) misc._raw_memcopy(source, ptr, self.size) return cdataobj.W_CDataNewStd(space, ptr, self)
def allocate(self, space, datasize, ctype, length=-1): from pypy.module._cffi_backend import cdataobj, ctypeptr if self.w_alloc is None: if self.should_clear_after_alloc: ptr = lltype.malloc(rffi.CCHARP.TO, datasize, flavor='raw', zero=True) else: ptr = lltype.malloc(rffi.CCHARP.TO, datasize, flavor='raw', zero=False) return cdataobj.W_CDataNewStd(space, ptr, ctype, length) else: w_raw_cdata = space.call_function(self.w_alloc, space.wrap(datasize)) if not isinstance(w_raw_cdata, cdataobj.W_CData): raise oefmt(space.w_TypeError, "alloc() must return a cdata object (got %T)", w_raw_cdata) if not isinstance(w_raw_cdata.ctype, ctypeptr.W_CTypePtrOrArray): raise oefmt(space.w_TypeError, "alloc() must return a cdata pointer, not '%s'", w_raw_cdata.ctype.name) # ptr = w_raw_cdata.unsafe_escaping_ptr() if not ptr: raise oefmt(space.w_MemoryError, "alloc() returned NULL") # if self.should_clear_after_alloc: rffi.c_memset(rffi.cast(rffi.VOIDP, ptr), 0, rffi.cast(rffi.SIZE_T, datasize)) # if self.w_free is None: # use this class which does not have a __del__, but still # keeps alive w_raw_cdata res = cdataobj.W_CDataNewNonStdNoFree(space, ptr, ctype, length) else: res = cdataobj.W_CDataNewNonStdFree(space, ptr, ctype, length) res.w_free = self.w_free res.w_raw_cdata = w_raw_cdata return res
def allocate(self, space, datasize, ctype, length=-1): from pypy.module._cffi_backend import cdataobj, ctypeptr if self.w_alloc is None: if self.should_clear_after_alloc: ptr = lltype.malloc(rffi.CCHARP.TO, datasize, flavor='raw', zero=True, add_memory_pressure=True) else: ptr = lltype.malloc(rffi.CCHARP.TO, datasize, flavor='raw', zero=False, add_memory_pressure=True) return cdataobj.W_CDataNewStd(space, ptr, ctype, length) else: w_raw_cdata = space.call_function(self.w_alloc, space.newint(datasize)) if not isinstance(w_raw_cdata, cdataobj.W_CData): raise oefmt(space.w_TypeError, "alloc() must return a cdata object (got %T)", w_raw_cdata) if not isinstance(w_raw_cdata.ctype, ctypeptr.W_CTypePtrOrArray): raise oefmt(space.w_TypeError, "alloc() must return a cdata pointer, not '%s'", w_raw_cdata.ctype.name) # ptr = w_raw_cdata.unsafe_escaping_ptr() if not ptr: raise oefmt(space.w_MemoryError, "alloc() returned NULL") # if self.should_clear_after_alloc: rffi.c_memset(rffi.cast(rffi.VOIDP, ptr), 0, rffi.cast(rffi.SIZE_T, datasize)) # res = cdataobj.W_CDataNewNonStd(space, ptr, ctype, length) res.w_raw_cdata = w_raw_cdata if self.w_free is not None: res.w_free = self.w_free res.register_finalizer(space) rgc.add_memory_pressure(datasize) return res