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->val = tal(X, int);
17 * and the pointer X->val would be a "child" of the tal context "X";
18 * tal_free(X->val) would free X->val as expected, by tal_free(X) would
21 * With an overhead of approximately 4 pointers per object
22 * (vs. talloc's 12 pointers), it uses dynamic allocation for
23 * destructors and child lists, so those operations can fail. It does
24 * not support talloc's references or failing destructors.
27 * ccan/tal/str (useful string helpers)
32 * #include <ccan/talloc/talloc.h>
34 * // A structure containing a popened command.
40 * // When struct command is freed, we also want to pclose pipe.
41 * static void close_cmd(struct command *cmd)
46 * // This function opens a writable pipe to the given command.
47 * static struct command *open_output_cmd(const tal_t *ctx,
48 * const char *a0, const char *a1)
50 * struct command *cmd = tal(ctx, struct command);
55 * // Note that tal/str has helpers to make this much easier!
56 * cmd->command = tal_arrz(cmd, char, strlen(a0) + strlen(a1) + 2);
57 * if (!cmd->command) {
61 * strcat(cmd->command, a0);
62 * strcat(cmd->command, " ");
63 * strcat(cmd->command, a1);
65 * cmd->f = popen(cmd->command, "w");
70 * tal_add_destructor(cmd, close_cmd);
74 * int main(int argc, char *argv[])
76 * struct command *cmd;
79 * errx(1, "Usage: %s <command>\n", argv[0]);
81 * cmd = open_output_cmd(NULL, argv[1], "hello");
83 * err(1, "Running '%s hello'", argv[1]);
84 * fprintf(cmd->f, "This is a test\n");
91 int main(int argc, char *argv[])
96 if (strcmp(argv[1], "depends") == 0) {
97 printf("ccan/alignof\n");
98 printf("ccan/compiler\n");
99 printf("ccan/likely\n");
100 printf("ccan/list\n");
101 printf("ccan/str\n");
102 printf("ccan/take\n");
103 printf("ccan/typesafe_cb\n");