tlist: remove type arg from tlist_top(), tlist_tail()
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 1 Dec 2011 06:09:47 +0000 (16:39 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 1 Dec 2011 06:09:47 +0000 (16:39 +1030)
With the type canary, it's unnecessary.  Though the implementation is
a bit more awkward since they longer map directly through to list_top/tail.

ccan/failtest/failtest.c
ccan/failtest/failtest.h
ccan/lbalance/lbalance.c
ccan/tdb2/test/failtest_helper.c
ccan/tlist/test/compile_fail-tlist_tail.c
ccan/tlist/test/compile_fail-tlist_top.c
ccan/tlist/test/run.c
ccan/tlist/tlist.h
tools/ccanlint/async.c

index 97bcb62cb552d268cc4a6e93a447688715b76ae9..215ebfab360781e4592004f779c044b680148aec 100644 (file)
@@ -570,7 +570,7 @@ static void free_everything(void)
 {
        struct failtest_call *i;
 
-       while ((i = tlist_top(&history, struct failtest_call, list)) != NULL)
+       while ((i = tlist_top(&history, list)) != NULL)
                free_call(i);
 
        failtable_clear(&failtable);
@@ -755,7 +755,7 @@ static bool should_fail(struct failtest_call *call)
                        char *failpath;
                        struct failtest_call *c;
 
-                       c = tlist_tail(&history, struct failtest_call, list);
+                       c = tlist_tail(&history, list);
                        diff = time_sub(time_now(), start);
                        failpath = failpath_string();
                        p = strrchr(c->file, '/');
@@ -770,8 +770,7 @@ static bool should_fail(struct failtest_call *call)
                        free(failpath);
                }
                /* From here on, we have to clean up! */
-               our_history_start = tlist_tail(&history, struct failtest_call,
-                                              list);
+               our_history_start = tlist_tail(&history, list);
                close(control[0]);
                close(output[0]);
                /* Don't swallow stderr if we're tracing. */
index 5c80bfeea82cf74302517c0569d146561a139c68..9af5669678f2295edccb5cbea4cb777016f40b8f 100644 (file)
@@ -214,7 +214,7 @@ enum failtest_result {
  *     static enum failtest_result dont_fail_alloc(struct tlist_calls *history)
  *     {
  *             struct failtest_call *call;
- *             call = tlist_tail(history, struct failtest_call, list);
+ *             call = tlist_tail(history, list);
  *             if (call->type == FAILTEST_MALLOC
  *                     || call->type == FAILTEST_CALLOC
  *                     || call->type == FAILTEST_REALLOC)
index d3763a3f7884259bb5c8b70379f287dd66194cb8..a350c81dd29f9e1b1f10197f56f65d76c9bf2ea1 100644 (file)
@@ -315,7 +315,7 @@ void lbalance_free(struct lbalance *lb)
 {
        struct lbalance_task *task;
 
-       while ((task = tlist_top(&lb->tasks, struct lbalance_task, list))) {
+       while ((task = tlist_top(&lb->tasks, list))) {
                assert(task->lb == lb);
                tlist_del_from(&lb->tasks, task, list);
                lb->num_tasks--;
index 3172e3ad6e150478812c6b9e33434929ee147167..ab79de1978c5d80291426dfa565b7a544b8cf48b 100644 (file)
@@ -72,7 +72,7 @@ block_repeat_failures(struct tlist_calls *history)
 {
        const struct failtest_call *last;
 
-       last = tlist_tail(history, struct failtest_call, list);
+       last = tlist_tail(history, list);
 
        if (failtest_suppress)
                return FAIL_DONT_FAIL;
index 0a9dabace7bad8462898fa794d496d8fa9d02da6..48f394446e076421c307dddcf2c1a70e91683d79 100644 (file)
@@ -16,21 +16,16 @@ int main(int argc, char *argv[])
 {
        struct tlist_children children;
        struct child child = { "child" };
-       void *c;
+#ifdef FAIL
+       struct cousin *c;
+#else
+       struct child *c;
+#endif
 
        tlist_init(&children);
        tlist_add(&children, &child, list);
 
-       c = tlist_tail(&children,
-#ifdef FAIL
-#if !HAVE_FLEXIBLE_ARRAY_MEMBER
-#error Need flexible array members to check type
-#endif
-                     struct cousin,
-#else
-                     struct child,
-#endif
-                     list);
+       c = tlist_tail(&children, list);
        (void) c; /* Suppress unused-but-set-variable warning. */
        return 0;
 }
index d02c3add7a2e0a4fb780527e63e433a519510d38..21651400ef0d94bf7cf6d2f7c9a55fdd0399b2dc 100644 (file)
@@ -16,21 +16,16 @@ int main(int argc, char *argv[])
 {
        struct tlist_children children;
        struct child child = { "child" };
-       void *c;
+#ifdef FAIL
+       struct cousin *c;
+#else
+       struct child *c;
+#endif
 
        tlist_init(&children);
        tlist_add(&children, &child, list);
 
-       c = tlist_top(&children,
-#ifdef FAIL
-#if !HAVE_FLEXIBLE_ARRAY_MEMBER
-#error Need flexible array members to check type
-#endif
-                     struct cousin,
-#else
-                     struct child,
-#endif
-                     list);
+       c = tlist_top(&children, list);
        (void) c; /* Suppress unused-but-set-variable warning. */
        return 0;
 }
index 027028e602401f899e0035f5f61281e4dda11748..f94438eaf69551bc117754143d554ee9246ca326 100644 (file)
@@ -73,10 +73,10 @@ int main(int argc, char *argv[])
        ok1(tlist_check(&parent.children, NULL));
 
        /* Test tlist_top */
-       ok1(tlist_top(&parent.children, struct child, list) == &c1);
+       ok1(tlist_top(&parent.children, list) == &c1);
 
        /* Test list_tail */
-       ok1(tlist_tail(&parent.children, struct child, list) == &c3);
+       ok1(tlist_tail(&parent.children, list) == &c3);
 
        /* Test tlist_for_each. */
        i = 0;
@@ -141,7 +141,7 @@ int main(int argc, char *argv[])
        ok1(tlist_empty(&parent.children));
 
        /* Test list_top/list_tail on empty list. */
-       ok1(tlist_top(&parent.children, struct child, list) == NULL);
-       ok1(tlist_tail(&parent.children, struct child, list) == NULL);
+       ok1(tlist_top(&parent.children, list) == (struct child *)NULL);
+       ok1(tlist_tail(&parent.children, list) == (struct child *)NULL);
        return exit_status();
 }
index 3576146c04f1a82488d2f4f707d1ca7b6b06798d..1ce0b85ed9f03c01333c8412c2d260541f8724cb 100644 (file)
 /**
  * tlist_top - get the first entry in a list
  * @h: the tlist
- * @type: the type of the entry
  * @member: the list_node member of the type
  *
  * If the list is empty, returns NULL.
  *
  * Example:
  *     struct child *first;
- *     first = tlist_top(&parent->children, struct child, list);
+ *     first = tlist_top(&parent->children, list);
  */
-#define tlist_top(h, type, member) \
-       list_top(tlist_raw((h), (type *)NULL), type, member)
+#define tlist_top(h, member)                                           \
+       ((tcon_type((h), canary))                                       \
+        list_top_(&(h)->raw,                                           \
+                  (char *)(&(h)->_tcon[0].canary->member) -            \
+                  (char *)((h)->_tcon[0].canary)))
 
 /**
  * tlist_tail - get the last entry in a list
  * @h: the tlist
- * @type: the type of the entry
  * @member: the list_node member of the type
  *
  * If the list is empty, returns NULL.
  *
  * Example:
  *     struct child *last;
- *     last = tlist_tail(&parent->children, struct child, list);
+ *     last = tlist_tail(&parent->children, list);
  */
-#define tlist_tail(h, type, member) \
-       list_tail(tlist_raw((h), (type *)NULL), type, member)
+#define tlist_tail(h, member)                                          \
+       ((tcon_type((h), canary))                                       \
+        list_tail_(&(h)->raw,                                          \
+                   (char *)(&(h)->_tcon[0].canary->member) -           \
+                   (char *)((h)->_tcon[0].canary)))
 
 /**
  * tlist_for_each - iterate through a list.
index e12e140daae9b0a4d5cccd0ae08b076fb782f5c4..4f51dd62db942d7a4d4f63a9e33ec98d6e85906d 100644 (file)
@@ -48,7 +48,7 @@ static void run_more(void)
        while (num_running < lbalance_target(lb)) {
                int p[2];
 
-               c = tlist_top(&pending, struct command, list);
+               c = tlist_top(&pending, list);
                if (!c)
                        break;
 
@@ -189,7 +189,7 @@ void *collect_command(bool *ok, char **output)
        struct command *c;
        const void *ctx;
 
-       while ((c = tlist_top(&done, struct command, list)) == NULL) {
+       while ((c = tlist_top(&done, list)) == NULL) {
                if (tlist_empty(&pending) && tlist_empty(&running))
                        return NULL;
                reap_output();