From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21364 invoked by alias); 18 Jun 2009 18:18:42 -0000 Received: (qmail 21356 invoked by uid 22791); 18 Jun 2009 18:18:41 -0000 X-SWARE-Spam-Status: No, hits=0.1 required=5.0 tests=AWL,BAYES_50,J_CHICKENPOX_44,SARE_MSGID_LONG40,SPF_PASS X-Spam-Check-By: sourceware.org Received: from mail-ew0-f205.google.com (HELO mail-ew0-f205.google.com) (209.85.219.205) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 18 Jun 2009 18:18:33 +0000 Received: by ewy1 with SMTP id 1so1730356ewy.8 for ; Thu, 18 Jun 2009 11:18:30 -0700 (PDT) MIME-Version: 1.0 Received: by 10.211.195.15 with SMTP id x15mr2137801ebp.62.1245349108914; Thu, 18 Jun 2009 11:18:28 -0700 (PDT) In-Reply-To: References: <898285d30906180800q3b5bd3b2h1d4a11d6a8245e34@mail.gmail.com> Date: Thu, 18 Jun 2009 18:18:00 -0000 Message-ID: <898285d30906181118q9f35066i209e0d58c26f65c5@mail.gmail.com> Subject: Re: problem when mapping malloc to GC_malloc. From: abhishek desai To: Hans Boehm Cc: "java@gcc.gnu.org" Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes Mailing-List: contact java-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: java-owner@gcc.gnu.org X-SW-Source: 2009-06/txt/msg00043.txt.bz2 On Thu, Jun 18, 2009 at 10:09 PM, Hans Boehm wrote: > > > 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) >> { >> =A0 =A0 =A0 return GC_malloc(size); >> } >> >> void *realloc(void *ptr, size_t size) >> { >> =A0 =A0 =A0 return GC_realloc(ptr, size); >> } >> >> void free(void *ptr) >> { >> =A0 =A0 =A0 GC_free(ptr); >> } >> >> >> regards >> abhishek >> > The collector itself supports a REDIRECT_MALLOC option that might get you > closer. =A0In general, this is very hard. > > There are other functions (calloc, memalign, etc.) that you would also ha= ve > to replace, so that their clients don't end up using the original malloc > with GC_free. =A0This 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. =A0Recent version= s of > the GC (7.1+) contains some hacks to try to handle this on Linux. =A0But = the > multithreaded versions still are sometimes not 100% robust. =A0Gcj's vers= ion > 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 > >if you replace malloc, low level parts of the system > will also end up using GC_malloc This is what I am trying to achieve but this is turning out to be a tough t= ask. Some more background here. I am using the gcc version 3.4.6 and the libgcj associated with it. I know its ancient but the project demands it. Cant help it. I am compiling for a mipsel architecture using uclibc. There are a lot of calls to malloc through the libgcj library or possibly in the other libraries used by libgcj. The calls to malloc happen even before the main function begins. uclibc implementation of memory management (malloc, realloc) relies on mmap and sbrk to get memory which is also used by the gc for the same purpose. So is not possible to use the gc instead of the uclibc memory manager ? Which areas could be causing the problem ? Even if low level parts of the system make calls to GC_malloc how does it affect the over all behavior ? --Abhishek