]> git.ozlabs.org Git - ccan/blob - ccan/talloc/test/run-set_allocator.c
e485c62337c64a6487e5018f03a5bc0f4025659d
[ccan] / ccan / talloc / test / run-set_allocator.c
1 #include <ccan/failtest/failtest_override.h>
2 #include <ccan/talloc/talloc.c>
3 #include <stdbool.h>
4 #include <ccan/tap/tap.h>
5 #include <ccan/failtest/failtest.h>
6
7 static unsigned my_malloc_count, my_free_count, my_realloc_count;
8
9 static void *my_malloc(size_t size)
10 {
11         my_malloc_count++;
12         return malloc(size);
13 }
14
15 static void my_free(void *ptr)
16 {
17         my_free_count++;
18         free(ptr);
19 }
20
21 static void *my_realloc(void *ptr, size_t size)
22 {
23         my_realloc_count++;
24         ok1(ptr);
25         ok1(size);
26         return realloc(ptr, size);
27 }
28
29 int main(int argc, char *argv[])
30 {
31         int *p1, *p2;
32
33         plan_tests(14);
34         failtest_init(argc, argv);
35         talloc_set_allocator(my_malloc, my_free, my_realloc);
36         p1 = talloc_array(NULL, int, 10);
37         ok1(my_malloc_count == 1);
38         ok1(my_free_count == 0);
39         ok1(my_realloc_count == 0);
40
41         p1 = talloc_realloc(NULL, p1, int, 10000);
42         ok1(my_malloc_count == 1);
43         ok1(my_free_count == 0);
44         ok1(my_realloc_count == 1);
45
46         p2 = talloc(p1, int);
47         ok1(my_malloc_count == 2);
48         ok1(my_free_count == 0);
49         ok1(my_realloc_count == 1);
50
51         talloc_free(p1);
52         ok1(my_malloc_count == 2);
53         ok1(my_free_count == 2);
54         ok1(my_realloc_count == 1);
55
56         failtest_exit(exit_status());
57 }