From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:47459) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QuytI-0005dB-Tx for qemu-devel@nongnu.org; Sat, 20 Aug 2011 23:40:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QuytH-0006ZX-NE for qemu-devel@nongnu.org; Sat, 20 Aug 2011 23:40:04 -0400 Received: from mail-gw0-f45.google.com ([74.125.83.45]:55173) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QuytH-0006ZS-GW for qemu-devel@nongnu.org; Sat, 20 Aug 2011 23:40:03 -0400 Received: by gwb19 with SMTP id 19so2899722gwb.4 for ; Sat, 20 Aug 2011 20:40:02 -0700 (PDT) Message-ID: <4E507E11.3080009@codemonkey.ws> Date: Sat, 20 Aug 2011 22:40:01 -0500 From: Anthony Liguori MIME-Version: 1.0 References: <1313689697-23627-1-git-send-email-avi@redhat.com> In-Reply-To: <1313689697-23627-1-git-send-email-avi@redhat.com> Content-Type: multipart/mixed; boundary="------------000804010907060200020009" Subject: Re: [Qemu-devel] [PATCH] Wire g_new() and friends to the qemu_malloc() family List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Avi Kivity Cc: Blue Swirl , qemu-devel@nongnu.org This is a multi-part message in MIME format. --------------000804010907060200020009 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 08/18/2011 12:48 PM, Avi Kivity wrote: > This makes the tracing infrastructure available to users of g_new(). > > Signed-off-by: Avi Kivity Here's my version, adapted to a world with no qemu_malloc. Regards, Anthony Liguori > --- > qemu-common.h | 1 + > qemu-malloc.c | 15 +++++++++++++++ > vl.c | 1 + > 3 files changed, 17 insertions(+), 0 deletions(-) > > diff --git a/qemu-common.h b/qemu-common.h > index 74d5c4b..fbe2de0 100644 > --- a/qemu-common.h > +++ b/qemu-common.h > @@ -180,6 +180,7 @@ const char *path(const char *pathname); > #define qemu_isascii(c) isascii((unsigned char)(c)) > #define qemu_toascii(c) toascii((unsigned char)(c)) > > +void qemu_malloc_init(void); > void *qemu_oom_check(void *ptr); > void *qemu_malloc(size_t size); > void *qemu_realloc(void *ptr, size_t size); > diff --git a/qemu-malloc.c b/qemu-malloc.c > index b9b3851..8b0c1ec 100644 > --- a/qemu-malloc.c > +++ b/qemu-malloc.c > @@ -24,6 +24,21 @@ > #include "qemu-common.h" > #include "trace.h" > #include > +#include > + > +static GMemVTable gmemvtable = { > + .malloc = qemu_malloc, > + .realloc = qemu_realloc, > + .free = qemu_free, > +}; > + > +/** > + * qemu_malloc_init: initialize memory management > + */ > +void qemu_malloc_init(void) > +{ > + g_mem_set_vtable(&gmemvtable); > +} > > void qemu_free(void *ptr) > { > diff --git a/vl.c b/vl.c > index c714127..7c4f8da 100644 > --- a/vl.c > +++ b/vl.c > @@ -2106,6 +2106,7 @@ int main(int argc, char **argv, char **envp) > > atexit(qemu_run_exit_notifiers); > error_set_progname(argv[0]); > + qemu_malloc_init(); > > init_clocks(); > --------------000804010907060200020009 Content-Type: text/x-patch; name="0001-Add-trace-points-for-g_malloc-g_free-functions.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Add-trace-points-for-g_malloc-g_free-functions.patch" >>From fd639107f8ee2a489a9010e007cc3181732b1f06 Mon Sep 17 00:00:00 2001 From: Anthony Liguori Date: Sat, 20 Aug 2011 22:38:31 -0500 Subject: [PATCH] Add trace points for g_malloc/g_free functions Derived from a patch submitted by Avi Kivity. Signed-off-by: Anthony Liguori --- vl.c | 27 +++++++++++++++++++++++++++ 1 files changed, 27 insertions(+), 0 deletions(-) diff --git a/vl.c b/vl.c index e9e14a6..c98961c 100644 --- a/vl.c +++ b/vl.c @@ -2075,6 +2075,26 @@ static const QEMUOption *lookup_opt(int argc, char **argv, return popt; } +static gpointer malloc_and_trace(gsize n_bytes) +{ + void *ptr = malloc(n_bytes); + trace_qemu_malloc(n_bytes, ptr); + return ptr; +} + +static gpointer realloc_and_trace(gpointer mem, gsize n_bytes) +{ + void *ptr = realloc(mem, n_bytes); + trace_qemu_realloc(mem, n_bytes, ptr); + return ptr; +} + +static void free_and_trace(gpointer mem) +{ + trace_qemu_free(mem); + free(mem); +} + int main(int argc, char **argv, char **envp) { const char *gdbstub_dev = NULL; @@ -2103,10 +2123,17 @@ int main(int argc, char **argv, char **envp) const char *trace_file = NULL; const char *log_mask = NULL; const char *log_file = NULL; + GMemVTable mem_trace = { + .malloc = malloc_and_trace, + .realloc = realloc_and_trace, + .free = free_and_trace, + }; atexit(qemu_run_exit_notifiers); error_set_progname(argv[0]); + g_mem_set_vtable(&mem_trace); + init_clocks(); qemu_cache_utils_init(envp); -- 1.7.4.1 --------------000804010907060200020009--