473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

CPython and a C extension using Boehm GC

Hi everyone,

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?

Best Regards,

Muhammad Alkarouri
Dec 25 '07 #1
5 2009
Perhaps, you can pre-load the extension library when Python is
invoked. It is probably trying.

Pre-loading is commonly done done for memory management and profiling
libraries and it may (or may not) work for libraries including the
Boehm-GC. And if it does work, call GC_INIT inside the initialization
function of the extension library. The latter will be called just
before Python's main is.

If you are using the GNU C, writing the initialization function could
be as simple as

void __attribute__((constructor))
_initializer (void) /* any name */
{
call GC_INIT();
}

For more details, see <http://gcc.gnu.org/onlinedocs/gcc/Function-
Attributes.htmlunder 'constructor'. Other compilers may support a
#pragma like init for this purpose.

Pre-loading a (shared) library on Linux is typically done using the
env command, e.g.:

$ env LD_PRELOAD=<path_to_the_library python ....

Some command shells support other ways and the name LD_PRELOAD may be
different on other O/S's.

HTH, /Jean Brouwers

On Dec 25, 3:34*am, malkarouri <malkaro...@gmail.comwrote:
Hi everyone,

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?

Best Regards,

Muhammad Alkarouri
Dec 25 '07 #2
Correction. The second line should be ... It is probably worth
trying.

/Jean Brouwers

On Dec 25, 12:19*pm, MrJean1 <MrJe...@gmail.comwrote:
Perhaps, you can pre-load the extension library when Python is
invoked. It is probably trying.

Pre-loading is commonly done done for memory management and profiling
libraries and it may (or may not) work for libraries including the
Boehm-GC. *And if it does work, call GC_INIT inside the initialization
function of the extension library. *The latter will be called just
before Python's main is.

If you are using the GNU C, writing the initialization function could
be as simple as

* * void __attribute__((constructor))
* * _initializer (void) */* any name */
* * {
* * * *call GC_INIT();
* * }

For more details, see <http://gcc.gnu.org/onlinedocs/gcc/Function-
Attributes.htmlunder 'constructor'. *Other compilers may support a
#pragma like init for this purpose.

Pre-loading a (shared) library on Linux is typically done using the
env command, e.g.:

* $ env *LD_PRELOAD=<path_to_the_library*python ....

Some command shells support other ways and the name LD_PRELOAD may be
different on other O/S's.

HTH, /Jean Brouwers

On Dec 25, 3:34*am, malkarouri <malkaro...@gmail.comwrote:
Hi everyone,
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?
Best Regards,
Muhammad Alkarouri
Dec 25 '07 #3
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: an*****@bullseye.apana.org.au (pref) | Snail: PO Box 370
an*****@pcug.org.au (alt) | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia
Dec 26 '07 #4
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...@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 thoughtsare mine alone..."
E-mail: andy...@bullseye.apana.org.au *(pref) | Snail: PO Box 370
* * * * andy...@pcug.org.au * * * * * * (alt) | * * * *Belconnen ACT 2616
Web: * *http://www.andymac.org/* * * * * * * | * * * *Australia
Dec 26 '07 #5
FWIIW, I built GC 6.7 on a RHEL 3 (Opteron) system using

./configure --prefix=... --enable-redirect-malloc --enable-
threads=posix --enable-thread-local-alloc
make; make check; make install

Then, I tried running a few examples with 3 different, existing Python
binaries each pre-loaded with the libgc.so library

env LD_PRELOAD=.../libgc.so <python....

One is Python 2.2.3 included in RHEL 3, one is a Python 2.5.1 build
and is a Python 3.0a2 build, all 64-bit. All seemed to work OK.

These are 3 existing Python binaries without any call to GC_INIT().
AFAICT, on Linux, GC_INIT is a no-op anyway.

/Jean Brouwers
On Dec 26, 7:14*am, MrJean1 <MrJe...@gmail.comwrote:
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...@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 ofboehm-gcfor 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 theboehm-gcand 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 toboehm-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...@bullseye.apana.org.au *(pref) | Snail: PO Box 370
* * * * andy...@pcug.org.au * * * * * * (alt) | * * * *Belconnen ACT 2616
Web: * *http://www.andymac.org/** * * * * * | * * * *Australia
Dec 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jim Hargrave | last post by:
I've read that it is possible to compile jython to native code using GCJ. PyLucene uses this approach, they then use SWIG to create a Python wrapper around the natively compiled (java) Lucene. Has...
4
by: Pedro Miguel Carvalho | last post by:
Greetings. I'm creating a project that as a intricate relation between object kind of like a set where each object in the set can be connect to a subset of object of the set and objects not in...
1
by: Carl Waldbieser | last post by:
Has anyone had any experience embedding a CPython engine in a .NET application? In the COM/ActiveX world, it was pretty easy to use Mark Hammond's win32 modules to create a script engine component...
3
by: Carl Johan Rehn | last post by:
What is the difference between CPython, Python for .NET, and IronPython? For example, if I'm running IronPython, can I access modules such as Numeric and numarray? As I understand it,...
3
by: Lloyd Dupont | last post by:
I am using an OpenSource library I have wrapped in Managed C++. There is no way I can do without it :-(. Due to on going problem with this library (which I cannot debug as it is not in a...
1
by: John Machin | last post by:
Here are some data points that illustrate the improvement in speed since 2.1 for one (probably atypical) application: rummaging through a 120MB Microsoft Excel spreadsheet file using the xlrd...
3
by: Jack | last post by:
I learned a lot from the other thread 'Is a "real" C-Python possible?' about Python performance and optimization. I'm almost convinced that Python's performance is pretty good for this dynamic...
1
by: Christof Hoeke | last post by:
hi, I was wondering if there is any way to use XSLT2 or maybe even XQuery with "normal" CPython. Using Saxon/XSLT2 with Jython is no problem (I have not tried Saxon.NET with IronPython but suspect...
22
by: dmitrey | last post by:
Hi all, the url http://torquedev.blogspot.com/2008/02/changes-in-air.html (blog of a game developers) says IronPython is faster than CPython in 1.6 times. Is it really true? If yes, what are...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.