]> git.ozlabs.org Git - ccan/commitdiff
rfc822: switch to ccan/tal. origin/rfc822
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 3 Jan 2013 01:06:55 +0000 (11:36 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 3 Jan 2013 01:06:55 +0000 (11:36 +1030)
We use TAL_USE_TALLOC to use libtalloc as the backend: you can test that with
ccanlint --compiler="cc -DTAL_USE_TALLOC".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/rfc822/_info
ccan/rfc822/rfc822.c
ccan/rfc822/rfc822.h
ccan/rfc822/test/helper.c
ccan/rfc822/test/run-default-alloc-failure.c
ccan/rfc822/test/run-hdr-and-body.c
ccan/rfc822/test/run-hdr-of-name.c
ccan/rfc822/test/run-testdata.c
ccan/rfc822/test/run-unfold.c
ccan/rfc822/test/testdata.h

index 7b8eda62ac3e976679ce6c4aa5dc8d8689723d65..dac530cd84b4e369c55a54b8ed7437ced4f42931 100644 (file)
  *   RFC822 compliant, don't SEGV and try to return as much useful
  *   data as possible.
  *
  *   RFC822 compliant, don't SEGV and try to return as much useful
  *   data as possible.
  *
+ * Define TAL_USE_TALLOC to use libtalloc as the allocator, otherwise
+ * it will use ccan/tal (usually done on the cmdline, as tal/str will need
+ * it too).
+ *
  * Example:
  *     // Given '' outputs 'body'
  *     // Given 'From' outputs ' <from@example.com>'
  * Example:
  *     // Given '' outputs 'body'
  *     // Given 'From' outputs ' <from@example.com>'
@@ -53,7 +57,11 @@ int main(int argc, char *argv[])
                return 1;
 
        if (strcmp(argv[1], "depends") == 0) {
                return 1;
 
        if (strcmp(argv[1], "depends") == 0) {
-               printf("ccan/talloc\n");
+#ifdef TAL_USE_TALLOC
+               printf("ccan/tal/talloc\n");
+#else
+               printf("ccan/tal\n");
+#endif
                printf("ccan/list\n");
                printf("ccan/str\n");
                printf("ccan/bytestring\n");
                printf("ccan/list\n");
                printf("ccan/str\n");
                printf("ccan/bytestring\n");
@@ -64,6 +72,7 @@ int main(int argc, char *argv[])
                printf("ccan/failtest\n");
                printf("ccan/foreach\n");
                printf("ccan/array_size\n");
                printf("ccan/failtest\n");
                printf("ccan/foreach\n");
                printf("ccan/array_size\n");
+               printf("ccan/tal/str\n");
                return 0;
        }
 
                return 0;
        }
 
index 7f9442a8d04d235908abfd8c15e32520d97acd0f..08c0541328396c10f0afa59486429602610b4c16 100644 (file)
@@ -5,11 +5,17 @@
 #include <string.h>
 
 #include <ccan/str/str.h>
 #include <string.h>
 
 #include <ccan/str/str.h>
-#include <ccan/talloc/talloc.h>
 #include <ccan/list/list.h>
 #include <ccan/list/list.h>
+#include <stdio.h>
 
 #include <ccan/rfc822/rfc822.h>
 
 
 #include <ccan/rfc822/rfc822.h>
 
+#ifdef TAL_USE_TALLOC
+#include <ccan/tal/talloc/talloc.h>
+#else
+#include <ccan/tal/tal.h>
+#endif
+
 #if !HAVE_MEMMEM
 void *memmem(const void *haystack, size_t haystacklen,
             const void *needle, size_t needlelen)
 #if !HAVE_MEMMEM
 void *memmem(const void *haystack, size_t haystacklen,
             const void *needle, size_t needlelen)
@@ -92,6 +98,8 @@ struct rfc822_msg *rfc822_check(const struct rfc822_msg *msg,
        assert(msg);
        if (!list_check(&msg->headers, abortstr))
                 return NULL;
        assert(msg);
        if (!list_check(&msg->headers, abortstr))
                 return NULL;
+       if (!tal_check(msg, abortstr))
+               return NULL;
         return (struct rfc822_msg *)msg;
 }
 
         return (struct rfc822_msg *)msg;
 }
 
@@ -106,7 +114,7 @@ struct rfc822_msg *rfc822_start(const void *ctx, const char *p, size_t len)
        struct rfc822_msg *msg;
        int i;
 
        struct rfc822_msg *msg;
        int i;
 
-       msg = talloc(ctx, struct rfc822_msg);
+       msg = tal(ctx, struct rfc822_msg);
        ALLOC_CHECK(msg, NULL);
 
        msg->data = p;
        ALLOC_CHECK(msg, NULL);
 
        msg->data = p;
