Re: [glib] malloc and bdwgc



Am 07.01.2016 um 12:26 schrieb 张海:
Yes, but glib is the only one I'm using now.

Still I don't know how to make GC_malloc() call the original malloc()
if I'm using LD_PRELOAD or the wrapper function and dlsym way.

Just now I found I've misread
http://www.gnu.org/software/libc/manual/html_node/Hooks-for-Malloc.html
, and that such replacement can possibly be done in a single-threaded
environment, because the hook is done with a global function pointer
variable initialization and it seems that the function is called
before memory allocation initialization

However as in http://stackoverflow.com/questions/4184280/are-the-gcc-memory-hooks-sometimes-bypassed
, there are nasty race conditions for such hook. And my program needs
threads.

Are there any other way around?

You can simply implement malloc() and friends in your application, then the linker shouldn't resolve calls to the libc implementation. If you need to call the original malloc() from that you can use secondary names / aliases (__libc_malloc() etc). If the latter does not work for your C library then you can alternatively access them by dlopen(RTLD_NEXT, "malloc");

That said, the above is probably only portable on Unix systems. It should be race free as well.

Best regards


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]