]> git.ozlabs.org Git - ccan/blob - ccan/talloc/test/run-external-alloc.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / talloc / test / run-external-alloc.c
1 #include <ccan/talloc/talloc.c>
2 #include <ccan/tap/tap.h>
3 #include <assert.h>
4
5 /* Much testing already done in run.c */
6
7 static int ext_alloc_count, ext_free_count, ext_realloc_count, lock_count, unlock_count;
8 static void *expected_parent;
9
10 static void *ext_realloc(const void *parent, void *ptr, size_t size)
11 {
12         ok1(parent == expected_parent);
13         if (ptr == NULL)
14                 ext_alloc_count++;
15         if (size == 0)
16                 ext_free_count++;
17         if (ptr && size)
18                 ext_realloc_count++;
19         return realloc(ptr, size);
20 }
21
22 static void ext_lock(const void *ctx)
23 {
24         lock_count++;
25 }
26
27 static void ext_unlock(void)
28 {
29         unlock_count++;
30 }
31
32 int main(void)
33 {
34         char *p, *p2, *head;
35         plan_tests(15);
36
37         expected_parent = NULL;
38         head = talloc_add_external(NULL, ext_realloc, ext_lock, ext_unlock);
39         assert(head);
40         ok1(ext_alloc_count == 1);
41
42         expected_parent = head;
43         p = talloc_array(head, char, 1);
44         ok1(ext_alloc_count == 2);
45         assert(p);
46
47         /* Child is also externally allocated */
48         expected_parent = p;
49         p2 = talloc(p, char);
50         ok1(ext_alloc_count == 3);
51
52         expected_parent = head;
53         p = talloc_realloc(NULL, p, char, 1000);
54         ok1(ext_realloc_count == 1);
55         assert(p);
56
57         expected_parent = p;
58         talloc_free(p2);
59         ok1(ext_free_count == 1);
60
61         expected_parent = head;
62         talloc_free(p);
63         ok1(ext_free_count == 2);
64
65         expected_parent = NULL;
66         talloc_free(head);
67         ok1(ext_free_count == 3);
68
69         ok1(lock_count == unlock_count);
70
71         return exit_status();
72 }