From 033815fe14e9e6254f1483113a6dd6d5b130ef58 Mon Sep 17 00:00:00 2001 From: Anthony Liguori Date: Wed, 11 Nov 2009 10:48:51 -0600 Subject: Add operations to qlist to allow it to be used as a stack 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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'qlist.c') diff --git a/qlist.c b/qlist.c index ba2c66c01c..5fccb7d095 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 */ -- cgit v1.2.3