[asterisk-commits] murf: branch murf/bug11210 r95373 - in /team/murf/bug11210: include/asterisk/...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Sun Dec 30 01:14:00 CST 2007
Author: murf
Date: Sun Dec 30 01:13:50 2007
New Revision: 95373
URL: http://svn.digium.com/view/asterisk?view=rev&rev=95373
Log:
Just to be sure my dlinkedlist.h macros were clean, I wrote up a test that would exersize every macro (well, all the IMPORTANT macros, at least). It's good I did, I found and fixed some problems, especially in the backwards traversal stuff. I stuck the regression tests in sched.c for want of a better place; someday, maybe we can put this kind of thing in a separate module or something. It gets run at the tail end of 'sip reload'. At first I thought my fixes might reflect back into the singly-linked-list macros, but further thinking rejected that: most of the problems were in what to set __new_prev after an operation. Backwards traversals just have different requirements.
Modified:
team/murf/bug11210/include/asterisk/dlinkedlists.h
team/murf/bug11210/main/sched.c
Modified: team/murf/bug11210/include/asterisk/dlinkedlists.h
URL: http://svn.digium.com/view/asterisk/team/murf/bug11210/include/asterisk/dlinkedlists.h?view=diff&rev=95373&r1=95372&r2=95373
==============================================================================
--- team/murf/bug11210/include/asterisk/dlinkedlists.h (original)
+++ team/murf/bug11210/include/asterisk/dlinkedlists.h Sun Dec 30 01:13:50 2007
@@ -6,7 +6,7 @@
* Steve Murphy <murf at digium.com>
*
* Doubly-Linked List Macros--
- * Based on linklists.h (to the point of plagiarism!), which is by:
+ * Based on linkedlists.h (to the point of plagiarism!), which is by:
*
* Mark Spencer <markster at digium.com>
* Kevin P. Fleming <kpfleming at digium.com>
@@ -628,7 +628,10 @@
#define AST_DLLIST_REMOVE_CURRENT(field) do { \
__new_prev->field.next = NULL; \
__new_prev->field.prev = NULL; \
- __new_prev = __list_prev; \
+ if (__list_next) \
+ __new_prev = __list_prev; \
+ else \
+ __new_prev = NULL; \
if (__list_prev) { \
if (__list_next) \
__list_next->field.prev = __list_prev; \
@@ -653,10 +656,15 @@
#define AST_RWDLLIST_MOVE_CURRENT AST_DLLIST_MOVE_CURRENT
#define AST_DLLIST_MOVE_CURRENT_BACKWARDS(newhead, field) do { \
- typeof ((newhead)->first) __list_cur = __new_prev; \
- AST_DLLIST_REMOVE_CURRENT(field); \
- AST_DLLIST_INSERT_HEAD((newhead), __list_cur, field); \
- } while (0)
+ typeof ((newhead)->first) __list_cur = __new_prev; \
+ if (!__list_next) { \
+ AST_DLLIST_REMOVE_CURRENT(field); \
+ __new_prev = NULL; \
+ AST_DLLIST_INSERT_HEAD((newhead), __list_cur, field); \
+ } else { \
+ AST_DLLIST_REMOVE_CURRENT(field); \
+ AST_DLLIST_INSERT_HEAD((newhead), __list_cur, field); \
+ }} while (0)
#define AST_RWDLLIST_MOVE_CURRENT_BACKWARDS AST_DLLIST_MOVE_CURRENT
@@ -669,19 +677,19 @@
\note This macro can \b only be used inside an AST_DLLIST_TRAVERSE_SAFE_BEGIN()
block.
*/
-#define AST_DLLIST_INSERT_BEFORE_CURRENT(elm, field) do { \
- if (__list_prev) { \
+#define AST_DLLIST_INSERT_BEFORE_CURRENT(elm, field) do { \
+ if (__list_prev) { \
(elm)->field.next = __list_prev->field.next; \
- (elm)->field.prev = __list_prev; \
+ (elm)->field.prev = __list_prev; \
if (__list_prev->field.next) \
__list_prev->field.next->field.prev = (elm); \
__list_prev->field.next = (elm); \
- } else { \
+ } else { \
(elm)->field.next = __list_head->first; \
- (elm)->field.prev = NULL; \
- __list_head->first = (elm); \
- } \
- __new_prev = (elm); \
+ __list_head->first->field.prev = (elm); \
+ (elm)->field.prev = NULL; \
+ __list_head->first = (elm); \
+ } \
} while (0)
#define AST_RWDLLIST_INSERT_BEFORE_CURRENT AST_DLLIST_INSERT_BEFORE_CURRENT
@@ -700,16 +708,17 @@
strange things!
*/
#define AST_DLLIST_INSERT_BEFORE_CURRENT_BACKWARDS(elm, field) do { \
- if (__list_next) { \
- (elm)->field.next = __list_next; \
- (elm)->field.prev = __new_prev; \
- __new_prev->field.next = (elm); \
- __list_next->field.prev = (elm); \
- } else { \
- (elm)->field.prev = __list_head->last; \
- (elm)->field.next = NULL; \
- __list_head->last = (elm); \
- } \
+ if (__list_next) { \
+ (elm)->field.next = __list_next; \
+ (elm)->field.prev = __new_prev; \
+ __new_prev->field.next = (elm); \
+ __list_next->field.prev = (elm); \
+ } else { \
+ (elm)->field.prev = __list_head->last; \
+ (elm)->field.next = NULL; \
+ __list_head->last->field.next = (elm); \
+ __list_head->last = (elm); \
+ } \
} while (0)
#define AST_RWDLLIST_INSERT_BEFORE_CURRENT_BACKWARDS AST_DLLIST_INSERT_BEFORE_CURRENT_BACKWARDS
@@ -720,6 +729,13 @@
#define AST_DLLIST_TRAVERSE_SAFE_END }
#define AST_RWDLLIST_TRAVERSE_SAFE_END AST_DLLIST_TRAVERSE_SAFE_END
+
+/*!
+ \brief Closes a safe loop traversal block.
+ */
+#define AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END }
+
+#define AST_RWDLLIST_TRAVERSE_BACKWARDS_SAFE_END AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END
/*!
\brief Initializes a list head structure.
@@ -839,15 +855,18 @@
actually the head of a list itself, the entire list will be appended
temporarily (until the next AST_DLLIST_INSERT_TAIL is performed).
*/
-#define AST_DLLIST_INSERT_TAIL(head, elm, field) do { \
+#define AST_DLLIST_INSERT_TAIL(head, elm, field) do { \
if (!(head)->first) { \
(head)->first = (elm); \
+ (head)->last = (elm); \
+ (elm)->field.next = NULL; \
+ (elm)->field.prev = NULL; \
+ } else { \
+ (head)->last->field.next = (elm); \
+ (elm)->field.prev = (head)->last; \
+ (elm)->field.next = NULL; \
(head)->last = (elm); \
- } else { \
- (head)->last->field.next = (elm); \
- (elm)->field.prev = (head)->last; \
- (head)->last = (elm); \
- } \
+ } \
} while (0)
#define AST_RWDLLIST_INSERT_TAIL AST_DLLIST_INSERT_TAIL
Modified: team/murf/bug11210/main/sched.c
URL: http://svn.digium.com/view/asterisk/team/murf/bug11210/main/sched.c?view=diff&rev=95373&r1=95372&r2=95373
==============================================================================
--- team/murf/bug11210/main/sched.c (original)
+++ team/murf/bug11210/main/sched.c Sun Dec 30 01:13:50 2007
@@ -344,7 +344,320 @@
return 0;
}
-
+/* Tests for DLLists! We really should, and here is a nice place to do it in asterisk */
+
+struct test1
+{
+ char name[10];
+ AST_DLLIST_ENTRY(test1) list;
+};
+
+struct test_container
+{
+ AST_DLLIST_HEAD(entries, test1) entries;
+ int count;
+};
+
+static void print_list(struct test_container *x, char *expect)
+{
+ struct test1 *t1;
+ char buff[1000];
+ buff[0] = 0;
+ AST_DLLIST_TRAVERSE(&x->entries, t1, list) {
+ strcat(buff,t1->name);
+ if (t1 != AST_DLLIST_LAST(&x->entries))
+ strcat(buff," <=> ");
+ }
+
+ ast_log(LOG_NOTICE,"Got: %s [expect %s]\n", buff, expect);
+}
+
+static void print_list_backwards(struct test_container *x, char *expect)
+{
+ struct test1 *t1;
+ char buff[1000];
+ buff[0] = 0;
+ AST_DLLIST_TRAVERSE_BACKWARDS(&x->entries, t1, list) {
+ strcat(buff,t1->name);
+ if (t1 != AST_DLLIST_FIRST(&x->entries))
+ strcat(buff," <=> ");
+ }
+
+ ast_log(LOG_NOTICE,"Got: %s [expect %s]\n", buff, expect);
+}
+
+static struct test_container *make_cont(void)
+{
+ struct test_container *t = ast_calloc(sizeof(struct test_container),1);
+ return t;
+}
+
+static struct test1 *make_test1(char *name)
+{
+ struct test1 *t1 = ast_calloc(sizeof(struct test1),1);
+ strcpy(t1->name, name);
+ return t1;
+}
+
+static void destroy_test_container(struct test_container *x)
+{
+ /* remove all the test1's */
+ struct test1 *t1;
+ AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(&x->entries, t1, list) {
+ AST_DLLIST_REMOVE_CURRENT(list);
+ free(t1);
+ }
+ AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END;
+ free(x);
+}
+
+
+/* Macros to test:
+AST_DLLIST_LOCK(head)
+AST_RWDLLIST_WRLOCK(head)
+AST_RWDLLIST_WRLOCK(head)
+AST_RWDLLIST_RDLOCK(head)
+AST_DLLIST_TRYLOCK(head)
+AST_RWDLLIST_TRYWRLOCK(head)
+AST_RWDLLIST_TRYRDLOCK(head)
+AST_DLLIST_UNLOCK(head)
+AST_RWDLLIST_UNLOCK(head)
+
+AST_DLLIST_HEAD(name, type)
+AST_RWDLLIST_HEAD(name, type)
+AST_DLLIST_HEAD_NOLOCK(name, type)
+AST_DLLIST_HEAD_STATIC(name, type)
+AST_RWDLLIST_HEAD_STATIC(name, type)
+AST_DLLIST_HEAD_NOLOCK_STATIC(name, type)
+AST_DLLIST_HEAD_SET(head, entry)
+AST_RWDLLIST_HEAD_SET(head, entry)
+AST_DLLIST_HEAD_SET_NOLOCK(head, entry)
+AST_DLLIST_HEAD_INIT(head)
+AST_RWDLLIST_HEAD_INIT(head)
+AST_DLLIST_HEAD_INIT_NOLOCK(head)
+
+AST_RWDLLIST_HEAD_DESTROY(head)
+
+AST_DLLIST_ENTRY(type)
+
+--- the above not going to be dealt with here ---
+
+AST_DLLIST_INSERT_HEAD(head, elm, field)
+AST_DLLIST_TRAVERSE(head,var,field)
+AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(head, var, field)
+AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END
+AST_DLLIST_FIRST(head)
+AST_DLLIST_LAST(head)
+AST_DLLIST_NEXT(elm, field)
+AST_DLLIST_PREV(elm, field)
+AST_DLLIST_EMPTY(head)
+AST_DLLIST_TRAVERSE_BACKWARDS(head,var,field)
+AST_DLLIST_INSERT_AFTER(head, listelm, elm, field)
+AST_DLLIST_INSERT_TAIL(head, elm, field)
+AST_DLLIST_REMOVE_HEAD(head, field)
+AST_DLLIST_REMOVE(head, elm, field)
+AST_DLLIST_TRAVERSE_SAFE_BEGIN(head, var, field)
+AST_DLLIST_TRAVERSE_SAFE_END
+AST_DLLIST_REMOVE_CURRENT(field)
+AST_DLLIST_MOVE_CURRENT(newhead, field)
+AST_DLLIST_INSERT_BEFORE_CURRENT(elm, field)
+
+
+AST_DLLIST_MOVE_CURRENT_BACKWARDS(newhead, field)
+AST_DLLIST_INSERT_BEFORE_CURRENT_BACKWARDS(elm, field)
+AST_DLLIST_HEAD_DESTROY(head)
+
+AST_DLLIST_APPEND_DLLIST(head, list, field)
+
+*/
+
+
+static void dll_tests(void)
+{
+ struct test_container *tc;
+ struct test1 *a;
+ struct test1 *b;
+ struct test1 *c;
+ struct test1 *d;
+ struct test1 *e;
+
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_INSERT_HEAD, AST_DLLIST_TRAVERSE, AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN, AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END\n");
+ tc = make_cont();
+ a = make_test1("A");
+ b = make_test1("B");
+ c = make_test1("C");
+ d = make_test1("D");
+ AST_DLLIST_INSERT_HEAD(&tc->entries, d, list);
+ AST_DLLIST_INSERT_HEAD(&tc->entries, c, list);
+ AST_DLLIST_INSERT_HEAD(&tc->entries, b, list);
+ AST_DLLIST_INSERT_HEAD(&tc->entries, a, list);
+ print_list(tc, "A <=> B <=> C <=> D");
+
+ destroy_test_container(tc);
+
+ tc = make_cont();
+
+ if (AST_DLLIST_EMPTY(&tc->entries))
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_EMPTY....OK\n");
+ else
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_EMPTY....PROBLEM!!\n");
+
+
+ a = make_test1("A");
+ b = make_test1("B");
+ c = make_test1("C");
+ d = make_test1("D");
+
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_INSERT_TAIL\n");
+ AST_DLLIST_INSERT_TAIL(&tc->entries, a, list);
+ AST_DLLIST_INSERT_TAIL(&tc->entries, b, list);
+ AST_DLLIST_INSERT_TAIL(&tc->entries, c, list);
+ AST_DLLIST_INSERT_TAIL(&tc->entries, d, list);
+ print_list(tc, "A <=> B <=> C <=> D");
+
+ if (AST_DLLIST_FIRST(&tc->entries) == a)
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_FIRST....OK\n");
+ else
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_FIRST....PROBLEM\n");
+
+ if (AST_DLLIST_LAST(&tc->entries) == d)
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_LAST....OK\n");
+ else
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_LAST....PROBLEM\n");
+
+ if (AST_DLLIST_NEXT(a,list) == b)
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_NEXT....OK\n");
+ else
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_NEXT....PROBLEM\n");
+
+ if (AST_DLLIST_PREV(d,list) == c)
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_PREV....OK\n");
+ else
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_PREV....PROBLEM\n");
+
+ destroy_test_container(tc);
+
+ tc = make_cont();
+
+ a = make_test1("A");
+ b = make_test1("B");
+ c = make_test1("C");
+ d = make_test1("D");
+
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_INSERT_AFTER, AST_DLLIST_TRAVERSE_BACKWARDS\n");
+ AST_DLLIST_INSERT_HEAD(&tc->entries, a, list);
+ AST_DLLIST_INSERT_AFTER(&tc->entries, a, b, list);
+ AST_DLLIST_INSERT_AFTER(&tc->entries, b, c, list);
+ AST_DLLIST_INSERT_AFTER(&tc->entries, c, d, list);
+ print_list_backwards(tc, "D <=> C <=> B <=> A");
+
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_REMOVE_HEAD\n");
+ AST_DLLIST_REMOVE_HEAD(&tc->entries, list);
+ print_list_backwards(tc, "D <=> C <=> B");
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_REMOVE_HEAD\n");
+ AST_DLLIST_REMOVE_HEAD(&tc->entries, list);
+ print_list_backwards(tc, "D <=> C");
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_REMOVE_HEAD\n");
+ AST_DLLIST_REMOVE_HEAD(&tc->entries, list);
+ print_list_backwards(tc, "D");
+ AST_DLLIST_REMOVE_HEAD(&tc->entries, list);
+
+ if (AST_DLLIST_EMPTY(&tc->entries))
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_REMOVE_HEAD....OK\n");
+ else
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_REMOVE_HEAD....PROBLEM!!\n");
+
+ AST_DLLIST_INSERT_HEAD(&tc->entries, a, list);
+ AST_DLLIST_INSERT_AFTER(&tc->entries, a, b, list);
+ AST_DLLIST_INSERT_AFTER(&tc->entries, b, c, list);
+ AST_DLLIST_INSERT_AFTER(&tc->entries, c, d, list);
+
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_REMOVE\n");
+ AST_DLLIST_REMOVE(&tc->entries, c, list);
+ print_list(tc, "A <=> B <=> D");
+ AST_DLLIST_REMOVE(&tc->entries, a, list);
+ print_list(tc, "B <=> D");
+ AST_DLLIST_REMOVE(&tc->entries, d, list);
+ print_list(tc, "B");
+ AST_DLLIST_REMOVE(&tc->entries, b, list);
+
+ if (AST_DLLIST_EMPTY(&tc->entries))
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_REMOVE....OK\n");
+ else
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_REMOVE....PROBLEM!!\n");
+
+ AST_DLLIST_INSERT_HEAD(&tc->entries, a, list);
+ AST_DLLIST_INSERT_AFTER(&tc->entries, a, b, list);
+ AST_DLLIST_INSERT_AFTER(&tc->entries, b, c, list);
+ AST_DLLIST_INSERT_AFTER(&tc->entries, c, d, list);
+
+ AST_DLLIST_TRAVERSE_SAFE_BEGIN(&tc->entries, e, list) {
+ AST_DLLIST_REMOVE_CURRENT(list);
+ }
+ AST_DLLIST_TRAVERSE_SAFE_END;
+ if (AST_DLLIST_EMPTY(&tc->entries))
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_REMOVE_CURRENT... OK\n");
+ else
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_REMOVE_CURRENT... PROBLEM\n");
+
+ ast_log(LOG_NOTICE,"Test AST_DLLIST_MOVE_CURRENT, AST_DLLIST_INSERT_BEFORE_CURRENT\n");
+ AST_DLLIST_INSERT_HEAD(&tc->entries, a, list);
+ AST_DLLIST_INSERT_AFTER(&tc->entries, a, b, list);
+ AST_DLLIST_INSERT_AFTER(&tc->entries, b, c, list);
+ AST_DLLIST_TRAVERSE_SAFE_BEGIN(&tc->entries, e, list) {
+ if (e == a) {
+ AST_DLLIST_INSERT_BEFORE_CURRENT(d, list); /* D A B C */
+ }
+
+ if (e == b) {
+ AST_DLLIST_MOVE_CURRENT(&tc->entries, list); /* D A C B */
+ }
+
+ }
+ AST_DLLIST_TRAVERSE_SAFE_END;
+ print_list(tc, "D <=> A <=> C <=> B");
+
+ destroy_test_container(tc);
+
+ tc = make_cont();
+
+ a = make_test1("A");
+ b = make_test1("B");
+ c = make_test1("C");
+ d = make_test1("D");
+
+ ast_log(LOG_NOTICE,"Test:\n AST_DLLIST_MOVE_CURRENT_BACKWARDS\n AST_DLLIST_INSERT_BEFORE_CURRENT_BACKWARDS\n");
+ AST_DLLIST_INSERT_HEAD(&tc->entries, a, list);
+ AST_DLLIST_INSERT_AFTER(&tc->entries, a, b, list);
+ AST_DLLIST_INSERT_AFTER(&tc->entries, b, c, list);
+ AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(&tc->entries, e, list) {
+ if (e == c && AST_DLLIST_FIRST(&tc->entries) != c) {
+ AST_DLLIST_MOVE_CURRENT_BACKWARDS(&tc->entries, list); /* C A B */
+ print_list(tc, "C <=> A <=> B");
+ }
+
+ if (e == b) {
+ AST_DLLIST_REMOVE_CURRENT(list); /* C A */
+ print_list(tc, "C <=> A");
+ }
+ if (e == a) {
+ AST_DLLIST_INSERT_BEFORE_CURRENT_BACKWARDS(d, list); /* C A D */
+ print_list(tc, "C <=> A <=> D");
+ }
+
+ }
+ AST_DLLIST_TRAVERSE_SAFE_END;
+ print_list(tc, "C <=> A <=> D");
+
+}
+
+
+static void ast_sched_regressions(const struct sched_context *con)
+{
+ dll_tests();
+}
+
+
/*! \brief Dump the contents of the scheduler to LOG_DEBUG */
void ast_sched_dump(const struct sched_context *con)
{
@@ -370,6 +683,8 @@
(long int)delta.tv_usec);
}
ast_debug(1, "=============================================================\n");
+ ast_sched_regressions(con);
+
}
/*! \brief
More information about the asterisk-commits
mailing list