[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 02/11] Add operations to qlist to allow it to be use
From: |
Anthony Liguori |
Subject: |
[Qemu-devel] [PATCH 02/11] Add operations to qlist to allow it to be used as a stack |
Date: |
Wed, 11 Nov 2009 11:28:54 -0600 |
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 <address@hidden>
---
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 */
--
1.6.2.5
- [Qemu-devel] [PATCH 01/11] Properly escape QDECREF macro arguments, Anthony Liguori, 2009/11/11
- [Qemu-devel] [PATCH 09/11] Add a JSON parser, Anthony Liguori, 2009/11/11
- [Qemu-devel] [PATCH 05/11] Add unit test for QFloat, Anthony Liguori, 2009/11/11
- [Qemu-devel] [PATCH 11/11] Add a unit test for JSON support, Anthony Liguori, 2009/11/11
- [Qemu-devel] [PATCH 06/11] Add a QBool type, Anthony Liguori, 2009/11/11
- [Qemu-devel] [PATCH 03/11] Allow strings to grow in size, Anthony Liguori, 2009/11/11
- [Qemu-devel] [PATCH 08/11] Add a JSON message boundary identifier, Anthony Liguori, 2009/11/11
- [Qemu-devel] [PATCH 02/11] Add operations to qlist to allow it to be used as a stack,
Anthony Liguori <=
- Re: [Qemu-devel] [PATCH 02/11] Add operations to qlist to allow it to be used as a stack, Kevin Wolf, 2009/11/12
- Re: [Qemu-devel] [PATCH 02/11] Add operations to qlist to allow it to be used as a stack, Anthony Liguori, 2009/11/12
- Re: [Qemu-devel] [PATCH 02/11] Add operations to qlist to allow it to be used as a stack, Kevin Wolf, 2009/11/12
- Re: [Qemu-devel] [PATCH 02/11] Add operations to qlist to allow it to be used as a stack, Anthony Liguori, 2009/11/12
- Re: [Qemu-devel] [PATCH 02/11] Add operations to qlist to allow it to be used as a stack, Luiz Capitulino, 2009/11/12
- Re: [Qemu-devel] [PATCH 02/11] Add operations to qlist to allow it to be used as a stack, Kevin Wolf, 2009/11/13
- Re: [Qemu-devel] [PATCH 02/11] Add operations to qlist to allow it to be used as a stack, Ian Molton, 2009/11/12
[Qemu-devel] [PATCH 04/11] Add a QFloat datatype, Anthony Liguori, 2009/11/11
[Qemu-devel] [PATCH 10/11] Add a QObject JSON wrapper, Anthony Liguori, 2009/11/11