@@ -128,7 +136,7 @@ struct rfc822_msg *rfc822_start(const void *ctx, const char *p, size_t len)
 void rfc822_free(struct rfc822_msg *msg)
 {
        CHECK(msg, ">rfc822_free");
 void rfc822_free(struct rfc822_msg *msg)
 {
        CHECK(msg, ">rfc822_free");
-       talloc_free(msg);
+       tal_free(msg);
 }
 
 static struct rfc822_header *next_header_cached(struct rfc822_msg *msg,
 }
 
 static struct rfc822_header *next_header_cached(struct rfc822_msg *msg,
@@ -200,7 +208,7 @@ static struct rfc822_header *next_header_parse(struct rfc822_msg *msg)
                msg->remainder = eh;
 
 
                msg->remainder = eh;
 
 
-       hi = talloc_zero(msg, struct rfc822_header);
+       hi = talz(msg, struct rfc822_header);
        ALLOC_CHECK(hi, NULL);
 
        hi->all = bytestring(h, eh - h);
        ALLOC_CHECK(hi, NULL);
 
        hi->all = bytestring(h, eh - h);
@@ -354,7 +362,7 @@ struct bytestring rfc822_header_unfolded_value(struct rfc822_msg *msg,
                if (lines <= 1) {
                        hdr->unfolded = bytestring(raw.ptr, len);
                } else {
                if (lines <= 1) {
                        hdr->unfolded = bytestring(raw.ptr, len);
                } else {
-                       char *unfold = talloc_array(msg, char, len);
+                       char *unfold = tal_arr(msg, char, len);
                        char *p = unfold;
 
                        ALLOC_CHECK(unfold, bytestring_NULL);
                        char *p = unfold;
 
                        ALLOC_CHECK(unfold, bytestring_NULL);
@@ -447,7 +455,7 @@ static struct rfc822_header *index_header(struct rfc822_msg *msg,
        if (!hn) {
                unsigned hash = headerhash(hname);
 
        if (!hn) {
                unsigned hash = headerhash(hname);
 
-               hn = talloc_zero(msg, struct rfc822_headers_of_name);
+               hn = talz(msg, struct rfc822_headers_of_name);
                ALLOC_CHECK(hn, NULL);
 
                hn->name = hname;
                ALLOC_CHECK(hn, NULL);
 
                hn->name = hname;
index 24fb3cc37e74e2b58a8a0a1f4d00225708fe9d95..e770115c5c6a03fb0e700542752e057ead80d76f 100644 (file)
@@ -22,6 +22,9 @@ struct rfc822_msg;
  * callback is called with a string describing where the failure
  * occurred, which can be used to log a more useful error message.
  *
  * callback is called with a string describing where the failure
  * occurred, which can be used to log a more useful error message.
  *
+ * Note that tal also has a default function which calls abort() on allocation
+ * failure: see tal_set_backend().
+ *
  * Example:
  *     static void my_handler(const char *str)
  *     {
  * Example:
  *     static void my_handler(const char *str)
  *     {
@@ -53,15 +56,15 @@ static inline bool rfc822_iswsp(char c)
  * inconsistent, and the function will abort.  If the state of the
  * structure is valid it returns it unchanged.
  *
  * inconsistent, and the function will abort.  If the state of the
  * structure is valid it returns it unchanged.
  *
- * Returns the list head if the list is consistent, NULL if not (it
- * can never return NULL if @abortstr is set).
+ * Returns the @msg if the message is consistent, NULL if not (it can
+ * never return NULL if @abortstr is set).
  */
 struct rfc822_msg *rfc822_check(const struct rfc822_msg *msg,
                                const char *abortstr);
 
 /**
  * rfc822_start - start parsing a new rfc822 message
  */
 struct rfc822_msg *rfc822_check(const struct rfc822_msg *msg,
                                const char *abortstr);
 
 /**
  * rfc822_start - start parsing a new rfc822 message
- * @ctx: talloc context to make allocations in
+ * @ctx: tal context to make allocations in (or talloc #ifdef TAL_USE_TALLOC)
  * @p: pointer to a buffer containing the message text
  * @len: length of the message text
  *
  * @p: pointer to a buffer containing the message text
  * @len: length of the message text
  *
index 42894ed0288cbee530e38725133b4aa83be760fb..cc62a6621e4caf4bd94bd09f4374f6935bdae584 100644 (file)
@@ -1,7 +1,6 @@
 #include <stdlib.h>
 #include <stdio.h>
 
 #include <stdlib.h>
 #include <stdio.h>
 
-#include <ccan/talloc/talloc.h>
 #include <ccan/failtest/failtest_override.h>
 #include <ccan/failtest/failtest.h>
 
 #include <ccan/failtest/failtest_override.h>
 #include <ccan/failtest/failtest.h>
 
@@ -11,6 +10,7 @@
 
 /* failtest limitations mean we need these wrappers to test talloc
  * failure paths. */
 
 /* failtest limitations mean we need these wrappers to test talloc
  * failure paths. */
+#ifndef TAL_USE_TALLOC
 static void *malloc_wrapper(size_t size)
 {
        return malloc(size);
 static void *malloc_wrapper(size_t size)
 {
        return malloc(size);
@@ -25,6 +25,7 @@ static void *realloc_wrapper(void *ptr, size_t size)
 {
        return realloc(ptr, size);
 }
 {
        return realloc(ptr, size);
 }
+#endif
 
 #if 0
 static void allocation_failure_exit(const char *s)
 
 #if 0
 static void allocation_failure_exit(const char *s)
@@ -50,11 +51,29 @@ void allocation_failure_check(void)
        }
 }
 
        }
 }
 
+#ifdef TAL_USE_TALLOC
+#include <ccan/tal/talloc/talloc.h>
+#else
+#include <ccan/tal/tal.h>
+#endif
+
+/* Don't abort on allocation failures! */
+static void noabort_wrapper(const char *why)
+{
+       return;
+}
+
 void failtest_setup(int argc, char *argv[])
 {
        failtest_init(argc, argv);
        rfc822_set_allocation_failure_handler(allocation_failure_continue);
 void failtest_setup(int argc, char *argv[])
 {
        failtest_init(argc, argv);
        rfc822_set_allocation_failure_handler(allocation_failure_continue);
-       talloc_set_allocator(malloc_wrapper, free_wrapper, realloc_wrapper);
+#ifdef TAL_USE_TALLOC
+       /* FIXME: we can't inject allocation failures in talloc! */
+       tal_set_backend(NULL, NULL, NULL, noabort_wrapper);
+#else
+       tal_set_backend(malloc_wrapper, realloc_wrapper, free_wrapper,
+                       noabort_wrapper);
+#endif
 }
 
 void check_header(struct rfc822_msg *msg,
 }
 
 void check_header(struct rfc822_msg *msg,
index c208154281106b18a2371dfcabd62bd49a21d384..b81fd4c0c9644c4f89132d3713dbe54199740aa7 100644 (file)
 
 #include <ccan/rfc822/rfc822.h>
 
 
 #include <ccan/rfc822/rfc822.h>
 
-#include <ccan/talloc/talloc.h>
+#ifdef TAL_USE_TALLOC
+#include <ccan/tal/talloc/talloc.h>
+#else
+#include <ccan/tal/tal.h>
+#endif
 
 static bool should_fail = false;
 
 
 static bool should_fail = false;
 
@@ -18,16 +22,16 @@ static void *mayfail_alloc(const void *ctx, size_t size)
 {
        if (should_fail)
                return NULL;
 {
        if (should_fail)
                return NULL;
-       return talloc_zero_size(ctx, size);
+       return tal_arrz(ctx, char, size);
 }
 
 /* Override various tallocation functions. */
 }
 
 /* Override various tallocation functions. */
-#undef talloc
-#undef talloc_zero
-#undef talloc_array
-#define talloc(ctx, type) mayfail_alloc((ctx), sizeof(type))
-#define talloc_zero(ctx, type) mayfail_alloc((ctx), sizeof(type))
-#define talloc_array(ctx, type, num) mayfail_alloc((ctx), sizeof(type)*(num))
+#undef tal
+#undef talz
+#undef tal_arr
+#define tal(ctx, type) mayfail_alloc((ctx), sizeof(type))
+#define talz(ctx, type) mayfail_alloc((ctx), sizeof(type))
+#define tal_arr(ctx, type, num) mayfail_alloc((ctx), sizeof(type)*(num))
 
 #include <ccan/rfc822/rfc822.c>
 
 
 #include <ccan/rfc822/rfc822.c>
 
index 25e66cc2fd1330b2f31ccc3e51cdfc4778cd0f63..b7f4e7f179f3d7b13278650e92de457e296cda28 100644 (file)
@@ -137,7 +137,7 @@ int main(int argc, char *argv[])
                        test_hdrbody(e, buf, len, exname, crlf);
                        test_hdrhdr(e, buf, len, exname, crlf);
 
                        test_hdrbody(e, buf, len, exname, crlf);
                        test_hdrhdr(e, buf, len, exname, crlf);
 
-                       talloc_free(buf);
+                       tal_free(buf);
                }
        }
 
                }
        }
 
index aa98ac5b43aba2d2c684a61366a617fd8a413946..9c2e2a373c85fe6a91648397704903b3b71207a0 100644 (file)
@@ -75,7 +75,7 @@ int main(int argc, char *argv[])
 
                        test_hdrbyname(e, buf, len, exname, crlf);
 
 
                        test_hdrbyname(e, buf, len, exname, crlf);
 
-                       talloc_free(buf);
+                       tal_free(buf);
                }
        }
 
                }
        }
 
index dbe2848dbf487f66e7583677b9f7e05089386131..d15a5816471ea8dec050040abc76c224de635bd7 100644 (file)
@@ -39,7 +39,7 @@ static void test_assemble(const struct aexample *e, int crlf,
                len, cmplen);
        ok1(len == cmplen);
        ok1(memcmp(msg, cmp, cmplen) == 0);
                len, cmplen);
        ok1(len == cmplen);
        ok1(memcmp(msg, cmp, cmplen) == 0);
-       talloc_free(msg);
+       tal_free(msg);
 }
 
 int main(int argc, char *argv[])
 }
 
 int main(int argc, char *argv[])
