6 * talloc - tree allocator routines
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:
13 * struct foo *X = talloc(mem_ctx, struct foo);
14 * X->name = talloc_strdup(X, "foo");
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
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().
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).
28 * This version is based on svn://svnanon.samba.org/samba/branches/SAMBA_4_0/source/lib/talloc revision 23158.
34 * #include <ccan/talloc/talloc.h>
36 * // A structure containing a popened command.
40 * const char *command;
43 * // When struct command is freed, we also want to pclose pipe.
44 * static int close_cmd(struct command *cmd)
47 * // 0 means "we succeeded, continue freeing"
51 * // This function opens a writable pipe to the given command.
52 * static struct command *open_output_cmd(const void *ctx,
53 * const char *fmt, ...)
56 * struct command *cmd = talloc(ctx, struct command);
62 * cmd->command = talloc_vasprintf(cmd, fmt, ap);
64 * if (!cmd->command) {
69 * cmd->f = popen(cmd->command, "w");
74 * talloc_set_destructor(cmd, close_cmd);
78 * int main(int argc, char *argv[])
80 * struct command *cmd;
83 * errx(1, "Usage: %s <command>\n", argv[0]);
85 * cmd = open_output_cmd(NULL, "%s hello", argv[1]);
87 * err(1, "Running '%s hello'", argv[1]);
88 * fprintf(cmd->f, "This is a test\n");
93 * License: LGPL (v2.1 or any later version)
95 int main(int argc, char *argv[])
100 if (strcmp(argv[1], "depends") == 0) {
101 printf("ccan/compiler\n");
102 printf("ccan/typesafe_cb\n");
106 if (strcmp(argv[1], "testdepends") == 0) {
107 printf("ccan/failtest\n");