C question : redefining a new malloc() calling standard malloc() 
Author Message
 C question : redefining a new malloc() calling standard malloc()

Hello,

I am currently writing, in C (on SGI IRIX 6.4), a redefinition of
malloc(), but I would like to still be able to call the standard library
malloc().

I am looking for a way to link the all properly !

I so have the four (+1) files :

* real_malloc.h : to call the standard library malloc()
---------------
#include <stdlib.h>

void* real_malloc(size_t);

* real_malloc.c
---------------
#include "real_malloc.h"

void* real_malloc(size_t size)
{
   /* Should call the standard library malloc() */
   return malloc(size);

Quote:
}

* my_malloc.h : my redefinition of malloc()
-------------
#include <stdlib.h>

void* malloc(size_t);

* my_malloc.c
--------------
#include "my_malloc.h"
#include "real_malloc.h"

void* malloc(size_t size)
{
   /* some code */
   void* a = real_malloc(size);
   /* some code */

Quote:
}

* essai.c : a test file
----------

#include "my_malloc.h"

main()
{
   /* should call my malloc redefinition */
   void* a = malloc(...);

Quote:
}

How can I have the all correctly compile, link and WORK ???

Thanks for all response, comment, suggestion.

You can directly reply by mail.

---------------------
Xavier.



Sun, 07 May 2000 03:00:00 GMT  
 C question : redefining a new malloc() calling standard malloc()

Thanks for this first response,

but I have to precise my problem a little.

I also want other library functions (for instance, C++ new()) to call my
redefined function malloc() and not the standard library one.

So, I must have a function malloc() to create a symbol for link ! Not
just a #define that only my code can see...

Localy, I have two solutions :

* make a copy of the standard library and change the symbol name from
malloc to Malloc and call Malloc() in my real_malloc() function. This is
not very portable but can be useful for testing purpose.

