From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41050) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WGrNn-0003Oo-M3 for qemu-devel@nongnu.org; Fri, 21 Feb 2014 09:47:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WGrNh-0006iE-I8 for qemu-devel@nongnu.org; Fri, 21 Feb 2014 09:47:19 -0500 Received: from mx1.redhat.com ([209.132.183.28]:21124) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WGrNh-0006hw-BG for qemu-devel@nongnu.org; Fri, 21 Feb 2014 09:47:13 -0500 From: Stefan Hajnoczi Date: Fri, 21 Feb 2014 15:46:56 +0100 Message-Id: <1392994018-7786-5-git-send-email-stefanha@redhat.com> In-Reply-To: <1392994018-7786-1-git-send-email-stefanha@redhat.com> References: <1392994018-7786-1-git-send-email-stefanha@redhat.com> Subject: [Qemu-devel] [PATCH v4 4/6] qdev: add get_pointer_and_free() for temporary strings List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Paolo Bonzini , Fam Zheng , Michael Roth get_pointer() assumes the string has unspecified lifetime (at least as long as the object is alive). In some cases we can only produce a temporary string that should be freed when get_pointer() is done. Signed-off-by: Stefan Hajnoczi --- hw/core/qdev-properties-system.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/hw/core/qdev-properties-system.c b/hw/core/qdev-properties-system.c index 5f5957e..3bffc01 100644 --- a/hw/core/qdev-properties-system.c +++ b/hw/core/qdev-properties-system.c @@ -31,6 +31,20 @@ static void get_pointer(Object *obj, Visitor *v, Property *prop, visit_type_str(v, &p, name, errp); } +/* Same as get_pointer() but frees heap-allocated print() return value */ +static void get_pointer_and_free(Object *obj, Visitor *v, Property *prop, + char *(*print)(void *ptr), + const char *name, Error **errp) +{ + DeviceState *dev = DEVICE(obj); + void **ptr = qdev_get_prop_ptr(dev, prop); + char *p; + + p = *ptr ? print(*ptr) : g_strdup(""); + visit_type_str(v, &p, name, errp); + g_free(p); +} + static void set_pointer(Object *obj, Visitor *v, Property *prop, int (*parse)(DeviceState *dev, const char *str, void **ptr), -- 1.8.5.3