summaryrefslogtreecommitdiff
path: root/ccan/talloc/test/run-set_allocator.c
blob: e1c76d4c045995956b9e1b68961a77dd6da49aa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <ccan/failtest/failtest_override.h>
#include <ccan/talloc/talloc.c>
#include <stdbool.h>
#include <ccan/tap/tap.h>
#include <ccan/failtest/failtest.h>

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);
	if (!p1)
		failtest_exit(exit_status());
	ok1(my_malloc_count == 1);
	ok1(my_free_count == 0);
	ok1(my_realloc_count == 0);

	p2 = talloc_realloc(NULL, p1, int, 10000);
	if (!p2) {
		talloc_free(p1);
		failtest_exit(exit_status());
	}
	p1 = p2;
	ok1(my_malloc_count == 1);
	ok1(my_free_count == 0);
	ok1(my_realloc_count == 1);

	p2 = talloc(p1, int);
	if (!p2) {
		talloc_free(p1);
		failtest_exit(exit_status());
	}
	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());
}