CPython and a C extension using Boehm GC

MrJean1 MrJean1 at gmail.com
Wed Dec 26 10:14:47 EST 2007


It depends on how the GC inside the extension is built.  If it is a
drop-in replacement for malloc, then GC *must* be loaded and
initialized upfront if possible.  There is no need to memcpy anything
between Python and the extension.

However, if GC does not replace malloc, etc., then GC-ed memory is
only used within the extension.  GC_INIT can be called when the
extension is loaded and memcpy-ing between Python and the extension is
mandatory.

There are other details to consider.  For example, on some platforms,
GC *must* be initialized from the main executable.  That may preclude
both scenarios, altogether.

/Jean Brouwers



On Dec 25, 7:35 pm, Andrew MacIntyre <andy... at bullseye.apana.org.au>
wrote:
> malkarouri wrote:
> > Is it possible to write a Python extension that uses the Boehm garbage
> > collector?
> > I have a C library written that makes use of boehm-gc for memory
> > management. To use that, I have to call GC_INIT() at the start of the
> > program that uses the library. Now I want to encapsulate the library
> > as a CPython extension. The question is really is that possible? And
> > will there be conflicts between the boehm-gc and Python memory
> > management? And when should I call GC_INIT?
>
> It probably should be possible with some caveats:
> - memory allocated by Python is never passed into the library such that
>    it also ends up being subject to boehm-gc;
> - memory allocated by the library is never used by Python objects.
>
> So memcpy()ing between library allocated and Python allocated memory
> would seem to be a way to achieve this.
>
> I would call GC_INIT in the extension's import routine
> (init<module_name>()) for a C extension, and immediately after loading
> the library if using ctypes.
>
> --
> -------------------------------------------------------------------------
> Andrew I MacIntyre                     "These thoughts are mine alone..."
> E-mail: andy... at bullseye.apana.org.au  (pref) | Snail: PO Box 370
>         andy... at pcug.org.au             (alt) |        Belconnen ACT 2616
> Web:    http://www.andymac.org/              |        Australia




More information about the Python-list mailing list