X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftalloc%2Ftest%2Frun-set_allocator.c;fp=ccan%2Ftalloc%2Ftest%2Frun-set_allocator.c;h=e485c62337c64a6487e5018f03a5bc0f4025659d;hp=0000000000000000000000000000000000000000;hb=c520b4adbbfba5a663b94e71216b90eddd0bf877;hpb=1e962ba9b5808dafc0be9cc562b25e3ae068ca5d diff --git a/ccan/talloc/test/run-set_allocator.c b/ccan/talloc/test/run-set_allocator.c new file mode 100644 index 00000000..e485c623 --- /dev/null +++ b/ccan/talloc/test/run-set_allocator.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include + +static unsigned my_malloc_count, my_free_count, my_realloc_count; + +static void *my_malloc(size_t size) +{ + my_malloc_count++; + return malloc(size); +} + +static void my_free(void *ptr) +{ + my_free_count++; + free(ptr); +} + +static void *my_realloc(void *ptr, size_t size) +{ + my_realloc_count++; + ok1(ptr); + ok1(size); + return realloc(ptr, size); +} + +int main(int argc, char *argv[]) +{ + int *p1, *p2; + + plan_tests(14); + failtest_init(argc, argv); + talloc_set_allocator(my_malloc, my_free, my_realloc); + p1 = talloc_array(NULL, int, 10); + ok1(my_malloc_count == 1); + ok1(my_free_count == 0); + ok1(my_realloc_count == 0); + + p1 = talloc_realloc(NULL, p1, int, 10000); + ok1(my_malloc_count == 1); + ok1(my_free_count == 0); + ok1(my_realloc_count == 1); + + p2 = talloc(p1, int); + ok1(my_malloc_count == 2); + ok1(my_free_count == 0); + ok1(my_realloc_count == 1); + + talloc_free(p1); + ok1(my_malloc_count == 2); + ok1(my_free_count == 2); + ok1(my_realloc_count == 1); + + failtest_exit(exit_status()); +}