public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Basile Starynkevitch <basile@starynkevitch.net>
To: gcc@gcc.gnu.org
Subject: adding destroyable objects into Ggc
Date: Tue, 18 Oct 2011 16:35:00 -0000	[thread overview]
Message-ID: <20111018171201.361304028ab94f102f827bd2@starynkevitch.net> (raw)

Hello All,

I would like to add destroyable objects into Ggc (the GCC garbage collector, see files
gcc/ggc*.[ch]).

The main motivation is to permit C++ objects to be garbage collected (I discussed that
briefly in the Gcc meeting at Google in London): adding destroyable object is a
prerequisite for that goal.


##### What are destroyable Ggc objects?

What do I call a destroyable object: it is a memory zone, allocated inside Ggc heap,
which is provided a destructor routine at allocation time.

So the following should be added into ggc.h:

  /* Signature of destructors, as known to GGC.  */
  typedef void (ggc_destructor_t) (void*);

  /* Allocate an object with cleared memory and destructor */
  extern void *ggc_internal_alloc_destroyed_stat (size_t, ggc_destructor_t*
MEM_STAT_DECL); #define ggc_internal_alloc_destroyed(Sz,Destr)
ggc_internal_alloc_destroyed_stat(Sz, Destr, MEM_STAT_INFO)



