_info 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "config.h"
  2. #include <stdio.h>
  3. #include <string.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->val = tal(X, int);
  15. *
  16. * and the pointer X->val would be a "child" of the tal context "X";
  17. * tal_free(X->val) would free X->val as expected, by tal_free(X) would
  18. * free X and X->val.
  19. *
  20. * With an overhead of approximately 4 pointers per object
  21. * (vs. talloc's 12 pointers), it uses dynamic allocation for
  22. * destructors and child lists, so those operations can fail. It does
  23. * not support talloc's references or failing destructors.
  24. *
  25. * See Also:
  26. * ccan/tal/str (useful string helpers)
  27. *
  28. * Example:
  29. * #include <stdio.h>
  30. * #include <err.h>
  31. * #include <ccan/talloc/talloc.h>
  32. *
  33. * // A structure containing a popened command.
  34. * struct command {
  35. * FILE *f;
  36. * char *command;
  37. * };
  38. *
  39. * // When struct command is freed, we also want to pclose pipe.
  40. * static void close_cmd(struct command *cmd)
  41. * {
  42. * pclose(cmd->f);
  43. * }
  44. *
  45. * // This function opens a writable pipe to the given command.
  46. * static struct command *open_output_cmd(const tal_t *ctx,
  47. * const char *a0, const char *a1)
  48. * {
  49. * struct command *cmd = tal(ctx, struct command);
  50. *
  51. * if (!cmd)
  52. * return NULL;
  53. *
  54. * // Note that tal/str has helpers to make this much easier!
  55. * cmd->command = tal_arrz(cmd, char, strlen(a0) + strlen(a1) + 2);
  56. * if (!cmd->command) {
  57. * tal_free(cmd);
  58. * return NULL;
  59. * }
  60. * strcat(cmd->command, a0);
  61. * strcat(cmd->command, " ");
  62. * strcat(cmd->command, a1);
  63. *
  64. * cmd->f = popen(cmd->command, "w");
  65. * if (!cmd->f) {
  66. * tal_free(cmd);
  67. * return NULL;
  68. * }
  69. * tal_add_destructor(cmd, close_cmd);
  70. * return cmd;
  71. * }
  72. *
  73. * int main(int argc, char *argv[])
  74. * {
  75. * struct command *cmd;
  76. *
  77. * if (argc != 2)
  78. * errx(1, "Usage: %s <command>\n", argv[0]);
  79. *
  80. * cmd = open_output_cmd(NULL, argv[1], "hello");
  81. * if (!cmd)
  82. * err(1, "Running '%s hello'", argv[1]);
  83. * fprintf(cmd->f, "This is a test\n");
  84. * tal_free(cmd);
  85. * return 0;
  86. * }
  87. *
  88. * License: BSD-MIT
  89. */
  90. int main(int argc, char *argv[])
  91. {
  92. if (argc != 2)
  93. return 1;
  94. if (strcmp(argv[1], "depends") == 0) {
  95. printf("ccan/alignof\n");
  96. printf("ccan/compiler\n");
  97. printf("ccan/likely\n");
  98. printf("ccan/list\n");
  99. printf("ccan/str\n");
  100. printf("ccan/take\n");
  101. printf("ccan/typesafe_cb\n");
  102. return 0;
  103. }
  104. return 1;
  105. }