6 * tal - compact tree allocator routines (inspired by talloc)
8 * Tal is a hierarchical allocator; any pointer allocated by tal can
9 * become the parent of another allocation. When you free that parent,
10 * the children (and grandchildren, etc) are automatically freed.
12 * This allows you to build complex objects based on their lifetimes, eg:
14 * struct foo *X = tal(NULL, struct foo);
15 * X->name = tal_strdup(X, "foo");
17 * and the pointer X->name would be a "child" of the tal context "X";
18 * tal_free(X->name) would free X->name as expected, by tal_free(X) would
21 * With an overhead of approximately 2.1 pointers per object (vs. talloc's
22 * 12 pointers), it's a little slower in freeing single objects, though
23 * comparable for allocation and freeing whole object trees). It does not
24 * support talloc's references or failing destructors.
30 * #include <ccan/talloc/talloc.h>
32 * // A structure containing a popened command.
35 * const char *command;
38 * // When struct command is freed, we also want to pclose pipe.
39 * static void close_cmd(struct command *cmd)
44 * // This function opens a writable pipe to the given command.
45 * static struct command *open_output_cmd(const tal_t *ctx,
46 * const char *fmt, ...)
49 * struct command *cmd = tal(ctx, struct command);
55 * cmd->command = tal_vasprintf(cmd, fmt, ap);
57 * if (!cmd->command) {
62 * cmd->f = popen(cmd->command, "w");
67 * tal_add_destructor(cmd, close_cmd);
71 * int main(int argc, char *argv[])
73 * struct command *cmd;
76 * errx(1, "Usage: %s <command>\n", argv[0]);
78 * cmd = open_output_cmd(NULL, "%s hello", argv[1]);
80 * err(1, "Running '%s hello'", argv[1]);
81 * fprintf(cmd->f, "This is a test\n");
88 int main(int argc, char *argv[])
93 if (strcmp(argv[1], "depends") == 0) {
94 printf("ccan/compiler\n");
95 printf("ccan/hash\n");
96 printf("ccan/likely\n");
97 printf("ccan/list\n");
98 printf("ccan/typesafe_cb\n");