From patchwork Wed Nov 11 17:28:54 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 38158 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 21CA0B708B for ; Thu, 12 Nov 2009 04:57:00 +1100 (EST) Received: from localhost ([127.0.0.1]:48469 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N8HRB-0005OC-33 for incoming@patchwork.ozlabs.org; Wed, 11 Nov 2009 12:56:57 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1N8H0h-0006xG-8e for qemu-devel@nongnu.org; Wed, 11 Nov 2009 12:29:35 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1N8H0a-0006ry-Fm for qemu-devel@nongnu.org; Wed, 11 Nov 2009 12:29:32 -0500 Received: from [199.232.76.173] (port=48461 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N8H0Z-0006rd-VP for qemu-devel@nongnu.org; Wed, 11 Nov 2009 12:29:28 -0500 Received: from e34.co.us.ibm.com ([32.97.110.152]:48555) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1N8H0Z-00022N-Dt for qemu-devel@nongnu.org; Wed, 11 Nov 2009 12:29:27 -0500 Received: from d03relay04.boulder.ibm.com (d03relay04.boulder.ibm.com [9.17.195.106]) by e34.co.us.ibm.com (8.14.3/8.13.1) with ESMTP id nABHOKd3008658 for ; Wed, 11 Nov 2009 10:24:20 -0700 Received: from d03av03.boulder.ibm.com (d03av03.boulder.ibm.com [9.17.195.169]) by d03relay04.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id nABHT5hj213116 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 nABHT52A007209 for ; Wed, 11 Nov 2009 10:29:05 -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 nABHT43D007179; Wed, 11 Nov 2009 10:29:05 -0700 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Wed, 11 Nov 2009 11:28:54 -0600 Message-Id: <1257960543-26373-2-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 02/11] Add operations to qlist to allow it to be used as a stack 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 makes lists no longer invariant. It's a very useful bit of functionality though. To deal with the fact that lists are no longer invariant, introduce a deep copy mechanism for lists. Signed-off-by: Anthony Liguori --- qlist.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qlist.h | 4 ++++ 2 files changed, 60 insertions(+), 0 deletions(-) diff --git a/qlist.c b/qlist.c index ba2c66c..5fccb7d 100644 --- a/qlist.c +++ b/qlist.c @@ -37,6 +37,23 @@ QList *qlist_new(void) return qlist; } +static void qlist_copy_elem(QObject *obj, void *opaque) +{ + QList *dst = opaque; + + qobject_incref(obj); + qlist_append_obj(dst, obj); +} + +QList *qlist_copy(QList *src) +{ + QList *dst = qlist_new(); + + qlist_iter(src, qlist_copy_elem, dst); + + return dst; +} + /** * qlist_append_obj(): Append an QObject into QList * @@ -67,6 +84,45 @@ void qlist_iter(const QList *qlist, iter(entry->value, opaque); } +QObject *qlist_pop(QList *qlist) +{ + QListEntry *entry; + QObject *ret; + + if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) { + return NULL; + } + + entry = QTAILQ_FIRST(&qlist->head); + QTAILQ_REMOVE(&qlist->head, entry, next); + + ret = entry->value; + qemu_free(entry); + + return ret; +} + +QObject *qlist_peek(QList *qlist) +{ + QListEntry *entry; + QObject *ret; + + if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) { + return NULL; + } + + entry = QTAILQ_FIRST(&qlist->head); + + ret = entry->value; + + return ret; +} + +int qlist_empty(const QList *qlist) +{ + return QTAILQ_EMPTY(&qlist->head); +} + /** * qobject_to_qlist(): Convert a QObject into a QList */ diff --git a/qlist.h b/qlist.h index 3eb1eb8..afdc446 100644 --- a/qlist.h +++ b/qlist.h @@ -30,9 +30,13 @@ typedef struct QList { qlist_append_obj(qlist, QOBJECT(obj)) QList *qlist_new(void); +QList *qlist_copy(QList *src); void qlist_append_obj(QList *qlist, QObject *obj); void qlist_iter(const QList *qlist, void (*iter)(QObject *obj, void *opaque), void *opaque); +QObject *qlist_pop(QList *qlist); +QObject *qlist_peek(QList *qlist); +int qlist_empty(const QList *qlist); QList *qobject_to_qlist(const QObject *obj); #endif /* QLIST_H */