[pypy-commit] pypy ffistruct: low level support for pointer fields

antocuni noreply at buildbot.pypy.org
Thu Nov 10 11:07:20 CET 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: ffistruct
Changeset: r49116:fe97346f8494
Date: 2011-11-09 22:44 +0100
http://bitbucket.org/pypy/pypy/changeset/fe97346f8494/

Log:	low level support for pointer fields

diff --git a/pypy/rlib/clibffi.py b/pypy/rlib/clibffi.py
--- a/pypy/rlib/clibffi.py
+++ b/pypy/rlib/clibffi.py
@@ -233,6 +233,10 @@
     (rffi.LONGDOUBLE, ffi_type_longdouble),
     ]
 
+__ptr_type_map = [
+    (rffi.VOIDP, ffi_type_pointer),
+    ]
+
 __type_map = __int_type_map + __float_type_map + [
     (lltype.Void, ffi_type_void)
     ]
@@ -242,10 +246,11 @@
 TYPE_MAP = dict(__type_map)
 
 ffitype_map_int = unrolling_iterable(__int_type_map)
+ffitype_map_int_or_ptr = unrolling_iterable(__int_type_map + __ptr_type_map)
 ffitype_map_float = unrolling_iterable(__float_type_map)
 ffitype_map = unrolling_iterable(__type_map)
 
-del __int_type_map, __float_type_map, __type_map
+del __int_type_map, __float_type_map, __ptr_type_map, __type_map
 
 
 def external(name, args, result, **kwds):
diff --git a/pypy/rlib/libffi.py b/pypy/rlib/libffi.py
--- a/pypy/rlib/libffi.py
+++ b/pypy/rlib/libffi.py
@@ -420,7 +420,7 @@
     Return the field of type ``ffitype`` at ``addr+offset``, widened to
     lltype.Signed.
     """
-    for TYPE, ffitype2 in clibffi.ffitype_map_int:
+    for TYPE, ffitype2 in clibffi.ffitype_map_int_or_ptr:
         if ffitype is ffitype2:
             value = _struct_getfield(TYPE, addr, offset)
             return rffi.cast(lltype.Signed, value)
@@ -433,7 +433,7 @@
     Set the field of type ``ffitype`` at ``addr+offset``.  ``value`` is of
     type lltype.Signed, and it's automatically converted to the right type.
     """
-    for TYPE, ffitype2 in clibffi.ffitype_map_int:
+    for TYPE, ffitype2 in clibffi.ffitype_map_int_or_ptr:
         if ffitype is ffitype2:
             value = rffi.cast(TYPE, value)
             _struct_setfield(TYPE, addr, offset, value)
diff --git a/pypy/rlib/test/test_libffi.py b/pypy/rlib/test/test_libffi.py
--- a/pypy/rlib/test/test_libffi.py
+++ b/pypy/rlib/test/test_libffi.py
@@ -59,20 +59,26 @@
         longsize = 4 if IS_32_BIT else 8
         POINT = lltype.Struct('POINT',
                               ('x', rffi.LONG),
-                              ('y', rffi.SHORT)
+                              ('y', rffi.SHORT),
+                              ('z', rffi.VOIDP),
                               )
         y_ofs = longsize
+        z_ofs = longsize*2
         p = lltype.malloc(POINT, flavor='raw')
         p.x = 42
         p.y = rffi.cast(rffi.SHORT, -1)
+        p.z = rffi.cast(rffi.VOIDP, 0x1234)
         addr = rffi.cast(rffi.VOIDP, p)
         assert struct_getfield_int(types.slong, addr, 0) == 42
         assert struct_getfield_int(types.sshort, addr, y_ofs) == -1
+        assert struct_getfield_int(types.pointer, addr, z_ofs) == 0x1234
         #
         struct_setfield_int(types.slong, addr, 0, 43)
         struct_setfield_int(types.sshort, addr, y_ofs, 0x1234FFFE) # 0x1234 is masked out
+        struct_setfield_int(types.pointer, addr, z_ofs, 0x4321)
         assert p.x == 43
         assert p.y == -2
+        assert rffi.cast(rffi.LONG, p.z) == 0x4321
         #
         lltype.free(p, flavor='raw')
 


More information about the pypy-commit mailing list