user defined malloc
J. Johnston
jjohnstn@cygnus.com
Tue Jul 4 12:29:00 GMT 2000
"Ashif S. Harji" wrote:
>
> I originally posted a message to the cygiwn mailing list and it was
> suggested that I also post it here. The original message is included
> below.
>
> Basically, I am trying to find out if it is possible to provide my own
> version of malloc? If so, does this also require _malloc_r to be
> overriden (see message below?. And finally, if someone can provide some
> information or a link on what is required to override _malloc_r (if it is
> necessary).
>
The _malloc_r routine is indeed a version of malloc which uses an additional
parameter to receive a reentrancy structure. The malloc routine simply calls _malloc_r with
the default reentrancy structure. Internally, any reentrant library routines must
call _malloc_r instead of malloc and pass whichever reentrancy structure was
passed to them. Thus, you want to replace the _r interfaces rather than the
top level routines like malloc() and free().
Multithreaded applications must create their own separate reentrancy structures per
thread and must use _r routines where provided. For more information, see the
library info files (make info install-info). You can alternatively peruse the info source
for the reentrancy chapter in newlib/libc/reent directory (reent.tex).
A while back, _malloc_r used to use manage heap storage from pools that were
stored off of the reentrancy structure. Now, the reentrancy structure is used
as a mutex identifier for __malloc_lock and __malloc_unlock. By default,
__malloc_lock and __malloc_unlock are only provided as stubs.
If you don't need to worry about multithreaded applications using your malloc,
simply create versions of _malloc_r, _free_r, _calloc_r, and _realloc_r that
ignore the reentrancy structure. Otherwise, you will have to either use
a mutex system (and supply mutex routines) or use a separate pool per reentrancy
structure.
-- Jeff J.
> thanks,
> ashif harji.
>
> Original message to cygwin mailing list:
>
> To: cygwin@sourceware.cygnus.com
> Subject: user defined malloc
>
> It mentions in the faq that it is possible to override the default malloc
> routine. The problem that I am having is that there is at least one
> occasion when my malloc routine is not called for memory allocation.
> Instead the function _malloc_r is called directly. This by passes my
> malloc which results in already allocated memory being reallocated.
>
> This direct call to _malloc_r occurs in the __sfmoreglue function in the
> file findfp.c:
>
> struct _glue *
> __sfmoreglue (d, n)
> struct _reent *d;
> register int n;
> {
> struct _glue *g;
> FILE *p;
>
> g = (struct _glue *) _malloc_r (d, sizeof (*g) + n * sizeof (FILE));
> if (g == NULL)
> return NULL;
> p = (FILE *) (g + 1);
> g->_next = NULL;
> g->_niobs = n;
> g->_iobs = p;
> memset (p, 0, n * sizeof (FILE));
> return g;
> }
>
> While I am not sure exactly what this function does, it appears to be
> related to opening the /etc/passwd file and occurs even with an empty C++
> program:
>
> int main(){}
>
> My question is do I have to also override the _malloc_r routine, and if
> so, could someone provide some information or a link on what is required?
> It appears to be a reentrant version of malloc.
>
> thanks,
> ashif harji.
More information about the Newlib
mailing list