* get source code from malloc (for instance :
http://g.oswego.edu/dl/html/malloc.html) and adapt it to my problem
case. This is not very good since I would like to be able to call the
native malloc(). But it also works.

Any suggestions ???

Xavier.

Quote:

> > * my_malloc.h : my redefinition of malloc()
> > -------------
> > #include <stdlib.h>

> > void* malloc(size_t);

> Change to void* my_malloc(size_t);
> Then add #define malloc my_malloc

> > * my_malloc.c
> > --------------
> > #include "my_malloc.h"
> > #include "real_malloc.h"

> > void* malloc(size_t size)

> Change to void* my_malloc(size_t size)

> > {
> >    /* some code */
> >    void* a = real_malloc(size);
> >    /* some code */
> > }



Sun, 07 May 2000 03:00:00 GMT  
 C question : redefining a new malloc() calling standard malloc()

Quote:

> Hello,

> I am currently writing, in C (on SGI IRIX 6.4), a redefinition of
> malloc(), but I would like to still be able to call the standard library
> malloc().

> I am looking for a way to link the all properly !

> I so have the four (+1) files :

> * real_malloc.h : to call the standard library malloc()
> ---------------
> #include <stdlib.h>

> void* real_malloc(size_t);

> * real_malloc.c
> ---------------
> #include "real_malloc.h"

> void* real_malloc(size_t size)
> {
>    /* Should call the standard library malloc() */
>    return malloc(size);
> }

> * my_malloc.h : my redefinition of malloc()
> -------------
> #include <stdlib.h>

> void* malloc(size_t);

Change to void* my_malloc(size_t);
Then add #define malloc my_malloc

Quote:

> * my_malloc.c
> --------------
> #include "my_malloc.h"
> #include "real_malloc.h"

> void* malloc(size_t size)

Change to void* my_malloc(size_t size)

- Show quoted text -

Quote:
> {
>    /* some code */
>    void* a = real_malloc(size);
>    /* some code */
> }

> * essai.c : a test file
> ----------

> #include "my_malloc.h"

> main()
> {
>    /* should call my malloc redefinition */
>    void* a = malloc(...);
> }

> How can I have the all correctly compile, link and WORK ???

> Thanks for all response, comment, suggestion.

> You can directly reply by mail.

> ---------------------
> Xavier.


--
Jan Magne Hest?s


Sun, 07 May 2000 03:00:00 GMT  
 C question : redefining a new malloc() calling standard malloc()

Use a pointer to function.  If you want to use malloc(), then assign malloc()
to your pointer.  If you want to use your own allocater, assign your own
allocater to the pointer.  Then do all allocations through the pointer.
You'll probably want to do the same thing for free().

Quote:
>> Begin Off Topic Blather:

If you use global pointers and you have threading or otherwise reentrant code,
you will want to use critical sections or the like to prevent bad things from
happening.
<< End Off Topic Blather.
--
C-FAQ ftp sites: ftp://ftp.eskimo.com ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Want Software?  Algorithms?  Pubs? http://www.infoseek.com


Sun, 07 May 2000 03:00:00 GMT  
 C question : redefining a new malloc() calling standard malloc()



Quote:
>Thanks for this first response,

>but I have to precise my problem a little.

>I also want other library functions (for instance, C++ new()) to call my
>redefined function malloc() and not the standard library one.

What you are asking for is not possible in the C language, and the C++ bit is
entirely beyond the scope.

The external names claimed by the C library are reserved for use as external
names. A strictly conforming C program may not write its own version of
a library function.

Under many compilers, however, it is possible to easily overrride a library
function and, for instance, write your own malloc that will be linked to
by not only your program but any libraries that it uses. If, on that platform,
C++ implements new and delete using calls to malloc, they can be overriden
too.

Quote:
>So, I must have a function malloc() to create a symbol for link ! Not
>just a #define that only my code can see...

>Localy, I have two solutions :

>* make a copy of the standard library and change the symbol name from
>malloc to Malloc and call Malloc() in my real_malloc() function. This is
>not very portable but can be useful for testing purpose.

What you are asking for can't be really be made portable anyway.

Quote:
>* get source code from malloc (for instance :
>http://g.oswego.edu/dl/html/malloc.html) and adapt it to my problem
>case. This is not very good since I would like to be able to call the
>native malloc(). But it also works.

>Any suggestions ???

In Linux, for instance, you would just write your function and call it malloc.

To call the ``native'' malloc, you would refer to it as __libc_malloc or
something like that, if I recall correctly.

Quote:
>Xavier.


>> > * my_malloc.h : my redefinition of malloc()
>> > -------------
>> > #include <stdlib.h>

>> > void* malloc(size_t);

Right there, the program invokes undefined behavior on two counts. First of
all, when you #include a standard header into your translation units, the
identifiers exported by that header are completely off limits for any purpose.
The <stdlib.h> header could easily #define a macro called malloc, which would
make a mess of your subsequent declaration. But don't think that you can
just do an #undef malloc and be off the hook.

Secondly, if you are going to write your own malloc, you must not include the
<stdlib.h> header, and to remain strictly conforming, you must declare it
static because even if you don't include the header, the name malloc is still
reserved for external linkage.



Sun, 07 May 2000 03:00:00 GMT  
 C question : redefining a new malloc() calling standard malloc()

Sorry,

I think I do not really understand what you mean.

Indeed, as I said in my second post (this is the third), I want other
standard libraries functions (like C++ new, for instance), to also call
MY malloc().

So I cannot see how your solution work. I think you want me to make
allocations through another name than malloc(), and so it comes back to
the first proposed solution which was to use #define malloc my_malloc.

Could you please put a piece of code to explain your solution (if I do
not understand) ?

Thanks for contribution.

-------
Xavier.

Quote:

> Use a pointer to function.  If you want to use malloc(), then assign malloc()
> to your pointer.  If you want to use your own allocater, assign your own
> allocater to the pointer.  Then do all allocations through the pointer.
> You'll probably want to do the same thing for free().



Mon, 08 May 2000 03:00:00 GMT  
 C question : redefining a new malloc() calling standard malloc()



Quote:
>Sorry,

>I think I do not really understand what you mean.

>Indeed, as I said in my second post (this is the third), I want other
>standard libraries functions (like C++ new, for instance), to also call
>MY malloc().

There is a way to do this on SYS V release-4-like systems
such as IRIX.  The way on other types of systems
will surely be completely different.

You must write
void *_malloc(size);
void *malloc(size) { return _malloc(size)}
void * _realloc(...);
void *realloc(stuff...) { return _realloc(stuff...); }
void   _free();
void free() { _free()}

The above is sketchy. You are replacing the weak 'malloc' etc names.
You are also replacing the strong names.

Now all calls to malloc from anywhere will get to your functions
(except for code doing the trick I am talking about)

So, now how do you get your _malloc to call the libc malloc?

h = dlopen("libc.so.1",RTLD_LAZY);
f = dlsym(f,"_malloc");

Now call the real system malloc with
        f(...args...)

Use dlsym to get pointers to the other libc malloc functions.

So now your _malloc (as an example) looks something like:

void *
_malloc(size_t size)
{
        void *r;
        ... do your pre-processing
        r = f(size);
        ... do your post-processing
        return r;

Quote:
}

My sketch shows no checking of return values (for brevity).

This is not recommended practice.
You are on your own.
Any other code doing the  dlsym() stuff to get the address of
libc malloc will get libc malloc, not yours!

The dlopen will reference the existing libc instance, not
open a new copy!

See the dlopen and dlsym man pages for details.
Also see 'man dso'




Tue, 09 May 2000 03:00:00 GMT  
 C question : redefining a new malloc() calling standard malloc()

Speaking of, can anyone recommend to me a portable ANSI C
replacement for malloc() and the heap that might improve
its performance on the SGI?  SmartHeap for Windows is a dream,
but I don't want to buy a source license.

- John



Tue, 09 May 2000 03:00:00 GMT  
 C question : redefining a new malloc() calling standard malloc()

Quote:

> Speaking of, can anyone recommend to me a portable ANSI C
> replacement for malloc() and the heap that might improve
> its performance on the SGI?  SmartHeap for Windows is a dream,
> but I don't want to buy a source license.

ftp://gee.oswego.edu/pub/misc/malloc.c

or, for a thread-safe and low-lock-contention version of the same code

ftp://ftp.dent.med.uni-muenchen.de/pub/wmglo/ptmalloc.tar.gz

for performance comparisons, see

http://www.dent.med.uni-muenchen.de/~wmglo/malloc-slides.html

Regards,
Wolfram.



Wed, 10 May 2000 03:00:00 GMT  
 C question : redefining a new malloc() calling standard malloc()

Quote:

> I also want other library functions (for instance, C++ new()) to call my
> redefined function malloc() and not the standard library one.

OK, I now understand what you want to do. You want to replace the system
malloc with your malloc, and have everybody call it.

David Anderson's reply is a good one.

As a practical matter, on *SGI systems* (and probably any other systems
that use an SVR4-style shared library mechanism), what is done in the
standard malloc libraries is to make malloc(), etc., "weak" symbols that
are aliases for "strong" symbols called "_malloc", etc.  Thus, the
following dodge will work if you really want to call the system malloc
from your malloc:

  extern void *_malloc(int);

  void *malloc(int x)
  {
    ....
    return_value = _malloc(x);
    ....
    return return_value;
  }

Just make sure that you replace all of malloc, calloc, free and realloc
in this same way.

Now here is the key step: either link in this malloc library into your
application (all library calls to malloc *should* get diverted to your
malloc automatically), or make a shared library with this malloc and
link it in to your application *before* libc (or libmalloc).

Alternatively, you can just pick up any public-domain malloc library
(like the GNU malloc library) and compile it and link it into your
application; again, all calls to malloc within the system libraries
*should* get forwarded to your malloc automatically..  That way, you
have no dependency on getting the details right about calling the system
malloc (those details can vary from system to system); instead, you are
replacing the system malloc entirely with your malloc.

NOTE: neither of these will work reliably on, for instance, Win32
systems. Their shared libraries (DLLs) work differently.
--
Shankar Unni                       Powertel Global, Inc.




Fri, 12 May 2000 03:00:00 GMT  
 C question : redefining a new malloc() calling standard malloc()

| Just make sure that you replace all of malloc, calloc, free and realloc
| in this same way.

And memalign(); people often forget that one.
--

Dave Olson, Silicon Graphics



Sat, 13 May 2000 03:00:00 GMT  
 C question : redefining a new malloc() calling standard malloc()


Quote:


>| Just make sure that you replace all of malloc, calloc, free and realloc
>| in this same way.

>And memalign(); people often forget that one.

I've forgotten it so thoroughly I'm convinced I've never heard of
it.  I don't think K, R, ANSI or ISO have either.

John

(Newsgroups trimmed.)
--
John Winters.  Wallingford, Oxon, England.

Want to buy Linux CDs cheaply in the UK?  Join the Linux Buyers' Consortium.
See <http://www.polo.demon.co.uk/lbc.html>



Sat, 13 May 2000 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. to malloc() or not to malloc(), that is the question

2. Redefining malloc()

3. redefining malloc()

4. Redefine malloc and free

5. Redefining Malloc()

6. please help redefining malloc

7. Malloc 2: The revenge of Malloc

8. malloc vs 'no-malloc' - help

9. malloc crashes - malloc(256) returns 0(!) (_MT)

10. "malloc" without standard libary or heap

11. Common malloc/free practice violates ANSI standard ?

12. commom malloc/free practice breaks standard - author strikes back

 

 
Powered by phpBB® Forum Software