index 0f44cd5e30ab4526d08a2e17c6f4c7b32c44e234..3d848e16e6e58222a34eac0b32efa106a3ead4ac 100644 (file)
@@ -31,7 +31,7 @@ static struct bytestring fold_and_assemble(int foldat, int crlf, int truncated)
        char *buf, *p;
        int i, n = 0;
 
        char *buf, *p;
        int i, n = 0;
 
-       buf = talloc_array(NULL, char, strlen(BEFORE) + strlen(AFTER) + 3*strlen(UNFOLDED) + 2);
+       buf = tal_arr(NULL, char, strlen(BEFORE) + strlen(AFTER) + 3*strlen(UNFOLDED) + 2);
        if (!buf)
                exit(0);
 
        if (!buf)
                exit(0);
 
@@ -111,7 +111,7 @@ int main(int argc, char *argv[])
                        for (i = -1; i <= FOLD_POINTS; i++) {
                                msgbuf = fold_and_assemble(i, crlf, truncated);
                                check_folded_header(msgbuf.ptr, msgbuf.len);
                        for (i = -1; i <= FOLD_POINTS; i++) {
                                msgbuf = fold_and_assemble(i, crlf, truncated);
                                check_folded_header(msgbuf.ptr, msgbuf.len);
-                               talloc_free(msgbuf.ptr);
+                               tal_free(msgbuf.ptr);
                        }
                }
        }
                        }
                }
        }
