[asterisk-commits] mmichelson: branch mmichelson/ao2_containers r141993 - in /team/mmichelson/ao...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Tue Sep 9 02:08:08 CDT 2008
Author: mmichelson
Date: Tue Sep 9 02:08:07 2008
New Revision: 141993
URL: http://svn.digium.com/view/asterisk?view=rev&rev=141993
Log:
Adjust the linked list implementation so that instead
of having two versions of several functions, there is
now a direction variable which is passed to the
allocation function. This will determine how new items
are linked and which direction the version comparisons
should be when iterating
Modified:
team/mmichelson/ao2_containers/include/asterisk/astobj2.h
team/mmichelson/ao2_containers/main/astobj2_linkedlist.c
Modified: team/mmichelson/ao2_containers/include/asterisk/astobj2.h
URL: http://svn.digium.com/view/asterisk/team/mmichelson/ao2_containers/include/asterisk/astobj2.h?view=diff&rev=141993&r1=141992&r2=141993
==============================================================================
--- team/mmichelson/ao2_containers/include/asterisk/astobj2.h (original)
+++ team/mmichelson/ao2_containers/include/asterisk/astobj2.h Tue Sep 9 02:08:07 2008
@@ -678,8 +678,9 @@
struct ao2_container *ao2_hashtable_alloc(const uint n_buckets,
ao2_hash_fn *hash_fn, ao2_callback_fn *cmp_fn);
-struct ao2_container *ao2_linkedlist_alloc_front(ao2_callback_fn *cmp_fn);
-struct ao2_container *ao2_linkedlist_alloc_back(ao2_callback_fn *cmp_fn);
+#define AO2_LINKEDLIST_FRONT 1
+#define AO2_LINKEDLIST_BACK 2
+struct ao2_container *ao2_linkedlist_alloc(ao2_callback_fn *cmp_fn, int direction);
/*! \brief
* Returns the number of elements in a container.
Modified: team/mmichelson/ao2_containers/main/astobj2_linkedlist.c
URL: http://svn.digium.com/view/asterisk/team/mmichelson/ao2_containers/main/astobj2_linkedlist.c?view=diff&rev=141993&r1=141992&r2=141993
==============================================================================
--- team/mmichelson/ao2_containers/main/astobj2_linkedlist.c (original)
+++ team/mmichelson/ao2_containers/main/astobj2_linkedlist.c Tue Sep 9 02:08:07 2008
@@ -26,21 +26,17 @@
static void *linkedlist_iterator_next_front(struct ao2_iterator *a);
static void linkedlist_destroy(struct ao2_container *c);
-static const struct ao2_container_impl linkedlist_back_impl = {
- .link = linkedlist_link_back,
+static const struct ao2_container_impl linkedlist_impl = {
+ .link = linkedlist_link,
.callback = linkedlist_callback,
- .iterator_next = linkedlist_iterator_next_back,
+ .iterator_next = linkedlist_iterator_next,
.destroy = linkedlist_destroy,
};
-static const struct ao2_container_impl linkedlist_front_impl = {
- .link = linkedlist_link_front,
- .callback = linkedlist_callback,
- .iterator_next = linkedlist_iterator_next_front,
- .destroy = linkedlist_destroy,
-};
-
-AST_LIST_HEAD_NOLOCK(the_list, list_item);
+struct linkedlist_pvt {
+ AST_LIST_HEAD_NOLOCK(the_list, list_item);
+ int direction;
+}
struct list_item {
AST_LIST_ENTRY(list_item) entry;
@@ -48,46 +44,29 @@
struct astobj2 *astobj;
};
-struct ao2_container *ao2_linkedlist_alloc_back(ao2_callback_fn *cmp_fn)
+struct ao2_container *ao2_linkedlist_alloc_back(ao2_callback_fn *cmp_fn, int direction)
{
struct ao2_container *list = ao2_container_common_alloc(cmp_fn, &linkedlist_back_impl);
- struct the_list *new_list;
+ struct linkedlist_pvt *list_pvt;
if (!list) {
return NULL;
}
- if (!(new_list = ast_calloc(1, sizeof(*new_list)))) {
+ if (!(list_pvt = ast_calloc(1, sizeof(*new_list)))) {
ao2_ref(list, -1);
return NULL;
}
- list->private = new_list;
- return list;
-}
-
-struct ao2_container *ao2_linkedlist_alloc_front(ao2_callback_fn *cmp_fn)
-{
- struct ao2_container *list = ao2_container_common_alloc(cmp_fn, &linkedlist_front_impl);
- struct the_list *new_list;
-
- if (!list) {
- return NULL;
- }
-
- if (!(new_list = ast_calloc(1, sizeof(*new_list)))) {
- ao2_ref(list, -1);
- return NULL;
- }
-
- list->private = new_list;
+ list_pvt->direction = direction;
+ list->private = list_pvt;
return list;
}
static void *linkedlist_link_back(struct ao2_container *c, struct astobj2 *newobj)
{
struct list_item *item = ast_calloc(1, sizeof(*item));
- struct the_list *list = c->private;
+ struct linkedlist_pvt *list_pvt = c->private;
if (!item) {
return NULL;
@@ -96,23 +75,11 @@
item->astobj = newobj;
item->version = ast_atomic_fetchadd_int(&c->version, 1);
- AST_LIST_INSERT_TAIL(list, item, entry);
- return item;
-}
-
-static void *linkedlist_link_front(struct ao2_container *c, struct astobj2 *newobj)
-{
- struct list_item *item = ast_calloc(1, sizeof(*item));
- struct the_list *list = c->private;
-
- if (!item) {
- return NULL;
+ if (list_pvt->direction == AO2_LINKEDLIST_BACK) {
+ AST_LIST_INSERT_TAIL(list, item, entry);
+ } else {
+ AST_LIST_INSERT_HEAD(list, item, entry);
}
-
- item->astobj = newobj;
- item->version = ast_atomic_fetchadd_int(&c->version, 1);
-
- AST_LIST_INSERT_HEAD(list, item, entry);
return item;
}
@@ -168,8 +135,11 @@
}
AST_LIST_TRAVERSE(list, item, entry) {
- if (item->version > a->version)
+ if (list_pvt->direction == AO2_LINKEDLIST_BACK && item->version > a->version) {
goto found;
+ } else if (list_pvt->direction == AO2_LINKEDLIST_FRONT && item->version < a->version) {
+ goto found;
+ }
}
found:
@@ -183,34 +153,6 @@
return ret;
}
-static void *linkedlist_iterator_next_front(struct ao2_iterator *a)
-{
- struct the_list *list = a->c->private;
- struct list_item *item;
- void* ret = NULL;
-
- if (a->c->version == a->c_version && (item = a->obj)) {
- if ((item = AST_LIST_NEXT(item, entry))) {
- goto found;
- }
- a->obj = NULL;
- }
-
- AST_LIST_TRAVERSE(list, item, entry) {
- if (item->version < a->version)
- goto found;
- }
-
-found:
- if (item) {
- a->version = item->version;
- a->obj = item;
- a->c_version = a->c->version;
- ret = item->astobj;
- }
-
- return ret;
-}
static void linkedlist_destroy(struct ao2_container *c)
{
struct the_list *list = c->private;
More information about the asterisk-commits
mailing list