* [PATCH] Trace memalign and posix_memalign in mtrace
@ 2003-12-17 13:10 Jakub Jelinek
2003-12-17 23:32 ` Ulrich Drepper
0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2003-12-17 13:10 UTC (permalink / raw)
To: Ulrich Drepper; +Cc: Glibc hackers
Hi!
Without this patch, *memalign doesn't show up in MALLOC_TRACE= output
and free() of memalign alloced memory appears as freeing invalid memory
(and of course any leaks caused by memalign don't show up).
The malloc.c change is there so that *.mtrace don't contain posix_memalign
as caller, but the actual function which called posix_memalign.
2003-12-17 Jakub Jelinek <jakub@redhat.com>
* malloc/mtrace.c (tr_old_memalign_hook): New variable.
(tr_memalignhook): New function.
(mtrace): Register tr_memalignhook.
(muntrace): Deregister tr_memalignhook.
* malloc/malloc.c (__posix_memalign): If __memalign_hook != NULL,
call it directly instead of memalign_internal.
--- libc/malloc/mtrace.c.jj 2003-09-14 20:13:40.000000000 +0200
+++ libc/malloc/mtrace.c 2003-12-17 11:17:56.000000000 +0100
@@ -71,6 +71,9 @@ static __ptr_t (*tr_old_malloc_hook) __P
static __ptr_t (*tr_old_realloc_hook) __P ((__ptr_t ptr,
__malloc_size_t size,
const __ptr_t));
+static __ptr_t (*tr_old_memalign_hook) __P ((__malloc_size_t __alignment,
+ __malloc_size_t __size,
+ __const __ptr_t));
/* This function is called when the block being alloc'd, realloc'd, or
freed has an address matching the variable "mallwatch". In a debugger,
@@ -233,6 +236,39 @@ tr_reallochook (ptr, size, caller)
return hdr;
}
+static __ptr_t tr_memalignhook __P ((__malloc_size_t, __malloc_size_t,
+ const __ptr_t));
+static __ptr_t
+tr_memalignhook (alignment, size, caller)
+ __malloc_size_t alignment, size;
+ const __ptr_t caller;
+{
+ __ptr_t hdr;
+
+ __libc_lock_lock (lock);
+
+ __memalign_hook = tr_old_memalign_hook;
+ __malloc_hook = tr_old_malloc_hook;
+ if (tr_old_memalign_hook != NULL)
+ hdr = (__ptr_t) (*tr_old_memalign_hook) (alignment, size, caller);
+ else
+ hdr = (__ptr_t) memalign (alignment, size);
+ __memalign_hook = tr_memalignhook;
+ __malloc_hook = tr_mallochook;
+
+ tr_where (caller);
+ /* We could be printing a NULL here; that's OK. */
+ fprintf (mallstream, "+ %p %#lx\n", hdr, (unsigned long int) size);
+
+ __libc_lock_unlock (lock);
+
+ if (hdr == mallwatch)
+ tr_break ();
+
+ return hdr;
+}
+
+
#ifdef _LIBC
@@ -300,6 +336,8 @@ mtrace ()
__malloc_hook = tr_mallochook;
tr_old_realloc_hook = __realloc_hook;
__realloc_hook = tr_reallochook;
+ tr_old_memalign_hook = __memalign_hook;
+ __memalign_hook = tr_memalignhook;
#ifdef _LIBC
if (!added_atexit_handler)
{
@@ -327,4 +365,5 @@ muntrace ()
__free_hook = tr_old_free_hook;
__malloc_hook = tr_old_malloc_hook;
__realloc_hook = tr_old_realloc_hook;
+ __memalign_hook = tr_old_memalign_hook;
}
--- libc/malloc/malloc.c.jj 2003-10-02 21:51:07.000000000 +0200
+++ libc/malloc/malloc.c 2003-12-17 11:58:48.000000000 +0100
@@ -5389,13 +5389,21 @@ int
__posix_memalign (void **memptr, size_t alignment, size_t size)
{
void *mem;
+ __malloc_ptr_t (*hook) __MALLOC_PMT ((size_t, size_t,
+ __const __malloc_ptr_t)) =
+ __memalign_hook;
/* Test whether the SIZE argument is valid. It must be a power of
two multiple of sizeof (void *). */
if (alignment % sizeof (void *) != 0 || !powerof2 (alignment) != 0)
return EINVAL;
- mem = __memalign_internal (alignment, size);
+ /* Call the hook here, so that caller is posix_memalign's caller
+ and not posix_memalign itself. */
+ if (hook != NULL)
+ mem = (*hook)(alignment, size, RETURN_ADDRESS (0));
+ else
+ mem = __memalign_internal (alignment, size);
if (mem != NULL) {
*memptr = mem;
Jakub
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] Trace memalign and posix_memalign in mtrace
2003-12-17 13:10 [PATCH] Trace memalign and posix_memalign in mtrace Jakub Jelinek
@ 2003-12-17 23:32 ` Ulrich Drepper
0 siblings, 0 replies; 2+ messages in thread
From: Ulrich Drepper @ 2003-12-17 23:32 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: Glibc hackers
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Jakub Jelinek wrote:
> * malloc/mtrace.c (tr_old_memalign_hook): New variable.
> (tr_memalignhook): New function.
> (mtrace): Register tr_memalignhook.
> (muntrace): Deregister tr_memalignhook.
> * malloc/malloc.c (__posix_memalign): If __memalign_hook != NULL,
> call it directly instead of memalign_internal.
Applied. Thanks,
- --
⧠Ulrich Drepper ⧠Red Hat, Inc. ⧠444 Castro St ⧠Mountain View, CA â
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
iD8DBQE/4OcR2ijCOnn/RHQRAs5OAKCE9IoKTIU2e5CPQvz7qEk549s7UwCeMjww
0oExL7RjzPvK9pW1blFeYxI=
=Ota8
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2003-12-17 23:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-17 13:10 [PATCH] Trace memalign and posix_memalign in mtrace Jakub Jelinek
2003-12-17 23:32 ` Ulrich Drepper
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).