index f6dff03d61eb021bce50a470ffcce96dbd35c894..ada262b5f544300653a301f566785e1fcaa6011b 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef RFC822_TESTDATA_H
 #define RFC822_TESTDATA_H
 
 #ifndef RFC822_TESTDATA_H
 #define RFC822_TESTDATA_H
 
-#include <ccan/talloc/talloc.h>
+#include <ccan/tal/str/str.h>
 #include <ccan/array_size/array_size.h>
 #include <ccan/foreach/foreach.h>
 
 #include <ccan/array_size/array_size.h>
 #include <ccan/foreach/foreach.h>
 
@@ -127,30 +127,26 @@ static inline const char *assemble_msg(const struct aexample *e,
 {
        const char *nl = crlf ? "\r\n" : "\n";
        int nln = crlf ? 2 : 1;
 {
        const char *nl = crlf ? "\r\n" : "\n";
        int nln = crlf ? 2 : 1;
-       char *msg, *amsg;
+       char *msg;
        size_t n = 0;
        int i;
 
        size_t n = 0;
        int i;
 
-       msg = talloc_strdup(NULL, "");
+       msg = tal_strdup(NULL, "");
        if (!msg)
                return NULL;
 
        for (i = 0; i < e->nhdrs; i++) {
        if (!msg)
                return NULL;
 
        for (i = 0; i < e->nhdrs; i++) {
-               amsg = talloc_asprintf_append(msg, "%s:%s%s", e->hdrs[i].name,
-                                             e->hdrs[i].val, nl);
-               if (!amsg) {
-                       talloc_free(msg);
+               if (!tal_append_fmt(&msg, "%s:%s%s", e->hdrs[i].name,
+                                   e->hdrs[i].val, nl)) {
+                       tal_free(msg);
                        return NULL;
                }
                        return NULL;
                }
-               msg = amsg;
                n += strlen(e->hdrs[i].name) + strlen(e->hdrs[i].val) + 1 + nln;
        }
                n += strlen(e->hdrs[i].name) + strlen(e->hdrs[i].val) + 1 + nln;
        }
-       amsg = talloc_asprintf_append(msg, "%s%s", nl, e->body);
-       if (!amsg) {
-               talloc_free(msg);
+       if (!tal_append_fmt(&msg, "%s%s", nl, e->body)) {
+               tal_free(msg);
                return NULL;
        }
                return NULL;
        }
-       msg = amsg;
        n += strlen(e->body) + nln;
        *len = n;
        return msg;
        n += strlen(e->body) + nln;
        *len = n;
        return msg;