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 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 pool_alloc(pool, size): if isinstance(pool, Pool): pointer = lltype.malloc(rffi.VOIDP.TO, size, flavor='raw') pool.mark(pointer) if pool.autoclear: rffi.c_memset(pointer, 0, size) return pointer elif pool is None: raise unwind(LError(u"Would have to allocate to do this action")) else: raise unwind(LError(u"pool alloc with user objects not implemented"))
def Pool_alloc(pool, ctype, count, clear): if count: n = count.value size = sizeof_a(ctype, n) else: n = 1 size = sizeof(ctype) pointer = lltype.malloc(rffi.VOIDP.TO, size, flavor='raw') pool.mark(pointer) if (pool.autoclear and clear is None) or is_true(clear): rffi.c_memset(pointer, 0, size) return Mem(Pointer(ctype), pointer, n, pool)
def automem(ctype, count, clear): ctype = to_type(ctype) if count: n = count.value size = simple.sizeof_a(ctype, n) else: n = 1 size = simple.sizeof(ctype) pointer = lltype.malloc(rffi.VOIDP.TO, size, flavor='raw') if is_true(clear): rffi.c_memset(pointer, 0, size) return systemv.AutoMem(systemv.Pointer(ctype), pointer, n)
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
def memset(mem, num, count): rffi.c_memset(mem.pointer, num.value, count.value) return null