In my opinion, destroyable objects will be quite rare in the Ggc heap (because most of
the data in the Ggc heap is stuff like tree, gimple, ... and they don't need destructors,
being pure memory things). However, destroyable objects can be useful for C++ data (I
admit that we don't use C++ for Ggc data yet), and more significantly for data allocated
thru some library, like PPL [Parma Polyhedra Library, notably used by Graphite] data
(even using the C interface to PPL, since PPL is coded in C++, PPL data needs to be
explicitly destroyed), or GMP data, etc.

Plugins will profit from destroyable objects. In particular, MELT would profit from them.
Currently, there is a special hack inside the MELT runtime to deal with PPL boxed data,
and it is a bit of a pain (and the hack is MELT specific).


##### external API and allocation of destroyable objects

GCC code wanting to allocate a destroyable object should first define a destructor
function, like in C:
struct somedestroyable_st { .... };
static void my_destructor(struct somedestroyable_st*thing)
{ // code to destroy thing
}

Then allocate such objects with
struct somedestroyable_st* ob = 
  (struct somedestroyable_st*) 
    gcc_internal_alloc_destroyed(sizeof(struct somedestroyable_st), my_destructor);

This means that Ggc -which is a precise, mark & sweep collector, so it knows when every
object is destroyed- would call my_destructor before de-allocating ob. The destructor
routine my_destructor should not itself allocate any Ggc-allocated data. 

Zone for destroyed data should always be allocated with all bits cleared. This simplifies
the code, and ensure that after initialization the zone contain some deterministic
content. I don't feel that we should care about allocating destoyable objects in
uninitialized memory.


Of course, with C++, the destructor routine is really what C++ calls a destructor, e.g
something like extern "C" void my_destructor_for_class_C (class C* p)
{ delete (p) p; // call the placement version of operator delete, from <new> C++ library
header. }


##### implementation tricks

First, I am looking into gcc/ggc-zone.c and I feel that each page entry should 
have some destructor related data (which may be cleared if no destructor is involved).

For large_page_entry each large object has such a page [one page per large object], and
the page can easily contain a function pointer to the destructor.

For small_page_entry which contains several chunks for several objects, I have the
feeling that we would have an array of destructor function pointers (or null).

I am not sure to understand the exact role of alloc_bits in small_page_entry structure. I
believe that a bit is set there only for start address of each object, not for the entire
range of the object. Could someone confirm my understanding please?

With gcc/ggc-page.c I believe we should have an array of destructors inside the
page_entry structures.

############

Comments and helps are very welcome.

Regards.


-- 
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:[~2011-10-18 15:12 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-18 16:35 Basile Starynkevitch [this message]
2011-10-18 16:43 ` Duncan Sands
2011-10-18 16:53   ` Basile Starynkevitch
2011-10-18 17:11 ` Ian Lance Taylor
2011-10-18 17:14   ` Gabriel Dos Reis
2011-10-18 17:20   ` Basile Starynkevitch
2011-10-18 17:36     ` Ian Lance Taylor
2011-10-18 17:41   ` Basile Starynkevitch
2011-10-18 17:50     ` Ian Lance Taylor
2011-10-18 17:53       ` Basile Starynkevitch
2011-10-18 18:03         ` Jonathan Wakely
2011-10-18 18:11         ` Ian Lance Taylor
2011-10-18 19:41           ` Basile Starynkevitch
2011-10-18 20:52           ` Gabriel Dos Reis
2011-10-18 19:50         ` Gabriel Dos Reis
2011-10-18 23:11           ` Basile Starynkevitch
2011-10-19  0:49             ` Jonathan Wakely
2011-10-18 19:46     ` Gabriel Dos Reis
2011-10-19  6:04     ` Chiheng Xu
2011-10-19  6:08       ` Basile Starynkevitch
2011-10-19  6:43         ` Chiheng Xu
2011-10-19  6:12       ` Gabriel Dos Reis
2011-10-19  6:17         ` Basile Starynkevitch
2011-10-19  6:22         ` Chiheng Xu
     [not found]         ` <20111019080021.4e1acb3687fc8ceacc2fd7a3@starynkevitch.net>
     [not found]           ` <CAAiZkiB-aXfE8MyY_S6YvecdxgsBnuBHp3JDWx4kirVkQx8w+A@mail.gmail.com>
2011-10-19  7:41             ` Gabriel Dos Reis
2011-10-19  7:43             ` Basile Starynkevitch
2011-10-19 12:14               ` Gabriel Dos Reis
2011-10-19 13:31                 ` Duncan Sands
2011-10-19 22:19                   ` Jonathan Wakely
2011-10-19 15:06         ` David Malcolm
2011-10-20  6:03       ` Lawrence Crowl
2011-10-20  8:29         ` Basile Starynkevitch
2011-10-20  8:37           ` Marc Glisse
2011-10-20  8:38             ` Basile Starynkevitch
2011-10-20 11:57               ` Marc Glisse
2011-10-20 12:10                 ` Basile Starynkevitch
2011-10-20 15:34                   ` Marc Glisse
2011-10-21  9:03                     ` Basile Starynkevitch
2011-10-21 12:24                       ` Marc Glisse
2011-10-21 12:28                       ` Richard Guenther
2011-10-21 23:53                         ` Basile Starynkevitch
2011-10-22  1:31                           ` Gabriel Dos Reis
2011-10-22 11:20                             ` Basile Starynkevitch
2011-10-23 13:50                           ` Richard Guenther
2011-10-19 15:56     ` Laurynas Biveinis
2011-10-19 16:54       ` Basile Starynkevitch
2011-10-20  8:52         ` Laurynas Biveinis
2011-10-20 12:27           ` Basile Starynkevitch
2011-10-20 12:51             ` Andrew Haley
2011-10-20 14:07               ` Basile Starynkevitch
2011-10-20 13:10             ` Duncan Sands
2011-10-20 14:52               ` Torvald Riegel
2011-10-20 15:14             ` Jonathan Wakely
2011-10-20 15:26               ` Richard Guenther
2011-10-20 17:23               ` Basile Starynkevitch
2011-10-20 18:38                 ` Jonathan Wakely
2011-10-18 18:39 ` Jonathan Wakely
2011-10-18 18:48   ` Basile Starynkevitch
2011-10-18 19:42     ` Jonathan Wakely
2011-10-18 19:45       ` Basile Starynkevitch
2011-10-18 22:43         ` Gabriel Dos Reis
2011-10-18 21:24     ` Gabriel Dos Reis

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=20111018171201.361304028ab94f102f827bd2@starynkevitch.net \
    --to=basile@starynkevitch.net \
    --cc=gcc@gcc.gnu.org \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).