caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Basile Starynkevitch <basile@starynkevitch.net>
To: "CUOQ Pascal" <Pascal.CUOQ@cea.fr>
Cc: <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] Modify macro and caml_initialize function
Date: Sun, 5 Dec 2010 17:13:11 +0100	[thread overview]
Message-ID: <20101205171311.ff7cc923.basile@starynkevitch.net> (raw)
In-Reply-To: <5EFD4D7AC6265F4D9D3A849CEA92191902182AD6@LAXA.intra.cea.fr>

On Sun, 5 Dec 2010 17:02:37 +0100
"CUOQ Pascal" <Pascal.CUOQ@cea.fr> wrote:

> ygrek wrote:
> 
> > BTW, while we are on this topic, why the following is not in upstream yet?
> >
> > http://eigenclass.org/R2/writings/optimizing-caml_modify
> >
> > Looks like a clear win-win without drawbacks (increased code size shouldn't be significant
> > cause Modify is only used in a couple of places). What do you think?
> 
> This is very interesting. Thanks for pointing it out.


Sorry to be nitpicking, but...

IIRC, there might be some rare occasions where the Modify macro could
be called several times in a C routine. And then you've got several
caml_modify_maybe_add_to_ref_table labels in the same C routine.

With GCC, you could use a local __label__
http://gcc.gnu.org/onlinedocs/gcc/Local-Labels.html but if you want to
be portable to non GCC compilers, you'll need a Modify macro which
generates a unique label (perhaps using __LINE__ for that purpose).

I am thinking of something like [UNTESTED CODE]

#define Modify(fp, val) Modify_at(fp, val, __LINE__)

#define Modify_at(fp, val, lin) do{                                         \
  value in_heap = 0;                                                        \
  value _old_ = *(fp);                                                      \
  *(fp) = (val);                                                            \
  if (caml_gc_phase == Phase_mark) {                                        \
    if (Is_in_heap (fp)) {                                                  \
      caml_darken (_old_, NULL);                                            \
      in_heap = 1;                                                          \
      goto caml_modify_maybe_add_to_ref_table##Lin;                         \
    }                                                                       \
  } else {                                                                  \
      caml_modify_maybe_add_to_ref_table##Lin:                              \
      if (Is_block (val) && Is_young (val)                                  \
          && ! (Is_block (_old_) && Is_young (_old_)) &&                    \
          (in_heap || Is_in_heap (fp)) ){                                   \
        if (caml_ref_table.ptr >= caml_ref_table.limit){                    \
          CAMLassert (caml_ref_table.ptr == caml_ref_table.limit);          \
          caml_realloc_ref_table (&caml_ref_table);                         \
        }                                                                   \
        *caml_ref_table.ptr++ = (fp);                                       \
      }                                                                     \
  }                                                                         \
}while(0)

Cheers.
-- 
Basile STARYNKEVITCH         http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mine, sont seulement les miennes} ***


  reply	other threads:[~2010-12-05 16:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20101204110006.A6F91BC57@yquem.inria.fr>
2010-12-05 16:02 ` CUOQ Pascal
2010-12-05 16:13   ` Basile Starynkevitch [this message]
2010-12-05 16:49     ` [Caml-list] " Benedikt Meurer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20101205171311.ff7cc923.basile@starynkevitch.net \
    --to=basile@starynkevitch.net \
    --cc=Pascal.CUOQ@cea.fr \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).