[pypy-dev] Object pinning

Armin Rigo armin.rigo at gmail.com
Thu Dec 22 12:15:24 EST 2016


Hi Kunshan,

On 22 December 2016 at 05:56, Kunshan Wang <kunshan.wang at anu.edu.au> wrote:
> So the crux is how to translate the RPython primitives into the Mu
> counterparts.  If `cast_ptr_to_adr` and `keep_alive_until_here` is a
> well-obeyed idiom, we can simply translate them to `pin` and `unpin`,
> respectively.

No, I think what you need in this case is similar to what we have in
the stmgc-c8 branch.  In this branch, we are more careful than in
trunk about casting GC references to raw addresses; and we're also
using a separate GC (implemented in C) which may be closer to what
you're trying to do as well.

In this branch, when we translate an RPython program with --stm, we
have no notion of casting a GC reference to a raw address at all: it
is just forbidden.  The problem with stm is that a GC reference is not
really a raw address.  There are actually ways in which we can still
obtain the raw address if we're extra extra careful, but the default
casts explicitly don't work.  So for code like copy_string_contents(),
the branch adds the following (this example in the
copy_string_contents() function):

        # STM requires the slow character-by-character version,
        # which at least works without forcing the transaction
        # to become inevitable
        if rgc.stm_is_enabled():
            i = 0
            while i < length:
                dst.chars[dststart + i] = src.chars[srcstart + i]
                i += 1
            return

These check remove *all* casts.  For example, the RPython os.write()
by default works by trying to pin temporarily the string; but with
--stm there is no pinning and what occurs is that it always copies the
string into a raw-malloced buffer.

You can of course later think about how to avoid some of these copies
by re-introducing some pinning, but the above should allow you to
progress.


A bientôt,

Armin.


More information about the pypy-dev mailing list