From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54080) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vr6Bn-0001ne-0f for qemu-devel@nongnu.org; Thu, 12 Dec 2013 08:20:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Vr6Bh-0001iE-03 for qemu-devel@nongnu.org; Thu, 12 Dec 2013 08:20:26 -0500 Received: from mx1.redhat.com ([209.132.183.28]:37630) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vr6Bg-0001iA-P1 for qemu-devel@nongnu.org; Thu, 12 Dec 2013 08:20:20 -0500 From: Stefan Hajnoczi Date: Thu, 12 Dec 2013 14:19:42 +0100 Message-Id: <1386854384-1992-6-git-send-email-stefanha@redhat.com> In-Reply-To: <1386854384-1992-1-git-send-email-stefanha@redhat.com> References: <1386854384-1992-1-git-send-email-stefanha@redhat.com> Subject: [Qemu-devel] [RFC 5/7] 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 , Michael Roth , Stefan Hajnoczi 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 729efa8..200f853 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.4.2