]> git.ozlabs.org Git - ccan/blob - ccan/talloc/_info
various: make the _info License: wording uniform for GPL variants.
[ccan] / ccan / talloc / _info
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * talloc - tree allocator routines
7  *
8  * Talloc is a hierarchical memory pool system with destructors: you keep your
9  * objects in heirarchies reflecting their lifetime.  Every pointer returned
10  * from talloc() is itself a valid talloc context, from which other talloc()s
11  * can be attached.  This means you can do this:
12  *
13  *  struct foo *X = talloc(mem_ctx, struct foo);
14  *  X->name = talloc_strdup(X, "foo");
15  *
16  * and the pointer X->name would be a "child" of the talloc context "X" which
17  * is itself a child of mem_ctx.  So if you do talloc_free(mem_ctx) then it is
18  * all destroyed, whereas if you do talloc_free(X) then just X and X->name are
19  * destroyed, and if you do talloc_free(X->name) then just the name element of
20  * X is destroyed.
21  *
22  * If you think about this, then what this effectively gives you is an n-ary
23  * tree, where you can free any part of the tree with talloc_free().
24  *
25  * Talloc has been measured with a time overhead of around 4% over glibc
26  * malloc, and 48/80 bytes per allocation (32/64 bit).
27  *
28  * This version is based on svn://svnanon.samba.org/samba/branches/SAMBA_4_0/source/lib/talloc revision 23158.
29  *
30  * Example:
31  *      #include <stdio.h>
32  *      #include <stdarg.h>
33  *      #include <err.h>
34  *      #include <ccan/talloc/talloc.h>
35  *
36  *      // A structure containing a popened command.
37  *      struct command
38  *      {
39  *              FILE *f;
40  *              const char *command;
41  *      };
42  *
43  *      // When struct command is freed, we also want to pclose pipe.
44  *      static int close_cmd(struct command *cmd)
45  *      {
46  *              pclose(cmd->f);
47  *              // 0 means "we succeeded, continue freeing"
48  *              return 0;
49  *      }
50  *
51  *      // This function opens a writable pipe to the given command.
52  *      static struct command *open_output_cmd(const void *ctx, char *fmt, ...)
53  *      {
54  *              va_list ap;
55  *              struct command *cmd = talloc(ctx, struct command);
56  *
57  *              if (!cmd)
58  *                      return NULL;
59  *
60  *              va_start(ap, fmt);
61  *              cmd->command = talloc_vasprintf(cmd, fmt, ap);
62  *              va_end(ap);
63  *              if (!cmd->command) {
64  *                      talloc_free(cmd);
65  *                      return NULL;
66  *              }
67  *
68  *              cmd->f = popen(cmd->command, "w");
69  *              if (!cmd->f) {
70  *                      talloc_free(cmd);
71  *                      return NULL;
72  *              }
73  *              talloc_set_destructor(cmd, close_cmd);
74  *              return cmd;
75  *      }
76  *
77  *      int main(int argc, char *argv[])
78  *      {
79  *              struct command *cmd;
80  *
81  *              if (argc != 2)
82  *                      errx(1, "Usage: %s <command>\n", argv[0]);
83  *
84  *              cmd = open_output_cmd(NULL, "%s hello", argv[1]);
85  *              if (!cmd)
86  *                      err(1, "Running '%s hello'", argv[1]);
87  *              fprintf(cmd->f, "This is a test\n");
88  *              talloc_free(cmd);
89  *              return 0;
90  *      }
91  *
92  * License: LGPL (v2.1 or any later version)
93  */
94 int main(int argc, char *argv[])
95 {
96         if (argc != 2)
97                 return 1;
98
99         if (strcmp(argv[1], "depends") == 0) {
100                 printf("ccan/compiler\n");
101                 printf("ccan/typesafe_cb\n");
102                 printf("ccan/failtest\n");
103                 return 0;
104         }
105
106         return 1;
107 }