_info 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "config.h"
  4. /**
  5. * talloc_link - link helper for talloc
  6. *
  7. * Talloc references can be confusing and buggy. In the cases where an object
  8. * needs multiple parents, all parents need to be aware of the situation; thus
  9. * talloc_link is a helper where all "parents" talloc_link an object they
  10. * agree to share ownership of.
  11. *
  12. * Example:
  13. * // Silly program which keeps a cache of uppercased strings.
  14. * // The cache wants to keep strings around even after they may have
  15. * // been "freed" by the caller.
  16. * #include <stdio.h>
  17. * #include <err.h>
  18. * #include <string.h>
  19. * #include <ctype.h>
  20. * #include <ccan/talloc/talloc.h>
  21. * #include <ccan/talloc_link/talloc_link.h>
  22. *
  23. * struct upcache {
  24. * const char *str;
  25. * const char *upstr;
  26. * };
  27. *
  28. * static struct upcache *cache;
  29. * static unsigned int cache_hits = 0;
  30. * #define CACHE_SIZE 4
  31. * static void init_upcase(void)
  32. * {
  33. * cache = talloc_zero_array(NULL, struct upcache, CACHE_SIZE);
  34. * }
  35. *
  36. * static struct upcache *lookup_upcase(const char *str)
  37. * {
  38. * unsigned int i;
  39. * for (i = 0; i < CACHE_SIZE; i++)
  40. * if (cache[i].str && !strcmp(cache[i].str, str)) {
  41. * cache_hits++;
  42. * return &cache[i];
  43. * }
  44. * return NULL;
  45. * }
  46. *
  47. * static struct upcache *new_upcase(const char *str)
  48. * {
  49. * unsigned int i;
  50. * char *upstr;
  51. *
  52. * upstr = talloc_linked(cache, talloc_strdup(NULL, str));
  53. * if (!upstr)
  54. * return NULL;
  55. *
  56. * i = random() % CACHE_SIZE;
  57. *
  58. * // Throw out old: works fine if cache[i].upstr is NULL.
  59. * talloc_delink(cache, cache[i].upstr);
  60. *
  61. * // Replace with new.
  62. * cache[i].str = str;
  63. * cache[i].upstr = upstr;
  64. * while (*upstr) {
  65. * *upstr = toupper(*upstr);
  66. * upstr++;
  67. * }
  68. * return &cache[i];
  69. * }
  70. *
  71. * // If you want to keep the result, talloc_link it.
  72. * static const char *get_upcase(const char *str)
  73. * {
  74. * struct upcache *uc = lookup_upcase(str);
  75. * if (!uc)
  76. * uc = new_upcase(str);
  77. * if (!uc)
  78. * return NULL;
  79. * return uc->upstr;
  80. * }
  81. *
  82. * static void exit_upcase(void)
  83. * {
  84. * talloc_free(cache);
  85. * printf("Cache hits: %u\n", cache_hits);
  86. * }
  87. *
  88. * int main(int argc, char *argv[])
  89. * {
  90. * unsigned int i;
  91. * const char **values;
  92. *
  93. * // Will dump any memory leaks to stderr on exit.
  94. * talloc_enable_leak_report();
  95. *
  96. * // Initialize cache.
  97. * init_upcase();
  98. *
  99. * // Throw values in.
  100. * values = talloc_array(NULL, const char *, argc);
  101. * for (i = 1; i < argc; i++) {
  102. * values[i-1] = talloc_link(values, get_upcase(argv[i]));
  103. * if (!values[i-1])
  104. * err(1, "Out of memory");
  105. * }
  106. * // This will free all the values, but cache will still work.
  107. * talloc_free(values);
  108. *
  109. * // Repeat!
  110. * values = talloc_array(NULL, const char *, argc);
  111. * for (i = 1; i < argc; i++) {
  112. * values[i-1] = talloc_link(values, get_upcase(argv[i]));
  113. * if (!values[i-1])
  114. * err(1, "Out of memory");
  115. * }
  116. *
  117. * // This will remove cache links, but we still have a link.
  118. * exit_upcase();
  119. *
  120. * // Show values, so we output something.
  121. * for (i = 0; i < argc - 1; i++)
  122. * printf("%s ", values[i]);
  123. * printf("\n");
  124. *
  125. * // This will finally free the upcase strings (last link).
  126. * talloc_free(values);
  127. *
  128. * return 0;
  129. * }
  130. *
  131. * License: GPL (2 or any later version)
  132. * Author: Rusty Russell <rusty@rustcorp.com.au>
  133. */
  134. int main(int argc, char *argv[])
  135. {
  136. if (argc != 2)
  137. return 1;
  138. if (strcmp(argv[1], "depends") == 0) {
  139. printf("ccan/talloc\n");
  140. printf("ccan/list\n");
  141. return 0;
  142. }
  143. return 1;
  144. }