| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #include <ccan/talloc_link/talloc_link.h>
- #include <ccan/tap/tap.h>
- #include <ccan/talloc_link/talloc_link.c>
- #include <stdlib.h>
- #include <err.h>
- static unsigned int destroy_count = 0;
- static int destroy_obj(void *obj)
- {
- destroy_count++;
- return 0;
- }
- int main(int argc, char *argv[])
- {
- void *obj, *p1, *p2, *p3;
- plan_tests(16);
- talloc_enable_leak_report();
- /* Single parent case. */
- p1 = talloc(NULL, char);
- obj = talloc_linked(p1, talloc(NULL, char));
- talloc_set_destructor(obj, destroy_obj);
- ok(destroy_count == 0, "destroy_count = %u", destroy_count);
- talloc_free(p1);
- ok(destroy_count == 1, "destroy_count = %u", destroy_count);
- /* Dual parent case. */
- p1 = talloc(NULL, char);
- obj = talloc_linked(p1, talloc(NULL, char));
- talloc_set_destructor(obj, destroy_obj);
- p2 = talloc(NULL, char);
- talloc_link(p2, obj);
- talloc_free(p1);
- ok(destroy_count == 1, "destroy_count = %u", destroy_count);
- talloc_free(p2);
- ok(destroy_count == 2, "destroy_count = %u", destroy_count);
-
- /* Triple parent case. */
- p1 = talloc(NULL, char);
- obj = talloc_linked(p1, talloc(NULL, char));
- talloc_set_destructor(obj, destroy_obj);
- p2 = talloc(NULL, char);
- p3 = talloc(NULL, char);
- talloc_link(p2, obj);
- talloc_link(p3, obj);
- talloc_free(p1);
- ok(destroy_count == 2, "destroy_count = %u", destroy_count);
- talloc_free(p2);
- ok(destroy_count == 2, "destroy_count = %u", destroy_count);
- talloc_free(p3);
- ok(destroy_count == 3, "destroy_count = %u", destroy_count);
- /* Single delink case. */
- p1 = talloc(NULL, char);
- obj = talloc_linked(p1, talloc(NULL, char));
- talloc_set_destructor(obj, destroy_obj);
- ok(destroy_count == 3, "destroy_count = %u", destroy_count);
- talloc_delink(p1, obj);
- ok(destroy_count == 4, "destroy_count = %u", destroy_count);
- talloc_free(p1);
- /* Double delink case. */
- p1 = talloc(NULL, char);
- obj = talloc_linked(p1, talloc(NULL, char));
- talloc_set_destructor(obj, destroy_obj);
- p2 = talloc(NULL, char);
- talloc_link(p2, obj);
- talloc_delink(p1, obj);
- ok(destroy_count == 4, "destroy_count = %u", destroy_count);
- talloc_delink(p2, obj);
- ok(destroy_count == 5, "destroy_count = %u", destroy_count);
- talloc_free(p1);
- talloc_free(p2);
- /* Delink and free. */
- p1 = talloc(NULL, char);
- obj = talloc_linked(p1, talloc(NULL, char));
- talloc_set_destructor(obj, destroy_obj);
- p2 = talloc(NULL, char);
- talloc_link(p2, obj);
- talloc_delink(p1, obj);
- ok(destroy_count == 5, "destroy_count = %u", destroy_count);
- talloc_free(p2);
- ok(destroy_count == 6, "destroy_count = %u", destroy_count);
- talloc_free(p1);
- /* Free and delink. */
- p1 = talloc(NULL, char);
- obj = talloc_linked(p1, talloc(NULL, char));
- talloc_set_destructor(obj, destroy_obj);
- p2 = talloc(NULL, char);
- talloc_link(p2, obj);
- talloc_free(p1);
- ok(destroy_count == 6, "destroy_count = %u", destroy_count);
- talloc_delink(p2, obj);
- ok(destroy_count == 7, "destroy_count = %u", destroy_count);
- talloc_free(p2);
- /* No leaks? */
- ok1(talloc_total_size(NULL) == 0);
- return exit_status();
- }
|