_info 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "config.h"
  4. /**
  5. * tal - compact tree allocator routines (inspired by talloc)
  6. *
  7. * Tal is a hierarchical allocator; any pointer allocated by tal can
  8. * become the parent of another allocation. When you free that parent,
  9. * the children (and grandchildren, etc) are automatically freed.
  10. *
  11. * This allows you to build complex objects based on their lifetimes, eg:
  12. *
  13. * struct foo *X = tal(NULL, struct foo);
  14. * X->name = tal_strdup(X, "foo");
  15. *
  16. * and the pointer X->name would be a "child" of the tal context "X";
  17. * tal_free(X->name) would free X->name as expected, by tal_free(X) would
  18. * free X and X->name.
  19. *
  20. * With an overhead of approximately 2.1 pointers per object (vs. talloc's
  21. * 12 pointers), it's a little slower in freeing single objects, though
  22. * comparable for allocation and freeing whole object trees). It does not
  23. * support talloc's references or failing destructors.
  24. *
  25. * Example:
  26. * #include <stdio.h>
  27. * #include <stdarg.h>
  28. * #include <err.h>
  29. * #include <ccan/talloc/talloc.h>
  30. *
  31. * // A structure containing a popened command.
  32. * struct command {
  33. * FILE *f;
  34. * const char *command;
  35. * };
  36. *
  37. * // When struct command is freed, we also want to pclose pipe.
  38. * static void close_cmd(struct command *cmd)
  39. * {
  40. * pclose(cmd->f);
  41. * }
  42. *
  43. * // This function opens a writable pipe to the given command.
  44. * static struct command *open_output_cmd(const tal_t *ctx,
  45. * const char *fmt, ...)
  46. * {
  47. * va_list ap;
  48. * struct command *cmd = tal(ctx, struct command);
  49. *
  50. * if (!cmd)
  51. * return NULL;
  52. *
  53. * va_start(ap, fmt);
  54. * cmd->command = tal_vasprintf(cmd, fmt, ap);
  55. * va_end(ap);
  56. * if (!cmd->command) {
  57. * tal_free(cmd);
  58. * return NULL;
  59. * }
  60. *
  61. * cmd->f = popen(cmd->command, "w");
  62. * if (!cmd->f) {
  63. * tal_free(cmd);
  64. * return NULL;
  65. * }
  66. * tal_add_destructor(cmd, close_cmd);
  67. * return cmd;
  68. * }
  69. *
  70. * int main(int argc, char *argv[])
  71. * {
  72. * struct command *cmd;
  73. *
  74. * if (argc != 2)
  75. * errx(1, "Usage: %s <command>\n", argv[0]);
  76. *
  77. * cmd = open_output_cmd(NULL, "%s hello", argv[1]);
  78. * if (!cmd)
  79. * err(1, "Running '%s hello'", argv[1]);
  80. * fprintf(cmd->f, "This is a test\n");
  81. * tal_free(cmd);
  82. * return 0;
  83. * }
  84. *
  85. * License: BSD-MIT
  86. */
  87. int main(int argc, char *argv[])
  88. {
  89. if (argc != 2)
  90. return 1;
  91. if (strcmp(argv[1], "depends") == 0) {
  92. printf("ccan/compiler\n");
  93. printf("ccan/hash\n");
  94. printf("ccan/likely\n");
  95. printf("ccan/list\n");
  96. printf("ccan/str\n");
  97. printf("ccan/take\n");
  98. printf("ccan/typesafe_cb\n");
  99. return 0;
  100. }
  101. return 1;
  102. }