]> git.ozlabs.org Git - ccan/blob - ccan/take/_info
take: new module for parameter ownership.
[ccan] / ccan / take / _info
1 #include <string.h>
2 #include "config.h"
3
4 /**
5  * take - routines to mark pointers to be consumed by called functions.
6  *
7  * This code helps to implement ownership transfer on a per-arg basis:
8  * the caller wraps the pointer argument in take() and the callee checks
9  * taken() to see if it should consume it.
10  *
11  * Author: Rusty Russell <rusty@rustcorp.com.au>
12  * License: CC0 (Public domain)
13  *
14  * Example:
15  *      // Given foo/bar.c outputs basename is bar.c
16  *      #include <ccan/take/take.h>
17  *      #include <string.h>
18  *
19  *      // Dumb basename program and driver.
20  *      static char *base(const char *file)
21  *      {
22  *              const char *p = strrchr(file, '/');
23  *              if (!p) 
24  *                      p = file;
25  *              else
26  *                      p++;
27  *
28  *              // Use arg in place if we're allowed.
29  *              if (taken(file))
30  *                      return memmove((char *)file, p, strlen(p)+1);
31  *              else
32  *                      return strdup(p);
33  *      }
34  *
35  *      int main(int argc, char *argv[])
36  *      {
37  *              char *b;
38  *
39  *              if (argv[1]) // Mangle in place.
40  *                      b = base(take(argv[1]));
41  *              else
42  *                      b = base("test/string");
43  *
44  *              printf("basename is %s\n", b);
45  *              return 0;
46  *      }
47  */
48 int main(int argc, char *argv[])
49 {
50         if (argc != 2)
51                 return 1;
52
53         if (strcmp(argv[1], "depends") == 0) {
54                 printf("ccan/likely\n");
55                 return 0;
56         }
57
58         return 1;
59 }