run-external-alloc.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <ccan/talloc/talloc.c>
  2. #include <ccan/tap/tap.h>
  3. #include <assert.h>
  4. /* Much testing already done in run.c */
  5. static int ext_alloc_count, ext_free_count, ext_realloc_count, lock_count, unlock_count;
  6. static void *expected_parent;
  7. static void *ext_realloc(const void *parent, void *ptr, size_t size)
  8. {
  9. ok1(parent == expected_parent);
  10. if (ptr == NULL)
  11. ext_alloc_count++;
  12. if (size == 0)
  13. ext_free_count++;
  14. if (ptr && size)
  15. ext_realloc_count++;
  16. return realloc(ptr, size);
  17. }
  18. static void ext_lock(const void *ctx)
  19. {
  20. lock_count++;
  21. }
  22. static void ext_unlock(void)
  23. {
  24. unlock_count++;
  25. }
  26. int main(void)
  27. {
  28. char *p, *p2, *head;
  29. plan_tests(15);
  30. expected_parent = NULL;
  31. head = talloc_add_external(NULL, ext_realloc, ext_lock, ext_unlock);
  32. assert(head);
  33. ok1(ext_alloc_count == 1);
  34. expected_parent = head;
  35. p = talloc_array(head, char, 1);
  36. ok1(ext_alloc_count == 2);
  37. assert(p);
  38. /* Child is also externally allocated */
  39. expected_parent = p;
  40. p2 = talloc(p, char);
  41. ok1(ext_alloc_count == 3);
  42. expected_parent = head;
  43. p = talloc_realloc(NULL, p, char, 1000);
  44. ok1(ext_realloc_count == 1);
  45. assert(p);
  46. expected_parent = p;
  47. talloc_free(p2);
  48. ok1(ext_free_count == 1);
  49. expected_parent = head;
  50. talloc_free(p);
  51. ok1(ext_free_count == 2);
  52. expected_parent = NULL;
  53. talloc_free(head);
  54. ok1(ext_free_count == 3);
  55. ok1(lock_count == unlock_count);
  56. return exit_status();
  57. }