From patchwork Wed Nov 11 17:28:55 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 38155 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 27008B7088 for ; Thu, 12 Nov 2009 04:50:14 +1100 (EST) Received: from localhost ([127.0.0.1]:41647 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N8HKd-0004a0-OO for incoming@patchwork.ozlabs.org; Wed, 11 Nov 2009 12:50:11 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1N8H0a-0006sk-Sv for qemu-devel@nongnu.org; Wed, 11 Nov 2009 12:29:29 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1N8H0Q-0006kK-3n for qemu-devel@nongnu.org; Wed, 11 Nov 2009 12:29:24 -0500 Received: from [199.232.76.173] (port=48452 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N8H0P-0006k8-Hm for qemu-devel@nongnu.org; Wed, 11 Nov 2009 12:29:17 -0500 Received: from e34.co.us.ibm.com ([32.97.110.152]:48399) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1N8H0O-0001zs-Qs for qemu-devel@nongnu.org; Wed, 11 Nov 2009 12:29:17 -0500 Received: from d03relay02.boulder.ibm.com (d03relay02.boulder.ibm.com [9.17.195.227]) by e34.co.us.ibm.com (8.14.3/8.13.1) with ESMTP id nABHO70d007847 for ; Wed, 11 Nov 2009 10:24:07 -0700 Received: from d03av03.boulder.ibm.com (d03av03.boulder.ibm.com [9.17.195.169]) by d03relay02.boulder.ibm.com (8.13.8/8.13.8/NCO v9.1) with ESMTP id nABHT6Pw196890 for ; Wed, 11 Nov 2009 10:29:06 -0700 Received: from d03av03.boulder.ibm.com (loopback [127.0.0.1]) by d03av03.boulder.ibm.com (8.14.3/8.13.1/NCO v10.0 AVout) with ESMTP id nABHT6aK007233 for ; Wed, 11 Nov 2009 10:29:06 -0700 Received: from localhost.localdomain (sig-9-65-32-87.mts.ibm.com [9.65.32.87]) by d03av03.boulder.ibm.com (8.14.3/8.13.1/NCO v10.0 AVin) with ESMTP id nABHT43E007179; Wed, 11 Nov 2009 10:29:05 -0700 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Wed, 11 Nov 2009 11:28:55 -0600 Message-Id: <1257960543-26373-3-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.6.2.5 In-Reply-To: <1257960543-26373-1-git-send-email-aliguori@us.ibm.com> References: <1257960543-26373-1-git-send-email-aliguori@us.ibm.com> X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: Anthony Liguori , Luiz Capitulino Subject: [Qemu-devel] [PATCH 03/11] Allow strings to grow in size X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This lets us use QString for building larger strings Signed-off-by: Anthony Liguori --- qstring.c | 37 ++++++++++++++++++++++++++++++++++++- qstring.h | 4 ++++ 2 files changed, 40 insertions(+), 1 deletions(-) diff --git a/qstring.c b/qstring.c index 6d411da..441a9e6 100644 --- a/qstring.c +++ b/qstring.c @@ -21,6 +21,16 @@ static const QType qstring_type = { }; /** + * qstring_new(): Create a new empty QString + * + * Return strong reference. + */ +QString *qstring_new(void) +{ + return qstring_from_str(""); +} + +/** * qstring_from_str(): Create a new QString from a regular C string * * Return strong reference. @@ -30,12 +40,37 @@ QString *qstring_from_str(const char *str) QString *qstring; qstring = qemu_malloc(sizeof(*qstring)); - qstring->string = qemu_strdup(str); + + qstring->length = strlen(str); + qstring->capacity = qstring->length; + + qstring->string = qemu_malloc(qstring->capacity + 1); + memcpy(qstring->string, str, qstring->length); + qstring->string[qstring->length] = 0; + QOBJECT_INIT(qstring, &qstring_type); return qstring; } +/* qstring_append(): Append a C string to a QString + */ +void qstring_append(QString *qstring, const char *str) +{ + size_t len = strlen(str); + + if (qstring->capacity < (qstring->length + len)) { + qstring->capacity += len; + qstring->capacity *= 2; /* use exponential growth */ + + qstring->string = qemu_realloc(qstring->string, qstring->capacity + 1); + } + + memcpy(qstring->string + qstring->length, str, len); + qstring->length += len; + qstring->string[qstring->length] = 0; +} + /** * qobject_to_qstring(): Convert a QObject to a QString */ diff --git a/qstring.h b/qstring.h index e012cb7..65905d4 100644 --- a/qstring.h +++ b/qstring.h @@ -6,10 +6,14 @@ typedef struct QString { QObject_HEAD; char *string; + size_t length; + size_t capacity; } QString; +QString *qstring_new(void); QString *qstring_from_str(const char *str); const char *qstring_get_str(const QString *qstring); +void qstring_append(QString *qstring, const char *str); QString *qobject_to_qstring(const QObject *obj); #endif /* QSTRING_H */