_info.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "config.h"
  4. /**
  5. * talloc - tree allocator routines
  6. *
  7. * Talloc is a hierarchical memory pool system with destructors: you keep your
  8. * objects in heirarchies reflecting their lifetime. Every pointer returned
  9. * from talloc() is itself a valid talloc context, from which other talloc()s
  10. * can be attached. This means you can do this:
  11. *
  12. * struct foo *X = talloc(mem_ctx, struct foo);
  13. * X->name = talloc_strdup(X, "foo");
  14. *
  15. * and the pointer X->name would be a "child" of the talloc context "X" which
  16. * is itself a child of mem_ctx. So if you do talloc_free(mem_ctx) then it is
  17. * all destroyed, whereas if you do talloc_free(X) then just X and X->name are
  18. * destroyed, and if you do talloc_free(X->name) then just the name element of
  19. * X is destroyed.
  20. *
  21. * If you think about this, then what this effectively gives you is an n-ary
  22. * tree, where you can free any part of the tree with talloc_free().
  23. *
  24. * Talloc has been measured with a time overhead of around 4% over glibc
  25. * malloc, and 48/80 bytes per allocation (32/64 bit).
  26. *
  27. * This version is based on svn://svnanon.samba.org/samba/branches/SAMBA_4_0/source/lib/talloc revision 23158.
  28. *
  29. * Example:
  30. * #include <stdio.h>
  31. * #include <stdarg.h>
  32. * #include <err.h>
  33. * #include "talloc/talloc.h"
  34. *
  35. * // A structure containing a popened comman.
  36. * struct command
  37. * {
  38. * FILE *f;
  39. * const char *command;
  40. * };
  41. *
  42. * // When struct command is freed, we also want to pclose pipe.
  43. * static int close_cmd(struct command *cmd)
  44. * {
  45. * pclose(cmd->f);
  46. * // 0 means "we succeeded, continue freeing"
  47. * return 0;
  48. * }
  49. *
  50. * // This function opens a writable pipe to the given command.
  51. * struct command *open_output_cmd(const void *ctx, char *fmt, ...)
  52. * {
  53. * va_list ap;
  54. * struct command *cmd = talloc(ctx, struct command);
  55. *
  56. * if (!cmd)
  57. * return NULL;
  58. *
  59. * va_start(ap, fmt);
  60. * cmd->command = talloc_vasprintf(cmd, fmt, ap);
  61. * va_end(ap);
  62. * if (!cmd->command) {
  63. * talloc_free(cmd);
  64. * return NULL;
  65. * }
  66. *
  67. * cmd->f = popen(cmd->command, "w");
  68. * if (!cmd->f) {
  69. * talloc_free(cmd);
  70. * return NULL;
  71. * }
  72. * talloc_set_destructor(cmd, close_cmd);
  73. * return cmd;
  74. * }
  75. *
  76. * int main(int argc, char *argv[])
  77. * {
  78. * struct command *cmd;
  79. *
  80. * if (argc != 2)
  81. * errx(1, "Usage: %s <command>\n");
  82. *
  83. * cmd = open_output_cmd(NULL, "%s hello", argv[1]);
  84. * if (!cmd)
  85. * err(1, "Running '%s hello'", argv[1]);
  86. * fprintf(cmd->f, "This is a test\n");
  87. * talloc_free(cmd);
  88. * return 0;
  89. * }
  90. */
  91. int main(int argc, char *argv[])
  92. {
  93. if (argc != 2)
  94. return 1;
  95. if (strcmp(argv[1], "depends") == 0) {
  96. printf("typesafe_cb\n");
  97. return 0;
  98. }
  99. return 1;
  100. }