run-test-backend.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <stdlib.h>
  2. #include <stdbool.h>
  3. /* Make sure it always uses our allocation/resize/free fns! */
  4. static bool my_alloc_called;
  5. static void *my_alloc(size_t len)
  6. {
  7. my_alloc_called = true;
  8. return (char *)malloc(len + 16) + 16;
  9. }
  10. static void my_free(void *p)
  11. {
  12. if (p)
  13. free((char *)p - 16);
  14. }
  15. static void *my_realloc(void *old, size_t new_size)
  16. {
  17. return (char *)realloc((char *)old - 16, new_size + 16) + 16;
  18. }
  19. #define free ((void (*)(void *))abort)
  20. #define malloc ((void *(*)(size_t))abort)
  21. #define realloc ((void *(*)(void *, size_t))abort)
  22. #include <ccan/tal/tal.h>
  23. #include <ccan/tal/tal.c>
  24. #include <ccan/tap/tap.h>
  25. #define NUM_ALLOCS 1000
  26. static void destroy_p(void *p UNNEEDED)
  27. {
  28. }
  29. int main(void)
  30. {
  31. void *p, *c[NUM_ALLOCS];
  32. int i;
  33. char *name;
  34. /* Mostly we rely on the allocator (or valgrind) crashing. */
  35. plan_tests(1);
  36. tal_set_backend(my_alloc, my_realloc, my_free, NULL);
  37. p = tal(NULL, char);
  38. ok1(my_alloc_called);
  39. /* Adding properties makes us allocated. */
  40. tal_add_destructor(p, destroy_p);
  41. tal_set_name(p, "test");
  42. name = tal_arr(NULL, char, 6);
  43. strcpy(name, "test2");
  44. tal_set_name(p, name);
  45. /* makes us free old name */
  46. tal_set_name(p, name);
  47. tal_free(name);
  48. /* Add lots of children. */
  49. for (i = 0; i < NUM_ALLOCS; i++)
  50. c[i] = tal(p, char);
  51. /* Now steal a few. */
  52. for (i = 1; i < NUM_ALLOCS / 2; i++)
  53. tal_steal(c[0], c[i]);
  54. /* Now free individual ones.. */
  55. for (i = NUM_ALLOCS / 2; i < NUM_ALLOCS; i++)
  56. tal_free(c[i]);
  57. /* Finally, free the parent. */
  58. tal_free(p);
  59. tal_cleanup();
  60. return exit_status();
  61. }