problem when mapping malloc to GC_malloc.
Hans Boehm
Hans.Boehm@hp.com
Thu Jun 18 16:30:00 GMT 2009
On Thu, 18 Jun 2009, abhishek desai wrote:
> Hi,
>
> My JNI code includes redefinitions to the malloc, free and realloc
> functions (shown below). These functions call GC_malloc, GC_free and
> GC_realloc respectively. This is done so that any calls to the malloc
> get allocated through the garbage collector. However this is failing
> with segfault. Any clues why this does not work ?
> I am using this code along with the libgcj library linked dynamically
> with my application.
>
> void *malloc(size_t size)
> {
> return GC_malloc(size);
> }
>
> void *realloc(void *ptr, size_t size)
> {
> return GC_realloc(ptr, size);
> }
>
> void free(void *ptr)
> {
> GC_free(ptr);
> }
>
>
> regards
> abhishek
>
The collector itself supports a REDIRECT_MALLOC option that might get you
closer. In general, this is very hard.
There are other functions (calloc, memalign, etc.) that you would also
have to replace, so that their clients don't end up using the original
malloc with GC_free. This is the easy part.
The hard part is that if you replace malloc, low level parts of the system
will also end up using GC_malloc, and sometimes squirrel away pointers to
the results in places the GC doesn't really know about. Recent versions
of the GC (7.1+) contains some hacks to try to handle this on Linux. But
the multithreaded versions still are sometimes not 100% robust. Gcj's
version is unlikely to work in this mode, except possibly in
single-threaded mode.
A real fix here would probably require some new hooks in glibc and the
startup and libpthread code.
Hans
More information about the Java
mailing list