| 1 |
from pypy.interpreter.error import oefmt |
| 2 |
from pypy.interpreter.baseobjspace import W_Root |
| 3 |
from pypy.interpreter.typedef import TypeDef |
| 4 |
from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault |
| 5 |
|
| 6 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 7 |
from rpython.rlib import rgc |
| 8 |
|
| 9 |
|
| 10 |
class W_Allocator(W_Root): |
| 11 |
_immutable_ = True |
| 12 |
|
| 13 |
def __init__(self, ffi, w_alloc, w_free, should_clear_after_alloc): |
| 14 |
self.ffi = ffi |
| 15 |
self.w_alloc = w_alloc |
| 16 |
self.w_free = w_free |
| 17 |
self.should_clear_after_alloc = should_clear_after_alloc |
| 18 |
|
| 19 |
def allocate(self, space, datasize, ctype, length=-1): |
| 20 |
from pypy.module._cffi_backend import cdataobj, ctypeptr |
| 21 |
if self.w_alloc is None: |
| 22 |
if self.should_clear_after_alloc: |
| 23 |
ptr = lltype.malloc(rffi.CCHARP.TO, datasize, |
| 24 |
flavor='raw', zero=True) |
| 25 |
else: |
| 26 |
ptr = lltype.malloc(rffi.CCHARP.TO, datasize, |
| 27 |
flavor='raw', zero=False) |
| 28 |
w_res = cdataobj.W_CDataNewStd(space, ptr, ctype, length, datasize) |
| 29 |
rgc.add_memory_pressure(datasize, w_res) |
| 30 |
return w_res |
| 31 |
else: |
| 32 |
w_raw_cdata = space.call_function(self.w_alloc, |
| 33 |
space.newint(datasize)) |
| 34 |
if not isinstance(w_raw_cdata, cdataobj.W_CData): |
| 35 |
raise oefmt(space.w_TypeError, |
| 36 |
"alloc() must return a cdata object (got %T)", |
| 37 |
w_raw_cdata) |
| 38 |
if not isinstance(w_raw_cdata.ctype, ctypeptr.W_CTypePtrOrArray): |
| 39 |
raise oefmt(space.w_TypeError, |
| 40 |
"alloc() must return a cdata pointer, not '%s'", |
| 41 |
w_raw_cdata.ctype.name) |
| 42 |
|
| 43 |
ptr = w_raw_cdata.unsafe_escaping_ptr() |
| 44 |
if not ptr: |
| 45 |
raise oefmt(space.w_MemoryError, "alloc() returned NULL") |
| 46 |
|
| 47 |
if self.should_clear_after_alloc: |
| 48 |
rffi.c_memset(rffi.cast(rffi.VOIDP, ptr), 0, |
| 49 |
rffi.cast(rffi.SIZE_T, datasize)) |
| 50 |
|
| 51 |
res = cdataobj.W_CDataNewNonStd(space, ptr, ctype, length) |
| 52 |
res.w_raw_cdata = w_raw_cdata |
| 53 |
if self.w_free is not None: |
| 54 |
res.w_free = self.w_free |
| 55 |
res.register_finalizer(space) |
| 56 |
rgc.add_memory_pressure(datasize, res) |
| 57 |
return res |
| 58 |
|
| 59 |
@unwrap_spec(w_init=WrappedDefault(None)) |
| 60 |
def descr_call(self, space, w_arg, w_init): |
| 61 |
ffi = self.ffi |
| 62 |
assert ffi is not None |
| 63 |
w_ctype = ffi.ffi_type(w_arg, ffi.ACCEPT_STRING | ffi.ACCEPT_CTYPE) |
| 64 |
return w_ctype.newp(w_init, self) |
| 65 |
|
| 66 |
|
| 67 |
W_Allocator.typedef = TypeDef( |
| 68 |
'_cffi_backend.__FFIAllocator', |
| 69 |
__call__ = interp2app(W_Allocator.descr_call), |
| 70 |
) |
| 71 |
W_Allocator.typedef.acceptable_as_base_class = False |
| 72 |
|
| 73 |
|
| 74 |
def new_allocator(ffi, w_alloc, w_free, should_clear_after_alloc): |
| 75 |
space = ffi.space |
| 76 |
if space.is_none(w_alloc): |
| 77 |
w_alloc = None |
| 78 |
if space.is_none(w_free): |
| 79 |
w_free = None |
| 80 |
if w_alloc is None and w_free is not None: |
| 81 |
raise oefmt(space.w_TypeError, "cannot pass 'free' without 'alloc'") |
| 82 |
alloc = W_Allocator(ffi, w_alloc, w_free, bool(should_clear_after_alloc)) |
| 83 |
return alloc |
| 84 |
|
| 85 |
|
| 86 |
default_allocator = W_Allocator(None, None, None, should_clear_after_alloc=True) |
| 87 |
nonzero_allocator = W_Allocator(None, None, None,should_clear_after_alloc=False) |