Author: Armin Rigo <ar...@tunes.org> Branch: ffi-backend Changeset: r55775:c0767a4e786b Date: 2012-06-23 13:05 +0200 http://bitbucket.org/pypy/pypy/changeset/c0767a4e786b/
Log: alignof diff --git a/pypy/module/_ffi_backend/__init__.py b/pypy/module/_ffi_backend/__init__.py --- a/pypy/module/_ffi_backend/__init__.py +++ b/pypy/module/_ffi_backend/__init__.py @@ -16,4 +16,5 @@ 'newp': 'func.newp', 'cast': 'func.cast', 'sizeof': 'func.sizeof', + 'alignof': 'func.alignof', } diff --git a/pypy/module/_ffi_backend/ctypeobj.py b/pypy/module/_ffi_backend/ctypeobj.py --- a/pypy/module/_ffi_backend/ctypeobj.py +++ b/pypy/module/_ffi_backend/ctypeobj.py @@ -71,6 +71,9 @@ name_position = self.name_position + extra_position return name, name_position + def alignof(self): + xxx + class W_CTypePtrOrArray(W_CType): @@ -132,6 +135,10 @@ p = rffi.ptradd(cdata, i * self.ctitem.size) return cdataobj.W_CData(self.space, p, self) + def alignof(self): + from pypy.module._ffi_backend import newtype + return newtype.alignment_of_pointer + class W_CTypeArray(W_CTypePtrOrArray): @@ -141,6 +148,9 @@ self.length = length self.ctptr = ctptr + def alignof(self): + return self.ctitem.alignof() + def newp(self, w_init): space = self.space datasize = self.size @@ -209,6 +219,13 @@ class W_CTypePrimitive(W_CType): + def __init__(self, space, size, name, name_position, align): + W_CType.__init__(self, space, size, name, name_position) + self.align = align + + def alignof(self): + return self.align + def cast_single_char(self, w_ob): space = self.space s = space.str_w(w_ob) diff --git a/pypy/module/_ffi_backend/func.py b/pypy/module/_ffi_backend/func.py --- a/pypy/module/_ffi_backend/func.py +++ b/pypy/module/_ffi_backend/func.py @@ -1,7 +1,8 @@ from pypy.interpreter.error import OperationError, operationerrfmt from pypy.interpreter.baseobjspace import Wrappable from pypy.interpreter.gateway import interp2app, unwrap_spec -from pypy.rpython.lltypesystem import lltype, rffi +from pypy.rpython.lltypesystem import lltype, llmemory, rffi +from pypy.rlib.objectmodel import we_are_translated from pypy.module._ffi_backend import ctypeobj, cdataobj @@ -35,3 +36,12 @@ raise OperationError(space.w_TypeError, space.wrap("expected a 'cdata' or 'ctype' object")) return space.wrap(size) + +@unwrap_spec(ctype=ctypeobj.W_CType) +def alignof(space, ctype): + align = ctype.alignof() + if not we_are_translated(): + # obscure hack when untranslated, maybe, approximate, don't use + assert isinstance(align, llmemory.FieldOffset) + align = rffi.sizeof(align.TYPE.y) + return space.wrap(align) diff --git a/pypy/module/_ffi_backend/newtype.py b/pypy/module/_ffi_backend/newtype.py --- a/pypy/module/_ffi_backend/newtype.py +++ b/pypy/module/_ffi_backend/newtype.py @@ -6,13 +6,19 @@ from pypy.module._ffi_backend import ctypeobj +def alignment(TYPE): + S = lltype.Struct('aligncheck', ('x', lltype.Char), ('y', TYPE)) + return rffi.offsetof(S, 'y') + +alignment_of_pointer = alignment(rffi.CCHARP) + # ____________________________________________________________ PRIMITIVE_TYPES = {} def eptype(name, TYPE, ctypecls): - PRIMITIVE_TYPES[name] = ctypecls, rffi.sizeof(TYPE) + PRIMITIVE_TYPES[name] = ctypecls, rffi.sizeof(TYPE), alignment(TYPE) eptype("char", lltype.Char, ctypeobj.W_CTypePrimitiveChar) eptype("signed char", rffi.SIGNEDCHAR, ctypeobj.W_CTypePrimitiveSigned) @@ -31,10 +37,10 @@ @unwrap_spec(name=str) def new_primitive_type(space, name): try: - ctypecls, size = PRIMITIVE_TYPES[name] + ctypecls, size, align = PRIMITIVE_TYPES[name] except KeyError: raise OperationError(space.w_KeyError, space.wrap(name)) - ctype = ctypecls(space, size, name, len(name)) + ctype = ctypecls(space, size, name, len(name), align) return ctype # ____________________________________________________________ _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit